Skip to content

Commit f918124

Browse files
Allow deletion of resource_bigquery_table when it still has associated resource tags (#10568) (#7327)
[upstream:3254c2c31081538fe9df46ef6207deb0e29e57d1] Signed-off-by: Modular Magician <[email protected]>
1 parent 29c15a3 commit f918124

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

google-beta/services/bigquery/resource_bigquery_table.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,6 +1158,13 @@ func ResourceBigQueryTable() *schema.Resource {
11581158
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.`,
11591159
},
11601160

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+
11611168
// TableConstraints: [Optional] Defines the primary key and foreign keys.
11621169
"table_constraints": {
11631170
Type: schema.TypeList,
@@ -1828,13 +1835,15 @@ func resourceBigQueryTableDelete(d *schema.ResourceData, meta interface{}) error
18281835
return fmt.Errorf("cannot destroy table %v without setting deletion_protection=false and running `terraform apply`", d.Id())
18291836
}
18301837
if v, ok := d.GetOk("resource_tags"); ok {
1831-
var resourceTags []string
1838+
if !d.Get("allow_resource_tags_on_deletion").(bool) {
1839+
var resourceTags []string
18321840

1833-
for k, v := range v.(map[string]interface{}) {
1834-
resourceTags = append(resourceTags, fmt.Sprintf("%s:%s", k, v.(string)))
1835-
}
1841+
for k, v := range v.(map[string]interface{}) {
1842+
resourceTags = append(resourceTags, fmt.Sprintf("%s:%s", k, v.(string)))
1843+
}
18361844

1837-
return fmt.Errorf("cannot destroy table %v without clearing the following resource tags: %v", d.Id(), resourceTags)
1845+
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)
1846+
}
18381847
}
18391848

18401849
config := meta.(*transport_tpg.Config)
@@ -2668,6 +2677,9 @@ func resourceBigQueryTableImport(d *schema.ResourceData, meta interface{}) ([]*s
26682677
if err := d.Set("deletion_protection", true); err != nil {
26692678
return nil, fmt.Errorf("Error setting deletion_protection: %s", err)
26702679
}
2680+
if err := d.Set("allow_resource_tags_on_deletion", false); err != nil {
2681+
return nil, fmt.Errorf("Error setting allow_resource_tags_on_deletion: %s", err)
2682+
}
26712683

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

google-beta/services/bigquery/resource_bigquery_table_test.go

Lines changed: 6 additions & 3 deletions
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
})
@@ -3999,6 +3999,7 @@ resource "google_bigquery_table" "test" {
39993999
provider = google-beta
40004000
40014001
deletion_protection = false
4002+
allow_resource_tags_on_deletion = true
40024003
dataset_id = "${google_bigquery_dataset.test.dataset_id}"
40034004
table_id = "%{table_id}"
40044005
resource_tags = {
@@ -4048,6 +4049,7 @@ resource "google_bigquery_table" "test" {
40484049
provider = google-beta
40494050
40504051
deletion_protection = false
4052+
allow_resource_tags_on_deletion = true
40514053
dataset_id = "${google_bigquery_dataset.test.dataset_id}"
40524054
table_id = "%{table_id}"
40534055
resource_tags = {
@@ -4098,6 +4100,7 @@ resource "google_bigquery_table" "test" {
40984100
provider = google-beta
40994101
41004102
deletion_protection = false
4103+
allow_resource_tags_on_deletion = true
41014104
dataset_id = "${google_bigquery_dataset.test.dataset_id}"
41024105
table_id = "%{table_id}"
41034106
resource_tags = {}

0 commit comments

Comments
 (0)