Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add deletion_protection field to Redis Instance #11735

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions mmv1/products/redis/Instance.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ custom_code:
constants: 'templates/terraform/constants/redis_instance.go.tmpl'
encoder: 'templates/terraform/encoders/redis_location_id_for_fallback_zone.go.tmpl'
decoder: 'templates/terraform/decoders/redis_instance.go.tmpl'
pre_delete: 'templates/terraform/pre_delete/redis_instance.go.tmpl'
custom_diff:
- 'customdiff.ForceNewIfChange("redis_version", isRedisVersionDecreasing)'
- 'tpgresource.DefaultProviderProject'
Expand All @@ -55,6 +56,8 @@ examples:
'prevent_destroy': 'false'
oics_vars_overrides:
'prevent_destroy': 'false'
ignore_read_extra:
- 'deletion_protection'
- name: 'redis_instance_full'
primary_resource_id: 'cache'
vars:
Expand Down Expand Up @@ -124,6 +127,17 @@ examples:
oics_vars_overrides:
'prevent_destroy': 'false'
exclude_test: true
virtual_fields:
- name: 'deletion_protection'
description: |
Whether Terraform will be prevented from destroying the instance. Defaults to true.
When a`terraform destroy` or `terraform apply` would delete the instance,
the command will fail if this field is not set to false in Terraform state.
When the field is set to true or unset in Terraform state, a `terraform apply`
or `terraform destroy` that would delete the instance will fail.
When the field is set to false, deleting the instance is allowed.
type: Boolean
default_value: true
parameters:
# TODO: resourceref?
- name: 'region'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
resource "google_redis_instance" "{{$.PrimaryResourceId}}" {
name = "{{index $.Vars "instance_name"}}"
memory_size_gb = 1
deletion_protection = false

lifecycle {
prevent_destroy = {{index $.Vars "prevent_destroy"}}
Expand Down
3 changes: 3 additions & 0 deletions mmv1/templates/terraform/pre_delete/redis_instance.go.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
if d.Get("deletion_protection").(bool) {
return fmt.Errorf("cannot destroy redis instance without setting deletion_protection=false and running `terraform apply`")
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package redis_test

import (
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-provider-google/google/acctest"
"github.com/hashicorp/terraform-provider-google/google/envvar"
)

func TestAccRedisInstance_update(t *testing.T) {
Expand All @@ -26,7 +26,7 @@ func TestAccRedisInstance_update(t *testing.T) {
ResourceName: "google_redis_instance.test",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"labels", "terraform_labels"},
ImportStateVerifyIgnore: []string{"labels", "terraform_labels", "deletion_protection"},
},
{
Config: testAccRedisInstance_update2(name, true),
Expand All @@ -35,7 +35,7 @@ func TestAccRedisInstance_update(t *testing.T) {
ResourceName: "google_redis_instance.test",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"labels", "terraform_labels"},
ImportStateVerifyIgnore: []string{"labels", "terraform_labels", "deletion_protection"},
},
{
Config: testAccRedisInstance_update2(name, false),
Expand All @@ -44,6 +44,36 @@ func TestAccRedisInstance_update(t *testing.T) {
})
}

func TestAccRedisInstance_deletionprotection(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.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckRedisInstanceDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccRedisInstance_deletionprotection(name, "us-central1", true),
},
{
ResourceName: "google_redis_instance.test",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"labels", "terraform_labels", "deletion_protection"},
},
{
Config: testAccRedisInstance_deletionprotection(name, "us-west2", true),
ExpectError: regexp.MustCompile("deletion_protection"),
},
{
Config: testAccRedisInstance_update(name, true),
},
},
})
}

// Validate that read replica is enabled on the instance without having to recreate
func TestAccRedisInstance_updateReadReplicasMode(t *testing.T) {
t.Parallel()
Expand Down Expand Up @@ -332,6 +362,8 @@ resource "google_redis_instance" "test" {
name = "%s"
display_name = "pre-update"
memory_size_gb = 1
deletion_protection = false

region = "us-central1"
%s

Expand Down Expand Up @@ -378,6 +410,37 @@ resource "google_redis_instance" "test" {
`, name, lifecycleBlock)
}

func testAccRedisInstance_deletionprotection(name string, region string, preventDestroy bool) string {
lifecycleBlock := ""
if preventDestroy {
lifecycleBlock = `
lifecycle {
prevent_destroy = false
}`
}
return fmt.Sprintf(`
resource "google_redis_instance" "test" {
name = "%s"
region = "%s"
display_name = "pre-update"
memory_size_gb = 1
deletion_protection = true
%s

labels = {
my_key = "my_val"
other_key = "other_val"
}

redis_configs = {
maxmemory-policy = "allkeys-lru"
notify-keyspace-events = "KEA"
}
redis_version = "REDIS_4_0"
}
`, name, region, lifecycleBlock)
}

func testAccRedisInstance_regionFromLocation(name, zone string) string {
return fmt.Sprintf(`
resource "google_redis_instance" "test" {
Expand Down