make instance id parsing more generic
This commit is contained in:
parent
3a9c757e46
commit
7907585b65
@ -9,14 +9,19 @@ class EC2Backend(BaseBackend):
|
||||
def __init__(self):
|
||||
self.reservations = {}
|
||||
|
||||
def add_instance(self):
|
||||
def get_instance(self, instance_id):
|
||||
for instance in self.all_instances():
|
||||
if instance.id == instance_id:
|
||||
return instance
|
||||
|
||||
def add_instances(self, count):
|
||||
new_reservation = Reservation()
|
||||
new_reservation.id = random_reservation_id()
|
||||
for index in range(count):
|
||||
new_instance = Instance()
|
||||
new_instance.id = random_instance_id()
|
||||
new_instance._state = InstanceState(0, "pending")
|
||||
|
||||
new_reservation = Reservation()
|
||||
new_reservation.id = random_reservation_id()
|
||||
new_reservation.instances = [new_instance]
|
||||
new_reservation.instances.append(new_instance)
|
||||
self.reservations[new_reservation.id] = new_reservation
|
||||
return new_reservation
|
||||
|
||||
|
@ -1,36 +1,43 @@
|
||||
import boto
|
||||
|
||||
from urlparse import parse_qs
|
||||
|
||||
from jinja2 import Template
|
||||
|
||||
from .models import ec2_backend
|
||||
from .utils import instance_ids_from_querystring
|
||||
|
||||
|
||||
def instances(uri, body, headers):
|
||||
querystring = parse_qs(body)
|
||||
action = querystring['Action'][0]
|
||||
instance_ids = instance_ids_from_querystring(querystring)
|
||||
|
||||
if action == 'DescribeInstances':
|
||||
template = Template(EC2_DESCRIBE_INSTANCES)
|
||||
return template.render(reservations=ec2_backend.all_reservations())
|
||||
elif action == 'RunInstances':
|
||||
new_reservation = ec2_backend.add_instance()
|
||||
min_count = int(querystring.get('MinCount', ['1'])[0])
|
||||
new_reservation = ec2_backend.add_instances(min_count)
|
||||
template = Template(EC2_RUN_INSTANCES)
|
||||
return template.render(reservation=new_reservation)
|
||||
elif action == 'TerminateInstances':
|
||||
instance_ids = querystring.get('InstanceId.1')[0]
|
||||
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)
|
||||
# elif action == 'DescribeInstanceAttribute':
|
||||
# attribute = querystring.get("Attribute")[0]
|
||||
# instance_id = instance_ids[0]
|
||||
# instance = ec2_backend.get_instance(instance_id)
|
||||
# import pdb;pdb.set_trace()
|
||||
else:
|
||||
import pdb;pdb.set_trace()
|
||||
|
||||
@ -272,3 +279,12 @@ EC2_START_INSTANCES = """
|
||||
{% endfor %}
|
||||
</instancesSet>
|
||||
</StartInstancesResponse>"""
|
||||
|
||||
|
||||
EC2_DESCRIBE_INSTANCE_ATTRIBUTE = """<DescribeInstanceAttributeResponse xmlns="http://ec2.amazonaws.com/doc/2012-12-01/">
|
||||
<requestId>59dbff89-35bd-4eac-99ed-be587EXAMPLE</requestId>
|
||||
<instanceId>{{ instance.id }}</instanceId>
|
||||
<kernel>
|
||||
<value>aki-f70657b2</value>
|
||||
</kernel>
|
||||
</DescribeInstanceAttributeResponse>"""
|
||||
|
@ -8,8 +8,18 @@ def random_id(prefix=''):
|
||||
instance_tag = ''.join(unicode(random.choice(chars)) for x in range(size))
|
||||
return '{}-{}'.format(prefix, instance_tag)
|
||||
|
||||
|
||||
def random_instance_id():
|
||||
return random_id(prefix='i')
|
||||
|
||||
|
||||
def random_reservation_id():
|
||||
return random_id(prefix='r')
|
||||
|
||||
|
||||
def instance_ids_from_querystring(querystring_dict):
|
||||
instance_ids = []
|
||||
for key, value in querystring_dict.iteritems():
|
||||
if 'InstanceId' in key:
|
||||
instance_ids.append(value[0])
|
||||
return instance_ids
|
||||
|
@ -32,8 +32,9 @@ def test_instance_launch_and_terminate():
|
||||
@mock_ec2
|
||||
def test_instance_start_and_stop():
|
||||
conn = boto.connect_ec2('the_key', 'the_secret')
|
||||
reservation = conn.run_instances('<ami-image-id>', '<ami-image-id2>')
|
||||
reservation = conn.run_instances('<ami-image-id>', min_count=2)
|
||||
instances = reservation.instances
|
||||
instances.should.have.length_of(2)
|
||||
|
||||
stopped_instances = conn.stop_instances([instance.id for instance in instances])
|
||||
|
||||
@ -42,3 +43,10 @@ def test_instance_start_and_stop():
|
||||
|
||||
started_instances = conn.start_instances(instances[0].id)
|
||||
started_instances[0].state.should.equal('pending')
|
||||
|
||||
# @mock_ec2
|
||||
# def test_instance_attributes():
|
||||
# conn = boto.connect_ec2('the_key', 'the_secret')
|
||||
# reservation = conn.run_instances('<ami-image-id>')
|
||||
# instance = reservation.instances[0]
|
||||
# instance_type_value = instance.get_attribute("instanceType")
|
||||
|
Loading…
Reference in New Issue
Block a user