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

Adds CustomizeDiff to update computed member_clusters #17201

Merged
merged 2 commits into from
Jan 22, 2021
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/17201.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_elasticache_replication_group: Correctly update computed `member_clusters` values
```
8 changes: 8 additions & 0 deletions aws/resource_aws_elasticache_replication_group.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package aws

import (
"context"
"fmt"
"log"
"regexp"
Expand All @@ -10,6 +11,7 @@ import (
"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/terraform-plugin-sdk/v2/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
Expand Down Expand Up @@ -273,6 +275,12 @@ func resourceAwsElasticacheReplicationGroup() *schema.Resource {
Delete: schema.DefaultTimeout(40 * time.Minute),
Update: schema.DefaultTimeout(40 * time.Minute),
},

CustomizeDiff: customdiff.Sequence(
customdiff.ComputedIf("member_clusters", func(ctx context.Context, diff *schema.ResourceDiff, meta interface{}) bool {
return diff.HasChange("number_cache_clusters") || diff.HasChange("cluster_mode.0.num_node_groups")
}),
),
}
}

Expand Down
126 changes: 116 additions & 10 deletions aws/resource_aws_elasticache_replication_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ func TestAccAWSElasticacheReplicationGroup_basic(t *testing.T) {
testAccCheckResourceAttrRegionalARN(resourceName, "arn", "elasticache", fmt.Sprintf("replicationgroup:%s", rName)),
resource.TestCheckResourceAttr(resourceName, "number_cache_clusters", "2"),
resource.TestCheckResourceAttr(resourceName, "member_clusters.#", "2"),
resource.TestCheckTypeSetElemAttr(resourceName, "member_clusters.*", fmt.Sprintf("%s-001", rName)),
resource.TestCheckTypeSetElemAttr(resourceName, "member_clusters.*", fmt.Sprintf("%s-002", rName)),
resource.TestCheckResourceAttr(resourceName, "auto_minor_version_upgrade", "false"),
resource.TestCheckResourceAttr(resourceName, "parameter_group_name", "default.redis6.x"),
resource.TestCheckResourceAttr(resourceName, "cluster_mode.#", "1"),
Expand Down Expand Up @@ -392,14 +394,19 @@ func TestAccAWSElasticacheReplicationGroup_ClusterMode_Basic(t *testing.T) {
Config: testAccAWSElasticacheReplicationGroupNativeRedisClusterConfig(rName, 2, 1),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSElasticacheReplicationGroupExists(resourceName, &rg),
resource.TestCheckResourceAttr(resourceName, "number_cache_clusters", "4"),
resource.TestCheckResourceAttr(resourceName, "cluster_enabled", "true"),
resource.TestCheckResourceAttr(resourceName, "parameter_group_name", "default.redis6.x.cluster.on"),
resource.TestCheckResourceAttr(resourceName, "cluster_mode.#", "1"),
resource.TestCheckResourceAttr(resourceName, "cluster_mode.0.num_node_groups", "2"),
resource.TestCheckResourceAttr(resourceName, "cluster_mode.0.replicas_per_node_group", "1"),
resource.TestCheckResourceAttr(resourceName, "port", "6379"),
resource.TestCheckResourceAttrSet(resourceName, "configuration_endpoint_address"),
resource.TestCheckResourceAttr(resourceName, "number_cache_clusters", "4"),
resource.TestCheckResourceAttr(resourceName, "member_clusters.#", "4"),
resource.TestCheckTypeSetElemAttr(resourceName, "member_clusters.*", fmt.Sprintf("%s-0001-001", rName)),
resource.TestCheckTypeSetElemAttr(resourceName, "member_clusters.*", fmt.Sprintf("%s-0001-002", rName)),
resource.TestCheckTypeSetElemAttr(resourceName, "member_clusters.*", fmt.Sprintf("%s-0002-001", rName)),
resource.TestCheckTypeSetElemAttr(resourceName, "member_clusters.*", fmt.Sprintf("%s-0002-002", rName)),
),
},
{
Expand Down Expand Up @@ -432,8 +439,11 @@ func TestAccAWSElasticacheReplicationGroup_ClusterMode_NonClusteredParameterGrou
resource.TestCheckResourceAttr(resourceName, "cluster_mode.0.num_node_groups", "1"),
resource.TestCheckResourceAttr(resourceName, "cluster_mode.0.replicas_per_node_group", "1"),
resource.TestMatchResourceAttr(resourceName, "primary_endpoint_address", regexp.MustCompile(fmt.Sprintf("%s\\..+\\.%s", rName, testAccGetPartitionDNSSuffix()))),
resource.TestCheckResourceAttr(resourceName, "number_cache_clusters", "2"),
resource.TestCheckNoResourceAttr(resourceName, "configuration_endpoint_address"),
resource.TestCheckResourceAttr(resourceName, "number_cache_clusters", "2"),
resource.TestCheckResourceAttr(resourceName, "member_clusters.#", "2"),
resource.TestCheckTypeSetElemAttr(resourceName, "member_clusters.*", fmt.Sprintf("%s-001", rName)),
resource.TestCheckTypeSetElemAttr(resourceName, "member_clusters.*", fmt.Sprintf("%s-002", rName)),
),
},
{
Expand All @@ -460,12 +470,19 @@ func TestAccAWSElasticacheReplicationGroup_ClusterMode_NumNodeGroups(t *testing.
Config: testAccAWSElasticacheReplicationGroupNativeRedisClusterConfig(rName, 3, 1),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSElasticacheReplicationGroupExists(resourceName, &rg),
resource.TestCheckResourceAttr(resourceName, "number_cache_clusters", "6"),
resource.TestCheckResourceAttr(resourceName, "cluster_enabled", "true"),
resource.TestCheckResourceAttr(resourceName, "parameter_group_name", "default.redis6.x.cluster.on"),
resource.TestCheckResourceAttr(resourceName, "cluster_mode.#", "1"),
resource.TestCheckResourceAttr(resourceName, "cluster_mode.0.num_node_groups", "3"),
resource.TestCheckResourceAttr(resourceName, "cluster_mode.0.replicas_per_node_group", "1"),
resource.TestCheckResourceAttr(resourceName, "number_cache_clusters", "6"),
resource.TestCheckResourceAttr(resourceName, "member_clusters.#", "6"),
resource.TestCheckTypeSetElemAttr(resourceName, "member_clusters.*", fmt.Sprintf("%s-0001-001", rName)),
resource.TestCheckTypeSetElemAttr(resourceName, "member_clusters.*", fmt.Sprintf("%s-0001-002", rName)),
resource.TestCheckTypeSetElemAttr(resourceName, "member_clusters.*", fmt.Sprintf("%s-0002-001", rName)),
resource.TestCheckTypeSetElemAttr(resourceName, "member_clusters.*", fmt.Sprintf("%s-0002-002", rName)),
resource.TestCheckTypeSetElemAttr(resourceName, "member_clusters.*", fmt.Sprintf("%s-0003-001", rName)),
resource.TestCheckTypeSetElemAttr(resourceName, "member_clusters.*", fmt.Sprintf("%s-0003-002", rName)),
),
},
{
Expand All @@ -478,24 +495,88 @@ func TestAccAWSElasticacheReplicationGroup_ClusterMode_NumNodeGroups(t *testing.
Config: testAccAWSElasticacheReplicationGroupNativeRedisClusterConfig(rName, 1, 1),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSElasticacheReplicationGroupExists(resourceName, &rg),
resource.TestCheckResourceAttr(resourceName, "number_cache_clusters", "2"),
resource.TestCheckResourceAttr(resourceName, "cluster_enabled", "true"),
resource.TestCheckResourceAttr(resourceName, "parameter_group_name", "default.redis6.x.cluster.on"),
resource.TestCheckResourceAttr(resourceName, "cluster_mode.#", "1"),
resource.TestCheckResourceAttr(resourceName, "cluster_mode.0.num_node_groups", "1"),
resource.TestCheckResourceAttr(resourceName, "cluster_mode.0.replicas_per_node_group", "1"),
resource.TestCheckResourceAttr(resourceName, "number_cache_clusters", "2"),
resource.TestCheckResourceAttr(resourceName, "member_clusters.#", "2"),
resource.TestCheckTypeSetElemAttr(resourceName, "member_clusters.*", fmt.Sprintf("%s-0001-001", rName)),
resource.TestCheckTypeSetElemAttr(resourceName, "member_clusters.*", fmt.Sprintf("%s-0001-002", rName)),
),
},
{
Config: testAccAWSElasticacheReplicationGroupNativeRedisClusterConfig(rName, 2, 1),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSElasticacheReplicationGroupExists(resourceName, &rg),
resource.TestCheckResourceAttr(resourceName, "cluster_enabled", "true"),
resource.TestCheckResourceAttr(resourceName, "parameter_group_name", "default.redis6.x.cluster.on"),
resource.TestCheckResourceAttr(resourceName, "cluster_mode.#", "1"),
resource.TestCheckResourceAttr(resourceName, "cluster_mode.0.num_node_groups", "2"),
resource.TestCheckResourceAttr(resourceName, "cluster_mode.0.replicas_per_node_group", "1"),
resource.TestCheckResourceAttr(resourceName, "number_cache_clusters", "4"),
resource.TestCheckResourceAttr(resourceName, "member_clusters.#", "4"),
resource.TestCheckTypeSetElemAttr(resourceName, "member_clusters.*", fmt.Sprintf("%s-0001-001", rName)),
resource.TestCheckTypeSetElemAttr(resourceName, "member_clusters.*", fmt.Sprintf("%s-0001-002", rName)),
resource.TestCheckTypeSetElemAttr(resourceName, "member_clusters.*", fmt.Sprintf("%s-0002-001", rName)),
resource.TestCheckTypeSetElemAttr(resourceName, "member_clusters.*", fmt.Sprintf("%s-0002-002", rName)),
),
},
},
})
}

func TestAccAWSElasticacheReplicationGroup_ClusterMode_ReplicasPerNodeGroup(t *testing.T) {
var rg elasticache.ReplicationGroup
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_elasticache_replication_group.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSElasticacheReplicationDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSElasticacheReplicationGroupNativeRedisClusterConfig(rName, 2, 2),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSElasticacheReplicationGroupExists(resourceName, &rg),
resource.TestCheckResourceAttr(resourceName, "cluster_enabled", "true"),
resource.TestCheckResourceAttr(resourceName, "parameter_group_name", "default.redis6.x.cluster.on"),
resource.TestCheckResourceAttr(resourceName, "cluster_mode.#", "1"),
resource.TestCheckResourceAttr(resourceName, "cluster_mode.0.num_node_groups", "2"),
resource.TestCheckResourceAttr(resourceName, "cluster_mode.0.replicas_per_node_group", "2"),
resource.TestCheckResourceAttr(resourceName, "number_cache_clusters", "6"),
resource.TestCheckResourceAttr(resourceName, "member_clusters.#", "6"),
resource.TestCheckTypeSetElemAttr(resourceName, "member_clusters.*", fmt.Sprintf("%s-0001-001", rName)),
resource.TestCheckTypeSetElemAttr(resourceName, "member_clusters.*", fmt.Sprintf("%s-0001-002", rName)),
resource.TestCheckTypeSetElemAttr(resourceName, "member_clusters.*", fmt.Sprintf("%s-0001-003", rName)),
resource.TestCheckTypeSetElemAttr(resourceName, "member_clusters.*", fmt.Sprintf("%s-0002-001", rName)),
resource.TestCheckTypeSetElemAttr(resourceName, "member_clusters.*", fmt.Sprintf("%s-0002-002", rName)),
resource.TestCheckTypeSetElemAttr(resourceName, "member_clusters.*", fmt.Sprintf("%s-0002-003", rName)),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"apply_immediately"},
},
{
Config: testAccAWSElasticacheReplicationGroupNativeRedisClusterConfig(rName, 2, 1),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSElasticacheReplicationGroupExists(resourceName, &rg),
resource.TestCheckResourceAttr(resourceName, "cluster_enabled", "true"),
resource.TestCheckResourceAttr(resourceName, "parameter_group_name", "default.redis6.x.cluster.on"),
resource.TestCheckResourceAttr(resourceName, "cluster_mode.#", "1"),
resource.TestCheckResourceAttr(resourceName, "cluster_mode.0.num_node_groups", "2"),
resource.TestCheckResourceAttr(resourceName, "cluster_mode.0.replicas_per_node_group", "1"),
resource.TestCheckResourceAttr(resourceName, "number_cache_clusters", "4"),
resource.TestCheckResourceAttr(resourceName, "member_clusters.#", "4"),
resource.TestCheckTypeSetElemAttr(resourceName, "member_clusters.*", fmt.Sprintf("%s-0001-001", rName)),
resource.TestCheckTypeSetElemAttr(resourceName, "member_clusters.*", fmt.Sprintf("%s-0001-002", rName)),
resource.TestCheckTypeSetElemAttr(resourceName, "member_clusters.*", fmt.Sprintf("%s-0002-001", rName)),
resource.TestCheckTypeSetElemAttr(resourceName, "member_clusters.*", fmt.Sprintf("%s-0002-002", rName)),
),
},
},
Expand Down Expand Up @@ -627,7 +708,7 @@ func TestAccAWSElasticacheReplicationGroup_useCmkKmsKeyId(t *testing.T) {
})
}

func TestAccAWSElasticacheReplicationGroup_NumberCacheClusters(t *testing.T) {
func TestAccAWSElasticacheReplicationGroup_NumberCacheClusters_Basic(t *testing.T) {
var replicationGroup elasticache.ReplicationGroup
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_elasticache_replication_group.test"
Expand All @@ -643,6 +724,9 @@ func TestAccAWSElasticacheReplicationGroup_NumberCacheClusters(t *testing.T) {
testAccCheckAWSElasticacheReplicationGroupExists(resourceName, &replicationGroup),
resource.TestCheckResourceAttr(resourceName, "automatic_failover_enabled", "false"),
resource.TestCheckResourceAttr(resourceName, "number_cache_clusters", "2"),
resource.TestCheckResourceAttr(resourceName, "member_clusters.#", "2"),
resource.TestCheckTypeSetElemAttr(resourceName, "member_clusters.*", fmt.Sprintf("%s-001", rName)),
resource.TestCheckTypeSetElemAttr(resourceName, "member_clusters.*", fmt.Sprintf("%s-002", rName)),
),
},
{
Expand All @@ -657,6 +741,11 @@ func TestAccAWSElasticacheReplicationGroup_NumberCacheClusters(t *testing.T) {
testAccCheckAWSElasticacheReplicationGroupExists(resourceName, &replicationGroup),
resource.TestCheckResourceAttr(resourceName, "automatic_failover_enabled", "false"),
resource.TestCheckResourceAttr(resourceName, "number_cache_clusters", "4"),
resource.TestCheckResourceAttr(resourceName, "member_clusters.#", "4"),
resource.TestCheckTypeSetElemAttr(resourceName, "member_clusters.*", fmt.Sprintf("%s-001", rName)),
resource.TestCheckTypeSetElemAttr(resourceName, "member_clusters.*", fmt.Sprintf("%s-002", rName)),
resource.TestCheckTypeSetElemAttr(resourceName, "member_clusters.*", fmt.Sprintf("%s-003", rName)),
resource.TestCheckTypeSetElemAttr(resourceName, "member_clusters.*", fmt.Sprintf("%s-004", rName)),
),
},
{
Expand All @@ -665,6 +754,9 @@ func TestAccAWSElasticacheReplicationGroup_NumberCacheClusters(t *testing.T) {
testAccCheckAWSElasticacheReplicationGroupExists(resourceName, &replicationGroup),
resource.TestCheckResourceAttr(resourceName, "automatic_failover_enabled", "false"),
resource.TestCheckResourceAttr(resourceName, "number_cache_clusters", "2"),
resource.TestCheckResourceAttr(resourceName, "member_clusters.#", "2"),
resource.TestCheckTypeSetElemAttr(resourceName, "member_clusters.*", fmt.Sprintf("%s-001", rName)),
resource.TestCheckTypeSetElemAttr(resourceName, "member_clusters.*", fmt.Sprintf("%s-002", rName)),
),
},
},
Expand All @@ -687,6 +779,10 @@ func TestAccAWSElasticacheReplicationGroup_NumberCacheClusters_Failover_AutoFail
testAccCheckAWSElasticacheReplicationGroupExists(resourceName, &replicationGroup),
resource.TestCheckResourceAttr(resourceName, "automatic_failover_enabled", "false"),
resource.TestCheckResourceAttr(resourceName, "number_cache_clusters", "3"),
resource.TestCheckResourceAttr(resourceName, "member_clusters.#", "3"),
resource.TestCheckTypeSetElemAttr(resourceName, "member_clusters.*", fmt.Sprintf("%s-001", rName)),
resource.TestCheckTypeSetElemAttr(resourceName, "member_clusters.*", fmt.Sprintf("%s-002", rName)),
resource.TestCheckTypeSetElemAttr(resourceName, "member_clusters.*", fmt.Sprintf("%s-003", rName)),
),
},
{
Expand Down Expand Up @@ -716,6 +812,9 @@ func TestAccAWSElasticacheReplicationGroup_NumberCacheClusters_Failover_AutoFail
testAccCheckAWSElasticacheReplicationGroupExists(resourceName, &replicationGroup),
resource.TestCheckResourceAttr(resourceName, "automatic_failover_enabled", "false"),
resource.TestCheckResourceAttr(resourceName, "number_cache_clusters", "2"),
resource.TestCheckResourceAttr(resourceName, "member_clusters.#", "2"),
resource.TestCheckTypeSetElemAttr(resourceName, "member_clusters.*", fmt.Sprintf("%s-001", rName)),
resource.TestCheckTypeSetElemAttr(resourceName, "member_clusters.*", fmt.Sprintf("%s-002", rName)),
),
},
},
Expand All @@ -738,6 +837,10 @@ func TestAccAWSElasticacheReplicationGroup_NumberCacheClusters_Failover_AutoFail
testAccCheckAWSElasticacheReplicationGroupExists(resourceName, &replicationGroup),
resource.TestCheckResourceAttr(resourceName, "automatic_failover_enabled", "true"),
resource.TestCheckResourceAttr(resourceName, "number_cache_clusters", "3"),
resource.TestCheckResourceAttr(resourceName, "member_clusters.#", "3"),
resource.TestCheckTypeSetElemAttr(resourceName, "member_clusters.*", fmt.Sprintf("%s-001", rName)),
resource.TestCheckTypeSetElemAttr(resourceName, "member_clusters.*", fmt.Sprintf("%s-002", rName)),
resource.TestCheckTypeSetElemAttr(resourceName, "member_clusters.*", fmt.Sprintf("%s-003", rName)),
),
},
{
Expand Down Expand Up @@ -789,6 +892,9 @@ func TestAccAWSElasticacheReplicationGroup_NumberCacheClusters_Failover_AutoFail
testAccCheckAWSElasticacheReplicationGroupExists(resourceName, &replicationGroup),
resource.TestCheckResourceAttr(resourceName, "automatic_failover_enabled", "true"),
resource.TestCheckResourceAttr(resourceName, "number_cache_clusters", "2"),
resource.TestCheckResourceAttr(resourceName, "member_clusters.#", "2"),
resource.TestCheckTypeSetElemAttr(resourceName, "member_clusters.*", fmt.Sprintf("%s-001", rName)),
resource.TestCheckTypeSetElemAttr(resourceName, "member_clusters.*", fmt.Sprintf("%s-002", rName)),
),
},
},
Expand Down Expand Up @@ -1430,7 +1536,7 @@ resource "aws_subnet" "test2" {
}

resource "aws_elasticache_subnet_group" "test" {
name = "tf-test-%[1]s"
name = %[1]q
description = "tf-test-cache-subnet-group-descr"

subnet_ids = [
Expand All @@ -1440,7 +1546,7 @@ resource "aws_elasticache_subnet_group" "test" {
}

resource "aws_security_group" "test" {
name = "tf-test-%[1]s"
name = %[1]q
description = "tf-test-security-group-descr"
vpc_id = aws_vpc.test.id

Expand All @@ -1453,7 +1559,7 @@ resource "aws_security_group" "test" {
}

resource "aws_elasticache_replication_group" "test" {
replication_group_id = "tf-%[1]s"
replication_group_id = %[1]q
replication_group_description = "test description"
node_type = "cache.t2.micro"
port = 6379
Expand All @@ -1462,8 +1568,8 @@ resource "aws_elasticache_replication_group" "test" {
automatic_failover_enabled = true

cluster_mode {
num_node_groups = %d
replicas_per_node_group = %d
num_node_groups = %[2]d
replicas_per_node_group = %[3]d
}
}
`, rName, numNodeGroups, replicasPerNodeGroup)
Expand Down