Skip to content

Commit 40e5c97

Browse files
authored
Adding zone distribution mode in the Cluster resource for Memorystore Redis cluster (GoogleCloudPlatform#10458)
1 parent 710c06b commit 40e5c97

File tree

5 files changed

+142
-18
lines changed

5 files changed

+142
-18
lines changed

mmv1/products/redis/Cluster.yaml

+31
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,19 @@ examples:
6262
prevent_destroy: 'false'
6363
oics_vars_overrides:
6464
prevent_destroy: 'false'
65+
- !ruby/object:Provider::Terraform::Examples
66+
name: "redis_cluster_ha_single_zone"
67+
primary_resource_id: "cluster-ha-single-zone"
68+
vars:
69+
cluster_name: "ha-cluster-single-zone"
70+
policy_name: "mypolicy"
71+
subnet_name: "mysubnet"
72+
network_name: "mynetwork"
73+
prevent_destroy: 'true'
74+
test_vars_overrides:
75+
prevent_destroy: 'false'
76+
oics_vars_overrides:
77+
prevent_destroy: 'false'
6578
properties:
6679
- !ruby/object:Api::Type::Time
6780
name: createTime
@@ -123,6 +136,24 @@ properties:
123136
default_from_api: true
124137
immutable: true
125138
required: false
139+
- !ruby/object:Api::Type::NestedObject
140+
name: zoneDistributionConfig
141+
description: Immutable. Zone distribution config for Memorystore Redis cluster.
142+
immutable: true
143+
properties:
144+
- !ruby/object:Api::Type::Enum
145+
name: mode
146+
description: |
147+
Immutable. The mode for zone distribution for Memorystore Redis cluster.
148+
If not provided, MULTI_ZONE will be used as default
149+
values:
150+
- :MULTI_ZONE
151+
- :SINGLE_ZONE
152+
default_from_api: true
153+
- !ruby/object:Api::Type::String
154+
name: zone
155+
description: |
156+
Immutable. The zone for single zone Memorystore Redis cluster.
126157
- !ruby/object:Api::Type::Array
127158
name: 'pscConfigs'
128159
description: |

mmv1/templates/terraform/examples/go/redis_cluster_ha.tf.tmpl

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ resource "google_redis_cluster" "{{$.PrimaryResourceId}}" {
99
node_type = "REDIS_SHARED_CORE_NANO"
1010
transit_encryption_mode = "TRANSIT_ENCRYPTION_MODE_DISABLED"
1111
authorization_mode = "AUTH_MODE_DISABLED"
12+
zone_distribution_config {
13+
mode = "MULTI_ZONE"
14+
}
1215
depends_on = [
1316
google_network_connectivity_service_connection_policy.default
1417
]

mmv1/templates/terraform/examples/redis_cluster_ha.tf.erb

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ resource "google_redis_cluster" "<%= ctx[:primary_resource_id] %>" {
1212
redis_configs = {
1313
maxmemory-policy = "volatile-ttl"
1414
}
15+
zone_distribution_config {
16+
mode = "MULTI_ZONE"
17+
}
1518
depends_on = [
1619
google_network_connectivity_service_connection_policy.default
1720
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
resource "google_redis_cluster" "<%= ctx[:primary_resource_id] %>" {
2+
name = "<%= ctx[:vars]['cluster_name'] %>"
3+
shard_count = 3
4+
psc_configs {
5+
network = google_compute_network.producer_net.id
6+
}
7+
region = "us-central1"
8+
zone_distribution_config {
9+
mode = "SINGLE_ZONE"
10+
zone = "us-central1-f"
11+
}
12+
depends_on = [
13+
google_network_connectivity_service_connection_policy.default
14+
]
15+
16+
lifecycle {
17+
prevent_destroy = <%= ctx[:vars]['prevent_destroy'] %>
18+
}
19+
}
20+
21+
resource "google_network_connectivity_service_connection_policy" "default" {
22+
name = "<%= ctx[:vars]['policy_name'] %>"
23+
location = "us-central1"
24+
service_class = "gcp-memorystore-redis"
25+
description = "my basic service connection policy"
26+
network = google_compute_network.producer_net.id
27+
psc_config {
28+
subnetworks = [google_compute_subnetwork.producer_subnet.id]
29+
}
30+
}
31+
32+
resource "google_compute_subnetwork" "producer_subnet" {
33+
name = "<%= ctx[:vars]['subnet_name'] %>"
34+
ip_cidr_range = "10.0.0.248/29"
35+
region = "us-central1"
36+
network = google_compute_network.producer_net.id
37+
}
38+
39+
resource "google_compute_network" "producer_net" {
40+
name = "<%= ctx[:vars]['network_name'] %>"
41+
auto_create_subnetworks = false
42+
}

mmv1/third_party/terraform/services/redis/resource_redis_cluster_test.go.erb

+63-18
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func TestAccRedisCluster_createClusterWithNodeType(t *testing.T) {
2323
Steps: []resource.TestStep{
2424
{
2525
// create cluster with replica count 1
26-
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 1, shardCount: 3, preventDestroy: true, nodeType: "REDIS_STANDARD_SMALL"}),
26+
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 1, shardCount: 3, preventDestroy: true, nodeType: "REDIS_STANDARD_SMALL", zoneDistributionMode: "MULTI_ZONE"}),
2727
},
2828
{
2929
ResourceName: "google_redis_cluster.test",
@@ -33,12 +33,42 @@ func TestAccRedisCluster_createClusterWithNodeType(t *testing.T) {
3333
},
3434
{
3535
// clean up the resource
36-
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 0, shardCount: 3, preventDestroy: false, nodeType: "REDIS_STANDARD_SMALL"}),
36+
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 0, shardCount: 3, preventDestroy: false, nodeType: "REDIS_STANDARD_SMALL", zoneDistributionMode: "MULTI_ZONE"}),
3737
},
3838
},
3939
})
4040
}
4141

42+
43+
// Validate zone distribution for the cluster.
44+
func TestAccRedisCluster_createClusterWithZoneDistribution(t *testing.T) {
45+
t.Parallel()
46+
47+
name := fmt.Sprintf("tf-test-%d", acctest.RandInt(t))
48+
49+
acctest.VcrTest(t, resource.TestCase{
50+
PreCheck: func() { acctest.AccTestPreCheck(t) },
51+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderBetaFactories(t),
52+
CheckDestroy: testAccCheckRedisClusterDestroyProducer(t),
53+
Steps: []resource.TestStep{
54+
{
55+
// create cluster with replica count 1
56+
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 0, shardCount: 3, preventDestroy: false, zoneDistributionMode: "SINGLE_ZONE", zone: "us-central1-b"}),
57+
},
58+
{
59+
ResourceName: "google_redis_cluster.test",
60+
ImportState: true,
61+
ImportStateVerify: true,
62+
ImportStateVerifyIgnore: []string{"psc_configs"},
63+
},
64+
{
65+
// clean up the resource
66+
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 0, shardCount: 3, preventDestroy: false, zoneDistributionMode: "SINGLE_ZONE", zone: "us-central1-b"}),
67+
},
68+
},
69+
})
70+
}
71+
4272
// Validate that replica count is updated for the cluster
4373
func TestAccRedisCluster_updateReplicaCount(t *testing.T) {
4474
t.Parallel()
@@ -52,7 +82,7 @@ func TestAccRedisCluster_updateReplicaCount(t *testing.T) {
5282
Steps: []resource.TestStep{
5383
{
5484
// create cluster with replica count 1
55-
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 1, shardCount: 3, preventDestroy: true}),
85+
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 1, shardCount: 3, preventDestroy: true, zoneDistributionMode: "MULTI_ZONE"}),
5686
},
5787
{
5888
ResourceName: "google_redis_cluster.test",
@@ -62,7 +92,7 @@ func TestAccRedisCluster_updateReplicaCount(t *testing.T) {
6292
},
6393
{
6494
// update replica count to 2
65-
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 2, shardCount: 3, preventDestroy: true}),
95+
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 2, shardCount: 3, preventDestroy: true, zoneDistributionMode: "MULTI_ZONE"}),
6696
},
6797
{
6898
ResourceName: "google_redis_cluster.test",
@@ -72,11 +102,11 @@ func TestAccRedisCluster_updateReplicaCount(t *testing.T) {
72102
},
73103
{
74104
// clean up the resource
75-
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 1, shardCount: 3, preventDestroy: false}),
105+
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 1, shardCount: 3, preventDestroy: false, zoneDistributionMode: "MULTI_ZONE"}),
76106
},
77107
{
78108
// update replica count to 0
79-
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 0, shardCount: 3, preventDestroy: true}),
109+
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 0, shardCount: 3, preventDestroy: true, zoneDistributionMode: "MULTI_ZONE"}),
80110
},
81111
{
82112
ResourceName: "google_redis_cluster.test",
@@ -86,7 +116,7 @@ func TestAccRedisCluster_updateReplicaCount(t *testing.T) {
86116
},
87117
{
88118
// clean up the resource
89-
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 0, shardCount: 3, preventDestroy: false}),
119+
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 0, shardCount: 3, preventDestroy: false, zoneDistributionMode: "MULTI_ZONE"}),
90120
},
91121
},
92122
})
@@ -105,7 +135,7 @@ func TestAccRedisCluster_updateShardCount(t *testing.T) {
105135
Steps: []resource.TestStep{
106136
{
107137
// create cluster with shard count 3
108-
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 1, shardCount: 3, preventDestroy: true}),
138+
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 1, shardCount: 3, preventDestroy: true, zoneDistributionMode: "MULTI_ZONE"}),
109139
},
110140
{
111141
ResourceName: "google_redis_cluster.test",
@@ -115,7 +145,7 @@ func TestAccRedisCluster_updateShardCount(t *testing.T) {
115145
},
116146
{
117147
// update shard count to 5
118-
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 1, shardCount: 5, preventDestroy: true}),
148+
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 1, shardCount: 5, preventDestroy: true, zoneDistributionMode: "MULTI_ZONE"}),
119149
},
120150
{
121151
ResourceName: "google_redis_cluster.test",
@@ -125,7 +155,7 @@ func TestAccRedisCluster_updateShardCount(t *testing.T) {
125155
},
126156
{
127157
// clean up the resource
128-
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 1, shardCount: 5, preventDestroy: false}),
158+
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, replicaCount: 1, shardCount: 5, preventDestroy: false, zoneDistributionMode: "MULTI_ZONE"}),
129159
},
130160
},
131161
})
@@ -147,6 +177,7 @@ func TestAccRedisCluster_updateRedisConfigs(t *testing.T) {
147177
Config: createOrUpdateRedisCluster(&ClusterParams{
148178
name: name,
149179
shardCount: 3,
180+
zoneDistributionMode: "MULTI_ZONE",
150181
redisConfigs: map[string]string{
151182
"maxmemory-policy": "volatile-ttl",
152183
}}),
@@ -162,6 +193,7 @@ func TestAccRedisCluster_updateRedisConfigs(t *testing.T) {
162193
Config: createOrUpdateRedisCluster(&ClusterParams{
163194
name: name,
164195
shardCount: 3,
196+
zoneDistributionMode: "MULTI_ZONE",
165197
redisConfigs: map[string]string{
166198
"maxmemory-policy": "allkeys-lru",
167199
"maxmemory-clients": "90%",
@@ -175,20 +207,22 @@ func TestAccRedisCluster_updateRedisConfigs(t *testing.T) {
175207
},
176208
{
177209
// remove all redis configs
178-
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, shardCount: 3}),
210+
Config: createOrUpdateRedisCluster(&ClusterParams{name: name, shardCount: 3, zoneDistributionMode: "MULTI_ZONE"}),
179211
},
180212

181213
},
182214
})
183215
}
184216

185217
type ClusterParams struct {
186-
name string
187-
replicaCount int
188-
shardCount int
189-
preventDestroy bool
190-
nodeType string
191-
redisConfigs map[string]string
218+
name string
219+
replicaCount int
220+
shardCount int
221+
preventDestroy bool
222+
nodeType string
223+
redisConfigs map[string]string
224+
zoneDistributionMode string
225+
zone string
192226
}
193227

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

241+
zoneDistributionConfigBlock := ``
242+
if params.zoneDistributionMode != "" {
243+
zoneDistributionConfigBlock = fmt.Sprintf(`
244+
zone_distribution_config {
245+
mode = "%s"
246+
zone = "%s"
247+
}
248+
`, params.zoneDistributionMode, params.zone)
249+
}
250+
207251
return fmt.Sprintf(`
208252
resource "google_redis_cluster" "test" {
209253
provider = google-beta
@@ -218,6 +262,7 @@ resource "google_redis_cluster" "test" {
218262
redis_configs = {
219263
%s
220264
}
265+
%s
221266
depends_on = [
222267
google_network_connectivity_service_connection_policy.default
223268
]
@@ -249,7 +294,7 @@ resource "google_compute_network" "producer_net" {
249294
name = "%s"
250295
auto_create_subnetworks = false
251296
}
252-
`, params.name, params.replicaCount, params.shardCount, params.nodeType, strBuilder.String(), lifecycleBlock, params.name, params.name, params.name)
297+
`, params.name, params.replicaCount, params.shardCount, params.nodeType, strBuilder.String(), zoneDistributionConfigBlock, lifecycleBlock, params.name, params.name, params.name)
253298
}
254299

255300
<% end -%>

0 commit comments

Comments
 (0)