Fix EC2 response tags. Closes #37.
This commit is contained in:
parent
af31744dbd
commit
421a5e60af
@ -47,6 +47,10 @@ class Instance(BotoInstance):
|
|||||||
self._state.name = "pending"
|
self._state.name = "pending"
|
||||||
self._state.code = 0
|
self._state.code = 0
|
||||||
|
|
||||||
|
def get_tags(self):
|
||||||
|
tags = ec2_backend.describe_tags(self.id)
|
||||||
|
return tags
|
||||||
|
|
||||||
|
|
||||||
class InstanceBackend(object):
|
class InstanceBackend(object):
|
||||||
|
|
||||||
@ -165,18 +169,21 @@ class TagBackend(object):
|
|||||||
def delete_tag(self, resource_id, key):
|
def delete_tag(self, resource_id, key):
|
||||||
return self.tags[resource_id].pop(key)
|
return self.tags[resource_id].pop(key)
|
||||||
|
|
||||||
def describe_tags(self):
|
def describe_tags(self, filter_resource_ids=None):
|
||||||
results = []
|
results = []
|
||||||
for resource_id, tags in self.tags.iteritems():
|
for resource_id, tags in self.tags.iteritems():
|
||||||
ami = 'ami' in resource_id
|
ami = 'ami' in resource_id
|
||||||
for key, value in tags.iteritems():
|
for key, value in tags.iteritems():
|
||||||
result = {
|
if not filter_resource_ids or resource_id in filter_resource_ids:
|
||||||
'resource_id': resource_id,
|
# If we're not filtering, or we are filtering and this
|
||||||
'key': key,
|
# resource id is in the filter list, add this tag
|
||||||
'value': value,
|
result = {
|
||||||
'resource_type': 'image' if ami else 'instance',
|
'resource_id': resource_id,
|
||||||
}
|
'key': key,
|
||||||
results.append(result)
|
'value': value,
|
||||||
|
'resource_type': 'image' if ami else 'instance',
|
||||||
|
}
|
||||||
|
results.append(result)
|
||||||
return results
|
return results
|
||||||
|
|
||||||
|
|
||||||
|
@ -184,7 +184,16 @@ EC2_DESCRIBE_INSTANCES = """<DescribeInstancesResponse xmlns='http://ec2.amazona
|
|||||||
<blockDeviceMapping />
|
<blockDeviceMapping />
|
||||||
<virtualizationType>hvm</virtualizationType>
|
<virtualizationType>hvm</virtualizationType>
|
||||||
<clientToken>ABCDE1234567890123</clientToken>
|
<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>
|
<hypervisor>xen</hypervisor>
|
||||||
<networkInterfaceSet />
|
<networkInterfaceSet />
|
||||||
</item>
|
</item>
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
import itertools
|
||||||
|
|
||||||
import boto
|
import boto
|
||||||
import sure # flake8: noqa
|
import sure # flake8: noqa
|
||||||
|
|
||||||
@ -19,3 +21,17 @@ def test_instance_launch_and_terminate():
|
|||||||
|
|
||||||
instance.remove_tag("a key")
|
instance.remove_tag("a key")
|
||||||
conn.get_all_tags().should.have.length_of(0)
|
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")
|
||||||
|
Loading…
Reference in New Issue
Block a user