diff --git a/aws/resource_aws_opsworks_stack.go b/aws/resource_aws_opsworks_stack.go index 177a4a93453b..53a8604ff2ca 100644 --- a/aws/resource_aws_opsworks_stack.go +++ b/aws/resource_aws_opsworks_stack.go @@ -5,15 +5,15 @@ import ( "log" "time" - "github.com/hashicorp/terraform-plugin-sdk/helper/resource" - "github.com/hashicorp/terraform-plugin-sdk/helper/schema" - "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/arn" "github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/aws/request" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/opsworks" + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" + "github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags" ) func resourceAwsOpsworksStack() *schema.Resource { @@ -335,7 +335,8 @@ func resourceAwsOpsworksStackRead(d *schema.ResourceData, meta interface{}) erro } stack := resp.Stacks[0] - d.Set("arn", stack.Arn) + arn := aws.StringValue(stack.Arn) + d.Set("arn", arn) d.Set("agent_version", stack.AgentVersion) d.Set("name", stack.Name) d.Set("region", stack.Region) @@ -366,6 +367,16 @@ func resourceAwsOpsworksStackRead(d *schema.ResourceData, meta interface{}) erro } resourceAwsOpsworksSetStackCustomCookbooksSource(d, stack.CustomCookbooksSource) + tags, err := keyvaluetags.OpsworksListTags(client, arn) + + if err != nil { + return fmt.Errorf("error listing tags for Opsworks stack (%s): %s", arn, err) + } + + if err := d.Set("tags", tags.IgnoreAws().Map()); err != nil { + return fmt.Errorf("error setting tags: %s", err) + } + return nil } @@ -528,18 +539,6 @@ func resourceAwsOpsworksStackUpdate(d *schema.ResourceData, meta interface{}) er req.Attributes["Color"] = aws.String(v.(string)) } - arn := arn.ARN{ - Partition: meta.(*AWSClient).partition, - Region: meta.(*AWSClient).region, - Service: "opsworks", - AccountID: meta.(*AWSClient).accountid, - Resource: fmt.Sprintf("stack/%s/", d.Id()), - } - - if tagErr := setTagsOpsworks(client, d, arn.String()); tagErr != nil { - return tagErr - } - req.ChefConfiguration = &opsworks.ChefConfiguration{ BerkshelfVersion: aws.String(d.Get("berkshelf_version").(string)), ManageBerkshelf: aws.Bool(d.Get("manage_berkshelf").(bool)), @@ -557,6 +556,21 @@ func resourceAwsOpsworksStackUpdate(d *schema.ResourceData, meta interface{}) er return err } + arn := arn.ARN{ + Partition: meta.(*AWSClient).partition, + Region: meta.(*AWSClient).region, + Service: "opsworks", + AccountID: meta.(*AWSClient).accountid, + Resource: fmt.Sprintf("stack/%s/", d.Id()), + }.String() + if d.HasChange("tags") { + o, n := d.GetChange("tags") + + if err := keyvaluetags.OpsworksUpdateTags(client, arn, o, n); err != nil { + return fmt.Errorf("error updating Opsworks stack (%s) tags: %s", arn, err) + } + } + return resourceAwsOpsworksStackRead(d, meta) } diff --git a/aws/resource_aws_opsworks_stack_test.go b/aws/resource_aws_opsworks_stack_test.go index 4b22e44114af..b69166597c0f 100644 --- a/aws/resource_aws_opsworks_stack_test.go +++ b/aws/resource_aws_opsworks_stack_test.go @@ -130,10 +130,9 @@ func TestAccAWSOpsworksStack_noVpcCreateTags(t *testing.T) { ), }, { - ResourceName: resourceName, - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"tags"}, + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, }, { Config: testAccAwsOpsworksStackConfigNoVpcUpdateTags(stackName), diff --git a/aws/tagsOpsworks.go b/aws/tagsOpsworks.go deleted file mode 100644 index 24163859cfe7..000000000000 --- a/aws/tagsOpsworks.go +++ /dev/null @@ -1,50 +0,0 @@ -package aws - -import ( - "log" - - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/opsworks" - "github.com/hashicorp/terraform-plugin-sdk/helper/schema" -) - -// setTags is a helper to set the tags for a resource. It expects the -// tags field to be named "tags" -func setTagsOpsworks(conn *opsworks.OpsWorks, d *schema.ResourceData, arn string) error { - if d.HasChange("tags") { - oraw, nraw := d.GetChange("tags") - o := oraw.(map[string]interface{}) - n := nraw.(map[string]interface{}) - create, remove := diffTagsGeneric(o, n) - - // Set tags - if len(remove) > 0 { - log.Printf("[DEBUG] Removing tags: %#v", remove) - keys := make([]*string, 0, len(remove)) - for k := range remove { - keys = append(keys, aws.String(k)) - } - - _, err := conn.UntagResource(&opsworks.UntagResourceInput{ - ResourceArn: aws.String(arn), - TagKeys: keys, - }) - if err != nil { - return err - } - } - if len(create) > 0 { - log.Printf("[DEBUG] Creating tags: %#v", create) - - _, err := conn.TagResource(&opsworks.TagResourceInput{ - ResourceArn: aws.String(arn), - Tags: create, - }) - if err != nil { - return err - } - } - } - - return nil -}