Skip to content

Commit

Permalink
Adding zone distribution mode in the Cluster resource for Memorystore…
Browse files Browse the repository at this point in the history
… Redis cluster (GoogleCloudPlatform#10458)
  • Loading branch information
vickramp authored Jun 3, 2024
1 parent 710c06b commit 40e5c97
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 18 deletions.
31 changes: 31 additions & 0 deletions mmv1/products/redis/Cluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@ examples:
prevent_destroy: 'false'
oics_vars_overrides:
prevent_destroy: 'false'
- !ruby/object:Provider::Terraform::Examples
name: "redis_cluster_ha_single_zone"
primary_resource_id: "cluster-ha-single-zone"
vars:
cluster_name: "ha-cluster-single-zone"
policy_name: "mypolicy"
subnet_name: "mysubnet"
network_name: "mynetwork"
prevent_destroy: 'true'
test_vars_overrides:
prevent_destroy: 'false'
oics_vars_overrides:
prevent_destroy: 'false'
properties:
- !ruby/object:Api::Type::Time
name: createTime
Expand Down Expand Up @@ -123,6 +136,24 @@ properties:
default_from_api: true
immutable: true
required: false
- !ruby/object:Api::Type::NestedObject
name: zoneDistributionConfig
description: Immutable. Zone distribution config for Memorystore Redis cluster.
immutable: true
properties:
- !ruby/object:Api::Type::Enum
name: mode
description: |
Immutable. The mode for zone distribution for Memorystore Redis cluster.
If not provided, MULTI_ZONE will be used as default
values:
- :MULTI_ZONE
- :SINGLE_ZONE
default_from_api: true
- !ruby/object:Api::Type::String
name: zone
description: |
Immutable. The zone for single zone Memorystore Redis cluster.
- !ruby/object:Api::Type::Array
name: 'pscConfigs'
description: |
Expand Down
3 changes: 3 additions & 0 deletions mmv1/templates/terraform/examples/go/redis_cluster_ha.tf.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ resource "google_redis_cluster" "{{$.PrimaryResourceId}}" {
node_type = "REDIS_SHARED_CORE_NANO"
transit_encryption_mode = "TRANSIT_ENCRYPTION_MODE_DISABLED"
authorization_mode = "AUTH_MODE_DISABLED"
zone_distribution_config {
mode = "MULTI_ZONE"
}
depends_on = [
google_network_connectivity_service_connection_policy.default
]
Expand Down
3 changes: 3 additions & 0 deletions mmv1/templates/terraform/examples/redis_cluster_ha.tf.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ resource "google_redis_cluster" "<%= ctx[:primary_resource_id] %>" {
redis_configs = {
maxmemory-policy = "volatile-ttl"
}
zone_distribution_config {
mode = "MULTI_ZONE"
}
depends_on = [
google_network_connectivity_service_connection_policy.default
]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
resource "google_redis_cluster" "<%= ctx[:primary_resource_id] %>" {
name = "<%= ctx[:vars]['cluster_name'] %>"
shard_count = 3
psc_configs {
network = google_compute_network.producer_net.id
}
region = "us-central1"
zone_distribution_config {
mode = "SINGLE_ZONE"
zone = "us-central1-f"
}
depends_on = [
google_network_connectivity_service_connection_policy.default
]

lifecycle {
prevent_destroy = <%= ctx[:vars]['prevent_destroy'] %>
}
}

resource "google_network_connectivity_service_connection_policy" "default" {
name = "<%= ctx[:vars]['policy_name'] %>"
location = "us-central1"
service_class = "gcp-memorystore-redis"
description = "my basic service connection policy"
network = google_compute_network.producer_net.id
psc_config {
subnetworks = [google_compute_subnetwork.producer_subnet.id]
}
}

resource "google_compute_subnetwork" "producer_subnet" {
name = "<%= ctx[:vars]['subnet_name'] %>"
ip_cidr_range = "10.0.0.248/29"
region = "us-central1"
network = google_compute_network.producer_net.id
}

resource "google_compute_network" "producer_net" {
name = "<%= ctx[:vars]['network_name'] %>"
auto_create_subnetworks = false
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestAccRedisCluster_createClusterWithNodeType(t *testing.T) {
Steps: []resource.TestStep{
{
// create cluster with replica count 1
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 1, shardCount: 3, preventDestroy: true, nodeType: "REDIS_STANDARD_SMALL"}),
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 1, shardCount: 3, preventDestroy: true, nodeType: "REDIS_STANDARD_SMALL", zoneDistributionMode: "MULTI_ZONE"}),
},
{
ResourceName: "google_redis_cluster.test",
Expand All @@ -33,12 +33,42 @@ func TestAccRedisCluster_createClusterWithNodeType(t *testing.T) {
},
{
// clean up the resource
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 0, shardCount: 3, preventDestroy: false, nodeType: "REDIS_STANDARD_SMALL"}),
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 0, shardCount: 3, preventDestroy: false, nodeType: "REDIS_STANDARD_SMALL", zoneDistributionMode: "MULTI_ZONE"}),
},
},
})
}


// Validate zone distribution for the cluster.
func TestAccRedisCluster_createClusterWithZoneDistribution(t *testing.T) {
t.Parallel()

name := fmt.Sprintf("tf-test-%d", acctest.RandInt(t))

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderBetaFactories(t),
CheckDestroy: testAccCheckRedisClusterDestroyProducer(t),
Steps: []resource.TestStep{
{
// create cluster with replica count 1
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 0, shardCount: 3, preventDestroy: false, zoneDistributionMode: "SINGLE_ZONE", zone: "us-central1-b"}),
},
{
ResourceName: "google_redis_cluster.test",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"psc_configs"},
},
{
// clean up the resource
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 0, shardCount: 3, preventDestroy: false, zoneDistributionMode: "SINGLE_ZONE", zone: "us-central1-b"}),
},
},
})
}

// Validate that replica count is updated for the cluster
func TestAccRedisCluster_updateReplicaCount(t *testing.T) {
t.Parallel()
Expand All @@ -52,7 +82,7 @@ func TestAccRedisCluster_updateReplicaCount(t *testing.T) {
Steps: []resource.TestStep{
{
// create cluster with replica count 1
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 1, shardCount: 3, preventDestroy: true}),
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 1, shardCount: 3, preventDestroy: true, zoneDistributionMode: "MULTI_ZONE"}),
},
{
ResourceName: "google_redis_cluster.test",
Expand All @@ -62,7 +92,7 @@ func TestAccRedisCluster_updateReplicaCount(t *testing.T) {
},
{
// update replica count to 2
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 2, shardCount: 3, preventDestroy: true}),
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 2, shardCount: 3, preventDestroy: true, zoneDistributionMode: "MULTI_ZONE"}),
},
{
ResourceName: "google_redis_cluster.test",
Expand All @@ -72,11 +102,11 @@ func TestAccRedisCluster_updateReplicaCount(t *testing.T) {
},
{
// clean up the resource
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 1, shardCount: 3, preventDestroy: false}),
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 1, shardCount: 3, preventDestroy: false, zoneDistributionMode: "MULTI_ZONE"}),
},
{
// update replica count to 0
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 0, shardCount: 3, preventDestroy: true}),
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 0, shardCount: 3, preventDestroy: true, zoneDistributionMode: "MULTI_ZONE"}),
},
{
ResourceName: "google_redis_cluster.test",
Expand All @@ -86,7 +116,7 @@ func TestAccRedisCluster_updateReplicaCount(t *testing.T) {
},
{
// clean up the resource
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 0, shardCount: 3, preventDestroy: false}),
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 0, shardCount: 3, preventDestroy: false, zoneDistributionMode: "MULTI_ZONE"}),
},
},
})
Expand All @@ -105,7 +135,7 @@ func TestAccRedisCluster_updateShardCount(t *testing.T) {
Steps: []resource.TestStep{
{
// create cluster with shard count 3
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 1, shardCount: 3, preventDestroy: true}),
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 1, shardCount: 3, preventDestroy: true, zoneDistributionMode: "MULTI_ZONE"}),
},
{
ResourceName: "google_redis_cluster.test",
Expand All @@ -115,7 +145,7 @@ func TestAccRedisCluster_updateShardCount(t *testing.T) {
},
{
// update shard count to 5
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 1, shardCount: 5, preventDestroy: true}),
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 1, shardCount: 5, preventDestroy: true, zoneDistributionMode: "MULTI_ZONE"}),
},
{
ResourceName: "google_redis_cluster.test",
Expand All @@ -125,7 +155,7 @@ func TestAccRedisCluster_updateShardCount(t *testing.T) {
},
{
// clean up the resource
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 1, shardCount: 5, preventDestroy: false}),
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 1, shardCount: 5, preventDestroy: false, zoneDistributionMode: "MULTI_ZONE"}),
},
},
})
Expand All @@ -147,6 +177,7 @@ func TestAccRedisCluster_updateRedisConfigs(t *testing.T) {
Config: createOrUpdateRedisCluster(&ClusterParams{
name: name,
shardCount: 3,
zoneDistributionMode: "MULTI_ZONE",
redisConfigs: map[string]string{
"maxmemory-policy": "volatile-ttl",
}}),
Expand All @@ -162,6 +193,7 @@ func TestAccRedisCluster_updateRedisConfigs(t *testing.T) {
Config: createOrUpdateRedisCluster(&ClusterParams{
name: name,
shardCount: 3,
zoneDistributionMode: "MULTI_ZONE",
redisConfigs: map[string]string{
"maxmemory-policy": "allkeys-lru",
"maxmemory-clients": "90%",
Expand All @@ -175,20 +207,22 @@ func TestAccRedisCluster_updateRedisConfigs(t *testing.T) {
},
{
// remove all redis configs
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, shardCount: 3}),
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, shardCount: 3, zoneDistributionMode: "MULTI_ZONE"}),
},

},
})
}

type ClusterParams struct {
name string
replicaCount int
shardCount int
preventDestroy bool
nodeType string
redisConfigs map[string]string
name string
replicaCount int
shardCount int
preventDestroy bool
nodeType string
redisConfigs map[string]string
zoneDistributionMode string
zone string
}

func createOrUpdateRedisCluster(params *ClusterParams) string {
Expand All @@ -204,6 +238,16 @@ func createOrUpdateRedisCluster(params *ClusterParams) string {
strBuilder.WriteString(fmt.Sprintf("%s = \"%s\"\n", key, value))
}

zoneDistributionConfigBlock := ``
if params.zoneDistributionMode != "" {
zoneDistributionConfigBlock = fmt.Sprintf(`
zone_distribution_config {
mode = "%s"
zone = "%s"
}
`, params.zoneDistributionMode, params.zone)
}

return fmt.Sprintf(`
resource "google_redis_cluster" "test" {
provider = google-beta
Expand All @@ -218,6 +262,7 @@ resource "google_redis_cluster" "test" {
redis_configs = {
%s
}
%s
depends_on = [
google_network_connectivity_service_connection_policy.default
]
Expand Down Expand Up @@ -249,7 +294,7 @@ resource "google_compute_network" "producer_net" {
name = "%s"
auto_create_subnetworks = false
}
`, params.name, params.replicaCount, params.shardCount, params.nodeType, strBuilder.String(), lifecycleBlock, params.name, params.name, params.name)
`, params.name, params.replicaCount, params.shardCount, params.nodeType, strBuilder.String(), zoneDistributionConfigBlock, lifecycleBlock, params.name, params.name, params.name)
}

<% end -%>

0 comments on commit 40e5c97

Please sign in to comment.