From 1d42cc3f53049cf1b259d0cf73a8853db5cad36a Mon Sep 17 00:00:00 2001 From: Steve Pulec Date: Tue, 19 Feb 2013 23:55:01 -0500 Subject: [PATCH] add instance rebooting --- moto/ec2/models.py | 10 ++++++++++ moto/ec2/responses.py | 15 ++++++++++++++- tests/test_ec2/test_ec2.py | 9 +++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/moto/ec2/models.py b/moto/ec2/models.py index cf12120f6..54c761d35 100644 --- a/moto/ec2/models.py +++ b/moto/ec2/models.py @@ -52,6 +52,16 @@ class EC2Backend(BaseBackend): return terminated_instances + def reboot_instances(self, instance_ids): + rebooted_instances = [] + for instance in self.all_instances(): + if instance.id in instance_ids: + # TODO double check instances go to pending when reboot + instance._state = InstanceState(0, 'pending') + rebooted_instances.append(instance) + + return rebooted_instances + def all_instances(self): instances = [] for reservation in self.all_reservations(): diff --git a/moto/ec2/responses.py b/moto/ec2/responses.py index 4e6b0eef6..7be47253a 100644 --- a/moto/ec2/responses.py +++ b/moto/ec2/responses.py @@ -9,7 +9,11 @@ from .utils import instance_ids_from_querystring, camelcase_to_underscores def instances(uri, body, headers): - querystring = parse_qs(body) + if body: + querystring = parse_qs(body) + else: + querystring = parse_qs(headers) + action = querystring['Action'][0] instance_ids = instance_ids_from_querystring(querystring) @@ -25,6 +29,10 @@ def instances(uri, body, headers): instances = ec2_backend.terminate_instances(instance_ids) template = Template(EC2_TERMINATE_INSTANCES) return template.render(instances=instances) + elif action == 'RebootInstances': + instances = ec2_backend.reboot_instances(instance_ids) + template = Template(EC2_REBOOT_INSTANCES) + return template.render(instances=instances) elif action == 'StopInstances': instances = ec2_backend.stop_instances(instance_ids) template = Template(EC2_STOP_INSTANCES) @@ -55,6 +63,7 @@ def instances(uri, body, headers): return EC2_MODIFY_INSTANCE_ATTRIBUTE else: import pdb;pdb.set_trace() + return EC2_REBOOT_INSTANCE EC2_RUN_INSTANCES = """ @@ -295,6 +304,10 @@ EC2_START_INSTANCES = """ """ +EC2_REBOOT_INSTANCES = """ + 59dbff89-35bd-4eac-99ed-be587EXAMPLE + true +""" EC2_DESCRIBE_INSTANCE_ATTRIBUTE = """ 59dbff89-35bd-4eac-99ed-be587EXAMPLE diff --git a/tests/test_ec2/test_ec2.py b/tests/test_ec2/test_ec2.py index 8408a498d..97bb730ee 100644 --- a/tests/test_ec2/test_ec2.py +++ b/tests/test_ec2/test_ec2.py @@ -45,6 +45,15 @@ def test_instance_start_and_stop(): started_instances[0].state.should.equal('pending') +@mock_ec2 +def test_instance_reboot(): + conn = boto.connect_ec2('the_key', 'the_secret') + reservation = conn.run_instances('') + instance = reservation.instances[0] + instance.reboot() + instance.state.should.equal('pending') + + @mock_ec2 def test_instance_attribute_instance_type(): conn = boto.connect_ec2('the_key', 'the_secret')