Skip to content

Commit bc77dea

Browse files
Add deletion_protection field to Memcache Instance (#15025)
1 parent 46aff67 commit bc77dea

File tree

4 files changed

+83
-1
lines changed

4 files changed

+83
-1
lines changed

mmv1/products/memcache/Instance.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ async:
3838
result:
3939
resource_inside_response: true
4040
custom_code:
41+
pre_delete: 'templates/terraform/pre_delete/memcache_instance.go.tmpl'
4142
examples:
4243
- name: 'memcache_instance_basic'
4344
primary_resource_id: 'instance'
@@ -46,6 +47,8 @@ examples:
4647
network_name: 'test-network'
4748
address_name: 'address'
4849
exclude_test: true
50+
ignore_read_extra:
51+
- 'deletion_protection'
4952
- name: 'memcache_instance_basic_test'
5053
primary_resource_id: 'instance'
5154
vars:
@@ -330,3 +333,13 @@ properties:
330333
ignore_read: true
331334
item_type:
332335
type: String
336+
virtual_fields:
337+
- name: 'deletion_protection'
338+
description: |
339+
Whether Terraform will be prevented from destroying the instance.
340+
When a `terraform destroy` or `terraform apply` would delete the instance,
341+
the command will fail if this field is not set to false in Terraform state.
342+
When the field is set to true or unset in Terraform state, a `terraform apply`
343+
or `terraform destroy` that would delete the instance will fail.
344+
When the field is set to false, deleting the instance is allowed.
345+
type: Boolean

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ resource "google_service_networking_connection" "private_service_connection" {
2727
resource "google_memcache_instance" "{{$.PrimaryResourceId}}" {
2828
name = "{{index $.Vars "instance_name"}}"
2929
authorized_network = google_service_networking_connection.private_service_connection.network
30-
30+
deletion_protection = false
31+
3132
labels = {
3233
env = "test"
3334
}
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 memcache instance without setting deletion_protection=false and running `terraform apply`")
3+
}

mmv1/third_party/terraform/services/memcache/resource_memcache_instance_test.go

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

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

78
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
@@ -97,3 +98,67 @@ data "google_compute_network" "memcache_network" {
9798
}
9899
`, name, network)
99100
}
101+
102+
func TestAccMemcacheInstance_deletionprotection(t *testing.T) {
103+
t.Parallel()
104+
105+
prefix := fmt.Sprintf("%d", acctest.RandInt(t))
106+
name := fmt.Sprintf("tf-test-%s", prefix)
107+
network := acctest.BootstrapSharedServiceNetworkingConnection(t, "memcache-instance-update-1")
108+
109+
acctest.VcrTest(t, resource.TestCase{
110+
PreCheck: func() { acctest.AccTestPreCheck(t) },
111+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
112+
CheckDestroy: testAccCheckMemcacheInstanceDestroyProducer(t),
113+
Steps: []resource.TestStep{
114+
{
115+
Config: testAccMemcacheInstanceConfig(prefix, name, network, "us-central1", true), // deletion_protection = true
116+
},
117+
{
118+
ResourceName: "google_memcache_instance.test",
119+
ImportState: true,
120+
ImportStateVerify: true,
121+
ImportStateVerifyIgnore: []string{"reserved_ip_range_id", "deletion_protection"},
122+
},
123+
{
124+
Config: testAccMemcacheInstanceConfig(prefix, name, network, "us-west2", true), // deletion_protection = true
125+
ExpectError: regexp.MustCompile("deletion_protection"),
126+
},
127+
{
128+
Config: testAccMemcacheInstanceConfig(prefix, name, network, "us-central1", false), // deletion_protection = false
129+
},
130+
{
131+
ResourceName: "google_memcache_instance.test",
132+
ImportState: true,
133+
ImportStateVerify: true,
134+
ImportStateVerifyIgnore: []string{"reserved_ip_range_id", "deletion_protection"},
135+
},
136+
},
137+
})
138+
}
139+
140+
func testAccMemcacheInstanceConfig(prefix, name, network, region string, deletionProtection bool) string {
141+
return fmt.Sprintf(`
142+
resource "google_memcache_instance" "test" {
143+
name = "%s"
144+
region = "%s"
145+
authorized_network = data.google_compute_network.memcache_network.id
146+
deletion_protection = %t
147+
node_config {
148+
cpu_count = 1
149+
memory_size_mb = 1024
150+
}
151+
node_count = 1
152+
memcache_parameters {
153+
params = {
154+
"listen-backlog" = "2048"
155+
"max-item-size" = "8388608"
156+
}
157+
}
158+
reserved_ip_range_id = ["tf-bootstrap-addr-memcache-instance-update-1"]
159+
}
160+
data "google_compute_network" "memcache_network" {
161+
name = "%s"
162+
}
163+
`, name, region, deletionProtection, network)
164+
}

0 commit comments

Comments
 (0)