provider/aws: Convert AWS RDS to use upstream aws-sdk-go

This commit is contained in:
Clint Shryock 2015-04-13 14:01:41 -05:00
parent 220f62eb72
commit d579b4b75c
5 changed files with 73 additions and 65 deletions

View File

@ -16,7 +16,9 @@ import (
"github.com/hashicorp/aws-sdk-go/gen/s3" "github.com/hashicorp/aws-sdk-go/gen/s3"
awsSDK "github.com/awslabs/aws-sdk-go/aws" awsSDK "github.com/awslabs/aws-sdk-go/aws"
awsIAM "github.com/awslabs/aws-sdk-go/service/IAM"
awsEC2 "github.com/awslabs/aws-sdk-go/service/ec2" awsEC2 "github.com/awslabs/aws-sdk-go/service/ec2"
awsRDS "github.com/awslabs/aws-sdk-go/service/rds"
) )
type Config struct { type Config struct {
@ -36,6 +38,8 @@ type AWSClient struct {
rdsconn *rds.RDS rdsconn *rds.RDS
iamconn *iam.IAM iamconn *iam.IAM
ec2SDKconn *awsEC2.EC2 ec2SDKconn *awsEC2.EC2
rdsSDKconn *awsRDS.RDS
iamSDKconn *awsIAM.IAM
} }
// Client configures and returns a fully initailized AWSClient // Client configures and returns a fully initailized AWSClient
@ -60,12 +64,15 @@ func (c *Config) Client() (interface{}, error) {
log.Println("[INFO] Building AWS auth structure") log.Println("[INFO] Building AWS auth structure")
creds := aws.DetectCreds(c.AccessKey, c.SecretKey, c.Token) creds := aws.DetectCreds(c.AccessKey, c.SecretKey, c.Token)
log.Println("[INFO] Building AWS SDK auth structure")
sdkCreds := awsSDK.DetectCreds(c.AccessKey, c.SecretKey, c.Token) sdkCreds := awsSDK.DetectCreds(c.AccessKey, c.SecretKey, c.Token)
log.Println("[INFO] Initializing ELB connection") awsConfig := &awsSDK.Config{
client.elbconn = elb.New(&awsSDK.Config{
Credentials: sdkCreds, Credentials: sdkCreds,
Region: c.Region, Region: c.Region,
}) }
log.Println("[INFO] Initializing ELB SDK connection")
client.elbconn = elb.New(awsConfig)
log.Println("[INFO] Initializing AutoScaling connection") log.Println("[INFO] Initializing AutoScaling connection")
client.autoscalingconn = autoscaling.New(creds, c.Region, nil) client.autoscalingconn = autoscaling.New(creds, c.Region, nil)
@ -81,12 +88,15 @@ func (c *Config) Client() (interface{}, error) {
client.r53conn = route53.New(creds, "us-east-1", nil) client.r53conn = route53.New(creds, "us-east-1", nil)
log.Println("[INFO] Initializing EC2 Connection") log.Println("[INFO] Initializing EC2 Connection")
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.ec2SDKconn = awsEC2.New(&awsSDK.Config{ log.Println("[INFO] Initializing EC2 SDK Connection")
Credentials: sdkCreds, client.ec2SDKconn = awsEC2.New(awsConfig)
Region: c.Region,
}) log.Println("[INFO] Initializing RDS SDK Connection")
client.rdsSDKconn = awsRDS.New(awsConfig)
log.Println("[INFO] Initializing IAM SDK Connection")
client.iamSDKconn = awsIAM.New(awsConfig)
} }
if len(errs) > 0 { if len(errs) > 0 {

View File

@ -5,9 +5,9 @@ import (
"log" "log"
"time" "time"
"github.com/hashicorp/aws-sdk-go/aws" "github.com/awslabs/aws-sdk-go/aws"
"github.com/hashicorp/aws-sdk-go/gen/iam" "github.com/awslabs/aws-sdk-go/service/IAM"
"github.com/hashicorp/aws-sdk-go/gen/rds" "github.com/awslabs/aws-sdk-go/service/rds"
"github.com/hashicorp/terraform/helper/hashcode" "github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/resource"
@ -194,10 +194,10 @@ func resourceAwsDbInstance() *schema.Resource {
} }
func resourceAwsDbInstanceCreate(d *schema.ResourceData, meta interface{}) error { func resourceAwsDbInstanceCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).rdsconn conn := meta.(*AWSClient).rdsSDKconn
tags := tagsFromMapRDS(d.Get("tags").(map[string]interface{})) tags := tagsFromMapRDS(d.Get("tags").(map[string]interface{}))
opts := rds.CreateDBInstanceMessage{ opts := rds.CreateDBInstanceInput{
AllocatedStorage: aws.Integer(d.Get("allocated_storage").(int)), AllocatedStorage: aws.Long(int64(d.Get("allocated_storage").(int))),
DBInstanceClass: aws.String(d.Get("instance_class").(string)), DBInstanceClass: aws.String(d.Get("instance_class").(string)),
DBInstanceIdentifier: aws.String(d.Get("identifier").(string)), DBInstanceIdentifier: aws.String(d.Get("identifier").(string)),
DBName: aws.String(d.Get("name").(string)), DBName: aws.String(d.Get("name").(string)),
@ -214,14 +214,14 @@ func resourceAwsDbInstanceCreate(d *schema.ResourceData, meta interface{}) error
} }
attr := d.Get("backup_retention_period") attr := d.Get("backup_retention_period")
opts.BackupRetentionPeriod = aws.Integer(attr.(int)) opts.BackupRetentionPeriod = aws.Long(int64(attr.(int)))
if attr, ok := d.GetOk("iops"); ok { if attr, ok := d.GetOk("iops"); ok {
opts.IOPS = aws.Integer(attr.(int)) opts.IOPS = aws.Long(int64(attr.(int)))
} }
if attr, ok := d.GetOk("port"); ok { if attr, ok := d.GetOk("port"); ok {
opts.Port = aws.Integer(attr.(int)) opts.Port = aws.Long(int64(attr.(int)))
} }
if attr, ok := d.GetOk("multi_az"); ok { if attr, ok := d.GetOk("multi_az"); ok {
@ -253,17 +253,17 @@ func resourceAwsDbInstanceCreate(d *schema.ResourceData, meta interface{}) error
} }
if attr := d.Get("vpc_security_group_ids").(*schema.Set); attr.Len() > 0 { if attr := d.Get("vpc_security_group_ids").(*schema.Set); attr.Len() > 0 {
var s []string var s []*string
for _, v := range attr.List() { for _, v := range attr.List() {
s = append(s, v.(string)) s = append(s, aws.String(v.(string)))
} }
opts.VPCSecurityGroupIDs = s opts.VPCSecurityGroupIDs = s
} }
if attr := d.Get("security_group_names").(*schema.Set); attr.Len() > 0 { if attr := d.Get("security_group_names").(*schema.Set); attr.Len() > 0 {
var s []string var s []*string
for _, v := range attr.List() { for _, v := range attr.List() {
s = append(s, v.(string)) s = append(s, aws.String(v.(string)))
} }
opts.DBSecurityGroups = s opts.DBSecurityGroups = s
} }
@ -345,7 +345,7 @@ func resourceAwsDbInstanceRead(d *schema.ResourceData, meta interface{}) error {
// list tags for resource // list tags for resource
// set tags // set tags
conn := meta.(*AWSClient).rdsconn conn := meta.(*AWSClient).rdsSDKconn
arn, err := buildRDSARN(d, meta) arn, err := buildRDSARN(d, meta)
if err != nil { if err != nil {
name := "<empty>" name := "<empty>"
@ -355,7 +355,7 @@ func resourceAwsDbInstanceRead(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] Error building ARN for DB Instance, not setting Tags for DB %s", name) log.Printf("[DEBUG] Error building ARN for DB Instance, not setting Tags for DB %s", name)
} else { } else {
resp, err := conn.ListTagsForResource(&rds.ListTagsForResourceMessage{ resp, err := conn.ListTagsForResource(&rds.ListTagsForResourceInput{
ResourceName: aws.String(arn), ResourceName: aws.String(arn),
}) })
@ -363,7 +363,7 @@ func resourceAwsDbInstanceRead(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] Error retreiving tags for ARN: %s", arn) log.Printf("[DEBUG] Error retreiving tags for ARN: %s", arn)
} }
var dt []rds.Tag var dt []*rds.Tag
if len(resp.TagList) > 0 { if len(resp.TagList) > 0 {
dt = resp.TagList dt = resp.TagList
} }
@ -396,11 +396,11 @@ func resourceAwsDbInstanceRead(d *schema.ResourceData, meta interface{}) error {
} }
func resourceAwsDbInstanceDelete(d *schema.ResourceData, meta interface{}) error { func resourceAwsDbInstanceDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).rdsconn conn := meta.(*AWSClient).rdsSDKconn
log.Printf("[DEBUG] DB Instance destroy: %v", d.Id()) log.Printf("[DEBUG] DB Instance destroy: %v", d.Id())
opts := rds.DeleteDBInstanceMessage{DBInstanceIdentifier: aws.String(d.Id())} opts := rds.DeleteDBInstanceInput{DBInstanceIdentifier: aws.String(d.Id())}
finalSnapshot := d.Get("final_snapshot_identifier").(string) finalSnapshot := d.Get("final_snapshot_identifier").(string)
if finalSnapshot == "" { if finalSnapshot == "" {
@ -433,11 +433,11 @@ func resourceAwsDbInstanceDelete(d *schema.ResourceData, meta interface{}) error
} }
func resourceAwsDbInstanceUpdate(d *schema.ResourceData, meta interface{}) error { func resourceAwsDbInstanceUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).rdsconn conn := meta.(*AWSClient).rdsSDKconn
d.Partial(true) d.Partial(true)
req := &rds.ModifyDBInstanceMessage{ req := &rds.ModifyDBInstanceInput{
ApplyImmediately: aws.Boolean(d.Get("apply_immediately").(bool)), ApplyImmediately: aws.Boolean(d.Get("apply_immediately").(bool)),
DBInstanceIdentifier: aws.String(d.Id()), DBInstanceIdentifier: aws.String(d.Id()),
} }
@ -445,11 +445,11 @@ func resourceAwsDbInstanceUpdate(d *schema.ResourceData, meta interface{}) error
if d.HasChange("allocated_storage") { if d.HasChange("allocated_storage") {
d.SetPartial("allocated_storage") d.SetPartial("allocated_storage")
req.AllocatedStorage = aws.Integer(d.Get("allocated_storage").(int)) req.AllocatedStorage = aws.Long(int64(d.Get("allocated_storage").(int)))
} }
if d.HasChange("backup_retention_period") { if d.HasChange("backup_retention_period") {
d.SetPartial("backup_retention_period") d.SetPartial("backup_retention_period")
req.BackupRetentionPeriod = aws.Integer(d.Get("backup_retention_period").(int)) req.BackupRetentionPeriod = aws.Long(int64(d.Get("backup_retention_period").(int)))
} }
if d.HasChange("instance_class") { if d.HasChange("instance_class") {
d.SetPartial("instance_class") d.SetPartial("instance_class")
@ -465,7 +465,7 @@ func resourceAwsDbInstanceUpdate(d *schema.ResourceData, meta interface{}) error
} }
if d.HasChange("iops") { if d.HasChange("iops") {
d.SetPartial("iops") d.SetPartial("iops")
req.IOPS = aws.Integer(d.Get("iops").(int)) req.IOPS = aws.Long(int64(d.Get("iops").(int)))
} }
if d.HasChange("backup_window") { if d.HasChange("backup_window") {
d.SetPartial("backup_window") d.SetPartial("backup_window")
@ -490,9 +490,9 @@ func resourceAwsDbInstanceUpdate(d *schema.ResourceData, meta interface{}) error
if d.HasChange("vpc_security_group_ids") { if d.HasChange("vpc_security_group_ids") {
if attr := d.Get("vpc_security_group_ids").(*schema.Set); attr.Len() > 0 { if attr := d.Get("vpc_security_group_ids").(*schema.Set); attr.Len() > 0 {
var s []string var s []*string
for _, v := range attr.List() { for _, v := range attr.List() {
s = append(s, v.(string)) s = append(s, aws.String(v.(string)))
} }
req.VPCSecurityGroupIDs = s req.VPCSecurityGroupIDs = s
} }
@ -500,9 +500,9 @@ func resourceAwsDbInstanceUpdate(d *schema.ResourceData, meta interface{}) error
if d.HasChange("vpc_security_group_ids") { if d.HasChange("vpc_security_group_ids") {
if attr := d.Get("security_group_names").(*schema.Set); attr.Len() > 0 { if attr := d.Get("security_group_names").(*schema.Set); attr.Len() > 0 {
var s []string var s []*string
for _, v := range attr.List() { for _, v := range attr.List() {
s = append(s, v.(string)) s = append(s, aws.String(v.(string)))
} }
req.DBSecurityGroups = s req.DBSecurityGroups = s
} }
@ -527,9 +527,9 @@ func resourceAwsDbInstanceUpdate(d *schema.ResourceData, meta interface{}) error
func resourceAwsBbInstanceRetrieve( func resourceAwsBbInstanceRetrieve(
d *schema.ResourceData, meta interface{}) (*rds.DBInstance, error) { d *schema.ResourceData, meta interface{}) (*rds.DBInstance, error) {
conn := meta.(*AWSClient).rdsconn conn := meta.(*AWSClient).rdsSDKconn
opts := rds.DescribeDBInstancesMessage{ opts := rds.DescribeDBInstancesInput{
DBInstanceIdentifier: aws.String(d.Id()), DBInstanceIdentifier: aws.String(d.Id()),
} }
@ -552,9 +552,7 @@ func resourceAwsBbInstanceRetrieve(
} }
} }
v := resp.DBInstances[0] return resp.DBInstances[0], nil
return &v, nil
} }
func resourceAwsDbInstanceStateRefreshFunc( func resourceAwsDbInstanceStateRefreshFunc(
@ -576,10 +574,10 @@ func resourceAwsDbInstanceStateRefreshFunc(
} }
func buildRDSARN(d *schema.ResourceData, meta interface{}) (string, error) { func buildRDSARN(d *schema.ResourceData, meta interface{}) (string, error) {
iamconn := meta.(*AWSClient).iamconn iamconn := meta.(*AWSClient).iamSDKconn
region := meta.(*AWSClient).region region := meta.(*AWSClient).region
// An zero value GetUserRequest{} defers to the currently logged in user // An zero value GetUserInput{} defers to the currently logged in user
resp, err := iamconn.GetUser(&iam.GetUserRequest{}) resp, err := iamconn.GetUser(&iam.GetUserInput{})
if err != nil { if err != nil {
return "", err return "", err
} }

View File

@ -9,8 +9,8 @@ import (
"github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
"github.com/hashicorp/aws-sdk-go/aws" "github.com/awslabs/aws-sdk-go/aws"
"github.com/hashicorp/aws-sdk-go/gen/rds" "github.com/awslabs/aws-sdk-go/service/rds"
) )
func TestAccAWSDBInstance(t *testing.T) { func TestAccAWSDBInstance(t *testing.T) {
@ -47,7 +47,7 @@ func TestAccAWSDBInstance(t *testing.T) {
} }
func testAccCheckAWSDBInstanceDestroy(s *terraform.State) error { func testAccCheckAWSDBInstanceDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).rdsconn conn := testAccProvider.Meta().(*AWSClient).rdsSDKconn
for _, rs := range s.RootModule().Resources { for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_db_instance" { if rs.Type != "aws_db_instance" {
@ -56,7 +56,7 @@ func testAccCheckAWSDBInstanceDestroy(s *terraform.State) error {
// Try to find the Group // Try to find the Group
resp, err := conn.DescribeDBInstances( resp, err := conn.DescribeDBInstances(
&rds.DescribeDBInstancesMessage{ &rds.DescribeDBInstancesInput{
DBInstanceIdentifier: aws.String(rs.Primary.ID), DBInstanceIdentifier: aws.String(rs.Primary.ID),
}) })
@ -110,9 +110,9 @@ func testAccCheckAWSDBInstanceExists(n string, v *rds.DBInstance) resource.TestC
return fmt.Errorf("No DB Instance ID is set") return fmt.Errorf("No DB Instance ID is set")
} }
conn := testAccProvider.Meta().(*AWSClient).rdsconn conn := testAccProvider.Meta().(*AWSClient).rdsSDKconn
opts := rds.DescribeDBInstancesMessage{ opts := rds.DescribeDBInstancesInput{
DBInstanceIdentifier: aws.String(rs.Primary.ID), DBInstanceIdentifier: aws.String(rs.Primary.ID),
} }
@ -127,7 +127,7 @@ func testAccCheckAWSDBInstanceExists(n string, v *rds.DBInstance) resource.TestC
return fmt.Errorf("DB Instance not found") return fmt.Errorf("DB Instance not found")
} }
*v = resp.DBInstances[0] *v = *resp.DBInstances[0]
return nil return nil
} }

View File

@ -3,8 +3,8 @@ package aws
import ( import (
"log" "log"
"github.com/hashicorp/aws-sdk-go/aws" "github.com/awslabs/aws-sdk-go/aws"
"github.com/hashicorp/aws-sdk-go/gen/rds" "github.com/awslabs/aws-sdk-go/service/rds"
"github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/schema"
) )
@ -20,12 +20,12 @@ func setTagsRDS(conn *rds.RDS, d *schema.ResourceData, arn string) error {
// Set tags // Set tags
if len(remove) > 0 { if len(remove) > 0 {
log.Printf("[DEBUG] Removing tags: %#v", remove) log.Printf("[DEBUG] Removing tags: %#v", remove)
k := make([]string, len(remove), len(remove)) k := make([]*string, len(remove), len(remove))
for i, t := range remove { for i, t := range remove {
k[i] = *t.Key k[i] = t.Key
} }
err := conn.RemoveTagsFromResource(&rds.RemoveTagsFromResourceMessage{ _, err := conn.RemoveTagsFromResource(&rds.RemoveTagsFromResourceInput{
ResourceName: aws.String(arn), ResourceName: aws.String(arn),
TagKeys: k, TagKeys: k,
}) })
@ -35,7 +35,7 @@ func setTagsRDS(conn *rds.RDS, d *schema.ResourceData, arn string) error {
} }
if len(create) > 0 { if len(create) > 0 {
log.Printf("[DEBUG] Creating tags: %#v", create) log.Printf("[DEBUG] Creating tags: %#v", create)
err := conn.AddTagsToResource(&rds.AddTagsToResourceMessage{ _, err := conn.AddTagsToResource(&rds.AddTagsToResourceInput{
ResourceName: aws.String(arn), ResourceName: aws.String(arn),
Tags: create, Tags: create,
}) })
@ -51,7 +51,7 @@ func setTagsRDS(conn *rds.RDS, d *schema.ResourceData, arn string) error {
// diffTags takes our tags locally and the ones remotely and returns // diffTags takes our tags locally and the ones remotely and returns
// the set of tags that must be created, and the set of tags that must // the set of tags that must be created, and the set of tags that must
// be destroyed. // be destroyed.
func diffTagsRDS(oldTags, newTags []rds.Tag) ([]rds.Tag, []rds.Tag) { func diffTagsRDS(oldTags, newTags []*rds.Tag) ([]*rds.Tag, []*rds.Tag) {
// First, we're creating everything we have // First, we're creating everything we have
create := make(map[string]interface{}) create := make(map[string]interface{})
for _, t := range newTags { for _, t := range newTags {
@ -59,7 +59,7 @@ func diffTagsRDS(oldTags, newTags []rds.Tag) ([]rds.Tag, []rds.Tag) {
} }
// Build the list of what to remove // Build the list of what to remove
var remove []rds.Tag var remove []*rds.Tag
for _, t := range oldTags { for _, t := range oldTags {
old, ok := create[*t.Key] old, ok := create[*t.Key]
if !ok || old != *t.Value { if !ok || old != *t.Value {
@ -72,10 +72,10 @@ func diffTagsRDS(oldTags, newTags []rds.Tag) ([]rds.Tag, []rds.Tag) {
} }
// tagsFromMap returns the tags for the given map of data. // tagsFromMap returns the tags for the given map of data.
func tagsFromMapRDS(m map[string]interface{}) []rds.Tag { func tagsFromMapRDS(m map[string]interface{}) []*rds.Tag {
result := make([]rds.Tag, 0, len(m)) result := make([]*rds.Tag, 0, len(m))
for k, v := range m { for k, v := range m {
result = append(result, rds.Tag{ result = append(result, &rds.Tag{
Key: aws.String(k), Key: aws.String(k),
Value: aws.String(v.(string)), Value: aws.String(v.(string)),
}) })
@ -85,7 +85,7 @@ func tagsFromMapRDS(m map[string]interface{}) []rds.Tag {
} }
// tagsToMap turns the list of tags into a map. // tagsToMap turns the list of tags into a map.
func tagsToMapRDS(ts []rds.Tag) map[string]string { func tagsToMapRDS(ts []*rds.Tag) map[string]string {
result := make(map[string]string) result := make(map[string]string)
for _, t := range ts { for _, t := range ts {
result[*t.Key] = *t.Value result[*t.Key] = *t.Value

View File

@ -5,7 +5,7 @@ import (
"reflect" "reflect"
"testing" "testing"
"github.com/hashicorp/aws-sdk-go/gen/rds" "github.com/awslabs/aws-sdk-go/service/rds"
"github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
) )
@ -63,9 +63,9 @@ func TestDiffRDSTags(t *testing.T) {
// testAccCheckTags can be used to check the tags on a resource. // testAccCheckTags can be used to check the tags on a resource.
func testAccCheckRDSTags( func testAccCheckRDSTags(
ts *[]rds.Tag, key string, value string) resource.TestCheckFunc { ts []*rds.Tag, key string, value string) resource.TestCheckFunc {
return func(s *terraform.State) error { return func(s *terraform.State) error {
m := tagsToMapRDS(*ts) m := tagsToMapRDS(ts)
v, ok := m[key] v, ok := m[key]
if value != "" && !ok { if value != "" && !ok {
return fmt.Errorf("Missing tag: %s", key) return fmt.Errorf("Missing tag: %s", key)