Merge pull request #1117 from hashicorp/aws-go-key-pair

provider/aws: Convert AWS Key Pair to aws-sdk-go
This commit is contained in:
Clint 2015-03-05 09:17:22 -06:00
commit cebe8b9cbe
2 changed files with 46 additions and 30 deletions

View File

@ -1,9 +1,13 @@
package aws package aws
import ( import (
"encoding/base64"
"fmt" "fmt"
"github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/aws-sdk-go/aws"
"github.com/hashicorp/aws-sdk-go/gen/ec2"
) )
func resourceAwsKeyPair() *schema.Resource { func resourceAwsKeyPair() *schema.Resource {
@ -33,42 +37,50 @@ func resourceAwsKeyPair() *schema.Resource {
} }
func resourceAwsKeyPairCreate(d *schema.ResourceData, meta interface{}) error { func resourceAwsKeyPairCreate(d *schema.ResourceData, meta interface{}) error {
ec2conn := meta.(*AWSClient).ec2conn ec2conn := meta.(*AWSClient).awsEC2conn
keyName := d.Get("key_name").(string) keyName := d.Get("key_name").(string)
publicKey := d.Get("public_key").(string) publicKey := d.Get("public_key").(string)
resp, err := ec2conn.ImportKeyPair(keyName, publicKey) req := &ec2.ImportKeyPairRequest{
KeyName: aws.String(keyName),
PublicKeyMaterial: []byte(base64.StdEncoding.EncodeToString([]byte(publicKey))),
}
resp, err := ec2conn.ImportKeyPair(req)
if err != nil { if err != nil {
return fmt.Errorf("Error import KeyPair: %s", err) return fmt.Errorf("Error import KeyPair: %s", err)
} }
d.SetId(resp.KeyName) d.SetId(*resp.KeyName)
return nil return nil
} }
func resourceAwsKeyPairRead(d *schema.ResourceData, meta interface{}) error { func resourceAwsKeyPairRead(d *schema.ResourceData, meta interface{}) error {
ec2conn := meta.(*AWSClient).ec2conn ec2conn := meta.(*AWSClient).awsEC2conn
resp, err := ec2conn.KeyPairs([]string{d.Id()}, nil) req := &ec2.DescribeKeyPairsRequest{
KeyNames: []string{d.Id()},
}
resp, err := ec2conn.DescribeKeyPairs(req)
if err != nil { if err != nil {
return fmt.Errorf("Error retrieving KeyPair: %s", err) return fmt.Errorf("Error retrieving KeyPair: %s", err)
} }
for _, keyPair := range resp.Keys { for _, keyPair := range resp.KeyPairs {
if keyPair.Name == d.Id() { if *keyPair.KeyName == d.Id() {
d.Set("key_name", keyPair.Name) d.Set("key_name", keyPair.KeyName)
d.Set("fingerprint", keyPair.Fingerprint) d.Set("fingerprint", keyPair.KeyFingerprint)
return nil return nil
} }
} }
return fmt.Errorf("Unable to find key pair within: %#v", resp.Keys) return fmt.Errorf("Unable to find key pair within: %#v", resp.KeyPairs)
} }
func resourceAwsKeyPairDelete(d *schema.ResourceData, meta interface{}) error { func resourceAwsKeyPairDelete(d *schema.ResourceData, meta interface{}) error {
ec2conn := meta.(*AWSClient).ec2conn ec2conn := meta.(*AWSClient).awsEC2conn
_, err := ec2conn.DeleteKeyPair(d.Id()) err := ec2conn.DeleteKeyPair(&ec2.DeleteKeyPairRequest{
KeyName: aws.String(d.Id()),
})
return err return err
} }

View File

@ -4,13 +4,14 @@ import (
"fmt" "fmt"
"testing" "testing"
"github.com/hashicorp/aws-sdk-go/aws"
"github.com/hashicorp/aws-sdk-go/gen/ec2"
"github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
"github.com/mitchellh/goamz/ec2"
) )
func TestAccAWSKeyPair_normal(t *testing.T) { func TestAccAWSKeyPair_normal(t *testing.T) {
var conf ec2.KeyPair var conf ec2.KeyPairInfo
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) }, PreCheck: func() { testAccPreCheck(t) },
@ -29,7 +30,7 @@ func TestAccAWSKeyPair_normal(t *testing.T) {
} }
func testAccCheckAWSKeyPairDestroy(s *terraform.State) error { func testAccCheckAWSKeyPairDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).ec2conn ec2conn := testAccProvider.Meta().(*AWSClient).awsEC2conn
for _, rs := range s.RootModule().Resources { for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_key_pair" { if rs.Type != "aws_key_pair" {
@ -37,17 +38,18 @@ func testAccCheckAWSKeyPairDestroy(s *terraform.State) error {
} }
// Try to find key pair // Try to find key pair
resp, err := conn.KeyPairs( resp, err := ec2conn.DescribeKeyPairs(&ec2.DescribeKeyPairsRequest{
[]string{rs.Primary.ID}, nil) KeyNames: []string{rs.Primary.ID},
})
if err == nil { if err == nil {
if len(resp.Keys) > 0 { if len(resp.KeyPairs) > 0 {
return fmt.Errorf("still exist.") return fmt.Errorf("still exist.")
} }
return nil return nil
} }
// Verify the error is what we want // Verify the error is what we want
ec2err, ok := err.(*ec2.Error) ec2err, ok := err.(aws.APIError)
if !ok { if !ok {
return err return err
} }
@ -59,16 +61,16 @@ func testAccCheckAWSKeyPairDestroy(s *terraform.State) error {
return nil return nil
} }
func testAccCheckAWSKeyPairFingerprint(expectedFingerprint string, conf *ec2.KeyPair) resource.TestCheckFunc { func testAccCheckAWSKeyPairFingerprint(expectedFingerprint string, conf *ec2.KeyPairInfo) resource.TestCheckFunc {
return func(s *terraform.State) error { return func(s *terraform.State) error {
if conf.Fingerprint != expectedFingerprint { if *conf.KeyFingerprint != expectedFingerprint {
return fmt.Errorf("incorrect fingerprint. expected %s, got %s", expectedFingerprint, conf.Fingerprint) return fmt.Errorf("incorrect fingerprint. expected %s, got %s", expectedFingerprint, *conf.KeyFingerprint)
} }
return nil return nil
} }
} }
func testAccCheckAWSKeyPairExists(n string, res *ec2.KeyPair) resource.TestCheckFunc { func testAccCheckAWSKeyPairExists(n string, res *ec2.KeyPairInfo) resource.TestCheckFunc {
return func(s *terraform.State) error { return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n] rs, ok := s.RootModule().Resources[n]
if !ok { if !ok {
@ -79,18 +81,20 @@ func testAccCheckAWSKeyPairExists(n string, res *ec2.KeyPair) resource.TestCheck
return fmt.Errorf("No KeyPair name is set") return fmt.Errorf("No KeyPair name is set")
} }
conn := testAccProvider.Meta().(*AWSClient).ec2conn ec2conn := testAccProvider.Meta().(*AWSClient).awsEC2conn
resp, err := conn.KeyPairs( resp, err := ec2conn.DescribeKeyPairs(&ec2.DescribeKeyPairsRequest{
[]string{rs.Primary.ID}, nil) KeyNames: []string{rs.Primary.ID},
})
if err != nil { if err != nil {
return err return err
} }
if len(resp.Keys) != 1 || if len(resp.KeyPairs) != 1 ||
resp.Keys[0].Name != rs.Primary.ID { *resp.KeyPairs[0].KeyName != rs.Primary.ID {
return fmt.Errorf("KeyPair not found") return fmt.Errorf("KeyPair not found")
} }
*res = resp.Keys[0]
*res = resp.KeyPairs[0]
return nil return nil
} }