add instance rebooting

This commit is contained in:
Steve Pulec 2013-02-19 23:55:01 -05:00
parent 4fa9cac7d6
commit 1d42cc3f53
3 changed files with 33 additions and 1 deletions

View File

@ -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():

View File

@ -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 = """<RunInstancesResponse xmlns="http://ec2.amazonaws.com/doc/2012-12-01/">
@ -295,6 +304,10 @@ EC2_START_INSTANCES = """
</instancesSet>
</StartInstancesResponse>"""
EC2_REBOOT_INSTANCES = """<RebootInstancesResponse xmlns="http://ec2.amazonaws.com/doc/2012-12-01/">
<requestId>59dbff89-35bd-4eac-99ed-be587EXAMPLE</requestId>
<return>true</return>
</RebootInstancesResponse>"""
EC2_DESCRIBE_INSTANCE_ATTRIBUTE = """<DescribeInstanceAttributeResponse xmlns="http://ec2.amazonaws.com/doc/2012-12-01/">
<requestId>59dbff89-35bd-4eac-99ed-be587EXAMPLE</requestId>

View File

@ -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('<ami-image-id>')
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')