@@ -12,6 +12,7 @@ import (
12
12
)
13
13
14
14
func TestAccRedisCluster_createClusterWithNodeType (t * testing.T ) {
15
+
15
16
t .Parallel ()
16
17
17
18
name := fmt .Sprintf ("tf-test-%d" , acctest .RandInt (t ))
@@ -23,7 +24,7 @@ func TestAccRedisCluster_createClusterWithNodeType(t *testing.T) {
23
24
Steps : []resource.TestStep {
24
25
{
25
26
// create cluster with replica count 1
26
- Config : createOrUpdateRedisCluster (& ClusterParams {name : name , replicaCount : 1 , shardCount : 3 , preventDestroy : true , nodeType : "REDIS_STANDARD_SMALL" , zoneDistributionMode : "MULTI_ZONE" }),
27
+ Config : createOrUpdateRedisCluster (& ClusterParams {name : name , replicaCount : 1 , shardCount : 3 , deletionProtectionEnabled : true , nodeType : "REDIS_STANDARD_SMALL" , zoneDistributionMode : "MULTI_ZONE" }),
27
28
},
28
29
{
29
30
ResourceName : "google_redis_cluster.test" ,
@@ -33,7 +34,7 @@ func TestAccRedisCluster_createClusterWithNodeType(t *testing.T) {
33
34
},
34
35
{
35
36
// clean up the resource
36
- Config : createOrUpdateRedisCluster (& ClusterParams {name : name , replicaCount : 0 , shardCount : 3 , preventDestroy : false , nodeType : "REDIS_STANDARD_SMALL" , zoneDistributionMode : "MULTI_ZONE" }),
37
+ Config : createOrUpdateRedisCluster (& ClusterParams {name : name , replicaCount : 1 , shardCount : 3 , deletionProtectionEnabled : false , nodeType : "REDIS_STANDARD_SMALL" , zoneDistributionMode : "MULTI_ZONE" }),
37
38
},
38
39
},
39
40
})
@@ -52,7 +53,7 @@ func TestAccRedisCluster_createClusterWithZoneDistribution(t *testing.T) {
52
53
Steps : []resource.TestStep {
53
54
{
54
55
// create cluster with replica count 1
55
- Config : createOrUpdateRedisCluster (& ClusterParams {name : name , replicaCount : 0 , shardCount : 3 , preventDestroy : false , zoneDistributionMode : "SINGLE_ZONE" , zone : "us-central1-b" }),
56
+ Config : createOrUpdateRedisCluster (& ClusterParams {name : name , replicaCount : 0 , shardCount : 3 , deletionProtectionEnabled : false , zoneDistributionMode : "SINGLE_ZONE" , zone : "us-central1-b" }),
56
57
},
57
58
{
58
59
ResourceName : "google_redis_cluster.test" ,
@@ -62,7 +63,7 @@ func TestAccRedisCluster_createClusterWithZoneDistribution(t *testing.T) {
62
63
},
63
64
{
64
65
// clean up the resource
65
- Config : createOrUpdateRedisCluster (& ClusterParams {name : name , replicaCount : 0 , shardCount : 3 , preventDestroy : false , zoneDistributionMode : "SINGLE_ZONE" , zone : "us-central1-b" }),
66
+ Config : createOrUpdateRedisCluster (& ClusterParams {name : name , replicaCount : 0 , shardCount : 3 , deletionProtectionEnabled : false , zoneDistributionMode : "SINGLE_ZONE" , zone : "us-central1-b" }),
66
67
},
67
68
},
68
69
})
@@ -81,7 +82,7 @@ func TestAccRedisCluster_updateReplicaCount(t *testing.T) {
81
82
Steps : []resource.TestStep {
82
83
{
83
84
// create cluster with replica count 1
84
- Config : createOrUpdateRedisCluster (& ClusterParams {name : name , replicaCount : 1 , shardCount : 3 , preventDestroy : true , zoneDistributionMode : "MULTI_ZONE" }),
85
+ Config : createOrUpdateRedisCluster (& ClusterParams {name : name , replicaCount : 1 , shardCount : 3 , deletionProtectionEnabled : true , zoneDistributionMode : "MULTI_ZONE" }),
85
86
},
86
87
{
87
88
ResourceName : "google_redis_cluster.test" ,
@@ -91,21 +92,17 @@ func TestAccRedisCluster_updateReplicaCount(t *testing.T) {
91
92
},
92
93
{
93
94
// update replica count to 2
94
- Config : createOrUpdateRedisCluster (& ClusterParams {name : name , replicaCount : 2 , shardCount : 3 , preventDestroy : true , zoneDistributionMode : "MULTI_ZONE" }),
95
+ Config : createOrUpdateRedisCluster (& ClusterParams {name : name , replicaCount : 2 , shardCount : 3 , deletionProtectionEnabled : true , zoneDistributionMode : "MULTI_ZONE" }),
95
96
},
96
97
{
97
98
ResourceName : "google_redis_cluster.test" ,
98
99
ImportState : true ,
99
100
ImportStateVerify : true ,
100
101
ImportStateVerifyIgnore : []string {"psc_configs" },
101
102
},
102
- {
103
- // clean up the resource
104
- Config : createOrUpdateRedisCluster (& ClusterParams {name : name , replicaCount : 1 , shardCount : 3 , preventDestroy : false , zoneDistributionMode : "MULTI_ZONE" }),
105
- },
106
103
{
107
104
// update replica count to 0
108
- Config : createOrUpdateRedisCluster (& ClusterParams {name : name , replicaCount : 0 , shardCount : 3 , preventDestroy : true , zoneDistributionMode : "MULTI_ZONE" }),
105
+ Config : createOrUpdateRedisCluster (& ClusterParams {name : name , replicaCount : 0 , shardCount : 3 , deletionProtectionEnabled : true , zoneDistributionMode : "MULTI_ZONE" }),
109
106
},
110
107
{
111
108
ResourceName : "google_redis_cluster.test" ,
@@ -115,7 +112,7 @@ func TestAccRedisCluster_updateReplicaCount(t *testing.T) {
115
112
},
116
113
{
117
114
// clean up the resource
118
- Config : createOrUpdateRedisCluster (& ClusterParams {name : name , replicaCount : 0 , shardCount : 3 , preventDestroy : false , zoneDistributionMode : "MULTI_ZONE" }),
115
+ Config : createOrUpdateRedisCluster (& ClusterParams {name : name , replicaCount : 0 , shardCount : 3 , deletionProtectionEnabled : false , zoneDistributionMode : "MULTI_ZONE" }),
119
116
},
120
117
},
121
118
})
@@ -134,7 +131,7 @@ func TestAccRedisCluster_updateShardCount(t *testing.T) {
134
131
Steps : []resource.TestStep {
135
132
{
136
133
// create cluster with shard count 3
137
- Config : createOrUpdateRedisCluster (& ClusterParams {name : name , replicaCount : 1 , shardCount : 3 , preventDestroy : true , zoneDistributionMode : "MULTI_ZONE" }),
134
+ Config : createOrUpdateRedisCluster (& ClusterParams {name : name , replicaCount : 1 , shardCount : 3 , deletionProtectionEnabled : true , zoneDistributionMode : "MULTI_ZONE" }),
138
135
},
139
136
{
140
137
ResourceName : "google_redis_cluster.test" ,
@@ -144,7 +141,7 @@ func TestAccRedisCluster_updateShardCount(t *testing.T) {
144
141
},
145
142
{
146
143
// update shard count to 5
147
- Config : createOrUpdateRedisCluster (& ClusterParams {name : name , replicaCount : 1 , shardCount : 5 , preventDestroy : true , zoneDistributionMode : "MULTI_ZONE" }),
144
+ Config : createOrUpdateRedisCluster (& ClusterParams {name : name , replicaCount : 1 , shardCount : 5 , deletionProtectionEnabled : true , zoneDistributionMode : "MULTI_ZONE" }),
148
145
},
149
146
{
150
147
ResourceName : "google_redis_cluster.test" ,
@@ -154,7 +151,7 @@ func TestAccRedisCluster_updateShardCount(t *testing.T) {
154
151
},
155
152
{
156
153
// clean up the resource
157
- Config : createOrUpdateRedisCluster (& ClusterParams {name : name , replicaCount : 1 , shardCount : 5 , preventDestroy : false , zoneDistributionMode : "MULTI_ZONE" }),
154
+ Config : createOrUpdateRedisCluster (& ClusterParams {name : name , replicaCount : 1 , shardCount : 5 , deletionProtectionEnabled : false , zoneDistributionMode : "MULTI_ZONE" }),
158
155
},
159
156
},
160
157
})
@@ -212,25 +209,57 @@ func TestAccRedisCluster_updateRedisConfigs(t *testing.T) {
212
209
})
213
210
}
214
211
212
+ // Validate that deletion protection enabled/disabled cluster is created updated
213
+ func TestAccRedisCluster_createUpdateDeletionProtection (t * testing.T ) {
214
+ t .Parallel ()
215
+
216
+ name := fmt .Sprintf ("tf-test-%d" , acctest .RandInt (t ))
217
+
218
+ acctest .VcrTest (t , resource.TestCase {
219
+ PreCheck : func () { acctest .AccTestPreCheck (t ) },
220
+ ProtoV5ProviderFactories : acctest .ProtoV5ProviderBetaFactories (t ),
221
+ CheckDestroy : testAccCheckRedisClusterDestroyProducer (t ),
222
+ Steps : []resource.TestStep {
223
+ {
224
+ // create cluster with deletion protection set to false
225
+ Config : createOrUpdateRedisCluster (& ClusterParams {name : name , replicaCount : 0 , shardCount : 3 , deletionProtectionEnabled : false , zoneDistributionMode : "MULTI_ZONE" }),
226
+ },
227
+ {
228
+ ResourceName : "google_redis_cluster.test" ,
229
+ ImportState : true ,
230
+ ImportStateVerify : true ,
231
+ ImportStateVerifyIgnore : []string {"psc_configs" },
232
+ },
233
+ {
234
+ // update deletion protection to true
235
+ Config : createOrUpdateRedisCluster (& ClusterParams {name : name , replicaCount : 0 , shardCount : 3 , deletionProtectionEnabled : true , zoneDistributionMode : "MULTI_ZONE" }),
236
+ },
237
+ {
238
+ ResourceName : "google_redis_cluster.test" ,
239
+ ImportState : true ,
240
+ ImportStateVerify : true ,
241
+ ImportStateVerifyIgnore : []string {"psc_configs" },
242
+ },
243
+ {
244
+ // update deletion protection to false and delete the cluster
245
+ Config : createOrUpdateRedisCluster (& ClusterParams {name : name , replicaCount : 0 , shardCount : 3 , deletionProtectionEnabled : false , zoneDistributionMode : "MULTI_ZONE" }),
246
+ },
247
+ },
248
+ })
249
+ }
250
+
215
251
type ClusterParams struct {
216
- name string
217
- replicaCount int
218
- shardCount int
219
- preventDestroy bool
220
- nodeType string
221
- redisConfigs map [string ]string
222
- zoneDistributionMode string
223
- zone string
252
+ name string
253
+ replicaCount int
254
+ shardCount int
255
+ deletionProtectionEnabled bool
256
+ nodeType string
257
+ redisConfigs map [string ]string
258
+ zoneDistributionMode string
259
+ zone string
224
260
}
225
261
226
262
func createOrUpdateRedisCluster (params * ClusterParams ) string {
227
- lifecycleBlock := ""
228
- if params .preventDestroy {
229
- lifecycleBlock = `
230
- lifecycle {
231
- prevent_destroy = true
232
- }`
233
- }
234
263
var strBuilder strings.Builder
235
264
for key , value := range params .redisConfigs {
236
265
strBuilder .WriteString (fmt .Sprintf ("%s = \" %s\" \n " , key , value ))
@@ -253,6 +282,7 @@ resource "google_redis_cluster" "test" {
253
282
replica_count = %d
254
283
shard_count = %d
255
284
node_type = "%s"
285
+ deletion_protection_enabled = %v
256
286
region = "us-central1"
257
287
psc_configs {
258
288
network = google_compute_network.producer_net.id
@@ -262,9 +292,8 @@ resource "google_redis_cluster" "test" {
262
292
}
263
293
%s
264
294
depends_on = [
265
- google_network_connectivity_service_connection_policy.default
266
- ]
267
- %s
295
+ google_network_connectivity_service_connection_policy.default
296
+ ]
268
297
}
269
298
270
299
resource "google_network_connectivity_service_connection_policy" "default" {
@@ -292,5 +321,5 @@ resource "google_compute_network" "producer_net" {
292
321
name = "%s"
293
322
auto_create_subnetworks = false
294
323
}
295
- ` , params .name , params .replicaCount , params .shardCount , params .nodeType , strBuilder .String (), zoneDistributionConfigBlock , lifecycleBlock , params .name , params .name , params .name )
324
+ ` , params .name , params .replicaCount , params .shardCount , params .nodeType , params . deletionProtectionEnabled , strBuilder .String (), zoneDistributionConfigBlock , params .name , params .name , params .name )
296
325
}
0 commit comments