Skip to content

Commit be3e073

Browse files
Add deletion_protection field to Redis Instance (#15024)
1 parent 69f7e78 commit be3e073

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

mmv1/products/redis/Instance.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ custom_code:
4343
constants: 'templates/terraform/constants/redis_instance.go.tmpl'
4444
encoder: 'templates/terraform/encoders/redis_location_id_for_fallback_zone.go.tmpl'
4545
decoder: 'templates/terraform/decoders/redis_instance.go.tmpl'
46+
pre_delete: 'templates/terraform/pre_delete/redis_instance.go.tmpl'
4647
custom_diff:
4748
- 'customdiff.ForceNewIfChange("redis_version", isRedisVersionDecreasing)'
4849
- 'tpgresource.DefaultProviderProject'
@@ -57,6 +58,8 @@ examples:
5758
'prevent_destroy': 'false'
5859
oics_vars_overrides:
5960
'prevent_destroy': 'false'
61+
ignore_read_extra:
62+
- 'deletion_protection'
6063
- name: 'redis_instance_full'
6164
primary_resource_id: 'cache'
6265
vars:
@@ -126,6 +129,16 @@ examples:
126129
oics_vars_overrides:
127130
'prevent_destroy': 'false'
128131
exclude_test: true
132+
virtual_fields:
133+
- name: 'deletion_protection'
134+
description: |
135+
Whether Terraform will be prevented from destroying the instance.
136+
When a`terraform destroy` or `terraform apply` would delete the instance,
137+
the command will fail if this field is not set to false in Terraform state.
138+
When the field is set to true or unset in Terraform state, a `terraform apply`
139+
or `terraform destroy` that would delete the instance will fail.
140+
When the field is set to false, deleting the instance is allowed.
141+
type: Boolean
129142
parameters:
130143
# TODO: resourceref?
131144
- name: 'region'

mmv1/templates/terraform/examples/redis_instance_basic.tf.tmpl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
resource "google_redis_instance" "{{$.PrimaryResourceId}}" {
22
name = "{{index $.Vars "instance_name"}}"
33
memory_size_gb = 1
4+
deletion_protection = false
45

56
lifecycle {
67
prevent_destroy = {{index $.Vars "prevent_destroy"}}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
if d.Get("deletion_protection").(bool) {
2+
return fmt.Errorf("cannot destroy redis instance without setting deletion_protection=false and running `terraform apply`")
3+
}

mmv1/third_party/terraform/services/redis/resource_redis_instance_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package redis_test
22

33
import (
44
"fmt"
5+
"regexp"
56
"testing"
67

78
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
@@ -407,3 +408,55 @@ resource "google_redis_instance" "test" {
407408
}
408409
`, name)
409410
}
411+
412+
func TestAccRedisInstance_deletionprotection(t *testing.T) {
413+
t.Parallel()
414+
415+
name := fmt.Sprintf("tf-test-%d", acctest.RandInt(t))
416+
417+
acctest.VcrTest(t, resource.TestCase{
418+
PreCheck: func() { acctest.AccTestPreCheck(t) },
419+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
420+
CheckDestroy: testAccCheckRedisInstanceDestroyProducer(t),
421+
Steps: []resource.TestStep{
422+
{
423+
Config: testAccRedisInstance_deletionprotection(name, "us-central1", true),
424+
},
425+
{
426+
ResourceName: "google_redis_instance.test",
427+
ImportState: true,
428+
ImportStateVerify: true,
429+
ImportStateVerifyIgnore: []string{"labels", "terraform_labels", "deletion_protection"},
430+
},
431+
{
432+
Config: testAccRedisInstance_deletionprotection(name, "us-west2", true),
433+
ExpectError: regexp.MustCompile("deletion_protection"),
434+
},
435+
{
436+
Config: testAccRedisInstance_deletionprotection(name, "us-central1", false),
437+
},
438+
},
439+
})
440+
}
441+
442+
func testAccRedisInstance_deletionprotection(name string, region string, deletionProtection bool) string {
443+
return fmt.Sprintf(`
444+
resource "google_redis_instance" "test" {
445+
name = "%s"
446+
region = "%s"
447+
display_name = "tf-test-instance"
448+
memory_size_gb = 1
449+
deletion_protection = %t
450+
451+
labels = {
452+
my_key = "my_val"
453+
other_key = "other_val"
454+
}
455+
redis_configs = {
456+
maxmemory-policy = "allkeys-lru"
457+
notify-keyspace-events = "KEA"
458+
}
459+
redis_version = "REDIS_4_0"
460+
}
461+
`, name, region, deletionProtection)
462+
}

0 commit comments

Comments
 (0)