Skip to content

Commit

Permalink
resource/aws_opsworks_stack: Refactor tagging logic to keyvaluetags p…
Browse files Browse the repository at this point in the history
…ackage and refresh tags during read (#11373)

Output from acceptance testing:

```
--- PASS: TestAccAWSOpsworksStack_CustomCookbooks_SetPrivateProperties (30.54s)
--- PASS: TestAccAWSOpsworksStack_noVpcBasic (31.40s)
--- PASS: TestAccAWSOpsworksStack_noVpcCreateTags (39.24s)
--- PASS: TestAccAWSOpsworksStack_vpc (40.29s)
--- PASS: TestAccAWSOpsWorksStack_classicEndpoints (48.97s)
--- PASS: TestAccAWSOpsworksStack_noVpcChangeServiceRoleForceNew (53.44s)
```
  • Loading branch information
Kit Ewbank authored and bflad committed Dec 19, 2019
1 parent 224ce1a commit abd7feb
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 70 deletions.
46 changes: 30 additions & 16 deletions aws/resource_aws_opsworks_stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)),
Expand All @@ -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)
}

Expand Down
7 changes: 3 additions & 4 deletions aws/resource_aws_opsworks_stack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
50 changes: 0 additions & 50 deletions aws/tagsOpsworks.go

This file was deleted.

0 comments on commit abd7feb

Please sign in to comment.