Fix EC2 response tags. Closes #37.

This commit is contained in:
Steve Pulec 2013-07-17 12:48:21 -04:00
parent af31744dbd
commit 421a5e60af
3 changed files with 41 additions and 9 deletions

View File

@ -47,6 +47,10 @@ class Instance(BotoInstance):
self._state.name = "pending"
self._state.code = 0
def get_tags(self):
tags = ec2_backend.describe_tags(self.id)
return tags
class InstanceBackend(object):
@ -165,18 +169,21 @@ class TagBackend(object):
def delete_tag(self, resource_id, key):
return self.tags[resource_id].pop(key)
def describe_tags(self):
def describe_tags(self, filter_resource_ids=None):
results = []
for resource_id, tags in self.tags.iteritems():
ami = 'ami' in resource_id
for key, value in tags.iteritems():
result = {
'resource_id': resource_id,
'key': key,
'value': value,
'resource_type': 'image' if ami else 'instance',
}
results.append(result)
if not filter_resource_ids or resource_id in filter_resource_ids:
# If we're not filtering, or we are filtering and this
# resource id is in the filter list, add this tag
result = {
'resource_id': resource_id,
'key': key,
'value': value,
'resource_type': 'image' if ami else 'instance',
}
results.append(result)
return results

View File

@ -184,7 +184,16 @@ EC2_DESCRIBE_INSTANCES = """<DescribeInstancesResponse xmlns='http://ec2.amazona
<blockDeviceMapping />
<virtualizationType>hvm</virtualizationType>
<clientToken>ABCDE1234567890123</clientToken>
<tagSet />
<tagSet>
{% for tag in instance.get_tags() %}
<item>
<resourceId>{{ tag.resource_id }}</resourceId>
<resourceType>{{ tag.resource_type }}</resourceType>
<key>{{ tag.key }}</key>
<value>{{ tag.value }}</value>
</item>
{% endfor %}
</tagSet>
<hypervisor>xen</hypervisor>
<networkInterfaceSet />
</item>

View File

@ -1,3 +1,5 @@
import itertools
import boto
import sure # flake8: noqa
@ -19,3 +21,17 @@ def test_instance_launch_and_terminate():
instance.remove_tag("a key")
conn.get_all_tags().should.have.length_of(0)
@mock_ec2
def test_instance_launch_and_retrieve_all_instances():
conn = boto.connect_ec2('the_key', 'the_secret')
reservation = conn.run_instances('ami-1234abcd')
instance = reservation.instances[0]
instance.add_tag("a key", "some value")
chain = itertools.chain.from_iterable
existing_instances = list(chain([reservation.instances for reservation in conn.get_all_instances()]))
existing_instances.should.have.length_of(1)
existing_instance = existing_instances[0]
existing_instance.tags["a key"].should.equal("some value")