Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Add ELB access log support #2150

Merged
merged 1 commit into from
Mar 7, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions boto/ec2/elb/__init__.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ def modify_lb_attribute(self, load_balancer_name, attribute, value):
:param attribute: The attribute you wish to change. :param attribute: The attribute you wish to change.


* crossZoneLoadBalancing - Boolean (true) * crossZoneLoadBalancing - Boolean (true)
* accessLog - :py:class:`AccessLogAttribute` instance


:type value: string :type value: string
:param value: The new value for the attribute :param value: The new value for the attribute
Expand All @@ -405,6 +406,15 @@ def modify_lb_attribute(self, load_balancer_name, attribute, value):
if attribute.lower() == 'crosszoneloadbalancing': if attribute.lower() == 'crosszoneloadbalancing':
params['LoadBalancerAttributes.CrossZoneLoadBalancing.Enabled' params['LoadBalancerAttributes.CrossZoneLoadBalancing.Enabled'
] = value ] = value
elif attribute.lower() == 'accesslog':
params['LoadBalancerAttributes.AccessLog.Enabled'] = \
value.enabled and 'true' or 'false'
params['LoadBalancerAttributes.AccessLog.S3BucketName'] = \
value.s3_bucket_name
params['LoadBalancerAttributes.AccessLog.S3BucketPrefix'] = \
value.s3_bucket_prefix
params['LoadBalancerAttributes.AccessLog.EmitInterval'] = \
value.emit_interval
else: else:
raise ValueError('InvalidAttribute', attribute) raise ValueError('InvalidAttribute', attribute)
return self.get_status('ModifyLoadBalancerAttributes', params, return self.get_status('ModifyLoadBalancerAttributes', params,
Expand Down
45 changes: 42 additions & 3 deletions boto/ec2/elb/attributes.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -40,6 +40,41 @@ def endElement(self, name, value, connection):
else: else:
self.enabled = False self.enabled = False


class AccessLogAttribute(object):
"""
Represents the AccessLog segment of ELB attributes.
"""
def __init__(self, connection=None):
self.enabled = None
self.s3_bucket_name = None
self.s3_bucket_prefix = None
self.emit_interval = None

def __repr__(self):
return 'AccessLog(%s, %s, %s, %s)' % (
self.enabled,
self.s3_bucket_name,
self.s3_bucket_prefix,
self.emit_interval
)

def startElement(self, name, attrs, connection):
pass

def endElement(self, name, value, connection):
if name == 'Enabled':
if value.lower() == 'true':
self.enabled = True
else:
self.enabled = False
elif name == 'S3BucketName':
self.s3_bucket_name = value
elif name == 'S3BucketPrefix':
self.s3_bucket_prefix = value
elif name == 'EmitInterval':
self.emit_interval = int(value)


class LbAttributes(object): class LbAttributes(object):
""" """
Represents the Attributes of an Elastic Load Balancer. Represents the Attributes of an Elastic Load Balancer.
Expand All @@ -48,14 +83,18 @@ def __init__(self, connection=None):
self.connection = connection self.connection = connection
self.cross_zone_load_balancing = CrossZoneLoadBalancingAttribute( self.cross_zone_load_balancing = CrossZoneLoadBalancingAttribute(
self.connection) self.connection)
self.access_log = AccessLogAttribute(self.connection)


def __repr__(self): def __repr__(self):
return 'LbAttributes(%s)' % ( return 'LbAttributes(%s, %s)' % (
repr(self.cross_zone_load_balancing)) repr(self.cross_zone_load_balancing),
repr(self.access_log))


def startElement(self, name, attrs, connection): def startElement(self, name, attrs, connection):
if name == 'CrossZoneLoadBalancing': if name == 'CrossZoneLoadBalancing':
return self.cross_zone_load_balancing return self.cross_zone_load_balancing

if name == 'AccessLog':
return self.access_log

def endElement(self, name, value, connection): def endElement(self, name, value, connection):
pass pass
38 changes: 38 additions & 0 deletions tests/integration/ec2/elb/test_connection.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
Initial, and very limited, unit tests for ELBConnection. Initial, and very limited, unit tests for ELBConnection.
""" """


import boto
import time
import unittest import unittest
from boto.ec2.elb import ELBConnection from boto.ec2.elb import ELBConnection


Expand All @@ -39,6 +41,19 @@ def setUp(self):
self.listeners = [(80, 8000, 'HTTP')] self.listeners = [(80, 8000, 'HTTP')]
self.balancer = self.conn.create_load_balancer(self.name, self.availability_zones, self.listeners) self.balancer = self.conn.create_load_balancer(self.name, self.availability_zones, self.listeners)


# S3 bucket for log tests
self.s3 = boto.connect_s3()
self.timestamp = str(int(time.time()))
self.bucket_name = 'boto-elb-%s' % self.timestamp
self.bucket = self.s3.create_bucket(self.bucket_name)
self.bucket.set_canned_acl('public-read-write')
self.addCleanup(self.cleanup_bucket, self.bucket)

def cleanup_bucket(self, bucket):
for key in bucket.get_all_keys():
key.delete()
bucket.delete()

def tearDown(self): def tearDown(self):
""" Deletes the test load balancer after every test. """ Deletes the test load balancer after every test.
It does not delete EVERY load balancer in your account""" It does not delete EVERY load balancer in your account"""
Expand Down Expand Up @@ -161,5 +176,28 @@ def test_create_load_balancer_complex_listeners(self):
sorted([(80, 8000, 'HTTP', 'HTTP')] + complex_listeners) sorted([(80, 8000, 'HTTP', 'HTTP')] + complex_listeners)
) )


def test_load_balancer_access_log(self):
attributes = self.balancer.get_attributes()

self.assertEqual(False, attributes.access_log.enabled)

attributes.access_log.enabled = True
attributes.access_log.s3_bucket_name = self.bucket_name
attributes.access_log.s3_bucket_prefix = 'access-logs'
attributes.access_log.emit_interval = 5

self.conn.modify_lb_attribute(self.balancer.name, 'accessLog',
attributes.access_log)

new_attributes = self.balancer.get_attributes()

self.assertEqual(True, new_attributes.access_log.enabled)
self.assertEqual(self.bucket_name,
new_attributes.access_log.s3_bucket_name)
self.assertEqual('access-logs',
new_attributes.access_log.s3_bucket_prefix)
self.assertEqual(5, new_attributes.access_log.emit_interval)


if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()