Skip to content

Commit c9ed94a

Browse files
wj-chenpengq-google
authored andcommitted
Allow deletion of resource_bigquery_table when it still has associated resource tags (GoogleCloudPlatform#10568)
1 parent f943c55 commit c9ed94a

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

mmv1/products/bigquery/Table.yaml

+7
Original file line numberDiff line numberDiff line change
@@ -516,3 +516,10 @@ properties:
516516
in the namespaced format, for example "123456789012/environment" where 123456789012 is the
517517
ID of the parent organization or project resource for this tag key. Tag value is expected
518518
to be the short name, for example "Production".
519+
virtual_fields:
520+
- !ruby/object:Api::Type::Boolean
521+
name: 'allow_resource_tags_on_deletion'
522+
min_version: beta
523+
description: |
524+
Whether or not to allow table deletion when there are still resource tags attached.
525+
default_value: false

mmv1/third_party/terraform/services/bigquery/resource_bigquery_table.go.erb

+21-5
Original file line numberDiff line numberDiff line change
@@ -1157,6 +1157,15 @@ func ResourceBigQueryTable() *schema.Resource {
11571157
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.`,
11581158
},
11591159

1160+
<% unless version == 'ga' -%>
1161+
"allow_resource_tags_on_deletion": {
1162+
Type: schema.TypeBool,
1163+
Optional: true,
1164+
Default: false,
1165+
Description: `Whether or not to allow table deletion when there are still resource tags attached.`,
1166+
},
1167+
1168+
<% end -%>
11601169
// TableConstraints: [Optional] Defines the primary key and foreign keys.
11611170
"table_constraints": {
11621171
Type: schema.TypeList,
@@ -1834,13 +1843,15 @@ func resourceBigQueryTableDelete(d *schema.ResourceData, meta interface{}) error
18341843
}
18351844
<% unless version == 'ga' -%>
18361845
if v, ok := d.GetOk("resource_tags"); ok {
1837-
var resourceTags []string
1846+
if !d.Get("allow_resource_tags_on_deletion").(bool) {
1847+
var resourceTags []string
18381848

1839-
for k, v := range v.(map[string]interface{}) {
1840-
resourceTags = append(resourceTags, fmt.Sprintf("%s:%s", k, v.(string)))
1841-
}
1849+
for k, v := range v.(map[string]interface{}) {
1850+
resourceTags = append(resourceTags, fmt.Sprintf("%s:%s", k, v.(string)))
1851+
}
18421852

1843-
return fmt.Errorf("cannot destroy table %v without clearing the following resource tags: %v", d.Id(), resourceTags)
1853+
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)
1854+
}
18441855
}
18451856

18461857
<% end -%>
@@ -2675,6 +2686,11 @@ func resourceBigQueryTableImport(d *schema.ResourceData, meta interface{}) ([]*s
26752686
if err := d.Set("deletion_protection", true); err != nil {
26762687
return nil, fmt.Errorf("Error setting deletion_protection: %s", err)
26772688
}
2689+
<% unless version == 'ga' -%>
2690+
if err := d.Set("allow_resource_tags_on_deletion", false); err != nil {
2691+
return nil, fmt.Errorf("Error setting allow_resource_tags_on_deletion: %s", err)
2692+
}
2693+
<% end -%>
26782694

26792695
// Replace import id for the resource id
26802696
id, err := tpgresource.ReplaceVars(d, config, "projects/{{project}}/datasets/{{dataset_id}}/tables/{{table_id}}")

mmv1/third_party/terraform/services/bigquery/resource_bigquery_table_test.go.erb

+6-3
Original file line numberDiff line numberDiff line change
@@ -1579,7 +1579,7 @@ func TestAccBigQueryTable_ResourceTags(t *testing.T) {
15791579
ResourceName: "google_bigquery_table.test",
15801580
ImportState: true,
15811581
ImportStateVerify: true,
1582-
ImportStateVerifyIgnore: []string{"deletion_protection"},
1582+
ImportStateVerifyIgnore: []string{"deletion_protection", "allow_resource_tags_on_deletion"},
15831583
},
15841584
{
15851585
Config: testAccBigQueryTableWithResourceTagsUpdate(context),
@@ -1588,7 +1588,7 @@ func TestAccBigQueryTable_ResourceTags(t *testing.T) {
15881588
ResourceName: "google_bigquery_table.test",
15891589
ImportState: true,
15901590
ImportStateVerify: true,
1591-
ImportStateVerifyIgnore: []string{"deletion_protection"},
1591+
ImportStateVerifyIgnore: []string{"deletion_protection", "allow_resource_tags_on_deletion"},
15921592
},
15931593
// testAccBigQueryTableWithResourceTagsDestroy must be called at the end of this test to clear the resource tag bindings of the table before deletion.
15941594
{
@@ -1598,7 +1598,7 @@ func TestAccBigQueryTable_ResourceTags(t *testing.T) {
15981598
ResourceName: "google_bigquery_table.test",
15991599
ImportState: true,
16001600
ImportStateVerify: true,
1601-
ImportStateVerifyIgnore: []string{"deletion_protection"},
1601+
ImportStateVerifyIgnore: []string{"deletion_protection", "allow_resource_tags_on_deletion"},
16021602
},
16031603
},
16041604
})
@@ -4001,6 +4001,7 @@ resource "google_bigquery_table" "test" {
40014001
provider = google-beta
40024002

40034003
deletion_protection = false
4004+
allow_resource_tags_on_deletion = true
40044005
dataset_id = "${google_bigquery_dataset.test.dataset_id}"
40054006
table_id = "%{table_id}"
40064007
resource_tags = {
@@ -4050,6 +4051,7 @@ resource "google_bigquery_table" "test" {
40504051
provider = google-beta
40514052

40524053
deletion_protection = false
4054+
allow_resource_tags_on_deletion = true
40534055
dataset_id = "${google_bigquery_dataset.test.dataset_id}"
40544056
table_id = "%{table_id}"
40554057
resource_tags = {
@@ -4100,6 +4102,7 @@ resource "google_bigquery_table" "test" {
41004102
provider = google-beta
41014103

41024104
deletion_protection = false
4105+
allow_resource_tags_on_deletion = true
41034106
dataset_id = "${google_bigquery_dataset.test.dataset_id}"
41044107
table_id = "%{table_id}"
41054108
resource_tags = {}

0 commit comments

Comments
 (0)