From 95eb0e63fc4c1fcffd53f532d001986799ebbd65 Mon Sep 17 00:00:00 2001 From: Nick Bailey Date: Tue, 21 Aug 2012 12:29:22 -0400 Subject: [PATCH] Allow getting a list of all keypairs. --- libcloud/compute/drivers/ec2.py | 22 ++++++++++++++++++++ .../compute/fixtures/ec2/describe_key_pairs.xml | 11 ++++++++++ libcloud/test/compute/test_ec2.py | 8 +++++++ 3 files changed, 41 insertions(+), 0 deletions(-) create mode 100644 libcloud/test/compute/fixtures/ec2/describe_key_pairs.xml diff --git a/libcloud/compute/drivers/ec2.py b/libcloud/compute/drivers/ec2.py index 27354c2..0d5fd5f 100644 --- a/libcloud/compute/drivers/ec2.py +++ b/libcloud/compute/drivers/ec2.py @@ -644,6 +644,28 @@ class EC2NodeDriver(NodeDriver): 'keyFingerprint': key_fingerprint, } + def ex_describe_all_keypairs(self): + """Describes all keypairs + + @note: This is a non-standard extension API, and only works for EC2. + + @rtype: C{dict} + + """ + + params = { + 'Action': 'DescribeKeyPairs' + } + + response = self.connection.request(self.path, params=params).object + names = [] + for elem in findall(element=response, xpath='keySet/item', + namespace=NAMESPACE): + name = findtext(element=elem, xpath='keyName', namespace=NAMESPACE) + names.append(name) + + return names + def ex_describe_keypairs(self, name): """Describes a keypair by name diff --git a/libcloud/test/compute/fixtures/ec2/describe_key_pairs.xml b/libcloud/test/compute/fixtures/ec2/describe_key_pairs.xml new file mode 100644 index 0000000..a17868f --- /dev/null +++ b/libcloud/test/compute/fixtures/ec2/describe_key_pairs.xml @@ -0,0 +1,11 @@ + + 59dbff89-35bd-4eac-99ed-be587EXAMPLE + + + gsg-keypair + + 00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00 + + + + diff --git a/libcloud/test/compute/test_ec2.py b/libcloud/test/compute/test_ec2.py index 49b0740..e4f4f06 100644 --- a/libcloud/test/compute/test_ec2.py +++ b/libcloud/test/compute/test_ec2.py @@ -212,6 +212,10 @@ class EC2Tests(LibcloudTestCase, TestCaseMixin): self.assertEqual(availability_zone.zone_state, 'available') self.assertEqual(availability_zone.region_name, 'eu-west-1') + def test_ex_describe_all_keypairs(self): + keys = self.driver.ex_describe_all_keypairs() + self.assertEqual(keys, ['gsg-keypair']) + def test_ex_describe_tags(self): node = Node('i-4382922a', None, None, None, None, self.driver) tags = self.driver.ex_describe_tags(resource=node) @@ -378,6 +382,10 @@ class EC2MockHttp(MockHttp): body = self.fixtures.load('terminate_instances.xml') return (httplib.OK, body, {}, httplib.responses[httplib.OK]) + def _DescribeKeyPairs(self, method, url, body, headers): + body = self.fixtures.load('describe_key_pairs.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]) -- 1.7.5.4