Skip to content

Commit

Permalink
resource/aws_sns_topic: Refactor tagging logic to use keyvaluetags pa…
Browse files Browse the repository at this point in the history
…ckage and call Read after Create (#10741)

Reference: #10688

Output from acceptance testing:

```
--- PASS: TestAccAWSSNSTopic_name (13.72s)
--- PASS: TestAccAWSSNSTopic_namePrefix (13.88s)
--- PASS: TestAccAWSSNSTopic_basic (14.02s)
--- PASS: TestAccAWSSNSTopic_withDeliveryPolicy (14.52s)
--- PASS: TestAccAWSSNSTopic_policy (14.77s)
--- PASS: TestAccAWSSNSTopic_encryption (22.23s)
--- PASS: TestAccAWSSNSTopic_withIAMRole (22.35s)
--- PASS: TestAccAWSSNSTopic_tags (30.82s)
--- PASS: TestAccAWSSNSTopic_deliveryStatus (31.95s)
--- PASS: TestAccAWSSNSTopic_withFakeIAMRole (129.10s)
```
  • Loading branch information
bflad authored Nov 15, 2019
1 parent b689da3 commit 2a8e6d4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 239 deletions.
34 changes: 23 additions & 11 deletions aws/resource_aws_sns_topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/structure"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
)

// Mutable attributes
Expand Down Expand Up @@ -151,7 +152,7 @@ func resourceAwsSnsTopic() *schema.Resource {

func resourceAwsSnsTopicCreate(d *schema.ResourceData, meta interface{}) error {
snsconn := meta.(*AWSClient).snsconn
tags := tagsFromMapSNS(d.Get("tags").(map[string]interface{}))
tags := keyvaluetags.New(d.Get("tags").(map[string]interface{})).IgnoreAws().SnsTags()
var name string
if v, ok := d.GetOk("name"); ok {
name = v.(string)
Expand All @@ -174,7 +175,18 @@ func resourceAwsSnsTopicCreate(d *schema.ResourceData, meta interface{}) error {
}

d.SetId(*output.TopicArn)
return resourceAwsSnsTopicUpdate(d, meta)

for terraformAttrName, snsAttrName := range SNSAttributeMap {
if d.HasChange(terraformAttrName) {
_, terraformAttrValue := d.GetChange(terraformAttrName)
err := updateAwsSnsTopicAttribute(d.Id(), snsAttrName, terraformAttrValue, snsconn)
if err != nil {
return err
}
}
}

return resourceAwsSnsTopicRead(d, meta)
}

func resourceAwsSnsTopicUpdate(d *schema.ResourceData, meta interface{}) error {
Expand All @@ -189,9 +201,11 @@ func resourceAwsSnsTopicUpdate(d *schema.ResourceData, meta interface{}) error {
}
}
}
if !d.IsNewResource() {
if err := setTagsSNS(conn, d); err != nil {
return fmt.Errorf("error updating SNS Topic tags for %s: %s", d.Id(), err)

if d.HasChange("tags") {
o, n := d.GetChange("tags")
if err := keyvaluetags.SnsUpdateTags(conn, d.Id(), o, n); err != nil {
return fmt.Errorf("error updating tags: %s", err)
}
}

Expand Down Expand Up @@ -237,15 +251,13 @@ func resourceAwsSnsTopicRead(d *schema.ResourceData, meta interface{}) error {
}
}

// List tags
tags, err := keyvaluetags.SnsListTags(snsconn, d.Id())

tagList, err := snsconn.ListTagsForResource(&sns.ListTagsForResourceInput{
ResourceArn: aws.String(d.Id()),
})
if err != nil {
return fmt.Errorf("error listing SNS Topic tags for %s: %s", d.Id(), err)
return fmt.Errorf("error listing tags for resource (%s): %s", d.Id(), err)
}
if err := d.Set("tags", tagsToMapSNS(tagList.Tags)); err != nil {

if err := d.Set("tags", tags.IgnoreAws().Map()); err != nil {
return fmt.Errorf("error setting tags: %s", err)
}

Expand Down
118 changes: 0 additions & 118 deletions aws/tagsSNS.go

This file was deleted.

110 changes: 0 additions & 110 deletions aws/tagsSNS_test.go

This file was deleted.

0 comments on commit 2a8e6d4

Please sign in to comment.