From 4b1b0f8514817b5524206383476bc85fb34058e9 Mon Sep 17 00:00:00 2001 From: Andres Riancho Date: Wed, 20 Aug 2014 14:51:24 -0300 Subject: [PATCH 1/2] Minor refactoring to avoid code duplication --- moto/ec2/models.py | 42 ++++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/moto/ec2/models.py b/moto/ec2/models.py index 35fbd6f60..9170cae91 100644 --- a/moto/ec2/models.py +++ b/moto/ec2/models.py @@ -141,37 +141,33 @@ class InstanceBackend(object): def start_instances(self, instance_ids): started_instances = [] - for instance in self.all_instances(): - if instance.id in instance_ids: - instance.start() - started_instances.append(instance) + for instance in self.get_multi_instances_by_id(instance_ids): + instance.start() + started_instances.append(instance) return started_instances def stop_instances(self, instance_ids): stopped_instances = [] - for instance in self.all_instances(): - if instance.id in instance_ids: - instance.stop() - stopped_instances.append(instance) + for instance in self.get_multi_instances_by_id(instance_ids): + instance.stop() + stopped_instances.append(instance) return stopped_instances def terminate_instances(self, instance_ids): terminated_instances = [] - for instance in self.all_instances(): - if instance.id in instance_ids: - instance.terminate() - terminated_instances.append(instance) + for instance in self.get_multi_instances_by_id(instance_ids): + instance.terminate() + terminated_instances.append(instance) return terminated_instances def reboot_instances(self, instance_ids): rebooted_instances = [] - for instance in self.all_instances(): - if instance.id in instance_ids: - instance.reboot() - rebooted_instances.append(instance) + for instance in self.get_multi_instances_by_id(instance_ids): + instance.reboot() + rebooted_instances.append(instance) return rebooted_instances @@ -192,6 +188,20 @@ class InstanceBackend(object): instances.append(instance) return instances + def get_multi_instances_by_id(self, instance_ids): + """ + :param instance_ids: A string list with instance ids + :return: A list with instance objects + """ + result = [] + + for reservation in self.all_reservations(): + for instance in reservation.instances: + if instance.id in instance_ids: + result.append(instance) + + return result + def get_instance_by_id(self, instance_id): for reservation in self.all_reservations(): for instance in reservation.instances: From c65ff170b2f483b4ff630d3256959751c7afe3a5 Mon Sep 17 00:00:00 2001 From: Andres Riancho Date: Wed, 20 Aug 2014 14:52:23 -0300 Subject: [PATCH 2/2] Fix NotImplementedError: The describe_instance_status action has not been implemented #163 --- moto/ec2/responses/instances.py | 47 ++++++++++++++++++++++++++++++++ tests/test_ec2/test_instances.py | 34 +++++++++++++++++++++++ 2 files changed, 81 insertions(+) diff --git a/moto/ec2/responses/instances.py b/moto/ec2/responses/instances.py index 106702c14..b04376127 100644 --- a/moto/ec2/responses/instances.py +++ b/moto/ec2/responses/instances.py @@ -65,6 +65,17 @@ class InstanceResponse(BaseResponse): template = Template(EC2_START_INSTANCES) return template.render(instances=instances) + def describe_instance_status(self): + instance_ids = instance_ids_from_querystring(self.querystring) + + if instance_ids: + instances = ec2_backend.get_multi_instances_by_id(instance_ids) + else: + instances = ec2_backend.all_instances() + + template = Template(EC2_INSTANCE_STATUS) + return template.render(instances=instances) + def describe_instance_attribute(self): # TODO this and modify below should raise IncorrectInstanceState if # instance not in stopped state @@ -359,3 +370,39 @@ EC2_INVALID_INSTANCE_ID = """ The instance ID '{{ instance_id }}' does not exist 39070fe4-6f6d-4565-aecd-7850607e4555""" + + +EC2_INSTANCE_STATUS = """ + + 59dbff89-35bd-4eac-99ed-be587EXAMPLE + + {% for instance in instances %} + + {{ instance.id }} + us-east-1d + + 16 + running + + + ok +
+ + reachability + passed + +
+
+ + ok +
+ + reachability + passed + +
+
+
+ {% endfor %} +
+
""" \ No newline at end of file diff --git a/tests/test_ec2/test_instances.py b/tests/test_ec2/test_instances.py index 3ee2a2796..5b61deb8f 100644 --- a/tests/test_ec2/test_instances.py +++ b/tests/test_ec2/test_instances.py @@ -241,3 +241,37 @@ def test_run_instance_with_keypair(): instance = reservation.instances[0] instance.key_name.should.equal("keypair_name") + + +@mock_ec2 +def test_describe_instance_status_no_instances(): + conn = boto.connect_ec2('the_key', 'the_secret') + all_status = conn.get_all_instance_status() + len(all_status).should.equal(0) + + +@mock_ec2 +def test_describe_instance_status_with_instances(): + conn = boto.connect_ec2('the_key', 'the_secret') + conn.run_instances('ami-1234abcd', key_name="keypair_name") + + all_status = conn.get_all_instance_status() + len(all_status).should.equal(1) + all_status[0].instance_status.status.should.equal('ok') + all_status[0].system_status.status.should.equal('ok') + + +@mock_ec2 +def test_describe_instance_status_with_instance_filter(): + conn = boto.connect_ec2('the_key', 'the_secret') + + # We want to filter based on this one + reservation = conn.run_instances('ami-1234abcd', key_name="keypair_name") + instance = reservation.instances[0] + + # This is just to setup the test + conn.run_instances('ami-1234abcd', key_name="keypair_name") + + all_status = conn.get_all_instance_status(instance_ids=[instance.id]) + len(all_status).should.equal(1) + all_status[0].id.should.equal(instance.id) \ No newline at end of file