Skip to content

Commit

Permalink
Allow deletion of resource_bigquery_table when it still has associate…
Browse files Browse the repository at this point in the history
…d resource tags (#10568) (#7327)

[upstream:3254c2c31081538fe9df46ef6207deb0e29e57d1]

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored May 7, 2024
1 parent 29c15a3 commit f918124
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
22 changes: 17 additions & 5 deletions google-beta/services/bigquery/resource_bigquery_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -1158,6 +1158,13 @@ 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.`,
},

"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.`,
},

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

config := meta.(*transport_tpg.Config)
Expand Down Expand Up @@ -2668,6 +2677,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}}")
Expand Down
9 changes: 6 additions & 3 deletions google-beta/services/bigquery/resource_bigquery_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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.
{
Expand All @@ -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"},
},
},
})
Expand Down Expand Up @@ -3999,6 +3999,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 = {
Expand Down Expand Up @@ -4048,6 +4049,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 = {
Expand Down Expand Up @@ -4098,6 +4100,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 = {}
Expand Down

0 comments on commit f918124

Please sign in to comment.