Permalink
Browse files

Add ex_describe_tags method to EC2 driver.

git-svn-id: https://svn.apache.org/repos/asf/incubator/libcloud/trunk@1067376 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information...
1 parent 113480d commit a72dc0744b4df5824ef893e5be1f2b6e45c4a00c @Kami Kami committed Feb 5, 2011
Showing with 63 additions and 0 deletions.
  1. +27 −0 libcloud/drivers/ec2.py
  2. +23 −0 test/fixtures/ec2/describe_tags.xml
  3. +13 −0 test/test_ec2.py
View
@@ -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
@@ -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>
View
@@ -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')
@@ -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)

0 comments on commit a72dc07

Please sign in to comment.