Merge pull request #1397 from hashicorp/aws-upstream-keypair

provider/aws: Convert Key Pair to upstream aws-sdk-go
This commit is contained in:
Clint 2015-04-08 15:21:58 -05:00
commit abdfa450b2
3 changed files with 26 additions and 23 deletions

View File

@ -14,6 +14,9 @@ import (
"github.com/hashicorp/aws-sdk-go/gen/rds" "github.com/hashicorp/aws-sdk-go/gen/rds"
"github.com/hashicorp/aws-sdk-go/gen/route53" "github.com/hashicorp/aws-sdk-go/gen/route53"
"github.com/hashicorp/aws-sdk-go/gen/s3" "github.com/hashicorp/aws-sdk-go/gen/s3"
awsSDK "github.com/awslabs/aws-sdk-go/aws"
awsEC2 "github.com/awslabs/aws-sdk-go/service/ec2"
) )
type Config struct { type Config struct {
@ -32,6 +35,7 @@ type AWSClient struct {
region string region string
rdsconn *rds.RDS rdsconn *rds.RDS
iamconn *iam.IAM iamconn *iam.IAM
ec2SDKconn *awsEC2.EC2
} }
// Client configures and returns a fully initailized AWSClient // Client configures and returns a fully initailized AWSClient
@ -74,6 +78,7 @@ func (c *Config) Client() (interface{}, error) {
client.ec2conn = ec2.New(creds, c.Region, nil) client.ec2conn = ec2.New(creds, c.Region, nil)
client.iamconn = iam.New(creds, c.Region, nil) client.iamconn = iam.New(creds, c.Region, nil)
client.ec2SDKconn = awsEC2.New(&awsSDK.Config{Region: "us-west-2"})
} }
if len(errs) > 0 { if len(errs) > 0 {

View File

@ -1,13 +1,12 @@
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/awslabs/aws-sdk-go/aws"
"github.com/hashicorp/aws-sdk-go/gen/ec2" "github.com/awslabs/aws-sdk-go/service/ec2"
) )
func resourceAwsKeyPair() *schema.Resource { func resourceAwsKeyPair() *schema.Resource {
@ -37,15 +36,15 @@ func resourceAwsKeyPair() *schema.Resource {
} }
func resourceAwsKeyPairCreate(d *schema.ResourceData, meta interface{}) error { func resourceAwsKeyPairCreate(d *schema.ResourceData, meta interface{}) error {
ec2conn := meta.(*AWSClient).ec2conn conn := meta.(*AWSClient).ec2SDKconn
keyName := d.Get("key_name").(string) keyName := d.Get("key_name").(string)
publicKey := d.Get("public_key").(string) publicKey := d.Get("public_key").(string)
req := &ec2.ImportKeyPairRequest{ req := &ec2.ImportKeyPairInput{
KeyName: aws.String(keyName), KeyName: aws.String(keyName),
PublicKeyMaterial: []byte(base64.StdEncoding.EncodeToString([]byte(publicKey))), PublicKeyMaterial: []byte(publicKey),
} }
resp, err := ec2conn.ImportKeyPair(req) resp, err := conn.ImportKeyPair(req)
if err != nil { if err != nil {
return fmt.Errorf("Error import KeyPair: %s", err) return fmt.Errorf("Error import KeyPair: %s", err)
} }
@ -55,12 +54,11 @@ func resourceAwsKeyPairCreate(d *schema.ResourceData, meta interface{}) error {
} }
func resourceAwsKeyPairRead(d *schema.ResourceData, meta interface{}) error { func resourceAwsKeyPairRead(d *schema.ResourceData, meta interface{}) error {
ec2conn := meta.(*AWSClient).ec2conn conn := meta.(*AWSClient).ec2SDKconn
req := &ec2.DescribeKeyPairsInput{
req := &ec2.DescribeKeyPairsRequest{ KeyNames: []*string{aws.String(d.Id())},
KeyNames: []string{d.Id()},
} }
resp, err := ec2conn.DescribeKeyPairs(req) resp, err := conn.DescribeKeyPairs(req)
if err != nil { if err != nil {
return fmt.Errorf("Error retrieving KeyPair: %s", err) return fmt.Errorf("Error retrieving KeyPair: %s", err)
} }
@ -77,9 +75,9 @@ func resourceAwsKeyPairRead(d *schema.ResourceData, meta interface{}) error {
} }
func resourceAwsKeyPairDelete(d *schema.ResourceData, meta interface{}) error { func resourceAwsKeyPairDelete(d *schema.ResourceData, meta interface{}) error {
ec2conn := meta.(*AWSClient).ec2conn conn := meta.(*AWSClient).ec2SDKconn
err := ec2conn.DeleteKeyPair(&ec2.DeleteKeyPairRequest{ _, err := conn.DeleteKeyPair(&ec2.DeleteKeyPairInput{
KeyName: aws.String(d.Id()), KeyName: aws.String(d.Id()),
}) })
return err return err

View File

@ -4,8 +4,8 @@ import (
"fmt" "fmt"
"testing" "testing"
"github.com/hashicorp/aws-sdk-go/aws" "github.com/awslabs/aws-sdk-go/aws"
"github.com/hashicorp/aws-sdk-go/gen/ec2" "github.com/awslabs/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
) )
@ -30,7 +30,7 @@ func TestAccAWSKeyPair_normal(t *testing.T) {
} }
func testAccCheckAWSKeyPairDestroy(s *terraform.State) error { func testAccCheckAWSKeyPairDestroy(s *terraform.State) error {
ec2conn := testAccProvider.Meta().(*AWSClient).ec2conn ec2SDKconn := testAccProvider.Meta().(*AWSClient).ec2SDKconn
for _, rs := range s.RootModule().Resources { for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_key_pair" { if rs.Type != "aws_key_pair" {
@ -38,8 +38,8 @@ func testAccCheckAWSKeyPairDestroy(s *terraform.State) error {
} }
// Try to find key pair // Try to find key pair
resp, err := ec2conn.DescribeKeyPairs(&ec2.DescribeKeyPairsRequest{ resp, err := ec2SDKconn.DescribeKeyPairs(&ec2.DescribeKeyPairsInput{
KeyNames: []string{rs.Primary.ID}, KeyNames: []*string{aws.String(rs.Primary.ID)},
}) })
if err == nil { if err == nil {
if len(resp.KeyPairs) > 0 { if len(resp.KeyPairs) > 0 {
@ -81,10 +81,10 @@ func testAccCheckAWSKeyPairExists(n string, res *ec2.KeyPairInfo) resource.TestC
return fmt.Errorf("No KeyPair name is set") return fmt.Errorf("No KeyPair name is set")
} }
ec2conn := testAccProvider.Meta().(*AWSClient).ec2conn ec2SDKconn := testAccProvider.Meta().(*AWSClient).ec2SDKconn
resp, err := ec2conn.DescribeKeyPairs(&ec2.DescribeKeyPairsRequest{ resp, err := ec2SDKconn.DescribeKeyPairs(&ec2.DescribeKeyPairsInput{
KeyNames: []string{rs.Primary.ID}, KeyNames: []*string{aws.String(rs.Primary.ID)},
}) })
if err != nil { if err != nil {
return err return err
@ -94,7 +94,7 @@ func testAccCheckAWSKeyPairExists(n string, res *ec2.KeyPairInfo) resource.TestC
return fmt.Errorf("KeyPair not found") return fmt.Errorf("KeyPair not found")
} }
*res = resp.KeyPairs[0] *res = *resp.KeyPairs[0]
return nil return nil
} }