Skip to content

Commit

Permalink
Merge pull request #21185 from hashicorp/elasticache-replicationgroup…
Browse files Browse the repository at this point in the history
…-tags

resource/aws_elasticache_replication_group: Fix updating tags
  • Loading branch information
gdavison authored Oct 7, 2021
2 parents a2ea7a0 + c65c2a0 commit 331c8cd
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 91 deletions.
7 changes: 7 additions & 0 deletions .changelog/21185.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:bug
resource/aws_elasticache_replication_group: Properly updates tags on the Replication Group in addition to the member clusters
```

```release-note:bug
resource/aws_elasticache_replication_group: Properly updates tags on Replication Group member clusters when scaling up
```
8 changes: 8 additions & 0 deletions aws/resource_aws_elasticache_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
)

func init() {
RegisterServiceErrorCheckFunc(elasticache.EndpointsID, testAccErrorCheckSkipElasticache)

resource.AddTestSweepers("aws_elasticache_cluster", &resource.Sweeper{
Name: "aws_elasticache_cluster",
F: testSweepElasticacheClusters,
Expand Down Expand Up @@ -76,6 +78,12 @@ func testSweepElasticacheClusters(region string) error {
return sweeperErrs.ErrorOrNil()
}

func testAccErrorCheckSkipElasticache(t *testing.T) resource.ErrorCheckFunc {
return testAccErrorCheckSkipMessagesContaining(t,
"is not suppored in this region",
)
}

func TestAccAWSElasticacheCluster_Engine_Memcached(t *testing.T) {
var ec elasticache.CacheCluster
rName := acctest.RandomWithPrefix("tf-acc-test")
Expand Down
89 changes: 32 additions & 57 deletions aws/resource_aws_elasticache_replication_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/elasticache"
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
Expand Down Expand Up @@ -429,7 +428,7 @@ func resourceAwsElasticacheReplicationGroupCreate(d *schema.ResourceData, meta i
}
resp, err := conn.CreateReplicationGroup(params)
if err != nil {
return fmt.Errorf("Error creating ElastiCache Replication Group (%s): %w", d.Get("replication_group_id").(string), err)
return fmt.Errorf("error creating ElastiCache Replication Group (%s): %w", d.Get("replication_group_id").(string), err)
}

d.SetId(aws.StringValue(resp.ReplicationGroup.ReplicationGroupId))
Expand Down Expand Up @@ -512,6 +511,28 @@ func resourceAwsElasticacheReplicationGroupRead(d *schema.ResourceData, meta int
d.Set("replication_group_id", rgp.ReplicationGroupId)
d.Set("arn", rgp.ARN)

// Tags cannot be read when the replication group is not Available
_, err = waiter.ReplicationGroupAvailable(conn, d.Id(), d.Timeout(schema.TimeoutUpdate))
if err != nil {
return fmt.Errorf("error listing tags for resource (%s): %w", aws.StringValue(rgp.ARN), err)
}
tags, err := keyvaluetags.ElasticacheListTags(conn, aws.StringValue(rgp.ARN))

if err != nil {
return fmt.Errorf("error listing tags for resource (%s): %w", aws.StringValue(rgp.ARN), err)
}

tags = tags.IgnoreAws().IgnoreConfig(ignoreTagsConfig)

//lintignore:AWSR002
if err := d.Set("tags", tags.RemoveDefaultConfig(defaultTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags: %w", err)
}

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

if rgp.NodeGroups != nil {
if len(rgp.NodeGroups[0].NodeGroupMembers) == 0 {
return nil
Expand Down Expand Up @@ -556,31 +577,6 @@ func resourceAwsElasticacheReplicationGroupRead(d *schema.ResourceData, meta int
if c.AuthTokenEnabled != nil && !aws.BoolValue(c.AuthTokenEnabled) {
d.Set("auth_token", nil)
}

arn := arn.ARN{
Partition: meta.(*AWSClient).partition,
Service: "elasticache",
Region: meta.(*AWSClient).region,
AccountID: meta.(*AWSClient).accountid,
Resource: fmt.Sprintf("cluster:%s", aws.StringValue(c.CacheClusterId)),
}.String()

tags, err := keyvaluetags.ElasticacheListTags(conn, arn)

if err != nil {
return fmt.Errorf("error listing tags for resource (%s): %w", arn, err)
}

tags = tags.IgnoreAws().IgnoreConfig(ignoreTagsConfig)

//lintignore:AWSR002
if err := d.Set("tags", tags.RemoveDefaultConfig(defaultTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags: %w", err)
}

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

return nil
Expand Down Expand Up @@ -683,32 +679,24 @@ func resourceAwsElasticacheReplicationGroupUpdate(d *schema.ResourceData, meta i
}

if requestUpdate {
err := resourceAwsElasticacheReplicationGroupModify(conn, d.Timeout(schema.TimeoutUpdate), params)
_, err := conn.ModifyReplicationGroup(params)
if err != nil {
return fmt.Errorf("error updating ElastiCache Replication Group (%s): %w", d.Id(), err)
}
}

if d.HasChange("tags_all") {
clusters := d.Get("member_clusters").(*schema.Set).List()

for _, cluster := range clusters {

arn := arn.ARN{
Partition: meta.(*AWSClient).partition,
Service: "elasticache",
Region: meta.(*AWSClient).region,
AccountID: meta.(*AWSClient).accountid,
Resource: fmt.Sprintf("cluster:%s", cluster),
}.String()

o, n := d.GetChange("tags_all")
if err := keyvaluetags.ElasticacheUpdateTags(conn, arn, o, n); err != nil {
return fmt.Errorf("error updating tags: %w", err)
}
o, n := d.GetChange("tags_all")
if err := keyvaluetags.ElasticacheUpdateTags(conn, d.Get("arn").(string), o, n); err != nil {
return fmt.Errorf("error updating tags: %w", err)
}
}

_, err := waiter.ReplicationGroupAvailable(conn, d.Id(), d.Timeout(schema.TimeoutUpdate))
if err != nil {
return fmt.Errorf("error waiting for modification: %w", err)
}

return resourceAwsElasticacheReplicationGroupRead(d, meta)
}

Expand Down Expand Up @@ -973,19 +961,6 @@ func elasticacheReplicationGroupDecreaseNumCacheClusters(conn *elasticache.Elast
return nil
}

func resourceAwsElasticacheReplicationGroupModify(conn *elasticache.ElastiCache, timeout time.Duration, input *elasticache.ModifyReplicationGroupInput) error {
_, err := conn.ModifyReplicationGroup(input)
if err != nil {
return fmt.Errorf("error requesting modification: %w", err)
}

_, err = waiter.ReplicationGroupAvailable(conn, aws.StringValue(input.ReplicationGroupId), timeout)
if err != nil {
return fmt.Errorf("error waiting for modification: %w", err)
}
return nil
}

var validateReplicationGroupID schema.SchemaValidateFunc = validation.All(
validation.StringLenBetween(1, 40),
validation.StringMatch(regexp.MustCompile(`^[0-9a-zA-Z-]+$`), "must contain only alphanumeric characters and hyphens"),
Expand Down
Loading

0 comments on commit 331c8cd

Please sign in to comment.