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:
commit
cebe8b9cbe
|
@ -1,9 +1,13 @@
|
|||
package aws
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
|
||||
"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 {
|
||||
|
@ -33,42 +37,50 @@ func resourceAwsKeyPair() *schema.Resource {
|
|||
}
|
||||
|
||||
func resourceAwsKeyPairCreate(d *schema.ResourceData, meta interface{}) error {
|
||||
ec2conn := meta.(*AWSClient).ec2conn
|
||||
ec2conn := meta.(*AWSClient).awsEC2conn
|
||||
|
||||
keyName := d.Get("key_name").(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 {
|
||||
return fmt.Errorf("Error import KeyPair: %s", err)
|
||||
}
|
||||
|
||||
d.SetId(resp.KeyName)
|
||||
|
||||
d.SetId(*resp.KeyName)
|
||||
return nil
|
||||
}
|
||||
|
||||
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 {
|
||||
return fmt.Errorf("Error retrieving KeyPair: %s", err)
|
||||
}
|
||||
|
||||
for _, keyPair := range resp.Keys {
|
||||
if keyPair.Name == d.Id() {
|
||||
d.Set("key_name", keyPair.Name)
|
||||
d.Set("fingerprint", keyPair.Fingerprint)
|
||||
for _, keyPair := range resp.KeyPairs {
|
||||
if *keyPair.KeyName == d.Id() {
|
||||
d.Set("key_name", keyPair.KeyName)
|
||||
d.Set("fingerprint", keyPair.KeyFingerprint)
|
||||
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 {
|
||||
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
|
||||
}
|
||||
|
|
|
@ -4,13 +4,14 @@ import (
|
|||
"fmt"
|
||||
"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/terraform"
|
||||
"github.com/mitchellh/goamz/ec2"
|
||||
)
|
||||
|
||||
func TestAccAWSKeyPair_normal(t *testing.T) {
|
||||
var conf ec2.KeyPair
|
||||
var conf ec2.KeyPairInfo
|
||||
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
|
@ -29,7 +30,7 @@ func TestAccAWSKeyPair_normal(t *testing.T) {
|
|||
}
|
||||
|
||||
func testAccCheckAWSKeyPairDestroy(s *terraform.State) error {
|
||||
conn := testAccProvider.Meta().(*AWSClient).ec2conn
|
||||
ec2conn := testAccProvider.Meta().(*AWSClient).awsEC2conn
|
||||
|
||||
for _, rs := range s.RootModule().Resources {
|
||||
if rs.Type != "aws_key_pair" {
|
||||
|
@ -37,17 +38,18 @@ func testAccCheckAWSKeyPairDestroy(s *terraform.State) error {
|
|||
}
|
||||
|
||||
// Try to find key pair
|
||||
resp, err := conn.KeyPairs(
|
||||
[]string{rs.Primary.ID}, nil)
|
||||
resp, err := ec2conn.DescribeKeyPairs(&ec2.DescribeKeyPairsRequest{
|
||||
KeyNames: []string{rs.Primary.ID},
|
||||
})
|
||||
if err == nil {
|
||||
if len(resp.Keys) > 0 {
|
||||
if len(resp.KeyPairs) > 0 {
|
||||
return fmt.Errorf("still exist.")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Verify the error is what we want
|
||||
ec2err, ok := err.(*ec2.Error)
|
||||
ec2err, ok := err.(aws.APIError)
|
||||
if !ok {
|
||||
return err
|
||||
}
|
||||
|
@ -59,16 +61,16 @@ func testAccCheckAWSKeyPairDestroy(s *terraform.State) error {
|
|||
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 {
|
||||
if conf.Fingerprint != expectedFingerprint {
|
||||
return fmt.Errorf("incorrect fingerprint. expected %s, got %s", expectedFingerprint, conf.Fingerprint)
|
||||
if *conf.KeyFingerprint != expectedFingerprint {
|
||||
return fmt.Errorf("incorrect fingerprint. expected %s, got %s", expectedFingerprint, *conf.KeyFingerprint)
|
||||
}
|
||||
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 {
|
||||
rs, ok := s.RootModule().Resources[n]
|
||||
if !ok {
|
||||
|
@ -79,18 +81,20 @@ func testAccCheckAWSKeyPairExists(n string, res *ec2.KeyPair) resource.TestCheck
|
|||
return fmt.Errorf("No KeyPair name is set")
|
||||
}
|
||||
|
||||
conn := testAccProvider.Meta().(*AWSClient).ec2conn
|
||||
ec2conn := testAccProvider.Meta().(*AWSClient).awsEC2conn
|
||||
|
||||
resp, err := conn.KeyPairs(
|
||||
[]string{rs.Primary.ID}, nil)
|
||||
resp, err := ec2conn.DescribeKeyPairs(&ec2.DescribeKeyPairsRequest{
|
||||
KeyNames: []string{rs.Primary.ID},
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(resp.Keys) != 1 ||
|
||||
resp.Keys[0].Name != rs.Primary.ID {
|
||||
if len(resp.KeyPairs) != 1 ||
|
||||
*resp.KeyPairs[0].KeyName != rs.Primary.ID {
|
||||
return fmt.Errorf("KeyPair not found")
|
||||
}
|
||||
*res = resp.Keys[0]
|
||||
|
||||
*res = resp.KeyPairs[0]
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue