diff --git a/mmv1/products/memcache/Instance.yaml b/mmv1/products/memcache/Instance.yaml index 8d7b62b7b0c8..5a5fe3f42414 100644 --- a/mmv1/products/memcache/Instance.yaml +++ b/mmv1/products/memcache/Instance.yaml @@ -37,7 +37,8 @@ async: base_url: '{{op_id}}' result: resource_inside_response: true -custom_code: +custom_code: !ruby/object:Provider::Terraform::CustomCode + pre_delete: 'templates/terraform/pre_delete/memcache_instance.go.tmpl' examples: - name: 'memcache_instance_basic' primary_resource_id: 'instance' @@ -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: @@ -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, + 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 diff --git a/mmv1/products/metastore/Federation.yaml b/mmv1/products/metastore/Federation.yaml index 39e8475d14c2..40997e36d9be 100644 --- a/mmv1/products/metastore/Federation.yaml +++ b/mmv1/products/metastore/Federation.yaml @@ -44,7 +44,6 @@ 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' @@ -52,22 +51,12 @@ examples: 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 @@ -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 diff --git a/mmv1/templates/terraform/examples/memcache_instance_basic.tf.tmpl b/mmv1/templates/terraform/examples/memcache_instance_basic.tf.tmpl index 932acaa30c92..9eaaf660b5e3 100644 --- a/mmv1/templates/terraform/examples/memcache_instance_basic.tf.tmpl +++ b/mmv1/templates/terraform/examples/memcache_instance_basic.tf.tmpl @@ -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" } diff --git a/mmv1/templates/terraform/pre_delete/memcache_instance.go.tmpl b/mmv1/templates/terraform/pre_delete/memcache_instance.go.tmpl new file mode 100644 index 000000000000..2e195a06f441 --- /dev/null +++ b/mmv1/templates/terraform/pre_delete/memcache_instance.go.tmpl @@ -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`") +} diff --git a/mmv1/third_party/terraform/services/dataprocmetastore/resource_dataproc_metastore_federation_test.go b/mmv1/third_party/terraform/services/dataprocmetastore/resource_dataproc_metastore_federation_test.go index f3916e9615e5..2a6fc5ca1f56 100644 --- a/mmv1/third_party/terraform/services/dataprocmetastore/resource_dataproc_metastore_federation_test.go +++ b/mmv1/third_party/terraform/services/dataprocmetastore/resource_dataproc_metastore_federation_test.go @@ -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 } diff --git a/mmv1/third_party/terraform/services/memcache/resource_memcache_instance_test.go b/mmv1/third_party/terraform/services/memcache/resource_memcache_instance_test.go index 5abceeea37b9..357448609635 100644 --- a/mmv1/third_party/terraform/services/memcache/resource_memcache_instance_test.go +++ b/mmv1/third_party/terraform/services/memcache/resource_memcache_instance_test.go @@ -2,6 +2,7 @@ package memcache_test import ( "fmt" + "regexp" "testing" "github.com/hashicorp/terraform-plugin-testing/helper/resource" @@ -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"), + }, + { + 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), + }, + }, + }) +} + +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) +}