diff --git a/ec2/ec2.go b/ec2/ec2.go index 6c121395..60704e98 100644 --- a/ec2/ec2.go +++ b/ec2/ec2.go @@ -1759,6 +1759,11 @@ type KeyPairsResp struct { Keys []KeyPair `xml:"keySet>item"` } +type DescribeKeyPairsResp struct { + RequestId string `xml:"requestId"` + KeyPairs []KeyPair `xml:"keySet>item"` +} + type CreateKeyPairResp struct { RequestId string `xml:"requestId"` KeyName string `xml:"keyName"` @@ -1772,6 +1777,18 @@ type ImportKeyPairResponse struct { KeyFingerprint string `xml:"keyFingerprint"` } +// DescribeKeyPairs returns all the key pairs for the current region +// +// See http://goo.gl/pTMbH0 +func (ec2 *EC2) DescribeKeyPairs() (resp *DescribeKeyPairsResp, err error) { + params := makeParams("DescribeKeyPairs") + + resp = &DescribeKeyPairsResp{} + err = ec2.query(params, resp) + + return +} + // CreateKeyPair creates a new key pair and returns the private key contents. // // See http://goo.gl/0S6hV diff --git a/ec2/ec2_test.go b/ec2/ec2_test.go index 3ea2bdc7..07cb06c1 100644 --- a/ec2/ec2_test.go +++ b/ec2/ec2_test.go @@ -1,11 +1,12 @@ package ec2_test import ( + "testing" + "github.com/mitchellh/goamz/aws" "github.com/mitchellh/goamz/ec2" "github.com/mitchellh/goamz/testutil" . "github.com/motain/gocheck" - "testing" ) func Test(t *testing.T) { @@ -694,6 +695,24 @@ func (s *S) TestCopyImageExample(c *C) { c.Assert(resp.RequestId, Equals, "60bc441d-fa2c-494d-b155-5d6a3EXAMPLE") } +func (s *S) TestDescribeKeyPairsExample(c *C) { + testServer.Response(200, nil, DescribeKeyPairsExample) + + resp, err := s.ec2.DescribeKeyPairs() + + req := testServer.WaitRequest() + c.Assert(req.Form["Action"], DeepEquals, []string{"DescribeKeyPairs"}) + c.Assert(err, IsNil) + + k0 := resp.KeyPairs[0] + c.Assert(k0.Name, Equals, "my-key-pair") + c.Assert(k0.Fingerprint, Equals, "1f:51:ae:28:bf:89:e9:d8:1f:25:5d:37:2d:7d:b8:ca:9f:f5:f1:6f") + + k1 := resp.KeyPairs[1] + c.Assert(k1.Name, Equals, "my-other-key-pair") + c.Assert(k1.Fingerprint, Equals, "some-fingerprint-value") +} + func (s *S) TestCreateKeyPairExample(c *C) { testServer.Response(200, nil, CreateKeyPairExample) diff --git a/ec2/responses_test.go b/ec2/responses_test.go index 94a681c7..590ae63c 100644 --- a/ec2/responses_test.go +++ b/ec2/responses_test.go @@ -539,6 +539,22 @@ var CopyImageExample = ` ` +var DescribeKeyPairsExample = ` + + 59dbff89-35bd-4eac-99ed-be587EXAMPLE + + + my-key-pair + 1f:51:ae:28:bf:89:e9:d8:1f:25:5d:37:2d:7d:b8:ca:9f:f5:f1:6f + + + my-other-key-pair + some-fingerprint-value + + + +` + var CreateKeyPairExample = ` 59dbff89-35bd-4eac-99ed-be587EXAMPLE