From 6336b05644a773aad9a442e917b6f48be257531c Mon Sep 17 00:00:00 2001 From: stack72 Date: Mon, 13 Jun 2016 13:50:42 +0200 Subject: [PATCH] provider/aws: Change the way ARNs are built 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 89a3945d9695..c8da69e45263 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" @@ -688,7 +687,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 != "" { @@ -949,7 +948,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 { @@ -1016,16 +1015,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 91a484f0e825..72e5d375b9d3 100644 --- a/builtin/providers/aws/resource_aws_db_instance_test.go +++ b/builtin/providers/aws/resource_aws_db_instance_test.go @@ -284,9 +284,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 2dcbc7fca1c0..b7563a4fa623 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" ) @@ -146,7 +145,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 != "" { @@ -211,7 +210,7 @@ func resourceAwsDbParameterGroupUpdate(d *schema.ResourceData, meta interface{}) d.SetPartial("parameter") } - 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 { @@ -272,16 +271,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 6edffb6c5812..69db2aa6d900 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" @@ -171,7 +169,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 != "" { @@ -202,7 +200,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 { @@ -345,16 +343,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 718cdc711488..ae12cbfb1a94 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" @@ -128,7 +127,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 { @@ -176,7 +175,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 { @@ -225,18 +224,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 7ff086a2654c..cd79a2b1ca85 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 09580cc896a2..40f26405f775 100644 --- a/builtin/providers/aws/resource_aws_rds_cluster_instance.go +++ b/builtin/providers/aws/resource_aws_rds_cluster_instance.go @@ -191,7 +191,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 { @@ -250,7 +250,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 b07ee7d5009b..1906f1ee36e9 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" @@ -144,7 +142,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 != "" { @@ -209,7 +207,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 { @@ -260,16 +258,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 + }