add instance attribute description and modification

This commit is contained in:
Steve Pulec 2013-02-19 23:01:13 -05:00
commit 1af038290d
3 changed files with 67 additions and 16 deletions

View file

@ -5,7 +5,7 @@ from urlparse import parse_qs
from jinja2 import Template
from .models import ec2_backend
from .utils import instance_ids_from_querystring
from .utils import instance_ids_from_querystring, camelcase_to_underscores
def instances(uri, body, headers):
@ -33,11 +33,26 @@ def instances(uri, body, headers):
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()
elif action == 'DescribeInstanceAttribute':
# TODO this and modify below should raise IncorrectInstanceState if instance not in stopped state
attribute = querystring.get("Attribute")[0]
normalized_attribute = camelcase_to_underscores(attribute)
instance_id = instance_ids[0]
instance = ec2_backend.get_instance(instance_id)
value = getattr(instance, normalized_attribute)
template = Template(EC2_DESCRIBE_INSTANCE_ATTRIBUTE)
return template.render(instance=instance, attribute=attribute, value=value)
elif action == 'ModifyInstanceAttribute':
for key, value in querystring.iteritems():
if '.Value' in key:
break
value = querystring.get(key)[0]
normalized_attribute = camelcase_to_underscores(key.split(".")[0])
instance_id = instance_ids[0]
instance = ec2_backend.get_instance(instance_id)
setattr(instance, normalized_attribute, value)
return EC2_MODIFY_INSTANCE_ATTRIBUTE
else:
import pdb;pdb.set_trace()
@ -284,7 +299,12 @@ EC2_START_INSTANCES = """
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>
<{{ attribute }}>
<value>{{ value }}</value>
</{{ attribute }}>
</DescribeInstanceAttributeResponse>"""
EC2_MODIFY_INSTANCE_ATTRIBUTE = """<ModifyInstanceAttributeResponse xmlns="http://ec2.amazonaws.com/doc/2012-12-01/">
<requestId>59dbff89-35bd-4eac-99ed-be587EXAMPLE</requestId>
<return>true</return>
</ModifyInstanceAttributeResponse>"""

View file

@ -23,3 +23,15 @@ def instance_ids_from_querystring(querystring_dict):
if 'InstanceId' in key:
instance_ids.append(value[0])
return instance_ids
def camelcase_to_underscores(argument):
''' Converts a camelcase param like theNewAttribute to the equivalent
python underscore variable like the_new_attribute'''
result = ''
for index, char in enumerate(argument):
if char.istitle() and index:
# Only add underscore is char is capital and not first letter
result += "_"
result += char.lower()
return result