Skip to content

Commit

Permalink
Add resource tags to BigQuery Table (#10455) (hashicorp#7247)
Browse files Browse the repository at this point in the history
[upstream:c896dbe9a664c43d36e8acbe38b8b90a01e77326]

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Apr 16, 2024
1 parent 1f985d2 commit 33ea4b6
Show file tree
Hide file tree
Showing 3 changed files with 212 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .changelog/10455.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
bigquery: added `resource_tags` field to `google_bigquery_table` resource
```
28 changes: 27 additions & 1 deletion google-beta/services/bigquery/resource_bigquery_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -1307,6 +1307,12 @@ func ResourceBigQueryTable() *schema.Resource {
},
},
},
"resource_tags": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: `The tags attached to this table. Tag keys are globally unique. Tag key is expected to be 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".`,
},
},
UseJSONNumber: true,
}
Expand Down Expand Up @@ -1421,6 +1427,8 @@ func resourceTable(d *schema.ResourceData, meta interface{}) (*bigquery.Table, e
table.TableConstraints = tableConstraints
}

table.ResourceTags = tpgresource.ExpandStringMap(d, "resource_tags")

return table, nil
}

Expand Down Expand Up @@ -1694,6 +1702,10 @@ func resourceBigQueryTableRead(d *schema.ResourceData, meta interface{}) error {
}
}

if err := d.Set("resource_tags", res.ResourceTags); err != nil {
return fmt.Errorf("Error setting resource tags: %s", err)
}

// TODO: Update when the Get API fields for TableReplicationInfo are available in the client library.
url, err := tpgresource.ReplaceVars(d, config, "{{BigQueryBasePath}}projects/{{project}}/datasets/{{dataset_id}}/tables/{{table_id}}")
if err != nil {
Expand Down Expand Up @@ -1774,6 +1786,10 @@ func resourceBigQueryTableColumnDrop(config *transport_tpg.Config, userAgent str
return err
}

if table.Schema == nil {
return nil
}

newTableFields := map[string]bool{}
for _, field := range table.Schema.Fields {
newTableFields[field.Name] = true
Expand Down Expand Up @@ -1809,8 +1825,18 @@ func resourceBigQueryTableColumnDrop(config *transport_tpg.Config, userAgent str

func resourceBigQueryTableDelete(d *schema.ResourceData, meta interface{}) error {
if d.Get("deletion_protection").(bool) {
return fmt.Errorf("cannot destroy instance without setting deletion_protection=false and running `terraform apply`")
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

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)
}

config := meta.(*transport_tpg.Config)
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
if err != nil {
Expand Down
182 changes: 182 additions & 0 deletions google-beta/services/bigquery/resource_bigquery_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1554,6 +1554,56 @@ func TestAccBigQueryTable_TableReplicationInfo_WithReplicationInterval(t *testin
})
}

func TestAccBigQueryTable_ResourceTags(t *testing.T) {
t.Parallel()

context := map[string]interface{}{
"project_id": envvar.GetTestProjectFromEnv(),
"dataset_id": fmt.Sprintf("tf_test_dataset_%s", acctest.RandString(t, 10)),
"table_id": fmt.Sprintf("tf_test_table_%s", acctest.RandString(t, 10)),
"tag_key_name1": fmt.Sprintf("tf_test_tag_key1_%s", acctest.RandString(t, 10)),
"tag_value_name1": fmt.Sprintf("tf_test_tag_value1_%s", acctest.RandString(t, 10)),
"tag_key_name2": fmt.Sprintf("tf_test_tag_key2_%s", acctest.RandString(t, 10)),
"tag_value_name2": fmt.Sprintf("tf_test_tag_value2_%s", acctest.RandString(t, 10)),
}

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderBetaFactories(t),
CheckDestroy: testAccCheckBigQueryTableDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccBigQueryTableWithResourceTags(context),
},
{
ResourceName: "google_bigquery_table.test",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"deletion_protection"},
},
{
Config: testAccBigQueryTableWithResourceTagsUpdate(context),
},
{
ResourceName: "google_bigquery_table.test",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"deletion_protection"},
},
// testAccBigQueryTableWithResourceTagsDestroy must be called at the end of this test to clear the resource tag bindings of the table before deletion.
{
Config: testAccBigQueryTableWithResourceTagsDestroy(context),
},
{
ResourceName: "google_bigquery_table.test",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"deletion_protection"},
},
},
})
}

func testAccCheckBigQueryExtData(t *testing.T, expectedQuoteChar string) resource.TestCheckFunc {
return func(s *terraform.State) error {
for _, rs := range s.RootModule().Resources {
Expand Down Expand Up @@ -3923,6 +3973,138 @@ resource "time_sleep" "wait_10_seconds_last" {
`, sourceDatasetID, sourceTableID, sourceMVJobID, sourceDatasetID, sourceMVID, sourceDatasetID, sourceTableID, projectID, sourceMVID, replicaDatasetID, replicaMVID, projectID, sourceMVID, replicationIntervalExpr, dropMVJobID, sourceDatasetID, sourceMVID)
}

func testAccBigQueryTableWithResourceTags(context map[string]interface{}) string {
return acctest.Nprintf(`
resource "google_tags_tag_key" "key1" {
provider = google-beta
parent = "projects/%{project_id}"
short_name = "%{tag_key_name1}"
}
resource "google_tags_tag_value" "value1" {
provider = google-beta
parent = "tagKeys/${google_tags_tag_key.key1.name}"
short_name = "%{tag_value_name1}"
}
resource "google_bigquery_dataset" "test" {
provider = google-beta
dataset_id = "%{dataset_id}"
}
resource "google_bigquery_table" "test" {
provider = google-beta
deletion_protection = false
dataset_id = "${google_bigquery_dataset.test.dataset_id}"
table_id = "%{table_id}"
resource_tags = {
"%{project_id}/${google_tags_tag_key.key1.short_name}" = "${google_tags_tag_value.value1.short_name}"
}
}
`, context)
}

func testAccBigQueryTableWithResourceTagsUpdate(context map[string]interface{}) string {
return acctest.Nprintf(`
resource "google_tags_tag_key" "key1" {
provider = google-beta
parent = "projects/%{project_id}"
short_name = "%{tag_key_name1}"
}
resource "google_tags_tag_value" "value1" {
provider = google-beta
parent = "tagKeys/${google_tags_tag_key.key1.name}"
short_name = "%{tag_value_name1}"
}
resource "google_tags_tag_key" "key2" {
provider = google-beta
parent = "projects/%{project_id}"
short_name = "%{tag_key_name2}"
}
resource "google_tags_tag_value" "value2" {
provider = google-beta
parent = "tagKeys/${google_tags_tag_key.key2.name}"
short_name = "%{tag_value_name2}"
}
resource "google_bigquery_dataset" "test" {
provider = google-beta
dataset_id = "%{dataset_id}"
}
resource "google_bigquery_table" "test" {
provider = google-beta
deletion_protection = false
dataset_id = "${google_bigquery_dataset.test.dataset_id}"
table_id = "%{table_id}"
resource_tags = {
"%{project_id}/${google_tags_tag_key.key1.short_name}" = "${google_tags_tag_value.value1.short_name}"
"%{project_id}/${google_tags_tag_key.key2.short_name}" = "${google_tags_tag_value.value2.short_name}"
}
}
`, context)
}

func testAccBigQueryTableWithResourceTagsDestroy(context map[string]interface{}) string {
return acctest.Nprintf(`
resource "google_tags_tag_key" "key1" {
provider = google-beta
parent = "projects/%{project_id}"
short_name = "%{tag_key_name1}"
}
resource "google_tags_tag_value" "value1" {
provider = google-beta
parent = "tagKeys/${google_tags_tag_key.key1.name}"
short_name = "%{tag_value_name1}"
}
resource "google_tags_tag_key" "key2" {
provider = google-beta
parent = "projects/%{project_id}"
short_name = "%{tag_key_name2}"
}
resource "google_tags_tag_value" "value2" {
provider = google-beta
parent = "tagKeys/${google_tags_tag_key.key2.name}"
short_name = "%{tag_value_name2}"
}
resource "google_bigquery_dataset" "test" {
provider = google-beta
dataset_id = "%{dataset_id}"
}
resource "google_bigquery_table" "test" {
provider = google-beta
deletion_protection = false
dataset_id = "${google_bigquery_dataset.test.dataset_id}"
table_id = "%{table_id}"
resource_tags = {}
}
`, context)
}

var TEST_CSV = `lifelock,LifeLock,,web,Tempe,AZ,1-May-07,6850000,USD,b
lifelock,LifeLock,,web,Tempe,AZ,1-Oct-06,6000000,USD,a
lifelock,LifeLock,,web,Tempe,AZ,1-Jan-08,25000000,USD,c
Expand Down

0 comments on commit 33ea4b6

Please sign in to comment.