From 8ec17d0fd9b9fadf5318f9af74f46f353f0a85c4 Mon Sep 17 00:00:00 2001 From: Nathaniel Felsen Date: Mon, 21 Sep 2015 00:34:11 -0700 Subject: [PATCH] Adding support for access logs in ELB --- cloud/amazon/ec2_elb_lb.py | 48 ++++++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 4 deletions(-) diff --git a/cloud/amazon/ec2_elb_lb.py b/cloud/amazon/ec2_elb_lb.py index afd91eaf397..2a0a41a9af9 100644 --- a/cloud/amazon/ec2_elb_lb.py +++ b/cloud/amazon/ec2_elb_lb.py @@ -69,6 +69,11 @@ options: - An associative array of health check configuration settings (see example) require: false default: None + access_logs: + description: + - An associative array of access logs configuration settings (see example) + require: false + default: None region: description: - The AWS region to use. If not specified then the value of the EC2_REGION environment variable, if any, is used. @@ -163,7 +168,7 @@ EXAMPLES = """ load_balancer_port: 80 instance_port: 80 -# Configure a health check +# Configure a health check and the access logs - local_action: module: ec2_elb_lb name: "test-please-delete" @@ -182,6 +187,10 @@ EXAMPLES = """ interval: 30 # seconds unhealthy_threshold: 2 healthy_threshold: 10 + access_logs: + interval: 5 # minutes (defaults to 60) + s3_location: "my-bucket" # This value is required if access_logs is set + s3_prefix: "logs" # Ensure ELB is gone - local_action: @@ -308,7 +317,7 @@ class ElbManager(object): zones=None, purge_zones=None, security_group_ids=None, health_check=None, subnets=None, purge_subnets=None, scheme="internet-facing", connection_draining_timeout=None, - cross_az_load_balancing=None, + cross_az_load_balancing=None, access_logs=None, stickiness=None, region=None, **aws_connect_params): self.module = module @@ -324,6 +333,7 @@ class ElbManager(object): self.scheme = scheme self.connection_draining_timeout = connection_draining_timeout self.cross_az_load_balancing = cross_az_load_balancing + self.access_logs = access_logs self.stickiness = stickiness self.aws_connect_params = aws_connect_params @@ -352,6 +362,8 @@ class ElbManager(object): self._set_connection_draining_timeout() if self._check_attribute_support('cross_zone_load_balancing'): self._set_cross_az_load_balancing() + if self._check_attribute_support('access_log'): + self._set_access_log() # add sitcky options self.select_stickiness_policy() @@ -698,6 +710,32 @@ class ElbManager(object): self.elb_conn.modify_lb_attribute(self.name, 'CrossZoneLoadBalancing', attributes.cross_zone_load_balancing.enabled) + def _set_access_log(self): + attributes = self.elb.get_attributes() + if self.access_logs: + if 's3_location' not in self.access_logs: + self.module.fail_json(msg='s3_location information required') + + access_logs_config = { + "enabled": True, + "s3_bucket_name": self.access_logs['s3_location'], + "s3_bucket_prefix": self.access_logs.get('s3_prefix', ''), + "emit_interval": self.access_logs.get('interval', 60), + } + + update_access_logs_config = False + for attr, desired_value in access_logs_config.iteritems(): + if getattr(attributes.access_log, attr) != desired_value: + setattr(attributes.access_log, attr, desired_value) + update_access_logs_config = True + if update_access_logs_config: + self.elb_conn.modify_lb_attribute(self.name, 'AccessLog', attributes.access_log) + self.changed = True + elif attributes.access_log.enabled: + attributes.access_log.enabled = False + self.changed = True + self.elb_conn.modify_lb_attribute(self.name, 'AccessLog', attributes.access_log) + def _set_connection_draining_timeout(self): attributes = self.elb.get_attributes() if self.connection_draining_timeout is not None: @@ -833,7 +871,8 @@ def main(): scheme={'default': 'internet-facing', 'required': False}, connection_draining_timeout={'default': None, 'required': False}, cross_az_load_balancing={'default': None, 'required': False}, - stickiness={'default': None, 'required': False, 'type': 'dict'} + stickiness={'default': None, 'required': False, 'type': 'dict'}, + access_logs={'default': None, 'required': False, 'type': 'dict'} ) ) @@ -858,6 +897,7 @@ def main(): security_group_ids = module.params['security_group_ids'] security_group_names = module.params['security_group_names'] health_check = module.params['health_check'] + access_logs = module.params['access_logs'] subnets = module.params['subnets'] purge_subnets = module.params['purge_subnets'] scheme = module.params['scheme'] @@ -890,7 +930,7 @@ def main(): purge_zones, security_group_ids, health_check, subnets, purge_subnets, scheme, connection_draining_timeout, cross_az_load_balancing, - stickiness, + access_logs, stickiness, region=region, **aws_connect_params) # check for unsupported attributes for this version of boto