def modify_load_balancer_attributes(self): load_balancer_name = self._get_param('LoadBalancerName') load_balancer = self.elb_backend.get_load_balancer(load_balancer_name) cross_zone = self._get_dict_param("LoadBalancerAttributes.CrossZoneLoadBalancing.") if cross_zone: attribute = CrossZoneLoadBalancingAttribute() attribute.enabled = cross_zone["enabled"] == "true" self.elb_backend.set_cross_zone_load_balancing_attribute(load_balancer_name, attribute) access_log = self._get_dict_param("LoadBalancerAttributes.AccessLog.") if access_log: attribute = AccessLogAttribute() attribute.enabled = access_log["enabled"] == "true" attribute.s3_bucket_name = access_log['s3_bucket_name'] attribute.s3_bucket_prefix = access_log['s3_bucket_prefix'] attribute.emit_interval = access_log["emit_interval"] self.elb_backend.set_access_log_attribute(load_balancer_name, attribute) connection_draining = self._get_dict_param("LoadBalancerAttributes.ConnectionDraining.") if connection_draining: attribute = ConnectionDrainingAttribute() attribute.enabled = connection_draining["enabled"] == "true" attribute.timeout = connection_draining["timeout"] self.elb_backend.set_connection_draining_attribute(load_balancer_name, attribute) connection_settings = self._get_dict_param("LoadBalancerAttributes.ConnectionSettings.") if connection_settings: attribute = ConnectionSettingAttribute() attribute.idle_timeout = connection_settings["idle_timeout"] self.elb_backend.set_connection_settings_attribute(load_balancer_name, attribute) template = self.response_template(MODIFY_ATTRIBUTES_TEMPLATE) return template.render(attributes=load_balancer.attributes)
def get_default_attributes(cls): attributes = LbAttributes() cross_zone_load_balancing = CrossZoneLoadBalancingAttribute() cross_zone_load_balancing.enabled = False attributes.cross_zone_load_balancing = cross_zone_load_balancing connection_draining = ConnectionDrainingAttribute() connection_draining.enabled = False attributes.connection_draining = connection_draining access_log = AccessLogAttribute() access_log.enabled = False attributes.access_log = access_log connection_settings = ConnectionSettingAttribute() connection_settings.idle_timeout = 60 attributes.connecting_settings = connection_settings return attributes
def modify_load_balancer_attributes(self): load_balancer_name = self.querystring.get('LoadBalancerName')[0] load_balancer = self.elb_backend.describe_load_balancers( load_balancer_name)[0] def parse_attribute(attribute_name): """ Transform self.querystring parameters matching `LoadBalancerAttributes.attribute_name.attribute_key` into a dictionary of (attribute_name, attribute_key)` pairs. """ attribute_prefix = "LoadBalancerAttributes." + attribute_name return dict((key.lstrip(attribute_prefix), value[0]) for key, value in self.querystring.items() if key.startswith(attribute_prefix)) cross_zone = parse_attribute("CrossZoneLoadBalancing") if cross_zone: attribute = CrossZoneLoadBalancingAttribute() attribute.enabled = cross_zone["Enabled"] == "true" self.elb_backend.set_cross_zone_load_balancing_attribute( load_balancer_name, attribute) access_log = parse_attribute("AccessLog") if access_log: attribute = AccessLogAttribute() attribute.enabled = access_log["Enabled"] == "true" attribute.s3_bucket_name = access_log["S3BucketName"] attribute.s3_bucket_prefix = access_log["S3BucketPrefix"] attribute.emit_interval = access_log["EmitInterval"] self.elb_backend.set_access_log_attribute(load_balancer_name, attribute) connection_draining = parse_attribute("ConnectionDraining") if connection_draining: attribute = ConnectionDrainingAttribute() attribute.enabled = connection_draining["Enabled"] == "true" attribute.timeout = connection_draining["Timeout"] self.elb_backend.set_connection_draining_attribute( load_balancer_name, attribute) connection_settings = parse_attribute("ConnectionSettings") if connection_settings: attribute = ConnectionSettingAttribute() attribute.idle_timeout = connection_settings["IdleTimeout"] self.elb_backend.set_connection_settings_attribute( load_balancer_name, attribute) template = self.response_template(MODIFY_ATTRIBUTES_TEMPLATE) return template.render(attributes=load_balancer.attributes)
def setup_access_log(listeners=[(80, 80, 'TCP')], tag_prefix='', key_name=None, omi_id=None, instance_type='c4.large'): ocb = OCBase() lb = prepare_infra(listeners, tag_prefix, key_name, omi_id, instance_type) lb = ocb.lbu.get_all_load_balancers()[0] log_config = AccessLogAttribute() log_config.enabled = True log_config.s3_bucket_name = S3_BUCKET_NAME log_config.s3_bucket_prefix = S3_BUCKET_PREFIX log_config.emit_interval = 5 ocb.lbu.modify_lb_attribute(lb.name, 'accessLog', log_config)
def test_access_log_attribute(): conn = boto.connect_elb() ports = [(80, 8080, 'http'), (443, 8443, 'tcp')] lb = conn.create_load_balancer('my-lb', [], ports) access_log = AccessLogAttribute() access_log.enabled = True access_log.s3_bucket_name = 'bucket' access_log.s3_bucket_prefix = 'prefix' access_log.emit_interval = 60 conn.modify_lb_attribute("my-lb", "AccessLog", access_log) attributes = lb.get_attributes(force=True) attributes.access_log.enabled.should.be.true attributes.access_log.s3_bucket_name.should.equal("bucket") attributes.access_log.s3_bucket_prefix.should.equal("prefix") attributes.access_log.emit_interval.should.equal(60) access_log.enabled = False conn.modify_lb_attribute("my-lb", "AccessLog", access_log) attributes = lb.get_attributes(force=True) attributes.access_log.enabled.should.be.false
def set_attributes(name, attributes, region=None, key=None, keyid=None, profile=None): """ Set attributes on an ELB. name (string) Name of the ELB instance to set attributes for attributes A dict of attributes to set. Valid attributes are: access_log (dict) enabled (bool) Enable storage of access logs. s3_bucket_name (string) The name of the S3 bucket to place logs. s3_bucket_prefix (string) Prefix for the log file name. emit_interval (int) Interval for storing logs in S3 in minutes. Valid values are 5 and 60. connection_draining (dict) enabled (bool) Enable connection draining. timeout (int) Maximum allowed time in seconds for sending existing connections to an instance that is deregistering or unhealthy. Default is 300. cross_zone_load_balancing (dict) enabled (bool) Enable cross-zone load balancing. CLI example to set attributes on an ELB: .. code-block:: bash salt myminion boto_elb.set_attributes myelb '{"access_log": {"enabled": "true", "s3_bucket_name": "mybucket", "s3_bucket_prefix": "mylogs/", "emit_interval": "5"}}' region=us-east-1 """ conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile) al = attributes.get("access_log", {}) czlb = attributes.get("cross_zone_load_balancing", {}) cd = attributes.get("connection_draining", {}) cs = attributes.get("connecting_settings", {}) if not al and not czlb and not cd and not cs: log.error("No supported attributes for ELB.") return False if al: _al = AccessLogAttribute() _al.enabled = al.get("enabled", False) if not _al.enabled: msg = "Access log attribute configured, but enabled config missing" log.error(msg) return False _al.s3_bucket_name = al.get("s3_bucket_name", None) _al.s3_bucket_prefix = al.get("s3_bucket_prefix", None) _al.emit_interval = al.get("emit_interval", None) added_attr = conn.modify_lb_attribute(name, "accessLog", _al) if added_attr: log.info("Added access_log attribute to %s elb.", name) else: log.error("Failed to add access_log attribute to %s elb.", name) return False if czlb: _czlb = CrossZoneLoadBalancingAttribute() _czlb.enabled = czlb["enabled"] added_attr = conn.modify_lb_attribute(name, "crossZoneLoadBalancing", _czlb.enabled) if added_attr: log.info("Added cross_zone_load_balancing attribute to %s elb.", name) else: log.error("Failed to add cross_zone_load_balancing attribute.") return False if cd: _cd = ConnectionDrainingAttribute() _cd.enabled = cd["enabled"] _cd.timeout = cd.get("timeout", 300) added_attr = conn.modify_lb_attribute(name, "connectionDraining", _cd) if added_attr: log.info("Added connection_draining attribute to %s elb.", name) else: log.error("Failed to add connection_draining attribute.") return False if cs: _cs = ConnectionSettingAttribute() _cs.idle_timeout = cs.get("idle_timeout", 60) added_attr = conn.modify_lb_attribute(name, "connectingSettings", _cs) if added_attr: log.info("Added connecting_settings attribute to %s elb.", name) else: log.error("Failed to add connecting_settings attribute.") return False return True
def set_attributes(name, attributes, region=None, key=None, keyid=None, profile=None): ''' Set attributes on an ELB. CLI example to set attributes on an ELB: .. code-block:: bash salt myminion boto_elb.set_attributes myelb '{"access_log": {"enabled": "true", "s3_bucket_name": "mybucket", "s3_bucket_prefix": "mylogs/", "emit_interval": "5"}}' region=us-east-1 ''' conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile) al = attributes.get('access_log', {}) czlb = attributes.get('cross_zone_load_balancing', {}) cd = attributes.get('connection_draining', {}) cs = attributes.get('connecting_settings', {}) if not al and not czlb and not cd and not cs: log.error('No supported attributes for ELB.') return False if al: _al = AccessLogAttribute() _al.enabled = al.get('enabled', False) if not _al.enabled: msg = 'Access log attribute configured, but enabled config missing' log.error(msg) return False _al.s3_bucket_name = al.get('s3_bucket_name', None) _al.s3_bucket_prefix = al.get('s3_bucket_prefix', None) _al.emit_interval = al.get('emit_interval', None) added_attr = conn.modify_lb_attribute(name, 'accessLog', _al) if added_attr: log.info('Added access_log attribute to {0} elb.'.format(name)) else: msg = 'Failed to add access_log attribute to {0} elb.' log.error(msg.format(name)) return False if czlb: _czlb = CrossZoneLoadBalancingAttribute() _czlb.enabled = czlb['enabled'] added_attr = conn.modify_lb_attribute(name, 'crossZoneLoadBalancing', _czlb.enabled) if added_attr: msg = 'Added cross_zone_load_balancing attribute to {0} elb.' log.info(msg.format(name)) else: log.error('Failed to add cross_zone_load_balancing attribute.') return False if cd: _cd = ConnectionDrainingAttribute() _cd.enabled = cd['enabled'] _cd.timeout = cd.get('timeout', 300) added_attr = conn.modify_lb_attribute(name, 'connectionDraining', _cd) if added_attr: msg = 'Added connection_draining attribute to {0} elb.' log.info(msg.format(name)) else: log.error('Failed to add connection_draining attribute.') return False if cs: _cs = ConnectionSettingAttribute() _cs.idle_timeout = cs.get('idle_timeout', 60) added_attr = conn.modify_lb_attribute(name, 'connectingSettings', _cs) if added_attr: msg = 'Added connecting_settings attribute to {0} elb.' log.info(msg.format(name)) else: log.error('Failed to add connecting_settings attribute.') return False return True
def set_attributes(name, attributes, region=None, key=None, keyid=None, profile=None): """ Set attributes on an ELB. name (string) Name of the ELB instance to set attributes for attributes A dict of attributes to set. Valid attributes are: access_log (dict) enabled (bool) Enable storage of access logs. s3_bucket_name (string) The name of the S3 bucket to place logs. s3_bucket_prefix (string) Prefix for the log file name. emit_interval (int) Interval for storing logs in S3 in minutes. Valid values are 5 and 60. connection_draining (dict) enabled (bool) Enable connection draining. timeout (int) Maximum allowed time in seconds for sending existing connections to an instance that is deregistering or unhealthy. Default is 300. cross_zone_load_balancing (dict) enabled (bool) Enable cross-zone load balancing. CLI example to set attributes on an ELB: .. code-block:: bash salt myminion boto_elb.set_attributes myelb '{"access_log": {"enabled": "true", "s3_bucket_name": "mybucket", "s3_bucket_prefix": "mylogs/", "emit_interval": "5"}}' region=us-east-1 """ conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile) al = attributes.get("access_log", {}) czlb = attributes.get("cross_zone_load_balancing", {}) cd = attributes.get("connection_draining", {}) cs = attributes.get("connecting_settings", {}) if not al and not czlb and not cd and not cs: log.error("No supported attributes for ELB.") return False if al: _al = AccessLogAttribute() _al.enabled = al.get("enabled", False) if not _al.enabled: msg = "Access log attribute configured, but enabled config missing" log.error(msg) return False _al.s3_bucket_name = al.get("s3_bucket_name", None) _al.s3_bucket_prefix = al.get("s3_bucket_prefix", None) _al.emit_interval = al.get("emit_interval", None) added_attr = conn.modify_lb_attribute(name, "accessLog", _al) if added_attr: log.info("Added access_log attribute to {0} elb.".format(name)) else: msg = "Failed to add access_log attribute to {0} elb." log.error(msg.format(name)) return False if czlb: _czlb = CrossZoneLoadBalancingAttribute() _czlb.enabled = czlb["enabled"] added_attr = conn.modify_lb_attribute(name, "crossZoneLoadBalancing", _czlb.enabled) if added_attr: msg = "Added cross_zone_load_balancing attribute to {0} elb." log.info(msg.format(name)) else: log.error("Failed to add cross_zone_load_balancing attribute.") return False if cd: _cd = ConnectionDrainingAttribute() _cd.enabled = cd["enabled"] _cd.timeout = cd.get("timeout", 300) added_attr = conn.modify_lb_attribute(name, "connectionDraining", _cd) if added_attr: msg = "Added connection_draining attribute to {0} elb." log.info(msg.format(name)) else: log.error("Failed to add connection_draining attribute.") return False if cs: _cs = ConnectionSettingAttribute() _cs.idle_timeout = cs.get("idle_timeout", 60) added_attr = conn.modify_lb_attribute(name, "connectingSettings", _cs) if added_attr: msg = "Added connecting_settings attribute to {0} elb." log.info(msg.format(name)) else: log.error("Failed to add connecting_settings attribute.") return False return True
config["alive-check-token"]) elb.configure_health_check(hc) pkp_name = "PublicKeyPolicy-%s-BackendCert" % elb.name conn.create_lb_policy(elb.name, pkp_name, "PublicKeyPolicyType", {"PublicKey": cert}) besap_name = "BackendAuthPolicy-%s-BackendCert" % elb.name conn.create_lb_policy(elb.name, besap_name, "BackendServerAuthenticationPolicyType", {"PublicKeyPolicyName": pkp_name}) conn.set_lb_policies_of_backend_server(elb.name, config["backend_port"], [besap_name]) sp_name = "Sticky-%s" % elb.name conn.create_lb_cookie_stickiness_policy(None, elb.name, sp_name) conn.set_lb_policies_of_listener(elb.name, config["elb_port"], sp_name) access_log = AccessLogAttribute() access_log.enabled = True access_log.s3_bucket_name = config["accesslog_bucket_name"] access_log.s3_bucket_prefix = '' access_log.emit_interval = 5 conn.modify_lb_attribute(elb.name, "AccessLog", access_log) print "Created Elastic Load Balancer (%s) for VPC(%s)" % (elb.name, vpc.id) else: elb = elb_list[0] print "Elastic Load Balancer (%s) found for VPC(%s)" % (elb.name, elb.vpc_id) if (elb.vpc_id != vpc.id): raise Exception( "Error: Wrong VPC association: ELB(%s) is associated with VPC(%s) rather than VPC(%s)" % (elb.name, elb.vpc_id, vpc.id))