Skip to content

Add deletion_protection field to Memcache Instance #13603

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

Closed
Show file tree
Hide file tree
Changes from 15 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
16 changes: 15 additions & 1 deletion mmv1/products/memcache/Instance.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ async:
base_url: '{{op_id}}'
result:
resource_inside_response: true
custom_code:
custom_code: !ruby/object:Provider::Terraform::CustomCode
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is no longer ruby-based yaml, so this is not necessary.

Suggested change
custom_code: !ruby/object:Provider::Terraform::CustomCode
custom_code:

pre_delete: 'templates/terraform/pre_delete/memcache_instance.go.tmpl'
examples:
- name: 'memcache_instance_basic'
primary_resource_id: 'instance'
Expand All @@ -46,6 +47,8 @@ examples:
network_name: 'test-network'
address_name: 'address'
exclude_test: true
ignore_read_extra:
- 'deletion_protection'
- name: 'memcache_instance_basic_test'
primary_resource_id: 'instance'
vars:
Expand Down Expand Up @@ -330,3 +333,14 @@ properties:
ignore_read: true
item_type:
type: String
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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
When a`terraform destroy` or `terraform apply` would delete the instance,
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
20 changes: 9 additions & 11 deletions mmv1/products/metastore/Federation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,30 +44,19 @@ iam_policy:
- 'projects/{{project}}/locations/{{location}}/federations/{{federation_id}}'
- '{{federation_id}}'
custom_code:
pre_delete: 'templates/terraform/pre_delete/metastore_federation.go.tmpl'
examples:
- name: 'dataproc_metastore_federation_basic'
primary_resource_id: 'default'
primary_resource_name: 'fmt.Sprintf("tf-test-metastore-fed%s", context["random_suffix"])'
vars:
federation_id: 'metastore-fed'
service_id: 'metastore-service'
ignore_read_extra:
- 'deletion_protection'
- name: 'dataproc_metastore_federation_bigquery'
primary_resource_id: 'default'
primary_resource_name: 'fmt.Sprintf("tf-test-metastore-fed%s", context["random_suffix"])'
vars:
federation_id: 'metastore-fed'
service_id: 'metastore-service'
virtual_fields:
- name: 'deletion_protection'
description: |
Whether Terraform will be prevented from destroying the federation. Defaults to false.
When the field is set to true in Terraform state, a `terraform apply`
or `terraform destroy` that would delete the federation will fail.
type: Boolean
default_value: false
parameters:
- name: 'federationId'
type: String
Expand Down Expand Up @@ -155,3 +144,12 @@ properties:
- 'METASTORE_TYPE_UNSPECIFIED'
- 'DATAPROC_METASTORE'
- 'BIGQUERY'
- name: 'tags'
type: KeyValuePairs
description: |
A map of resource manager tags.
Resource manager tag keys and values have the same definition as resource manager tags.
Keys must be in the format tagKeys/{tag_key_id}, and values are in the format tagValues/{tag_value_id}.
The field is ignored (both PUT & PATCH) when empty.
immutable: true
ignore_read: true
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ resource "google_service_networking_connection" "private_service_connection" {
resource "google_memcache_instance" "{{$.PrimaryResourceId}}" {
name = "{{index $.Vars "instance_name"}}"
authorized_network = google_service_networking_connection.private_service_connection.network

deletion_protection = false

labels = {
env = "test"
}
Expand Down
3 changes: 3 additions & 0 deletions mmv1/templates/terraform/pre_delete/memcache_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 memcache instance without setting deletion_protection=false and running `terraform apply`")
}
Original file line number Diff line number Diff line change
@@ -1,99 +1,73 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package dataprocmetastore_test

import (
"fmt"
"github.com/hashicorp/terraform-provider-google/google/acctest"
"regexp"
"testing"

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

func TestAccMetastoreFederation_deletionprotection(t *testing.T) {
func TestAccMetastoreFederation_tags(t *testing.T) {
t.Parallel()

name := "tf-test-metastore-" + acctest.RandString(t, 10)
org := envvar.GetTestOrgFromEnv(t)
context := map[string]interface{}{
"random_suffix": acctest.RandString(t, 10),
}
tagKey := acctest.BootstrapSharedTestTagKey(t, "metastore-federations-tagkey")
tagValue := acctest.BootstrapSharedTestTagValue(t, "metastore-federations-tagvalue", tagKey)

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
Steps: []resource.TestStep{
{
Config: testAccMetastoreFederationDeletionProtection(name, "us-central1"),
Config: testAccMetastoreFederationTags(context, map[string]string{org + "/" + tagKey: tagValue}),
},
{
ResourceName: "google_dataproc_metastore_federation.default",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"deletion_protection"},
},
{
Config: testAccMetastoreFederationDeletionProtection(name, "us-west2"),
ExpectError: regexp.MustCompile("deletion_protection"),
},
{
Config: testAccMetastoreFederationDeletionProtectionFalse(name, "us-central1"),
},
{
ResourceName: "google_dataproc_metastore_federation.default",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"deletion_protection"},
ImportStateVerifyIgnore: []string{"tags"},
},
},
})
}

func testAccMetastoreFederationDeletionProtection(name string, location string) string {
func testAccMetastoreFederationTags(context map[string]interface{}, tags map[string]string) string {

r := acctest.Nprintf(`

return fmt.Sprintf(`
resource "google_dataproc_metastore_service" "default" {
service_id = "%s"
service_id = "metastore-srv-sep16"
location = "us-central1"
tier = "DEVELOPER"
hive_metastore_config {
version = "3.1.2"
endpoint_protocol = "GRPC"
}
}
resource "google_dataproc_metastore_federation" "default" {
federation_id = "%s"
location = "%s"
version = "3.1.2"
deletion_protection = true
backend_metastores {
rank = "1"
name = google_dataproc_metastore_service.default.id
metastore_type = "DATAPROC_METASTORE"
}
}
`, name, name, location)
}

func testAccMetastoreFederationDeletionProtectionFalse(name string, location string) string {

return fmt.Sprintf(`
resource "google_dataproc_metastore_service" "default" {
service_id = "%s"
location = "us-central1"
tier = "DEVELOPER"
hive_metastore_config {
version = "3.1.2"
endpoint_protocol = "GRPC"
}
}
resource "google_dataproc_metastore_federation" "default" {
federation_id = "%s"
location = "%s"
location = "us-central1"
federation_id = "metastore-fed-sep16"
version = "3.1.2"
deletion_protection = false

backend_metastores {
rank = "1"
name = google_dataproc_metastore_service.default.id
metastore_type = "DATAPROC_METASTORE"
}
}
`, name, name, location)
tags = {`, context)

l := ""
for key, value := range tags {
l += fmt.Sprintf("%q = %q\n", key, value)
}

l += fmt.Sprintf("}\n}")
return r + l
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package memcache_test

import (
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
Expand Down Expand Up @@ -97,3 +98,61 @@ data "google_compute_network" "memcache_network" {
}
`, name, network)
}

func TestAccMemcacheInstance_deletionprotection(t *testing.T) {
t.Parallel()

prefix := fmt.Sprintf("%d", acctest.RandInt(t))
name := fmt.Sprintf("tf-test-%s", prefix)
network := acctest.BootstrapSharedServiceNetworkingConnection(t, "memcache-instance-update-1")

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckMemcacheInstanceDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccMemcacheInstance_deletionprotection(prefix, name, network, "us-central1"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function needs to set deletion protection to true. It's currently not, which is why the test is failing to see the expected error in step 3

},
{
ResourceName: "google_memcache_instance.test",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"reserved_ip_range_id", "deletion_protection"},
},
{
Config: testAccMemcacheInstance_deletionprotection(prefix, name, network, "us-west2"),
ExpectError: regexp.MustCompile("deletion_protection"),
},
{
Config: testAccMemcacheInstance_update(prefix, name, network),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Config: testAccMemcacheInstance_update(prefix, name, network),
Config: testAccMemcacheInstance_update(prefix, name, network),
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectResourceAction("google_memcache_instance.test", plancheck.ResourceActionUpdate),
},
},

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function needs to set deletion protection to false.

},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
},
},
{
ResourceName: "google_memcache_instance.test",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"reserved_ip_range_id", "deletion_protection"},
},

},
})
}

func testAccMemcacheInstance_deletionprotection(prefix, name, network, region string) string {
return fmt.Sprintf(`
resource "google_memcache_instance" "test" {
name = "%s"
region = "%s"
authorized_network = data.google_compute_network.memcache_network.id
deletion_protection = false
node_config {
cpu_count = 1
memory_size_mb = 1024
}
node_count = 1
memcache_parameters {
params = {
"listen-backlog" = "2048"
"max-item-size" = "8388608"
}
}
reserved_ip_range_id = ["tf-bootstrap-addr-memcache-instance-update-1"]
}
data "google_compute_network" "memcache_network" {
name = "%s"
}
`, name, region, network)
}
Loading