Skip to content

Commit 33ea4b6

Browse files
Add resource tags to BigQuery Table (#10455) (#7247)
[upstream:c896dbe9a664c43d36e8acbe38b8b90a01e77326] Signed-off-by: Modular Magician <[email protected]>
1 parent 1f985d2 commit 33ea4b6

File tree

3 files changed

+212
-1
lines changed

3 files changed

+212
-1
lines changed

.changelog/10455.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
bigquery: added `resource_tags` field to `google_bigquery_table` resource
3+
```

google-beta/services/bigquery/resource_bigquery_table.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1307,6 +1307,12 @@ func ResourceBigQueryTable() *schema.Resource {
13071307
},
13081308
},
13091309
},
1310+
"resource_tags": {
1311+
Type: schema.TypeMap,
1312+
Optional: true,
1313+
Elem: &schema.Schema{Type: schema.TypeString},
1314+
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".`,
1315+
},
13101316
},
13111317
UseJSONNumber: true,
13121318
}
@@ -1421,6 +1427,8 @@ func resourceTable(d *schema.ResourceData, meta interface{}) (*bigquery.Table, e
14211427
table.TableConstraints = tableConstraints
14221428
}
14231429

1430+
table.ResourceTags = tpgresource.ExpandStringMap(d, "resource_tags")
1431+
14241432
return table, nil
14251433
}
14261434

@@ -1694,6 +1702,10 @@ func resourceBigQueryTableRead(d *schema.ResourceData, meta interface{}) error {
16941702
}
16951703
}
16961704

1705+
if err := d.Set("resource_tags", res.ResourceTags); err != nil {
1706+
return fmt.Errorf("Error setting resource tags: %s", err)
1707+
}
1708+
16971709
// TODO: Update when the Get API fields for TableReplicationInfo are available in the client library.
16981710
url, err := tpgresource.ReplaceVars(d, config, "{{BigQueryBasePath}}projects/{{project}}/datasets/{{dataset_id}}/tables/{{table_id}}")
16991711
if err != nil {
@@ -1774,6 +1786,10 @@ func resourceBigQueryTableColumnDrop(config *transport_tpg.Config, userAgent str
17741786
return err
17751787
}
17761788

1789+
if table.Schema == nil {
1790+
return nil
1791+
}
1792+
17771793
newTableFields := map[string]bool{}
17781794
for _, field := range table.Schema.Fields {
17791795
newTableFields[field.Name] = true
@@ -1809,8 +1825,18 @@ func resourceBigQueryTableColumnDrop(config *transport_tpg.Config, userAgent str
18091825

18101826
func resourceBigQueryTableDelete(d *schema.ResourceData, meta interface{}) error {
18111827
if d.Get("deletion_protection").(bool) {
1812-
return fmt.Errorf("cannot destroy instance without setting deletion_protection=false and running `terraform apply`")
1828+
return fmt.Errorf("cannot destroy table %v without setting deletion_protection=false and running `terraform apply`", d.Id())
1829+
}
1830+
if v, ok := d.GetOk("resource_tags"); ok {
1831+
var resourceTags []string
1832+
1833+
for k, v := range v.(map[string]interface{}) {
1834+
resourceTags = append(resourceTags, fmt.Sprintf("%s:%s", k, v.(string)))
1835+
}
1836+
1837+
return fmt.Errorf("cannot destroy table %v without clearing the following resource tags: %v", d.Id(), resourceTags)
18131838
}
1839+
18141840
config := meta.(*transport_tpg.Config)
18151841
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
18161842
if err != nil {

google-beta/services/bigquery/resource_bigquery_table_test.go

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1554,6 +1554,56 @@ func TestAccBigQueryTable_TableReplicationInfo_WithReplicationInterval(t *testin
15541554
})
15551555
}
15561556

1557+
func TestAccBigQueryTable_ResourceTags(t *testing.T) {
1558+
t.Parallel()
1559+
1560+
context := map[string]interface{}{
1561+
"project_id": envvar.GetTestProjectFromEnv(),
1562+
"dataset_id": fmt.Sprintf("tf_test_dataset_%s", acctest.RandString(t, 10)),
1563+
"table_id": fmt.Sprintf("tf_test_table_%s", acctest.RandString(t, 10)),
1564+
"tag_key_name1": fmt.Sprintf("tf_test_tag_key1_%s", acctest.RandString(t, 10)),
1565+
"tag_value_name1": fmt.Sprintf("tf_test_tag_value1_%s", acctest.RandString(t, 10)),
1566+
"tag_key_name2": fmt.Sprintf("tf_test_tag_key2_%s", acctest.RandString(t, 10)),
1567+
"tag_value_name2": fmt.Sprintf("tf_test_tag_value2_%s", acctest.RandString(t, 10)),
1568+
}
1569+
1570+
acctest.VcrTest(t, resource.TestCase{
1571+
PreCheck: func() { acctest.AccTestPreCheck(t) },
1572+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderBetaFactories(t),
1573+
CheckDestroy: testAccCheckBigQueryTableDestroyProducer(t),
1574+
Steps: []resource.TestStep{
1575+
{
1576+
Config: testAccBigQueryTableWithResourceTags(context),
1577+
},
1578+
{
1579+
ResourceName: "google_bigquery_table.test",
1580+
ImportState: true,
1581+
ImportStateVerify: true,
1582+
ImportStateVerifyIgnore: []string{"deletion_protection"},
1583+
},
1584+
{
1585+
Config: testAccBigQueryTableWithResourceTagsUpdate(context),
1586+
},
1587+
{
1588+
ResourceName: "google_bigquery_table.test",
1589+
ImportState: true,
1590+
ImportStateVerify: true,
1591+
ImportStateVerifyIgnore: []string{"deletion_protection"},
1592+
},
1593+
// testAccBigQueryTableWithResourceTagsDestroy must be called at the end of this test to clear the resource tag bindings of the table before deletion.
1594+
{
1595+
Config: testAccBigQueryTableWithResourceTagsDestroy(context),
1596+
},
1597+
{
1598+
ResourceName: "google_bigquery_table.test",
1599+
ImportState: true,
1600+
ImportStateVerify: true,
1601+
ImportStateVerifyIgnore: []string{"deletion_protection"},
1602+
},
1603+
},
1604+
})
1605+
}
1606+
15571607
func testAccCheckBigQueryExtData(t *testing.T, expectedQuoteChar string) resource.TestCheckFunc {
15581608
return func(s *terraform.State) error {
15591609
for _, rs := range s.RootModule().Resources {
@@ -3923,6 +3973,138 @@ resource "time_sleep" "wait_10_seconds_last" {
39233973
`, sourceDatasetID, sourceTableID, sourceMVJobID, sourceDatasetID, sourceMVID, sourceDatasetID, sourceTableID, projectID, sourceMVID, replicaDatasetID, replicaMVID, projectID, sourceMVID, replicationIntervalExpr, dropMVJobID, sourceDatasetID, sourceMVID)
39243974
}
39253975

3976+
func testAccBigQueryTableWithResourceTags(context map[string]interface{}) string {
3977+
return acctest.Nprintf(`
3978+
resource "google_tags_tag_key" "key1" {
3979+
provider = google-beta
3980+
3981+
parent = "projects/%{project_id}"
3982+
short_name = "%{tag_key_name1}"
3983+
}
3984+
3985+
resource "google_tags_tag_value" "value1" {
3986+
provider = google-beta
3987+
3988+
parent = "tagKeys/${google_tags_tag_key.key1.name}"
3989+
short_name = "%{tag_value_name1}"
3990+
}
3991+
3992+
resource "google_bigquery_dataset" "test" {
3993+
provider = google-beta
3994+
3995+
dataset_id = "%{dataset_id}"
3996+
}
3997+
3998+
resource "google_bigquery_table" "test" {
3999+
provider = google-beta
4000+
4001+
deletion_protection = false
4002+
dataset_id = "${google_bigquery_dataset.test.dataset_id}"
4003+
table_id = "%{table_id}"
4004+
resource_tags = {
4005+
"%{project_id}/${google_tags_tag_key.key1.short_name}" = "${google_tags_tag_value.value1.short_name}"
4006+
}
4007+
}
4008+
`, context)
4009+
}
4010+
4011+
func testAccBigQueryTableWithResourceTagsUpdate(context map[string]interface{}) string {
4012+
return acctest.Nprintf(`
4013+
resource "google_tags_tag_key" "key1" {
4014+
provider = google-beta
4015+
4016+
parent = "projects/%{project_id}"
4017+
short_name = "%{tag_key_name1}"
4018+
}
4019+
4020+
resource "google_tags_tag_value" "value1" {
4021+
provider = google-beta
4022+
4023+
parent = "tagKeys/${google_tags_tag_key.key1.name}"
4024+
short_name = "%{tag_value_name1}"
4025+
}
4026+
4027+
resource "google_tags_tag_key" "key2" {
4028+
provider = google-beta
4029+
4030+
parent = "projects/%{project_id}"
4031+
short_name = "%{tag_key_name2}"
4032+
}
4033+
4034+
resource "google_tags_tag_value" "value2" {
4035+
provider = google-beta
4036+
4037+
parent = "tagKeys/${google_tags_tag_key.key2.name}"
4038+
short_name = "%{tag_value_name2}"
4039+
}
4040+
4041+
resource "google_bigquery_dataset" "test" {
4042+
provider = google-beta
4043+
4044+
dataset_id = "%{dataset_id}"
4045+
}
4046+
4047+
resource "google_bigquery_table" "test" {
4048+
provider = google-beta
4049+
4050+
deletion_protection = false
4051+
dataset_id = "${google_bigquery_dataset.test.dataset_id}"
4052+
table_id = "%{table_id}"
4053+
resource_tags = {
4054+
"%{project_id}/${google_tags_tag_key.key1.short_name}" = "${google_tags_tag_value.value1.short_name}"
4055+
"%{project_id}/${google_tags_tag_key.key2.short_name}" = "${google_tags_tag_value.value2.short_name}"
4056+
}
4057+
}
4058+
`, context)
4059+
}
4060+
4061+
func testAccBigQueryTableWithResourceTagsDestroy(context map[string]interface{}) string {
4062+
return acctest.Nprintf(`
4063+
resource "google_tags_tag_key" "key1" {
4064+
provider = google-beta
4065+
4066+
parent = "projects/%{project_id}"
4067+
short_name = "%{tag_key_name1}"
4068+
}
4069+
4070+
resource "google_tags_tag_value" "value1" {
4071+
provider = google-beta
4072+
4073+
parent = "tagKeys/${google_tags_tag_key.key1.name}"
4074+
short_name = "%{tag_value_name1}"
4075+
}
4076+
4077+
resource "google_tags_tag_key" "key2" {
4078+
provider = google-beta
4079+
4080+
parent = "projects/%{project_id}"
4081+
short_name = "%{tag_key_name2}"
4082+
}
4083+
4084+
resource "google_tags_tag_value" "value2" {
4085+
provider = google-beta
4086+
4087+
parent = "tagKeys/${google_tags_tag_key.key2.name}"
4088+
short_name = "%{tag_value_name2}"
4089+
}
4090+
4091+
resource "google_bigquery_dataset" "test" {
4092+
provider = google-beta
4093+
4094+
dataset_id = "%{dataset_id}"
4095+
}
4096+
4097+
resource "google_bigquery_table" "test" {
4098+
provider = google-beta
4099+
4100+
deletion_protection = false
4101+
dataset_id = "${google_bigquery_dataset.test.dataset_id}"
4102+
table_id = "%{table_id}"
4103+
resource_tags = {}
4104+
}
4105+
`, context)
4106+
}
4107+
39264108
var TEST_CSV = `lifelock,LifeLock,,web,Tempe,AZ,1-May-07,6850000,USD,b
39274109
lifelock,LifeLock,,web,Tempe,AZ,1-Oct-06,6000000,USD,a
39284110
lifelock,LifeLock,,web,Tempe,AZ,1-Jan-08,25000000,USD,c

0 commit comments

Comments
 (0)