From e2f15074013d8dee0122d934d84886ff59ff9db3 Mon Sep 17 00:00:00 2001 From: Weijia Chen Date: Tue, 30 Apr 2024 22:46:57 +0000 Subject: [PATCH 1/5] allow deletion of resource_bigquery_table when it still has associated resource tags --- .../services/bigquery/resource_bigquery_table.go.erb | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/mmv1/third_party/terraform/services/bigquery/resource_bigquery_table.go.erb b/mmv1/third_party/terraform/services/bigquery/resource_bigquery_table.go.erb index a9ebe7804de4..8da8ec829332 100644 --- a/mmv1/third_party/terraform/services/bigquery/resource_bigquery_table.go.erb +++ b/mmv1/third_party/terraform/services/bigquery/resource_bigquery_table.go.erb @@ -1832,18 +1832,6 @@ func resourceBigQueryTableDelete(d *schema.ResourceData, meta interface{}) error if d.Get("deletion_protection").(bool) { return fmt.Errorf("cannot destroy table %v without setting deletion_protection=false and running `terraform apply`", d.Id()) } - <% unless version == 'ga' -%> - if v, ok := d.GetOk("resource_tags"); ok { - var resourceTags []string - - for k, v := range v.(map[string]interface{}) { - resourceTags = append(resourceTags, fmt.Sprintf("%s:%s", k, v.(string))) - } - - return fmt.Errorf("cannot destroy table %v without clearing the following resource tags: %v", d.Id(), resourceTags) - } - - <% end -%> config := meta.(*transport_tpg.Config) userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) if err != nil { From 14091711b6f446894a0c3a4f94415539f437abd3 Mon Sep 17 00:00:00 2001 From: Weijia Chen Date: Mon, 6 May 2024 20:00:57 +0000 Subject: [PATCH 2/5] add a virtual field allow_resource_tags_on_deletion --- mmv1/products/bigquery/Table.yaml | 7 +++++ .../bigquery/resource_bigquery_table.go.erb | 26 +++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/mmv1/products/bigquery/Table.yaml b/mmv1/products/bigquery/Table.yaml index 53403f29a895..f6d50351c5cd 100644 --- a/mmv1/products/bigquery/Table.yaml +++ b/mmv1/products/bigquery/Table.yaml @@ -516,3 +516,10 @@ properties: in the namespaced format, for example "123456789012/environment" where 123456789012 is the ID of the parent organization or project resource for this tag key. Tag value is expected to be the short name, for example "Production". +virtual_fields: + - !ruby/object:Api::Type::Boolean + name: 'allow_resource_tags_on_deletion' + min_version: beta + description: | + Whether or not to allow table deletion when there are still resource tags attached. + default_value: false diff --git a/mmv1/third_party/terraform/services/bigquery/resource_bigquery_table.go.erb b/mmv1/third_party/terraform/services/bigquery/resource_bigquery_table.go.erb index 8da8ec829332..130159f03a28 100644 --- a/mmv1/third_party/terraform/services/bigquery/resource_bigquery_table.go.erb +++ b/mmv1/third_party/terraform/services/bigquery/resource_bigquery_table.go.erb @@ -1157,6 +1157,15 @@ func ResourceBigQueryTable() *schema.Resource { Description: `Whether or not to allow Terraform to destroy the instance. Unless this field is set to false in Terraform state, a terraform destroy or terraform apply that would delete the instance will fail.`, }, + <% unless version == 'ga' -%> + "allow_resource_tags_on_deletion": { + Type: schema.TypeBool, + Optional: true, + Default: false, + Description: `Whether or not to allow table deletion when there are still resource tags attached.`, + }, + + <% end -%> // TableConstraints: [Optional] Defines the primary key and foreign keys. "table_constraints": { Type: schema.TypeList, @@ -1832,6 +1841,20 @@ func resourceBigQueryTableDelete(d *schema.ResourceData, meta interface{}) error if d.Get("deletion_protection").(bool) { return fmt.Errorf("cannot destroy table %v without setting deletion_protection=false and running `terraform apply`", d.Id()) } + <% unless version == 'ga' -%> + if v, ok := d.GetOk("resource_tags"); ok { + if !d.Get("allow_resource_tags_on_deletion").(bool) { + var resourceTags []string + + for k, v := range v.(map[string]interface{}) { + resourceTags = append(resourceTags, fmt.Sprintf("%s:%s", k, v.(string))) + } + + return fmt.Errorf("cannot destroy table %v without unsetting the following resource tags or setting allow_resource_tags_on_deletion=true: %v", d.Id(), resourceTags) + } + } + + <% end -%> config := meta.(*transport_tpg.Config) userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) if err != nil { @@ -2663,6 +2686,9 @@ func resourceBigQueryTableImport(d *schema.ResourceData, meta interface{}) ([]*s if err := d.Set("deletion_protection", true); err != nil { return nil, fmt.Errorf("Error setting deletion_protection: %s", err) } + if err := d.Set("allow_resource_tags_on_deletion", false); err != nil { + return nil, fmt.Errorf("Error setting allow_resource_tags_on_deletion: %s", err) + } // Replace import id for the resource id id, err := tpgresource.ReplaceVars(d, config, "projects/{{project}}/datasets/{{dataset_id}}/tables/{{table_id}}") From 19a5db6a637fb9267e67888009c8c6fbb2bcefc0 Mon Sep 17 00:00:00 2001 From: Weijia Chen Date: Tue, 7 May 2024 18:21:40 +0000 Subject: [PATCH 3/5] add new field to unit test config --- .../bigquery/resource_bigquery_table_test.go.erb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/mmv1/third_party/terraform/services/bigquery/resource_bigquery_table_test.go.erb b/mmv1/third_party/terraform/services/bigquery/resource_bigquery_table_test.go.erb index 3a7790cdcd36..0f16afdcf42f 100644 --- a/mmv1/third_party/terraform/services/bigquery/resource_bigquery_table_test.go.erb +++ b/mmv1/third_party/terraform/services/bigquery/resource_bigquery_table_test.go.erb @@ -1579,7 +1579,7 @@ func TestAccBigQueryTable_ResourceTags(t *testing.T) { ResourceName: "google_bigquery_table.test", ImportState: true, ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"deletion_protection"}, + ImportStateVerifyIgnore: []string{"deletion_protection", "allow_resource_tags_on_deletion"}, }, { Config: testAccBigQueryTableWithResourceTagsUpdate(context), @@ -1588,7 +1588,7 @@ func TestAccBigQueryTable_ResourceTags(t *testing.T) { ResourceName: "google_bigquery_table.test", ImportState: true, ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"deletion_protection"}, + ImportStateVerifyIgnore: []string{"deletion_protection", "allow_resource_tags_on_deletion"}, }, // testAccBigQueryTableWithResourceTagsDestroy must be called at the end of this test to clear the resource tag bindings of the table before deletion. { @@ -1598,7 +1598,7 @@ func TestAccBigQueryTable_ResourceTags(t *testing.T) { ResourceName: "google_bigquery_table.test", ImportState: true, ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"deletion_protection"}, + ImportStateVerifyIgnore: []string{"deletion_protection", "allow_resource_tags_on_deletion"}, }, }, }) @@ -4001,6 +4001,7 @@ resource "google_bigquery_table" "test" { provider = google-beta deletion_protection = false + allow_resource_tags_on_deletion = true dataset_id = "${google_bigquery_dataset.test.dataset_id}" table_id = "%{table_id}" resource_tags = { @@ -4050,6 +4051,7 @@ resource "google_bigquery_table" "test" { provider = google-beta deletion_protection = false + allow_resource_tags_on_deletion = true dataset_id = "${google_bigquery_dataset.test.dataset_id}" table_id = "%{table_id}" resource_tags = { @@ -4100,6 +4102,7 @@ resource "google_bigquery_table" "test" { provider = google-beta deletion_protection = false + allow_resource_tags_on_deletion = true dataset_id = "${google_bigquery_dataset.test.dataset_id}" table_id = "%{table_id}" resource_tags = {} From e30d3db2479df439f26507de9583d808c692df1b Mon Sep 17 00:00:00 2001 From: Weijia Chen Date: Tue, 7 May 2024 18:23:36 +0000 Subject: [PATCH 4/5] fix indentation --- .../services/bigquery/resource_bigquery_table_test.go.erb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mmv1/third_party/terraform/services/bigquery/resource_bigquery_table_test.go.erb b/mmv1/third_party/terraform/services/bigquery/resource_bigquery_table_test.go.erb index 0f16afdcf42f..1eebf95566f5 100644 --- a/mmv1/third_party/terraform/services/bigquery/resource_bigquery_table_test.go.erb +++ b/mmv1/third_party/terraform/services/bigquery/resource_bigquery_table_test.go.erb @@ -4001,7 +4001,7 @@ resource "google_bigquery_table" "test" { provider = google-beta deletion_protection = false - allow_resource_tags_on_deletion = true + allow_resource_tags_on_deletion = true dataset_id = "${google_bigquery_dataset.test.dataset_id}" table_id = "%{table_id}" resource_tags = { @@ -4051,7 +4051,7 @@ resource "google_bigquery_table" "test" { provider = google-beta deletion_protection = false - allow_resource_tags_on_deletion = true + allow_resource_tags_on_deletion = true dataset_id = "${google_bigquery_dataset.test.dataset_id}" table_id = "%{table_id}" resource_tags = { @@ -4102,7 +4102,7 @@ resource "google_bigquery_table" "test" { provider = google-beta deletion_protection = false - allow_resource_tags_on_deletion = true + allow_resource_tags_on_deletion = true dataset_id = "${google_bigquery_dataset.test.dataset_id}" table_id = "%{table_id}" resource_tags = {} From 49351c2cbe071802fc6ec98d3d0aebad02dbfde1 Mon Sep 17 00:00:00 2001 From: Weijia Chen Date: Tue, 7 May 2024 21:12:21 +0000 Subject: [PATCH 5/5] add version guard to import logic --- .../terraform/services/bigquery/resource_bigquery_table.go.erb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mmv1/third_party/terraform/services/bigquery/resource_bigquery_table.go.erb b/mmv1/third_party/terraform/services/bigquery/resource_bigquery_table.go.erb index 130159f03a28..505fb9856e71 100644 --- a/mmv1/third_party/terraform/services/bigquery/resource_bigquery_table.go.erb +++ b/mmv1/third_party/terraform/services/bigquery/resource_bigquery_table.go.erb @@ -2686,9 +2686,11 @@ func resourceBigQueryTableImport(d *schema.ResourceData, meta interface{}) ([]*s if err := d.Set("deletion_protection", true); err != nil { return nil, fmt.Errorf("Error setting deletion_protection: %s", err) } + <% unless version == 'ga' -%> if err := d.Set("allow_resource_tags_on_deletion", false); err != nil { return nil, fmt.Errorf("Error setting allow_resource_tags_on_deletion: %s", err) } + <% end -%> // Replace import id for the resource id id, err := tpgresource.ReplaceVars(d, config, "projects/{{project}}/datasets/{{dataset_id}}/tables/{{table_id}}")