Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resource/aws_sns_topic: Refactor tagging logic to use keyvaluetags package and call Read after Create #10741

Merged
merged 1 commit into from
Nov 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.