Skip to content

Commit

Permalink
add a virtual field allow_resource_tags_on_deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
wj-chen committed May 6, 2024
1 parent e2f1507 commit 1409171
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
7 changes: 7 additions & 0 deletions mmv1/products/bigquery/Table.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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}}")
Expand Down

0 comments on commit 1409171

Please sign in to comment.