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

dynamodb_table: Fix replica.*propagate_tags bug #26257

Merged
merged 4 commits into from
Aug 11, 2022
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
3 changes: 3 additions & 0 deletions .changelog/26257.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_dynamodb_table: Fix `replica.*.propagate_tags` not propagating tags to newly added replicas
```
34 changes: 22 additions & 12 deletions internal/service/dynamodb/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ func resourceTableCreate(d *schema.ResourceData, meta interface{}) error {
return create.Error(names.DynamoDB, create.ErrActionCreating, ResNameTable, d.Id(), fmt.Errorf("replicas: %w", err))
}

if err := updateReplicaTags(conn, aws.StringValue(output.TableArn), v.List(), nil, tags, meta.(*conns.AWSClient).TerraformVersion); err != nil {
if err := updateReplicaTags(conn, aws.StringValue(output.TableArn), v.List(), tags, meta.(*conns.AWSClient).TerraformVersion); err != nil {
return create.Error(names.DynamoDB, create.ErrActionCreating, ResNameTable, d.Id(), fmt.Errorf("replica tags: %w", err))
}
}
Expand Down Expand Up @@ -912,20 +912,27 @@ func resourceTableUpdate(d *schema.ResourceData, meta interface{}) error {
}
}

replicaTagsChange := false
if d.HasChange("replica") {
replicaTagsChange = true

if err := updateReplica(d, conn, meta.(*conns.AWSClient).TerraformVersion); err != nil {
return create.Error(names.DynamoDB, create.ErrActionUpdating, ResNameTable, d.Id(), err)
}
}

if d.HasChange("tags_all") {
replicaTagsChange = true

o, n := d.GetChange("tags_all")
if err := UpdateTags(conn, d.Get("arn").(string), o, n); err != nil {
return create.Error(names.DynamoDB, create.ErrActionUpdating, ResNameTable, d.Id(), err)
}
}

if replicaTagsChange {
if v, ok := d.Get("replica").(*schema.Set); ok && v.Len() > 0 {
if err := updateReplicaTags(conn, d.Get("arn").(string), v.List(), o, n, meta.(*conns.AWSClient).TerraformVersion); err != nil {
if err := updateReplicaTags(conn, d.Get("arn").(string), v.List(), d.Get("tags_all"), meta.(*conns.AWSClient).TerraformVersion); err != nil {
return create.Error(names.DynamoDB, create.ErrActionUpdating, ResNameTable, d.Id(), err)
}
}
Expand Down Expand Up @@ -1070,7 +1077,7 @@ func createReplicas(conn *dynamodb.DynamoDB, tableName string, tfList []interfac
return nil
}

func updateReplicaTags(conn *dynamodb.DynamoDB, rn string, replicas []interface{}, oldTags interface{}, newTags interface{}, terraformVersion string) error {
func updateReplicaTags(conn *dynamodb.DynamoDB, rn string, replicas []interface{}, newTags interface{}, terraformVersion string) error {
for _, tfMapRaw := range replicas {
tfMap, ok := tfMapRaw.(map[string]interface{})

Expand All @@ -1085,21 +1092,24 @@ func updateReplicaTags(conn *dynamodb.DynamoDB, rn string, replicas []interface{
}

if v, ok := tfMap["propagate_tags"].(bool); ok && v {
if aws.StringValue(conn.Config.Region) != region {
session, err := conns.NewSessionForRegion(&conn.Config, region, terraformVersion)
if err != nil {
return fmt.Errorf("updating replica (%s) tags: %w", region, err)
}

conn = dynamodb.New(session)
session, err := conns.NewSessionForRegion(&conn.Config, region, terraformVersion)
if err != nil {
return fmt.Errorf("updating replica (%s) tags: %w", region, err)
}

newARN, err := ARNForNewRegion(rn, region)
conn = dynamodb.New(session)

repARN, err := ARNForNewRegion(rn, region)
if err != nil {
return fmt.Errorf("per region ARN for replica (%s): %w", region, err)
}

if err := UpdateTags(conn, newARN, oldTags, newTags); err != nil {
oldTags, err := ListTags(conn, repARN)
if err != nil {
return fmt.Errorf("listing tags (%s): %w", repARN, err)
}

if err := UpdateTags(conn, repARN, oldTags, newTags); err != nil {
return fmt.Errorf("updating tags: %w", err)
}
}
Expand Down
Loading