@@ -12,6 +12,7 @@ import (
1212)
1313
1414func TestAccRedisCluster_createClusterWithNodeType (t * testing.T ) {
15+
1516 t .Parallel ()
1617
1718 name := fmt .Sprintf ("tf-test-%d" , acctest .RandInt (t ))
@@ -23,7 +24,7 @@ func TestAccRedisCluster_createClusterWithNodeType(t *testing.T) {
2324 Steps : []resource.TestStep {
2425 {
2526 // 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" }),
2728 },
2829 {
2930 ResourceName : "google_redis_cluster.test" ,
@@ -33,7 +34,7 @@ func TestAccRedisCluster_createClusterWithNodeType(t *testing.T) {
3334 },
3435 {
3536 // 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" }),
3738 },
3839 },
3940 })
@@ -52,7 +53,7 @@ func TestAccRedisCluster_createClusterWithZoneDistribution(t *testing.T) {
5253 Steps : []resource.TestStep {
5354 {
5455 // 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" }),
5657 },
5758 {
5859 ResourceName : "google_redis_cluster.test" ,
@@ -62,7 +63,7 @@ func TestAccRedisCluster_createClusterWithZoneDistribution(t *testing.T) {
6263 },
6364 {
6465 // 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" }),
6667 },
6768 },
6869 })
@@ -81,7 +82,7 @@ func TestAccRedisCluster_updateReplicaCount(t *testing.T) {
8182 Steps : []resource.TestStep {
8283 {
8384 // 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" }),
8586 },
8687 {
8788 ResourceName : "google_redis_cluster.test" ,
@@ -91,21 +92,17 @@ func TestAccRedisCluster_updateReplicaCount(t *testing.T) {
9192 },
9293 {
9394 // 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" }),
9596 },
9697 {
9798 ResourceName : "google_redis_cluster.test" ,
9899 ImportState : true ,
99100 ImportStateVerify : true ,
100101 ImportStateVerifyIgnore : []string {"psc_configs" },
101102 },
102- {
103- // clean up the resource
104- Config : createOrUpdateRedisCluster (& ClusterParams {name : name , replicaCount : 1 , shardCount : 3 , preventDestroy : false , zoneDistributionMode : "MULTI_ZONE" }),
105- },
106103 {
107104 // 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" }),
109106 },
110107 {
111108 ResourceName : "google_redis_cluster.test" ,
@@ -115,7 +112,7 @@ func TestAccRedisCluster_updateReplicaCount(t *testing.T) {
115112 },
116113 {
117114 // 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" }),
119116 },
120117 },
121118 })
@@ -134,7 +131,7 @@ func TestAccRedisCluster_updateShardCount(t *testing.T) {
134131 Steps : []resource.TestStep {
135132 {
136133 // 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" }),
138135 },
139136 {
140137 ResourceName : "google_redis_cluster.test" ,
@@ -144,7 +141,7 @@ func TestAccRedisCluster_updateShardCount(t *testing.T) {
144141 },
145142 {
146143 // 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" }),
148145 },
149146 {
150147 ResourceName : "google_redis_cluster.test" ,
@@ -154,7 +151,7 @@ func TestAccRedisCluster_updateShardCount(t *testing.T) {
154151 },
155152 {
156153 // 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" }),
158155 },
159156 },
160157 })
@@ -212,25 +209,57 @@ func TestAccRedisCluster_updateRedisConfigs(t *testing.T) {
212209 })
213210}
214211
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+
215251type 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
224260}
225261
226262func createOrUpdateRedisCluster (params * ClusterParams ) string {
227- lifecycleBlock := ""
228- if params .preventDestroy {
229- lifecycleBlock = `
230- lifecycle {
231- prevent_destroy = true
232- }`
233- }
234263 var strBuilder strings.Builder
235264 for key , value := range params .redisConfigs {
236265 strBuilder .WriteString (fmt .Sprintf ("%s = \" %s\" \n " , key , value ))
@@ -253,6 +282,7 @@ resource "google_redis_cluster" "test" {
253282 replica_count = %d
254283 shard_count = %d
255284 node_type = "%s"
285+ deletion_protection_enabled = %v
256286 region = "us-central1"
257287 psc_configs {
258288 network = google_compute_network.producer_net.id
@@ -262,9 +292,8 @@ resource "google_redis_cluster" "test" {
262292 }
263293 %s
264294 depends_on = [
265- google_network_connectivity_service_connection_policy.default
266- ]
267- %s
295+ google_network_connectivity_service_connection_policy.default
296+ ]
268297}
269298
270299resource "google_network_connectivity_service_connection_policy" "default" {
@@ -292,5 +321,5 @@ resource "google_compute_network" "producer_net" {
292321 name = "%s"
293322 auto_create_subnetworks = false
294323}
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 )
296325}
0 commit comments