Skip to content

Commit

Permalink
Add ex_describe_tags method to EC2 driver.
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/incubator/libcloud/trunk@1067376 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
Kami committed Feb 5, 2011
1 parent 113480d commit a72dc07
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
27 changes: 27 additions & 0 deletions libcloud/drivers/ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,33 @@ def ex_list_availability_zones(self, only_available=True):

return availability_zones

def ex_describe_tags(self, node):
"""
Return a dictionary of tags for this instance.
@type node: C{Node}
@param node: Node instance
@return dict Node tags
"""
params = { 'Action': 'DescribeTags',
'Filter.0.Name': 'resource-id',
'Filter.0.Value.0': node.id,
'Filter.1.Name': 'resource-type',
'Filter.1.Value.0': 'instance',
}

result = self.connection.request(self.path,
params=params.copy()).object

tags = {}
for element in self._findall(result, 'tagSet/item'):
key = self._findtext(element, 'key')
value = self._findtext(element, 'value')

tags[key] = value
return tags

def create_node(self, **kwargs):
"""Create a new EC2 node
Expand Down
23 changes: 23 additions & 0 deletions test/fixtures/ec2/describe_tags.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<DescribeTagsResponse xmlns="http://ec2.amazonaws.com/doc/2010-08-31/">
<requestId>fa7e0e44-df5e-49a0-98d7-5d4d19a29f95</requestId>
<tagSet>
<item>
<resourceId>i-4382922a</resourceId>
<resourceType>instance</resourceType>
<key>tag</key>
<value>test one</value>
</item>
<item>
<resourceId>i-4382922a</resourceId>
<resourceType>instance</resourceType>
<key>owner</key>
<value>libcloud</value>
</item>
<item>
<resourceId>i-4382922a</resourceId>
<resourceType>instance</resourceType>
<key>stack</key>
<value>Production</value>
</item>
</tagSet>
</DescribeTagsResponse>
13 changes: 13 additions & 0 deletions test/test_ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,15 @@ def test_ex_list_availability_zones(self):
self.assertEqual(availability_zone.zone_state, 'available')
self.assertEqual(availability_zone.region_name, 'eu-west-1')

def test_ex_describe_tags(self):
node = Node('i-4382922a', None, None, None, None, self.driver)
tags = self.driver.ex_describe_tags(node)

self.assertEqual(len(tags), 3)
self.assertTrue('tag' in tags)
self.assertTrue('owner' in tags)
self.assertTrue('stack' in tags)

class EC2MockHttp(MockHttp):

fixtures = FileFixtures('ec2')
Expand Down Expand Up @@ -200,6 +209,10 @@ def _TerminateInstances(self, method, url, body, headers):
body = self.fixtures.load('terminate_instances.xml')
return (httplib.OK, body, {}, httplib.responses[httplib.OK])

def _DescribeTags(self, method, url, body, headers):
body = self.fixtures.load('describe_tags.xml')
return (httplib.OK, body, {}, httplib.responses[httplib.OK])

class EC2APSETests(EC2Tests):
def setUp(self):
EC2APSENodeDriver.connectionCls.conn_classes = (None, EC2MockHttp)
Expand Down

0 comments on commit a72dc07

Please sign in to comment.