From 7907585b65de083884c499f34a003d9460f0cbc9 Mon Sep 17 00:00:00 2001 From: Steve Pulec Date: Mon, 18 Feb 2013 23:06:23 -0500 Subject: [PATCH] make instance id parsing more generic --- moto/ec2/models.py | 15 ++++++++++----- moto/ec2/responses.py | 24 ++++++++++++++++++++---- moto/ec2/utils.py | 10 ++++++++++ tests/test_ec2/test_ec2.py | 10 +++++++++- 4 files changed, 49 insertions(+), 10 deletions(-) diff --git a/moto/ec2/models.py b/moto/ec2/models.py index dcae2115b..cf12120f6 100644 --- a/moto/ec2/models.py +++ b/moto/ec2/models.py @@ -9,14 +9,19 @@ class EC2Backend(BaseBackend): def __init__(self): self.reservations = {} - def add_instance(self): - new_instance = Instance() - new_instance.id = random_instance_id() - new_instance._state = InstanceState(0, "pending") + def get_instance(self, instance_id): + for instance in self.all_instances(): + if instance.id == instance_id: + return instance + def add_instances(self, count): new_reservation = Reservation() new_reservation.id = random_reservation_id() - new_reservation.instances = [new_instance] + for index in range(count): + new_instance = Instance() + new_instance.id = random_instance_id() + new_instance._state = InstanceState(0, "pending") + new_reservation.instances.append(new_instance) self.reservations[new_reservation.id] = new_reservation return new_reservation diff --git a/moto/ec2/responses.py b/moto/ec2/responses.py index 52ddd3e00..aa6d630ed 100644 --- a/moto/ec2/responses.py +++ b/moto/ec2/responses.py @@ -1,36 +1,43 @@ +import boto + from urlparse import parse_qs from jinja2 import Template from .models import ec2_backend +from .utils import instance_ids_from_querystring def instances(uri, body, headers): querystring = parse_qs(body) action = querystring['Action'][0] + instance_ids = instance_ids_from_querystring(querystring) if action == 'DescribeInstances': template = Template(EC2_DESCRIBE_INSTANCES) return template.render(reservations=ec2_backend.all_reservations()) elif action == 'RunInstances': - new_reservation = ec2_backend.add_instance() + min_count = int(querystring.get('MinCount', ['1'])[0]) + new_reservation = ec2_backend.add_instances(min_count) template = Template(EC2_RUN_INSTANCES) return template.render(reservation=new_reservation) elif action == 'TerminateInstances': - instance_ids = querystring.get('InstanceId.1')[0] instances = ec2_backend.terminate_instances(instance_ids) template = Template(EC2_TERMINATE_INSTANCES) return template.render(instances=instances) elif action == 'StopInstances': - instance_ids = querystring.get('InstanceId.1')[0] instances = ec2_backend.stop_instances(instance_ids) template = Template(EC2_STOP_INSTANCES) return template.render(instances=instances) elif action == 'StartInstances': - instance_ids = querystring.get('InstanceId.1')[0] instances = ec2_backend.start_instances(instance_ids) template = Template(EC2_START_INSTANCES) return template.render(instances=instances) + # elif action == 'DescribeInstanceAttribute': + # attribute = querystring.get("Attribute")[0] + # instance_id = instance_ids[0] + # instance = ec2_backend.get_instance(instance_id) + # import pdb;pdb.set_trace() else: import pdb;pdb.set_trace() @@ -272,3 +279,12 @@ EC2_START_INSTANCES = """ {% endfor %} """ + + +EC2_DESCRIBE_INSTANCE_ATTRIBUTE = """ + 59dbff89-35bd-4eac-99ed-be587EXAMPLE + {{ instance.id }} + + aki-f70657b2 + +""" diff --git a/moto/ec2/utils.py b/moto/ec2/utils.py index 190ff4cdf..15f5c71c9 100644 --- a/moto/ec2/utils.py +++ b/moto/ec2/utils.py @@ -8,8 +8,18 @@ def random_id(prefix=''): instance_tag = ''.join(unicode(random.choice(chars)) for x in range(size)) return '{}-{}'.format(prefix, instance_tag) + def random_instance_id(): return random_id(prefix='i') + def random_reservation_id(): return random_id(prefix='r') + + +def instance_ids_from_querystring(querystring_dict): + instance_ids = [] + for key, value in querystring_dict.iteritems(): + if 'InstanceId' in key: + instance_ids.append(value[0]) + return instance_ids diff --git a/tests/test_ec2/test_ec2.py b/tests/test_ec2/test_ec2.py index 2506bc01f..c9d6670db 100644 --- a/tests/test_ec2/test_ec2.py +++ b/tests/test_ec2/test_ec2.py @@ -32,8 +32,9 @@ def test_instance_launch_and_terminate(): @mock_ec2 def test_instance_start_and_stop(): conn = boto.connect_ec2('the_key', 'the_secret') - reservation = conn.run_instances('', '') + reservation = conn.run_instances('', min_count=2) instances = reservation.instances + instances.should.have.length_of(2) stopped_instances = conn.stop_instances([instance.id for instance in instances]) @@ -42,3 +43,10 @@ def test_instance_start_and_stop(): started_instances = conn.start_instances(instances[0].id) started_instances[0].state.should.equal('pending') + +# @mock_ec2 +# def test_instance_attributes(): +# conn = boto.connect_ec2('the_key', 'the_secret') +# reservation = conn.run_instances('') +# instance = reservation.instances[0] +# instance_type_value = instance.get_attribute("instanceType")