From 0cc9927864c7bc45f5b0f1884144a62eb517f49a Mon Sep 17 00:00:00 2001 From: Wiktor Kolodziej Date: Sun, 26 Jun 2011 16:41:29 +0200 Subject: [PATCH 1/3] Fix node name handling in ec2 driver in list_nodes method (LIBCLOUD-89) --- libcloud/compute/drivers/ec2.py | 16 ++++-- .../fixtures/ec2/describe_instances_with_tags.xml | 53 ++++++++++++++++++++ test/compute/test_ec2.py | 16 ++++++ 3 files changed, 79 insertions(+), 6 deletions(-) create mode 100644 test/compute/fixtures/ec2/describe_instances_with_tags.xml diff --git a/libcloud/compute/drivers/ec2.py b/libcloud/compute/drivers/ec2.py index e1f5a30..d624317 100644 --- a/libcloud/compute/drivers/ec2.py +++ b/libcloud/compute/drivers/ec2.py @@ -298,11 +298,18 @@ class EC2NodeDriver(NodeDriver): except KeyError: state = NodeState.UNKNOWN + instance_id = findtext(element=element, xpath='instanceId', + namespace=NAMESPACE) + tags = dict((findtext(element=item, xpath='key', namespace=NAMESPACE), + findtext(element=item, xpath='value', namespace=NAMESPACE)) + for item in findall(element=element, xpath='tagSet/item', namespace=NAMESPACE)) + + name = tags.get('Name', instance_id) + n = Node( id=findtext(element=element, xpath='instanceId', namespace=NAMESPACE), - name=findtext(element=element, xpath='instanceId', - namespace=NAMESPACE), + name=name, state=state, public_ip=[findtext(element=element, xpath='ipAddress', namespace=NAMESPACE)], @@ -342,10 +349,7 @@ class EC2NodeDriver(NodeDriver): 'clienttoken' : findattr(element=element, xpath="clientToken", namespace=NAMESPACE), 'groups': groups, - 'tags': - dict((findtext(element=item, xpath='key', namespace=NAMESPACE), - findtext(element=item, xpath='value', namespace=NAMESPACE)) - for item in findall(element=element, xpath='tagSet/item', namespace=NAMESPACE)) + 'tags': tags, } ) return n diff --git a/test/compute/fixtures/ec2/describe_instances_with_tags.xml b/test/compute/fixtures/ec2/describe_instances_with_tags.xml new file mode 100644 index 0000000..61ab584 --- /dev/null +++ b/test/compute/fixtures/ec2/describe_instances_with_tags.xml @@ -0,0 +1,53 @@ + + 56d0fffa-8819-4658-bdd7-548f143a86d2 + + + r-07adf66e + 822272953071 + + + default + + + + + i-8474834a + ami-0f234b234 + + 0 + pending + + + + + 1.2.3.5 + 1.2.3.5 + 0 + + m1.micro + 2009-08-07T05:47:04.000Z + + us-west-1a + + + disabled + + + + Name + foobar1 + + + user_key1 + user_val1 + + + user_key2 + user_val2 + + + + + + + diff --git a/test/compute/test_ec2.py b/test/compute/test_ec2.py index 91d1cbb..557cb5a 100644 --- a/test/compute/test_ec2.py +++ b/test/compute/test_ec2.py @@ -93,11 +93,19 @@ class EC2Tests(LibcloudTestCase, TestCaseMixin): node = self.driver.list_nodes()[0] public_ips = sorted(node.public_ip) self.assertEqual(node.id, 'i-4382922a') + self.assertEqual(node.name, node.id) self.assertEqual(len(node.public_ip), 2) self.assertEqual(public_ips[0], '1.2.3.4') self.assertEqual(public_ips[1], '1.2.3.5') + def test_list_nodes_with_name_tag(self): + EC2MockHttp.type = 'WITH_TAGS' + node = self.driver.list_nodes()[0] + public_ips = sorted(node.public_ip) + self.assertEqual(node.id, 'i-8474834a') + self.assertEqual(node.name, 'foobar1') + def test_list_location(self): locations = self.driver.list_locations() self.assertTrue(len(locations) > 0) @@ -238,6 +246,10 @@ class EC2MockHttp(MockHttp): body = self.fixtures.load('describe_instances.xml') return (httplib.OK, body, {}, httplib.responses[httplib.OK]) + def _WITH_TAGS_DescribeInstances(self, method, url, body, headers): + body = self.fixtures.load('describe_instances_with_tags.xml') + return (httplib.OK, body, {}, httplib.responses[httplib.OK]) + def _DescribeAvailabilityZones(self, method, url, body, headers): body = self.fixtures.load('describe_availability_zones.xml') return (httplib.OK, body, {}, httplib.responses[httplib.OK]) @@ -282,6 +294,10 @@ class EC2MockHttp(MockHttp): body = self.fixtures.load('describe_addresses_multi.xml') return (httplib.OK, body, {}, httplib.responses[httplib.OK]) + def _WITH_TAGS_DescribeAddresses(self, method, url, body, headers): + body = self.fixtures.load('describe_addresses_multi.xml') + return (httplib.OK, body, {}, httplib.responses[httplib.OK]) + def _ModifyInstanceAttribute(self, method, url, body, headers): body = self.fixtures.load('modify_instance_attribute.xml') return (httplib.OK, body, {}, httplib.responses[httplib.OK]) -- 1.6.2.2.1669.g7eaf8