From 3a9c757e468b6f33e62eb9d1033a7031577f15a7 Mon Sep 17 00:00:00 2001 From: Steve Pulec Date: Mon, 18 Feb 2013 21:56:22 -0500 Subject: [PATCH] add instance stop/start --- moto/ec2/models.py | 18 +++++++++++++ moto/ec2/responses.py | 54 ++++++++++++++++++++++++++++++++++++-- moto/s3/responses.py | 7 ----- tests/test_ec2/test_ec2.py | 15 +++++++++++ 4 files changed, 85 insertions(+), 9 deletions(-) diff --git a/moto/ec2/models.py b/moto/ec2/models.py index cb74e5c3f..dcae2115b 100644 --- a/moto/ec2/models.py +++ b/moto/ec2/models.py @@ -20,6 +20,24 @@ class EC2Backend(BaseBackend): self.reservations[new_reservation.id] = new_reservation return new_reservation + def start_instances(self, instance_ids): + started_instances = [] + for instance in self.all_instances(): + if instance.id in instance_ids: + instance._state = InstanceState(0, 'pending') + 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._state = InstanceState(64, 'stopping') + stopped_instances.append(instance) + + return stopped_instances + def terminate_instances(self, instance_ids): terminated_instances = [] for instance in self.all_instances(): diff --git a/moto/ec2/responses.py b/moto/ec2/responses.py index d77c828c1..52ddd3e00 100644 --- a/moto/ec2/responses.py +++ b/moto/ec2/responses.py @@ -21,8 +21,18 @@ def instances(uri, body, headers): 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) else: - raise ValueError("Not implemented", action) + import pdb;pdb.set_trace() EC2_RUN_INSTANCES = """ @@ -221,4 +231,44 @@ EC2_TERMINATE_INSTANCES = """ {% endfor %} -""" \ No newline at end of file +""" + +EC2_STOP_INSTANCES = """ + + 59dbff89-35bd-4eac-99ed-be587EXAMPLE + + {% for instance in instances %} + + {{ instance.id }} + + 32 + {{ instance.state }} + + + 16 + running + + + {% endfor %} + +""" + +EC2_START_INSTANCES = """ + + 59dbff89-35bd-4eac-99ed-be587EXAMPLE + + {% for instance in instances %} + + {{ instance.id }} + + 32 + {{ instance.state }} + + + 16 + running + + + {% endfor %} + +""" diff --git a/moto/s3/responses.py b/moto/s3/responses.py index 49c488145..10445eda2 100644 --- a/moto/s3/responses.py +++ b/moto/s3/responses.py @@ -15,13 +15,6 @@ def bucket_response(uri, body, headers): hostname = uri.hostname method = uri.method - # s3_base_url = "s3.amazonaws.com" - # if hostname == s3_base_url: - # # No bucket specified. Listing all buckets - # all_buckets = s3_backend.get_all_buckets() - # template = Template(S3_ALL_BUCKETS) - # return template.render(buckets=all_buckets) - bucket_name = bucket_name_from_hostname(hostname) if method == 'GET': diff --git a/tests/test_ec2/test_ec2.py b/tests/test_ec2/test_ec2.py index 90e7e1565..2506bc01f 100644 --- a/tests/test_ec2/test_ec2.py +++ b/tests/test_ec2/test_ec2.py @@ -27,3 +27,18 @@ def test_instance_launch_and_terminate(): reservations = conn.get_all_instances() instance = reservations[0].instances[0] instance.state.should.equal('shutting-down') + + +@mock_ec2 +def test_instance_start_and_stop(): + conn = boto.connect_ec2('the_key', 'the_secret') + reservation = conn.run_instances('', '') + instances = reservation.instances + + stopped_instances = conn.stop_instances([instance.id for instance in instances]) + + for instance in stopped_instances: + instance.state.should.equal('stopping') + + started_instances = conn.start_instances(instances[0].id) + started_instances[0].state.should.equal('pending')