diff --git a/moto/ec2/models.py b/moto/ec2/models.py index 8a35ccb1c..039362a92 100644 --- a/moto/ec2/models.py +++ b/moto/ec2/models.py @@ -160,37 +160,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 @@ -211,6 +207,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: diff --git a/moto/ec2/responses/instances.py b/moto/ec2/responses/instances.py index 29aae5cd9..308e6cebb 100644 --- a/moto/ec2/responses/instances.py +++ b/moto/ec2/responses/instances.py @@ -60,6 +60,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 @@ -348,3 +359,45 @@ EC2_MODIFY_INSTANCE_ATTRIBUTE = """ +InvalidInstanceID.NotFound +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 %} +
+
""" diff --git a/tests/test_ec2/test_instances.py b/tests/test_ec2/test_instances.py index 0ef1211f5..f032924a1 100644 --- a/tests/test_ec2/test_instances.py +++ b/tests/test_ec2/test_instances.py @@ -243,3 +243,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)