Extend the DESCRIBE_SNAPSHOTS_RESPONSE to include the tagSet as documented by AWS. Also refactor the names of the tests I introduced in previous commits to be more descriptive. Finally some code cleanup, removing the get_tags method from classes and instead inherit from the TaggedEC2Resource class.

This commit is contained in:
Peter Van Bouwel 2014-11-09 15:00:40 +01:00
parent 17356fe56c
commit 2f1f993793
3 changed files with 36 additions and 14 deletions

View File

@ -109,7 +109,7 @@ class StateReason(object):
class TaggedEC2Resource(object):
def get_tags(self, *args, **kwargs):
tags = self.ec2_backend.describe_tags(filters={'resource-id': [self.id]})
tags = ec2_backend.describe_tags(filters={'resource-id': [self.id]})
return tags
def get_filter_value(self, filter_name):
@ -383,10 +383,6 @@ class Instance(BotoInstance, TaggedEC2Resource):
self._reason = ""
self._state_reason = StateReason()
def get_tags(self):
tags = self.ec2_backend.describe_tags(filters={'resource-id': [self.id]})
return tags
@property
def dynamic_group_list(self):
if self.nics:
@ -1227,7 +1223,7 @@ class VolumeAttachment(object):
return attachment
class Volume(object):
class Volume(TaggedEC2Resource):
def __init__(self, volume_id, size, zone):
self.id = volume_id
self.size = size
@ -1255,12 +1251,8 @@ class Volume(object):
else:
return 'available'
def get_tags(self):
tags = ec2_backend.describe_tags(filters={'resource-id': [self.id]})
return tags
class Snapshot(object):
class Snapshot(TaggedEC2Resource):
def __init__(self, snapshot_id, volume, description):
self.id = snapshot_id
self.volume = volume

View File

@ -197,6 +197,14 @@ DESCRIBE_SNAPSHOTS_RESPONSE = """<DescribeSnapshotsResponse xmlns="http://ec2.am
<volumeSize>{{ snapshot.volume.size }}</volumeSize>
<description>{{ snapshot.description }}</description>
<tagSet>
{% for tag in snapshot.get_tags() %}
<item>
<resourceId>{{ tag.resource_id }}</resourceId>
<resourceType>{{ tag.resource_type }}</resourceType>
<key>{{ tag.key }}</key>
<value>{{ tag.value }}</value>
</item>
{% endfor %}
</tagSet>
</item>
{% endfor %}

View File

@ -257,7 +257,7 @@ def test_get_all_tags_value_filter():
@mock_ec2
def test_create_tags_must_set_tags_on_retrieved_instances():
def test_retrieved_instances_must_contain_their_tags():
tag_key = 'Tag name'
tag_value = 'Tag value'
tags_to_be_set = {tag_key: tag_value}
@ -288,7 +288,7 @@ def test_create_tags_must_set_tags_on_retrieved_instances():
@mock_ec2
def test_create_tags_must_set_tags_on_retrieved_volumes():
def test_retrieved_volumes_must_contain_their_tags():
tag_key = 'Tag name'
tag_value = 'Tag value'
tags_to_be_set = {tag_key: tag_value}
@ -307,4 +307,26 @@ def test_create_tags_must_set_tags_on_retrieved_volumes():
volume.delete()
#Check whether tag is present with correct value
retrieved_tags[tag_key].should.equal(tag_value)
retrieved_tags[tag_key].should.equal(tag_value)
@mock_ec2
def test_retrieved_snapshots_must_contain_their_tags():
tag_key = 'Tag name'
tag_value = 'Tag value'
tags_to_be_set = {tag_key: tag_value}
conn = boto.connect_ec2('the_key', 'the_secret')
volume = conn.create_volume(80, "us-east-1a")
snapshot = conn.create_snapshot(volume.id)
conn.create_tags([snapshot.id], tags_to_be_set)
#Fetch the snapshot again
all_snapshots = conn.get_all_snapshots()
snapshot = all_snapshots[0]
retrieved_tags = snapshot.tags
conn.delete_snapshot(snapshot.id)
volume.delete()
#Check whether tag is present with correct value
retrieved_tags[tag_key].should.equal(tag_value)