From fbd9206f23bc2a74d8b13868d93006460b201e36 Mon Sep 17 00:00:00 2001 From: Steve Pulec Date: Sat, 23 Feb 2013 14:51:19 -0500 Subject: [PATCH] add basic zone and region endpoints --- moto/ec2/models.py | 43 +++++++++++++++++- moto/ec2/responses/__init__.py | 4 +- .../availability_zones_and_regions.py | 45 +++++++++++++++++++ .../availability_zonesand_regions.py | 13 ------ .../test_availability_zones_and_regions.py | 23 ++++++++++ .../test_availability_zonesand_regions.py | 9 ---- 6 files changed, 112 insertions(+), 25 deletions(-) create mode 100644 moto/ec2/responses/availability_zones_and_regions.py delete mode 100644 moto/ec2/responses/availability_zonesand_regions.py create mode 100644 tests/test_ec2/test_availability_zones_and_regions.py delete mode 100644 tests/test_ec2/test_availability_zonesand_regions.py diff --git a/moto/ec2/models.py b/moto/ec2/models.py index 56fb6a774..b3fe3f699 100644 --- a/moto/ec2/models.py +++ b/moto/ec2/models.py @@ -152,7 +152,48 @@ class AmiBackend(object): return True return False -class EC2Backend(BaseBackend, InstanceBackend, TagBackend, AmiBackend): + +class Region(object): + def __init__(self, name, endpoint): + self.name = name + self.endpoint = endpoint + + +class Zone(object): + def __init__(self, name, region_name): + self.name = name + self.region_name = region_name + + +class RegionsAndZonesBackend(object): + regions = [ + Region("eu-west-1", "ec2.eu-west-1.amazonaws.com"), + Region("sa-east-1", "ec2.sa-east-1.amazonaws.com"), + Region("us-east-1", "ec2.us-east-1.amazonaws.com"), + Region("ap-northeast-1", "ec2.ap-northeast-1.amazonaws.com"), + Region("us-west-2", "ec2.us-west-2.amazonaws.com"), + Region("us-west-1", "ec2.us-west-1.amazonaws.com"), + Region("ap-southeast-1", "ec2.ap-southeast-1.amazonaws.com"), + Region("ap-southeast-2", "ec2.ap-southeast-2.amazonaws.com"), + ] + + # TODO: cleanup. For now, pretend everything is us-east-1. 'merica. + zones = [ + Zone("us-east-1a", "us-east-1"), + Zone("us-east-1b", "us-east-1"), + Zone("us-east-1c", "us-east-1"), + Zone("us-east-1d", "us-east-1"), + Zone("us-east-1e", "us-east-1"), + ] + + def describe_regions(self): + return self.regions + + def describe_availability_zones(self): + return self.zones + + +class EC2Backend(BaseBackend, InstanceBackend, TagBackend, AmiBackend, RegionsAndZonesBackend): pass diff --git a/moto/ec2/responses/__init__.py b/moto/ec2/responses/__init__.py index ab7e2b685..c67c3e30e 100644 --- a/moto/ec2/responses/__init__.py +++ b/moto/ec2/responses/__init__.py @@ -4,7 +4,7 @@ from moto.ec2.utils import camelcase_to_underscores, method_namess_from_class from .amazon_dev_pay import AmazonDevPay from .amis import AmisResponse -from .availability_zonesand_regions import AvailabilityZonesandRegions +from .availability_zones_and_regions import AvailabilityZonesAndRegions from .customer_gateways import CustomerGateways from .dhcp_options import DHCPOptions from .elastic_block_store import ElasticBlockStore @@ -35,7 +35,7 @@ from .tags import TagResponse class EC2Response(object): - sub_responses = [InstanceResponse, TagResponse, AmisResponse] + sub_responses = [InstanceResponse, TagResponse, AmisResponse, AvailabilityZonesAndRegions] def dispatch(self, uri, body, headers): if body: diff --git a/moto/ec2/responses/availability_zones_and_regions.py b/moto/ec2/responses/availability_zones_and_regions.py new file mode 100644 index 000000000..229637025 --- /dev/null +++ b/moto/ec2/responses/availability_zones_and_regions.py @@ -0,0 +1,45 @@ +from jinja2 import Template + +from moto.ec2.models import ec2_backend +from moto.ec2.utils import resource_ids_from_querystring + + +class AvailabilityZonesAndRegions(object): + def __init__(self, querystring): + self.querystring = querystring + + def describe_availability_zones(self): + zones = ec2_backend.describe_availability_zones() + template = Template(DESCRIBE_ZONES_RESPONSE) + return template.render(zones=zones) + + def describe_regions(self): + regions = ec2_backend.describe_regions() + template = Template(DESCRIBE_REGIONS_RESPONSE) + return template.render(regions=regions) + +DESCRIBE_REGIONS_RESPONSE = """ + 59dbff89-35bd-4eac-99ed-be587EXAMPLE + + {% for region in regions %} + + {{ region.name }} + {{ region.endpoint }} + + {% endfor %} + +""" + +DESCRIBE_ZONES_RESPONSE = """ + 59dbff89-35bd-4eac-99ed-be587EXAMPLE + + {% for zone in zones %} + + {{ zone.name }} + available + {{ zone.region_name }} + + + {% endfor %} + +""" diff --git a/moto/ec2/responses/availability_zonesand_regions.py b/moto/ec2/responses/availability_zonesand_regions.py deleted file mode 100644 index bb794f71c..000000000 --- a/moto/ec2/responses/availability_zonesand_regions.py +++ /dev/null @@ -1,13 +0,0 @@ -from jinja2 import Template - -from moto.ec2.models import ec2_backend -from moto.ec2.utils import resource_ids_from_querystring - - -class AvailabilityZonesandRegions(object): - def describe_availability_zones(self): - raise NotImplementedError('AvailabilityZonesandRegions.describe_availability_zones is not yet implemented') - - def describe_regions(self): - raise NotImplementedError('AvailabilityZonesandRegions.describe_regions is not yet implemented') - diff --git a/tests/test_ec2/test_availability_zones_and_regions.py b/tests/test_ec2/test_availability_zones_and_regions.py new file mode 100644 index 000000000..4f1134e5a --- /dev/null +++ b/tests/test_ec2/test_availability_zones_and_regions.py @@ -0,0 +1,23 @@ +import boto +from sure import expect + +from moto import mock_ec2 + + +@mock_ec2 +def test_describe_regions(): + conn = boto.connect_ec2('the_key', 'the_secret') + regions = conn.get_all_regions() + regions.should.have.length_of(8) + regions[0].name.should.equal('eu-west-1') + regions[0].endpoint.should.equal('ec2.eu-west-1.amazonaws.com') + + +@mock_ec2 +def test_availability_zones(): + # Just testing us-east-1 for now + conn = boto.connect_ec2('the_key', 'the_secret') + zones = conn.get_all_zones() + zones.should.have.length_of(5) + zones[0].name.should.equal('us-east-1a') + zones[0].region_name.should.equal('us-east-1') diff --git a/tests/test_ec2/test_availability_zonesand_regions.py b/tests/test_ec2/test_availability_zonesand_regions.py deleted file mode 100644 index 0449fa584..000000000 --- a/tests/test_ec2/test_availability_zonesand_regions.py +++ /dev/null @@ -1,9 +0,0 @@ -import boto -from sure import expect - -from moto import mock_ec2 - - -@mock_ec2 -def test_availability_zonesand_regions(): - pass