From 5c0662f0eb7c1b2ac2bae0d81cc20a485589a79b Mon Sep 17 00:00:00 2001 From: Paul Stack Date: Sun, 7 Aug 2016 17:36:00 +1000 Subject: [PATCH] provider/aws: Change the way ARNs are built (#7151) ARNs used to be build using the iamconn.GetUser func call. This wouldn't work on some scenarios and was changed so that we can expose the AccountId and Region via meta This commit just changes the build ARN funcs to use this new way of doing things --- .../providers/aws/resource_aws_db_instance.go | 20 +++++++----------- .../aws/resource_aws_db_instance_test.go | 4 ++-- .../aws/resource_aws_db_parameter_group.go | 20 +++++++----------- .../aws/resource_aws_db_security_group.go | 21 +++++++------------ .../aws/resource_aws_db_subnet_group.go | 20 +++++++----------- .../aws/resource_aws_elasticache_cluster.go | 20 +++++++----------- .../aws/resource_aws_rds_cluster_instance.go | 4 ++-- ...esource_aws_rds_cluster_parameter_group.go | 21 +++++++------------ 8 files changed, 46 insertions(+), 84 deletions(-) diff --git a/builtin/providers/aws/resource_aws_db_instance.go b/builtin/providers/aws/resource_aws_db_instance.go index c7615161b..4fe66a2b4 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" @@ -693,7 +692,7 @@ func resourceAwsDbInstanceRead(d *schema.ResourceData, meta interface{}) error { // list tags for resource // set tags conn := meta.(*AWSClient).rdsconn - arn, err := buildRDSARN(d.Id(), meta) + arn, err := buildRDSARN(d.Id(), meta.(*AWSClient).accountid, meta.(*AWSClient).region) if err != nil { name := "" if v.DBName != nil && *v.DBName != "" { @@ -975,7 +974,7 @@ func resourceAwsDbInstanceUpdate(d *schema.ResourceData, meta interface{}) error } } - if arn, err := buildRDSARN(d.Id(), meta); err == nil { + if arn, err := buildRDSARN(d.Id(), meta.(*AWSClient).accountid, meta.(*AWSClient).region); err == nil { if err := setTagsRDS(conn, d, arn); err != nil { return err } else { @@ -1051,16 +1050,11 @@ 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 +func buildRDSARN(identifier, accountid, region string) (string, error) { + if accountid == "" { + return "", fmt.Errorf("Unable to construct RDS ARN because of missing AWS Account ID") } - userARN := *resp.User.Arn - accountID := strings.Split(userARN, ":")[4] - arn := fmt.Sprintf("arn:aws:rds:%s:%s:db:%s", region, accountID, identifier) + arn := fmt.Sprintf("arn:aws:rds:%s:%s:db:%s", region, accountid, identifier) return arn, nil + } diff --git a/builtin/providers/aws/resource_aws_db_instance_test.go b/builtin/providers/aws/resource_aws_db_instance_test.go index 509f875e3..11bfd75b2 100644 --- a/builtin/providers/aws/resource_aws_db_instance_test.go +++ b/builtin/providers/aws/resource_aws_db_instance_test.go @@ -347,9 +347,9 @@ func testAccCheckAWSDBInstanceSnapshot(s *terraform.State) error { if newerr.Code() == "DBSnapshotNotFound" { return fmt.Errorf("Snapshot %s not found", snapshot_identifier) } - } else { // snapshot was found + } else { // snapshot was found, // verify we have the tags copied to the snapshot - instanceARN, err := buildRDSARN(snapshot_identifier, testAccProvider.Meta()) + instanceARN, err := 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 { diff --git a/builtin/providers/aws/resource_aws_db_parameter_group.go b/builtin/providers/aws/resource_aws_db_parameter_group.go index 67ada3048..cd35fc36a 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" ) @@ -150,7 +149,7 @@ func resourceAwsDbParameterGroupRead(d *schema.ResourceData, meta interface{}) e d.Set("parameter", flattenParameters(describeParametersResp.Parameters)) paramGroup := describeResp.DBParameterGroups[0] - arn, err := buildRDSPGARN(d, meta) + arn, err := buildRDSPGARN(d.Id(), meta.(*AWSClient).accountid, meta.(*AWSClient).region) if err != nil { name := "" if paramGroup.DBParameterGroupName != nil && *paramGroup.DBParameterGroupName != "" { @@ -226,7 +225,7 @@ func resourceAwsDbParameterGroupUpdate(d *schema.ResourceData, meta interface{}) } } - if arn, err := buildRDSPGARN(d, meta); err == nil { + if arn, err := buildRDSPGARN(d.Id(), meta.(*AWSClient).accountid, meta.(*AWSClient).region); err == nil { if err := setTagsRDS(rdsconn, d, arn); err != nil { return err } else { @@ -287,16 +286,11 @@ 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 +func buildRDSPGARN(identifier, accountid, region string) (string, error) { + if accountid == "" { + return "", fmt.Errorf("Unable to construct RDS ARN because of missing AWS Account ID") } - userARN := *resp.User.Arn - accountID := strings.Split(userARN, ":")[4] - arn := fmt.Sprintf("arn:aws:rds:%s:%s:pg:%s", region, accountID, d.Id()) + arn := fmt.Sprintf("arn:aws:rds:%s:%s:pg:%s", region, accountid, identifier) return arn, nil + } diff --git a/builtin/providers/aws/resource_aws_db_security_group.go b/builtin/providers/aws/resource_aws_db_security_group.go index 3cb9693ef..972cfd8b3 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" @@ -173,7 +171,7 @@ func resourceAwsDbSecurityGroupRead(d *schema.ResourceData, meta interface{}) er d.Set("ingress", rules) conn := meta.(*AWSClient).rdsconn - arn, err := buildRDSSecurityGroupARN(d, meta) + arn, err := buildRDSSecurityGroupARN(d.Id(), meta.(*AWSClient).accountid, meta.(*AWSClient).region) if err != nil { name := "" if sg.DBSecurityGroupName != nil && *sg.DBSecurityGroupName != "" { @@ -204,7 +202,7 @@ func resourceAwsDbSecurityGroupUpdate(d *schema.ResourceData, meta interface{}) conn := meta.(*AWSClient).rdsconn d.Partial(true) - if arn, err := buildRDSSecurityGroupARN(d, meta); err == nil { + if arn, err := buildRDSSecurityGroupARN(d.Id(), meta.(*AWSClient).accountid, meta.(*AWSClient).region); err == nil { if err := setTagsRDS(conn, d, arn); err != nil { return err } else { @@ -418,16 +416,11 @@ 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 +func buildRDSSecurityGroupARN(identifier, accountid, region string) (string, error) { + if accountid == "" { + return "", fmt.Errorf("Unable to construct RDS ARN because of missing AWS Account ID") } - userARN := *resp.User.Arn - accountID := strings.Split(userARN, ":")[4] - arn := fmt.Sprintf("arn:aws:rds:%s:%s:secgrp:%s", region, accountID, d.Id()) + arn := fmt.Sprintf("arn:aws:rds:%s:%s:secgrp:%s", region, accountid, identifier) return arn, nil + } diff --git a/builtin/providers/aws/resource_aws_db_subnet_group.go b/builtin/providers/aws/resource_aws_db_subnet_group.go index 5d1ae1191..24852a75f 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" @@ -131,7 +130,7 @@ func resourceAwsDbSubnetGroupRead(d *schema.ResourceData, meta interface{}) erro // list tags for resource // set tags conn := meta.(*AWSClient).rdsconn - arn, err := buildRDSsubgrpARN(d, meta) + arn, err := buildRDSsubgrpARN(d.Id(), meta.(*AWSClient).accountid, meta.(*AWSClient).region) if err != nil { log.Printf("[DEBUG] Error building ARN for DB Subnet Group, not setting Tags for group %s", *subnetGroup.DBSubnetGroupName) } else { @@ -179,7 +178,7 @@ func resourceAwsDbSubnetGroupUpdate(d *schema.ResourceData, meta interface{}) er } } - if arn, err := buildRDSsubgrpARN(d, meta); err == nil { + if arn, err := buildRDSsubgrpARN(d.Id(), meta.(*AWSClient).accountid, meta.(*AWSClient).region); err == nil { if err := setTagsRDS(conn, d, arn); err != nil { return err } else { @@ -228,18 +227,13 @@ 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 +func buildRDSsubgrpARN(identifier, accountid, region string) (string, error) { + if accountid == "" { + return "", fmt.Errorf("Unable to construct RDS ARN because of missing AWS Account ID") } - userARN := *resp.User.Arn - accountID := strings.Split(userARN, ":")[4] - arn := fmt.Sprintf("arn:aws:rds:%s:%s:subgrp:%s", region, accountID, d.Id()) + arn := fmt.Sprintf("arn:aws:rds:%s:%s:subgrp:%s", region, accountid, identifier) return arn, nil + } 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 7ff086a26..cd79a2b1c 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,7 +346,7 @@ func resourceAwsElasticacheClusterRead(d *schema.ResourceData, meta interface{}) } // list tags for resource // set tags - arn, err := buildECARN(d, meta) + arn, err := buildECARN(d.Id(), meta.(*AWSClient).accountid, meta.(*AWSClient).region) if err != nil { log.Printf("[DEBUG] Error building ARN for ElastiCache Cluster, not setting Tags for cluster %s", *c.CacheClusterId) } else { @@ -372,7 +371,7 @@ func resourceAwsElasticacheClusterRead(d *schema.ResourceData, meta interface{}) func resourceAwsElasticacheClusterUpdate(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).elasticacheconn - arn, err := buildECARN(d, meta) + arn, err := buildECARN(d.Id(), meta.(*AWSClient).accountid, meta.(*AWSClient).region) if err != nil { log.Printf("[DEBUG] Error building ARN for ElastiCache Cluster, not updating Tags for cluster %s", d.Id()) } else { @@ -619,16 +618,11 @@ 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 +func buildECARN(identifier, accountid, region string) (string, error) { + if accountid == "" { + return "", fmt.Errorf("Unable to construct ElastiCache ARN because of missing AWS Account ID") } - userARN := *resp.User.Arn - accountID := strings.Split(userARN, ":")[4] - arn := fmt.Sprintf("arn:aws:elasticache:%s:%s:cluster:%s", region, accountID, d.Id()) + arn := fmt.Sprintf("arn:aws:elasticache:%s:%s:cluster:%s", region, accountid, identifier) return arn, nil + } diff --git a/builtin/providers/aws/resource_aws_rds_cluster_instance.go b/builtin/providers/aws/resource_aws_rds_cluster_instance.go index 745674c43..0ea5f13a2 100644 --- a/builtin/providers/aws/resource_aws_rds_cluster_instance.go +++ b/builtin/providers/aws/resource_aws_rds_cluster_instance.go @@ -212,7 +212,7 @@ func resourceAwsRDSClusterInstanceRead(d *schema.ResourceData, meta interface{}) } // Fetch and save tags - arn, err := buildRDSARN(d.Id(), meta) + arn, err := buildRDSARN(d.Id(), meta.(*AWSClient).accountid, meta.(*AWSClient).region) if err != nil { log.Printf("[DEBUG] Error building ARN for RDS Cluster Instance (%s), not setting Tags", *db.DBInstanceIdentifier) } else { @@ -271,7 +271,7 @@ func resourceAwsRDSClusterInstanceUpdate(d *schema.ResourceData, meta interface{ } - if arn, err := buildRDSARN(d.Id(), meta); err == nil { + if arn, err := buildRDSARN(d.Id(), meta.(*AWSClient).accountid, meta.(*AWSClient).region); err == nil { if err := setTagsRDS(conn, d, arn); err != nil { return err } diff --git a/builtin/providers/aws/resource_aws_rds_cluster_parameter_group.go b/builtin/providers/aws/resource_aws_rds_cluster_parameter_group.go index 5c9d7e407..31b40fd02 100644 --- a/builtin/providers/aws/resource_aws_rds_cluster_parameter_group.go +++ b/builtin/providers/aws/resource_aws_rds_cluster_parameter_group.go @@ -3,12 +3,10 @@ package aws import ( "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/terraform/helper/resource" @@ -148,7 +146,7 @@ func resourceAwsRDSClusterParameterGroupRead(d *schema.ResourceData, meta interf d.Set("parameter", flattenParameters(describeParametersResp.Parameters)) paramGroup := describeResp.DBClusterParameterGroups[0] - arn, err := buildRDSCPGARN(d, meta) + arn, err := buildRDSCPGARN(d.Id(), meta.(*AWSClient).accountid, meta.(*AWSClient).region) if err != nil { name := "" if paramGroup.DBClusterParameterGroupName != nil && *paramGroup.DBClusterParameterGroupName != "" { @@ -213,7 +211,7 @@ func resourceAwsRDSClusterParameterGroupUpdate(d *schema.ResourceData, meta inte d.SetPartial("parameter") } - if arn, err := buildRDSCPGARN(d, meta); err == nil { + if arn, err := buildRDSCPGARN(d.Id(), meta.(*AWSClient).accountid, meta.(*AWSClient).region); err == nil { if err := setTagsRDS(rdsconn, d, arn); err != nil { return err } else { @@ -264,16 +262,11 @@ func resourceAwsRDSClusterParameterGroupDeleteRefreshFunc( } } -func buildRDSCPGARN(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 +func buildRDSCPGARN(identifier, accountid, region string) (string, error) { + if accountid == "" { + return "", fmt.Errorf("Unable to construct RDS Cluster ARN because of missing AWS Account ID") } - userARN := *resp.User.Arn - accountID := strings.Split(userARN, ":")[4] - arn := fmt.Sprintf("arn:aws:rds:%s:%s:cluster-pg:%s", region, accountID, d.Id()) + arn := fmt.Sprintf("arn:aws:rds:%s:%s:cluster-pg:%s", region, accountid, identifier) return arn, nil + }