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

sns/topic: ISO-friendly tagging #22511

Merged
merged 16 commits into from
Jan 12, 2022
3 changes: 3 additions & 0 deletions .changelog/22511.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_sns_topic: Attempt `tags`-on-create, fallback to tag after create, and allow some `tags` errors to be non-fatal to support non-standard AWS partitions (i.e., ISO)
```
41 changes: 39 additions & 2 deletions internal/service/sns/topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,13 @@ func resourceTopicCreate(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] Creating SNS Topic: %s", input)
output, err := conn.CreateTopic(input)

// Some partitions may not support tag-on-create
if input.Tags != nil && (tfawserr.ErrCodeContains(err, "AccessDenied") || tfawserr.ErrCodeContains(err, "InvalidAction") || tfawserr.ErrCodeContains(err, "AuthorizationError")) {
ewbankkit marked this conversation as resolved.
Show resolved Hide resolved
log.Printf("[WARN] SNS Topic (%s) create failed (%s) with tags. Trying create without tags.", d.Id(), err)
input.Tags = nil
output, err = conn.CreateTopic(input)
}

if err != nil {
return fmt.Errorf("error creating SNS Topic (%s): %w", name, err)
}
Expand All @@ -262,6 +269,21 @@ func resourceTopicCreate(d *schema.ResourceData, meta interface{}) error {
return err
}

// Post-create tagging supported in some partitions
if input.Tags == nil && len(tags) > 0 {
err := UpdateTags(conn, d.Id(), nil, tags)

if v, ok := d.GetOk("tags"); !ok || len(v.(map[string]interface{})) == 0 && (tfawserr.ErrCodeContains(err, "AccessDenied") || tfawserr.ErrCodeContains(err, "InvalidAction") || tfawserr.ErrCodeContains(err, "AuthorizationError")) {
// if default tags only, log and continue (i.e., should error if explicitly setting tags and they can't be)
log.Printf("[WARN] error adding tags after create for SNS Topic (%s): %s", d.Id(), err)
return resourceTopicRead(d, meta)
}

if err != nil {
return fmt.Errorf("error creating SNS Topic (%s) tags: %w", name, err)
}
}

return resourceTopicRead(d, meta)
}

Expand Down Expand Up @@ -304,6 +326,12 @@ func resourceTopicRead(d *schema.ResourceData, meta interface{}) error {

tags, err := ListTags(conn, d.Id())

if tfawserr.ErrCodeContains(err, "AccessDenied") || tfawserr.ErrCodeContains(err, "AuthorizationError") || tfawserr.ErrCodeContains(err, "InvalidAction") {
// ISO partitions may not support tagging, giving error
log.Printf("[WARN] Unable to list tags for SNS topic %s: %s", d.Id(), err)
return nil
}

if err != nil {
return fmt.Errorf("error listing tags for SNS Topic (%s): %w", d.Id(), err)
}
Expand Down Expand Up @@ -341,8 +369,17 @@ func resourceTopicUpdate(d *schema.ResourceData, meta interface{}) error {

if d.HasChange("tags_all") {
o, n := d.GetChange("tags_all")
if err := UpdateTags(conn, d.Id(), o, n); err != nil {
return fmt.Errorf("error updating tags: %w", err)

err := UpdateTags(conn, d.Id(), o, n)

if tfawserr.ErrCodeContains(err, "AccessDenied") || tfawserr.ErrCodeContains(err, "AuthorizationError") || tfawserr.ErrCodeContains(err, "InvalidAction") {
// ISO partitions may not support tagging, giving error
log.Printf("[WARN] Unable to update tags for SNS topic %s: %s", d.Id(), err)
return resourceTopicRead(d, meta)
}

if err != nil {
return fmt.Errorf("error updating SNS topic tags: %w", err)
}
}

Expand Down