From 1d8252feeae2df406e9dbbd3d242a9d001e9961e Mon Sep 17 00:00:00 2001 From: Tony Pitluga Date: Mon, 24 Aug 2015 15:07:14 +0000 Subject: [PATCH] Make all existing ELB endpoints compatible with boto3 --- moto/elb/responses.py | 86 +++++++++++++++--------- tests/test_elb/test_elb.py | 132 +++++++++++++++++++++++++++++++++++++ 2 files changed, 187 insertions(+), 31 deletions(-) diff --git a/moto/elb/responses.py b/moto/elb/responses.py index 80e785709..75f48d326 100644 --- a/moto/elb/responses.py +++ b/moto/elb/responses.py @@ -220,9 +220,14 @@ class ELBResponse(BaseResponse): return template.render(instance_ids=instance_ids) -CREATE_LOAD_BALANCER_TEMPLATE = """ +CREATE_LOAD_BALANCER_TEMPLATE = """ + tests.us-east-1.elb.amazonaws.com -""" + + + 1549581b-12b7-11e3-895e-1334aEXAMPLE + +""" CREATE_LOAD_BALANCER_LISTENERS_TEMPLATE = """ @@ -231,8 +236,12 @@ CREATE_LOAD_BALANCER_LISTENERS_TEMPLATE = """ """ -DELETE_LOAD_BALANCER_TEMPLATE = """ -""" +DELETE_LOAD_BALANCER_TEMPLATE = """ + + + 1549581b-12b7-11e3-895e-1334aEXAMPLE + +""" DESCRIBE_LOAD_BALANCERS_TEMPLATE = """ @@ -345,35 +354,50 @@ DESCRIBE_LOAD_BALANCERS_TEMPLATE = """ - - {{ check.interval }} - {{ check.target }} - {{ check.healthy_threshold }} - {{ check.timeout }} - {{ check.unhealthy_threshold }} - -""" +CONFIGURE_HEALTH_CHECK_TEMPLATE = """ + + + {{ check.interval }} + {{ check.target }} + {{ check.healthy_threshold }} + {{ check.timeout }} + {{ check.unhealthy_threshold }} + + + + f9880f01-7852-629d-a6c3-3ae2-666a409287e6dc0c + +""" -REGISTER_INSTANCES_TEMPLATE = """ - - {% for instance_id in load_balancer.instance_ids %} - - {{ instance_id }} - - {% endfor %} - -""" +REGISTER_INSTANCES_TEMPLATE = """ + + + {% for instance_id in load_balancer.instance_ids %} + + {{ instance_id }} + + {% endfor %} + + + + f9880f01-7852-629d-a6c3-3ae2-666a409287e6dc0c + +""" -DEREGISTER_INSTANCES_TEMPLATE = """ - - {% for instance_id in load_balancer.instance_ids %} - - {{ instance_id }} - - {% endfor %} - -""" +DEREGISTER_INSTANCES_TEMPLATE = """ + + + {% for instance_id in load_balancer.instance_ids %} + + {{ instance_id }} + + {% endfor %} + + + + f9880f01-7852-629d-a6c3-3ae2-666a409287e6dc0c + +""" SET_LOAD_BALANCER_SSL_CERTIFICATE = """ diff --git a/tests/test_elb/test_elb.py b/tests/test_elb/test_elb.py index ec0abecc9..5068d3804 100644 --- a/tests/test_elb/test_elb.py +++ b/tests/test_elb/test_elb.py @@ -1,4 +1,5 @@ from __future__ import unicode_literals +import boto3 import boto import boto.ec2.elb from boto.ec2.elb import HealthCheck @@ -73,6 +74,22 @@ def test_create_load_balancer_with_certificate(): listener.ssl_certificate_id.should.equal('arn:aws:iam:123456789012:server-certificate/test-cert') +@mock_elb +def test_create_and_delete_boto3_support(): + client = boto3.client('elb', region_name='us-east-1') + + client.create_load_balancer( + LoadBalancerName='my-lb', + Listeners=[{'Protocol':'tcp', 'LoadBalancerPort':80, 'InstancePort':8080}], + AvailabilityZones=['us-east-1a', 'us-east-1b'] + ) + list(client.describe_load_balancers()['LoadBalancerDescriptions']).should.have.length_of(1) + + client.delete_load_balancer( + LoadBalancerName='my-lb' + ) + list(client.describe_load_balancers()['LoadBalancerDescriptions']).should.have.length_of(0) + @mock_elb def test_add_listener(): conn = boto.connect_elb() @@ -110,6 +127,31 @@ def test_delete_listener(): balancer.listeners.should.have.length_of(1) +@mock_elb +def test_create_and_delete_listener_boto3_support(): + client = boto3.client('elb', region_name='us-east-1') + + client.create_load_balancer( + LoadBalancerName='my-lb', + Listeners=[{'Protocol':'http', 'LoadBalancerPort':80, 'InstancePort':8080}], + AvailabilityZones=['us-east-1a', 'us-east-1b'] + ) + list(client.describe_load_balancers()['LoadBalancerDescriptions']).should.have.length_of(1) + + client.create_load_balancer_listeners( + LoadBalancerName='my-lb', + Listeners=[{'Protocol':'tcp', 'LoadBalancerPort':443, 'InstancePort':8443}] + ) + balancer = client.describe_load_balancers()['LoadBalancerDescriptions'][0] + list(balancer['ListenerDescriptions']).should.have.length_of(2) + balancer['ListenerDescriptions'][0]['Listener']['Protocol'].should.equal('HTTP') + balancer['ListenerDescriptions'][0]['Listener']['LoadBalancerPort'].should.equal(80) + balancer['ListenerDescriptions'][0]['Listener']['InstancePort'].should.equal(8080) + balancer['ListenerDescriptions'][1]['Listener']['Protocol'].should.equal('TCP') + balancer['ListenerDescriptions'][1]['Listener']['LoadBalancerPort'].should.equal(443) + balancer['ListenerDescriptions'][1]['Listener']['InstancePort'].should.equal(8443) + + @mock_elb def test_set_sslcertificate(): conn = boto.connect_elb() @@ -183,6 +225,34 @@ def test_create_health_check(): health_check.timeout.should.equal(23) +@mock_elb +def test_create_health_check_boto3(): + client = boto3.client('elb', region_name='us-east-1') + + client.create_load_balancer( + LoadBalancerName='my-lb', + Listeners=[{'Protocol':'http', 'LoadBalancerPort':80, 'InstancePort':8080}], + AvailabilityZones=['us-east-1a', 'us-east-1b'] + ) + client.configure_health_check( + LoadBalancerName='my-lb', + HealthCheck={ + 'Target': 'HTTP:8080/health', + 'Interval': 20, + 'Timeout': 23, + 'HealthyThreshold': 3, + 'UnhealthyThreshold': 5 + } + ) + + balancer = client.describe_load_balancers()['LoadBalancerDescriptions'][0] + balancer['HealthCheck']['Target'].should.equal('HTTP:8080/health') + balancer['HealthCheck']['Interval'].should.equal(20) + balancer['HealthCheck']['Timeout'].should.equal(23) + balancer['HealthCheck']['HealthyThreshold'].should.equal(3) + balancer['HealthCheck']['UnhealthyThreshold'].should.equal(5) + + @mock_ec2 @mock_elb def test_register_instances(): @@ -202,6 +272,32 @@ def test_register_instances(): set(instance_ids).should.equal(set([instance_id1, instance_id2])) +@mock_ec2 +@mock_elb +def test_register_instances_boto3(): + ec2 = boto3.resource('ec2', region_name='us-east-1') + response = ec2.create_instances(ImageId='ami-1234abcd', MinCount=2, MaxCount=2) + instance_id1 = response[0].id + instance_id2 = response[1].id + + client = boto3.client('elb', region_name='us-east-1') + client.create_load_balancer( + LoadBalancerName='my-lb', + Listeners=[{'Protocol':'http', 'LoadBalancerPort':80, 'InstancePort':8080}], + AvailabilityZones=['us-east-1a', 'us-east-1b'] + ) + client.register_instances_with_load_balancer( + LoadBalancerName='my-lb', + Instances=[ + {'InstanceId': instance_id1}, + {'InstanceId': instance_id2} + ] + ) + balancer = client.describe_load_balancers()['LoadBalancerDescriptions'][0] + instance_ids = [instance['InstanceId'] for instance in balancer['Instances']] + set(instance_ids).should.equal(set([instance_id1, instance_id2])) + + @mock_ec2 @mock_elb def test_deregister_instances(): @@ -223,6 +319,42 @@ def test_deregister_instances(): balancer.instances.should.have.length_of(1) balancer.instances[0].id.should.equal(instance_id2) +@mock_ec2 +@mock_elb +def test_deregister_instances_boto3(): + ec2 = boto3.resource('ec2', region_name='us-east-1') + response = ec2.create_instances(ImageId='ami-1234abcd', MinCount=2, MaxCount=2) + instance_id1 = response[0].id + instance_id2 = response[1].id + + client = boto3.client('elb', region_name='us-east-1') + client.create_load_balancer( + LoadBalancerName='my-lb', + Listeners=[{'Protocol':'http', 'LoadBalancerPort':80, 'InstancePort':8080}], + AvailabilityZones=['us-east-1a', 'us-east-1b'] + ) + client.register_instances_with_load_balancer( + LoadBalancerName='my-lb', + Instances=[ + {'InstanceId': instance_id1}, + {'InstanceId': instance_id2} + ] + ) + + balancer = client.describe_load_balancers()['LoadBalancerDescriptions'][0] + balancer['Instances'].should.have.length_of(2) + + client.deregister_instances_from_load_balancer( + LoadBalancerName='my-lb', + Instances=[ + {'InstanceId': instance_id1} + ] + ) + + balancer = client.describe_load_balancers()['LoadBalancerDescriptions'][0] + balancer['Instances'].should.have.length_of(1) + balancer['Instances'][0]['InstanceId'].should.equal(instance_id2) + @mock_elb def test_default_attributes():