From 72b932a4c9126433afd7a85de4a7cd37511a30de Mon Sep 17 00:00:00 2001 From: Kraig Amador Date: Thu, 5 May 2016 12:21:55 -0700 Subject: [PATCH] Use AWSClient.accountid rather than making API calls to attempt to retrieve the account id when generating ARNs --- .../providers/aws/resource_aws_db_instance.go | 59 +++++++----------- .../aws/resource_aws_db_instance_test.go | 5 +- .../aws/resource_aws_db_parameter_group.go | 60 +++++++------------ .../aws/resource_aws_db_security_group.go | 60 +++++++------------ .../aws/resource_aws_db_subnet_group.go | 55 +++++++---------- .../aws/resource_aws_elasticache_cluster.go | 52 ++++++---------- .../aws/resource_aws_rds_cluster_instance.go | 17 ++---- 7 files changed, 104 insertions(+), 204 deletions(-) diff --git a/builtin/providers/aws/resource_aws_db_instance.go b/builtin/providers/aws/resource_aws_db_instance.go index f5d664c55ddf..82f93817af58 100644 --- a/builtin/providers/aws/resource_aws_db_instance.go +++ b/builtin/providers/aws/resource_aws_db_instance.go @@ -9,7 +9,6 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/awserr" - "github.com/aws/aws-sdk-go/service/iam" "github.com/aws/aws-sdk-go/service/rds" "github.com/hashicorp/terraform/helper/resource" @@ -650,29 +649,21 @@ func resourceAwsDbInstanceRead(d *schema.ResourceData, meta interface{}) error { // list tags for resource // set tags conn := meta.(*AWSClient).rdsconn - arn, err := buildRDSARN(d.Id(), meta) - if err != nil { - name := "" - if v.DBName != nil && *v.DBName != "" { - name = *v.DBName - } - log.Printf("[DEBUG] Error building ARN for DB Instance, not setting Tags for DB %s", name) - } else { - d.Set("arn", arn) - resp, err := conn.ListTagsForResource(&rds.ListTagsForResourceInput{ - ResourceName: aws.String(arn), - }) + arn := buildRDSARN(d.Id(), meta.(*AWSClient).accountid, meta.(*AWSClient).region) + d.Set("arn", arn) + resp, err := conn.ListTagsForResource(&rds.ListTagsForResourceInput{ + ResourceName: aws.String(arn), + }) - if err != nil { - log.Printf("[DEBUG] Error retrieving tags for ARN: %s", arn) - } + if err != nil { + log.Printf("[DEBUG] Error retrieving tags for ARN: %s", arn) + } - var dt []*rds.Tag - if len(resp.TagList) > 0 { - dt = resp.TagList - } - d.Set("tags", tagsToMapRDS(dt)) + var dt []*rds.Tag + if len(resp.TagList) > 0 { + dt = resp.TagList } + d.Set("tags", tagsToMapRDS(dt)) // Create an empty schema.Set to hold all vpc security group ids ids := &schema.Set{ @@ -905,12 +896,11 @@ func resourceAwsDbInstanceUpdate(d *schema.ResourceData, meta interface{}) error } } - if arn, err := buildRDSARN(d.Id(), meta); err == nil { - if err := setTagsRDS(conn, d, arn); err != nil { - return err - } else { - d.SetPartial("tags") - } + arn := buildRDSARN(d.Id(), meta.(*AWSClient).accountid, meta.(*AWSClient).region) + if err := setTagsRDS(conn, d, arn); err != nil { + return err + } else { + d.SetPartial("tags") } d.Partial(false) @@ -972,16 +962,7 @@ func resourceAwsDbInstanceStateRefreshFunc( } } -func buildRDSARN(identifier string, meta interface{}) (string, error) { - iamconn := meta.(*AWSClient).iamconn - region := meta.(*AWSClient).region - // An zero value GetUserInput{} defers to the currently logged in user - resp, err := iamconn.GetUser(&iam.GetUserInput{}) - if err != nil { - return "", err - } - userARN := *resp.User.Arn - accountID := strings.Split(userARN, ":")[4] - arn := fmt.Sprintf("arn:aws:rds:%s:%s:db:%s", region, accountID, identifier) - return arn, nil +func buildRDSARN(identifier, accountid, region string) string { + arn := fmt.Sprintf("arn:aws:rds:%s:%s:db:%s", region, accountid, identifier) + return arn } diff --git a/builtin/providers/aws/resource_aws_db_instance_test.go b/builtin/providers/aws/resource_aws_db_instance_test.go index fcff06f65da2..1327c34fd7a0 100644 --- a/builtin/providers/aws/resource_aws_db_instance_test.go +++ b/builtin/providers/aws/resource_aws_db_instance_test.go @@ -239,12 +239,9 @@ func testAccCheckAWSDBInstanceSnapshot(s *terraform.State) error { } } else { // snapshot was found // verify we have the tags copied to the snapshot - instanceARN, err := buildRDSARN(snapshot_identifier, testAccProvider.Meta()) + instanceARN := buildRDSARN(snapshot_identifier, testAccProvider.Meta().(*AWSClient).accountid, testAccProvider.Meta().(*AWSClient).region) // tags have a different ARN, just swapping :db: for :snapshot: tagsARN := strings.Replace(instanceARN, ":db:", ":snapshot:", 1) - if err != nil { - return fmt.Errorf("Error building ARN for tags check with ARN (%s): %s", tagsARN, err) - } resp, err := conn.ListTagsForResource(&rds.ListTagsForResourceInput{ ResourceName: aws.String(tagsARN), }) diff --git a/builtin/providers/aws/resource_aws_db_parameter_group.go b/builtin/providers/aws/resource_aws_db_parameter_group.go index 2fde74cb5032..a7d871c32cec 100644 --- a/builtin/providers/aws/resource_aws_db_parameter_group.go +++ b/builtin/providers/aws/resource_aws_db_parameter_group.go @@ -13,7 +13,6 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/awserr" - "github.com/aws/aws-sdk-go/service/iam" "github.com/aws/aws-sdk-go/service/rds" ) @@ -144,30 +143,21 @@ func resourceAwsDbParameterGroupRead(d *schema.ResourceData, meta interface{}) e d.Set("parameter", flattenParameters(describeParametersResp.Parameters)) - paramGroup := describeResp.DBParameterGroups[0] - arn, err := buildRDSPGARN(d, meta) - if err != nil { - name := "" - if paramGroup.DBParameterGroupName != nil && *paramGroup.DBParameterGroupName != "" { - name = *paramGroup.DBParameterGroupName - } - log.Printf("[DEBUG] Error building ARN for DB Parameter Group, not setting Tags for Param Group %s", name) - } else { - d.Set("arn", arn) - resp, err := rdsconn.ListTagsForResource(&rds.ListTagsForResourceInput{ - ResourceName: aws.String(arn), - }) + arn := buildRDSPGARN(d.Id(), meta.(*AWSClient).accountid, meta.(*AWSClient).region) + d.Set("arn", arn) + resp, err := rdsconn.ListTagsForResource(&rds.ListTagsForResourceInput{ + ResourceName: aws.String(arn), + }) - if err != nil { - log.Printf("[DEBUG] Error retrieving tags for ARN: %s", arn) - } + if err != nil { + log.Printf("[DEBUG] Error retrieving tags for ARN: %s", arn) + } - var dt []*rds.Tag - if len(resp.TagList) > 0 { - dt = resp.TagList - } - d.Set("tags", tagsToMapRDS(dt)) + var dt []*rds.Tag + if len(resp.TagList) > 0 { + dt = resp.TagList } + d.Set("tags", tagsToMapRDS(dt)) return nil } @@ -210,12 +200,11 @@ func resourceAwsDbParameterGroupUpdate(d *schema.ResourceData, meta interface{}) d.SetPartial("parameter") } - if arn, err := buildRDSPGARN(d, meta); err == nil { - if err := setTagsRDS(rdsconn, d, arn); err != nil { - return err - } else { - d.SetPartial("tags") - } + arn := buildRDSPGARN(d.Id(), meta.(*AWSClient).accountid, meta.(*AWSClient).region) + if err := setTagsRDS(rdsconn, d, arn); err != nil { + return err + } else { + d.SetPartial("tags") } d.Partial(false) @@ -271,16 +260,7 @@ func resourceAwsDbParameterHash(v interface{}) int { return hashcode.String(buf.String()) } -func buildRDSPGARN(d *schema.ResourceData, meta interface{}) (string, error) { - iamconn := meta.(*AWSClient).iamconn - region := meta.(*AWSClient).region - // An zero value GetUserInput{} defers to the currently logged in user - resp, err := iamconn.GetUser(&iam.GetUserInput{}) - if err != nil { - return "", err - } - userARN := *resp.User.Arn - accountID := strings.Split(userARN, ":")[4] - arn := fmt.Sprintf("arn:aws:rds:%s:%s:pg:%s", region, accountID, d.Id()) - return arn, nil +func buildRDSPGARN(identifier, accountid, region string) string { + arn := fmt.Sprintf("arn:aws:rds:%s:%s:pg:%s", region, accountid, identifier) + return arn } diff --git a/builtin/providers/aws/resource_aws_db_security_group.go b/builtin/providers/aws/resource_aws_db_security_group.go index 86bce46cd950..e08ed28b4d4f 100644 --- a/builtin/providers/aws/resource_aws_db_security_group.go +++ b/builtin/providers/aws/resource_aws_db_security_group.go @@ -4,12 +4,10 @@ import ( "bytes" "fmt" "log" - "strings" "time" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/awserr" - "github.com/aws/aws-sdk-go/service/iam" "github.com/aws/aws-sdk-go/service/rds" "github.com/hashicorp/go-multierror" "github.com/hashicorp/terraform/helper/hashcode" @@ -170,29 +168,21 @@ func resourceAwsDbSecurityGroupRead(d *schema.ResourceData, meta interface{}) er d.Set("ingress", rules) conn := meta.(*AWSClient).rdsconn - arn, err := buildRDSSecurityGroupARN(d, meta) - if err != nil { - name := "" - if sg.DBSecurityGroupName != nil && *sg.DBSecurityGroupName != "" { - name = *sg.DBSecurityGroupName - } - log.Printf("[DEBUG] Error building ARN for DB Security Group, not setting Tags for DB Security Group %s", name) - } else { - d.Set("arn", arn) - resp, err := conn.ListTagsForResource(&rds.ListTagsForResourceInput{ - ResourceName: aws.String(arn), - }) + arn := buildRDSSecurityGroupARN(d.Id(), meta.(*AWSClient).accountid, meta.(*AWSClient).region) + d.Set("arn", arn) + resp, err := conn.ListTagsForResource(&rds.ListTagsForResourceInput{ + ResourceName: aws.String(arn), + }) - if err != nil { - log.Printf("[DEBUG] Error retrieving tags for ARN: %s", arn) - } + if err != nil { + log.Printf("[DEBUG] Error retrieving tags for ARN: %s", arn) + } - var dt []*rds.Tag - if len(resp.TagList) > 0 { - dt = resp.TagList - } - d.Set("tags", tagsToMapRDS(dt)) + var dt []*rds.Tag + if len(resp.TagList) > 0 { + dt = resp.TagList } + d.Set("tags", tagsToMapRDS(dt)) return nil } @@ -201,12 +191,11 @@ func resourceAwsDbSecurityGroupUpdate(d *schema.ResourceData, meta interface{}) conn := meta.(*AWSClient).rdsconn d.Partial(true) - if arn, err := buildRDSSecurityGroupARN(d, meta); err == nil { - if err := setTagsRDS(conn, d, arn); err != nil { - return err - } else { - d.SetPartial("tags") - } + arn := buildRDSSecurityGroupARN(d.Id(), meta.(*AWSClient).accountid, meta.(*AWSClient).region) + if err := setTagsRDS(conn, d, arn); err != nil { + return err + } else { + d.SetPartial("tags") } d.Partial(false) @@ -344,16 +333,7 @@ func resourceAwsDbSecurityGroupStateRefreshFunc( } } -func buildRDSSecurityGroupARN(d *schema.ResourceData, meta interface{}) (string, error) { - iamconn := meta.(*AWSClient).iamconn - region := meta.(*AWSClient).region - // An zero value GetUserInput{} defers to the currently logged in user - resp, err := iamconn.GetUser(&iam.GetUserInput{}) - if err != nil { - return "", err - } - userARN := *resp.User.Arn - accountID := strings.Split(userARN, ":")[4] - arn := fmt.Sprintf("arn:aws:rds:%s:%s:secgrp:%s", region, accountID, d.Id()) - return arn, nil +func buildRDSSecurityGroupARN(identifier, accountid, region string) string { + arn := fmt.Sprintf("arn:aws:rds:%s:%s:secgrp:%s", region, accountid, identifier) + return arn } diff --git a/builtin/providers/aws/resource_aws_db_subnet_group.go b/builtin/providers/aws/resource_aws_db_subnet_group.go index 233533396e26..9c61bb31a8d2 100644 --- a/builtin/providers/aws/resource_aws_db_subnet_group.go +++ b/builtin/providers/aws/resource_aws_db_subnet_group.go @@ -9,7 +9,6 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/awserr" - "github.com/aws/aws-sdk-go/service/iam" "github.com/aws/aws-sdk-go/service/rds" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/schema" @@ -127,25 +126,21 @@ func resourceAwsDbSubnetGroupRead(d *schema.ResourceData, meta interface{}) erro // list tags for resource // set tags conn := meta.(*AWSClient).rdsconn - arn, err := buildRDSsubgrpARN(d, meta) - if err != nil { - log.Printf("[DEBUG] Error building ARN for DB Subnet Group, not setting Tags for group %s", *subnetGroup.DBSubnetGroupName) - } else { - d.Set("arn", arn) - resp, err := conn.ListTagsForResource(&rds.ListTagsForResourceInput{ - ResourceName: aws.String(arn), - }) + arn := buildRDSsubgrpARN(d.Id(), meta.(*AWSClient).accountid, meta.(*AWSClient).region) + d.Set("arn", arn) + resp, err := conn.ListTagsForResource(&rds.ListTagsForResourceInput{ + ResourceName: aws.String(arn), + }) - if err != nil { - log.Printf("[DEBUG] Error retreiving tags for ARN: %s", arn) - } + if err != nil { + log.Printf("[DEBUG] Error retreiving tags for ARN: %s", arn) + } - var dt []*rds.Tag - if len(resp.TagList) > 0 { - dt = resp.TagList - } - d.Set("tags", tagsToMapRDS(dt)) + var dt []*rds.Tag + if len(resp.TagList) > 0 { + dt = resp.TagList } + d.Set("tags", tagsToMapRDS(dt)) return nil } @@ -175,12 +170,11 @@ func resourceAwsDbSubnetGroupUpdate(d *schema.ResourceData, meta interface{}) er } } - if arn, err := buildRDSsubgrpARN(d, meta); err == nil { - if err := setTagsRDS(conn, d, arn); err != nil { - return err - } else { - d.SetPartial("tags") - } + arn := buildRDSsubgrpARN(d.Id(), meta.(*AWSClient).accountid, meta.(*AWSClient).region) + if err := setTagsRDS(conn, d, arn); err != nil { + return err + } else { + d.SetPartial("tags") } return resourceAwsDbSubnetGroupRead(d, meta) @@ -224,18 +218,9 @@ func resourceAwsDbSubnetGroupDeleteRefreshFunc( } } -func buildRDSsubgrpARN(d *schema.ResourceData, meta interface{}) (string, error) { - iamconn := meta.(*AWSClient).iamconn - region := meta.(*AWSClient).region - // An zero value GetUserInput{} defers to the currently logged in user - resp, err := iamconn.GetUser(&iam.GetUserInput{}) - if err != nil { - return "", err - } - userARN := *resp.User.Arn - accountID := strings.Split(userARN, ":")[4] - arn := fmt.Sprintf("arn:aws:rds:%s:%s:subgrp:%s", region, accountID, d.Id()) - return arn, nil +func buildRDSsubgrpARN(identifier, accountid, region string) string { + arn := fmt.Sprintf("arn:aws:rds:%s:%s:subgrp:%s", region, accountid, identifier) + return arn } func validateSubnetGroupName(v interface{}, k string) (ws []string, errors []error) { diff --git a/builtin/providers/aws/resource_aws_elasticache_cluster.go b/builtin/providers/aws/resource_aws_elasticache_cluster.go index 7ff086a2654c..04a6010237f3 100644 --- a/builtin/providers/aws/resource_aws_elasticache_cluster.go +++ b/builtin/providers/aws/resource_aws_elasticache_cluster.go @@ -10,7 +10,6 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/service/elasticache" - "github.com/aws/aws-sdk-go/service/iam" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/schema" ) @@ -347,24 +346,20 @@ func resourceAwsElasticacheClusterRead(d *schema.ResourceData, meta interface{}) } // list tags for resource // set tags - arn, err := buildECARN(d, meta) + arn := buildECARN(d.Id(), meta.(*AWSClient).accountid, meta.(*AWSClient).region) + resp, err := conn.ListTagsForResource(&elasticache.ListTagsForResourceInput{ + ResourceName: aws.String(arn), + }) + if err != nil { - log.Printf("[DEBUG] Error building ARN for ElastiCache Cluster, not setting Tags for cluster %s", *c.CacheClusterId) - } else { - resp, err := conn.ListTagsForResource(&elasticache.ListTagsForResourceInput{ - ResourceName: aws.String(arn), - }) - - if err != nil { - log.Printf("[DEBUG] Error retrieving tags for ARN: %s", arn) - } + log.Printf("[DEBUG] Error retrieving tags for ARN: %s", arn) + } - var et []*elasticache.Tag - if len(resp.TagList) > 0 { - et = resp.TagList - } - d.Set("tags", tagsToMapEC(et)) + var et []*elasticache.Tag + if len(resp.TagList) > 0 { + et = resp.TagList } + d.Set("tags", tagsToMapEC(et)) } return nil @@ -372,13 +367,9 @@ func resourceAwsElasticacheClusterRead(d *schema.ResourceData, meta interface{}) func resourceAwsElasticacheClusterUpdate(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).elasticacheconn - arn, err := buildECARN(d, meta) - if err != nil { - log.Printf("[DEBUG] Error building ARN for ElastiCache Cluster, not updating Tags for cluster %s", d.Id()) - } else { - if err := setTagsEC(conn, d, arn); err != nil { - return err - } + arn := buildECARN(d.Id(), meta.(*AWSClient).accountid, meta.(*AWSClient).region) + if err := setTagsEC(conn, d, arn); err != nil { + return err } req := &elasticache.ModifyCacheClusterInput{ @@ -619,16 +610,7 @@ func cacheClusterStateRefreshFunc(conn *elasticache.ElastiCache, clusterID, give } } -func buildECARN(d *schema.ResourceData, meta interface{}) (string, error) { - iamconn := meta.(*AWSClient).iamconn - region := meta.(*AWSClient).region - // An zero value GetUserInput{} defers to the currently logged in user - resp, err := iamconn.GetUser(&iam.GetUserInput{}) - if err != nil { - return "", err - } - userARN := *resp.User.Arn - accountID := strings.Split(userARN, ":")[4] - arn := fmt.Sprintf("arn:aws:elasticache:%s:%s:cluster:%s", region, accountID, d.Id()) - return arn, nil +func buildECARN(identifier, accountid, region string) string { + arn := fmt.Sprintf("arn:aws:elasticache:%s:%s:cluster:%s", region, accountid, identifier) + return arn } diff --git a/builtin/providers/aws/resource_aws_rds_cluster_instance.go b/builtin/providers/aws/resource_aws_rds_cluster_instance.go index 59cf4c57ca29..69d92ef85b19 100644 --- a/builtin/providers/aws/resource_aws_rds_cluster_instance.go +++ b/builtin/providers/aws/resource_aws_rds_cluster_instance.go @@ -170,13 +170,9 @@ func resourceAwsRDSClusterInstanceRead(d *schema.ResourceData, meta interface{}) d.Set("publicly_accessible", db.PubliclyAccessible) // Fetch and save tags - arn, err := buildRDSARN(d.Id(), meta) - if err != nil { - log.Printf("[DEBUG] Error building ARN for RDS Cluster Instance (%s), not setting Tags", *db.DBInstanceIdentifier) - } else { - if err := saveTagsRDS(conn, d, arn); err != nil { - log.Printf("[WARN] Failed to save tags for RDS Cluster Instance (%s): %s", *db.DBClusterIdentifier, err) - } + arn := buildRDSARN(d.Id(), meta.(*AWSClient).accountid, meta.(*AWSClient).region) + if err := saveTagsRDS(conn, d, arn); err != nil { + log.Printf("[WARN] Failed to save tags for RDS Cluster Instance (%s): %s", *db.DBClusterIdentifier, err) } return nil @@ -185,10 +181,9 @@ func resourceAwsRDSClusterInstanceRead(d *schema.ResourceData, meta interface{}) func resourceAwsRDSClusterInstanceUpdate(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).rdsconn - if arn, err := buildRDSARN(d.Id(), meta); err == nil { - if err := setTagsRDS(conn, d, arn); err != nil { - return err - } + arn := buildRDSARN(d.Id(), meta.(*AWSClient).accountid, meta.(*AWSClient).region) + if err := setTagsRDS(conn, d, arn); err != nil { + return err } return resourceAwsRDSClusterInstanceRead(d, meta)