Skip to content

Commit 0913590

Browse files
authored
impl(google_bigquery_table): exposing TableMetadataView query param (#13240)
1 parent ad2ff30 commit 0913590

File tree

3 files changed

+82
-5
lines changed

3 files changed

+82
-5
lines changed

mmv1/products/bigquery/Table.yaml

+7
Original file line numberDiff line numberDiff line change
@@ -645,3 +645,10 @@ properties:
645645
from BigQuery Engine. The connection_id can have the form `<project_id>.<location_id>.<connection_id>`
646646
or `projects/<project_id>/locations/<location_id>/connections/<connection_id>`.
647647
min_version: beta
648+
virtual_fields:
649+
- name: 'table_metadata_view'
650+
type: String
651+
description: |
652+
View sets the optional parameter "view": Specifies the view that determines which table information is
653+
returned. By default, basic table information and storage statistics (STORAGE_STATS) are returned.
654+
Possible values: TABLE_METADATA_VIEW_UNSPECIFIED, BASIC, STORAGE_STATS, FULL

mmv1/third_party/terraform/services/bigquery/resource_bigquery_table.go.tmpl

+26-5
Original file line numberDiff line numberDiff line change
@@ -1447,6 +1447,11 @@ func ResourceBigQueryTable() *schema.Resource {
14471447
},
14481448
},
14491449
},
1450+
"table_metadata_view": {
1451+
Type: schema.TypeString,
1452+
Optional: true,
1453+
Description: `View sets the optional parameter "view": Specifies the view that determines which table information is returned. By default, basic table information and storage statistics (STORAGE_STATS) are returned. Possible values: TABLE_METADATA_VIEW_UNSPECIFIED, BASIC, STORAGE_STATS, FULL`,
1454+
},
14501455
// TableReplicationInfo: [Optional] Replication info of a table created using `AS REPLICA` DDL like: `CREATE MATERIALIZED VIEW mv1 AS REPLICA OF src_mv`.
14511456
"table_replication_info": {
14521457
Type: schema.TypeList,
@@ -1834,7 +1839,12 @@ func resourceBigQueryTableRead(d *schema.ResourceData, meta interface{}) error {
18341839
datasetID := d.Get("dataset_id").(string)
18351840
tableID := d.Get("table_id").(string)
18361841

1837-
res, err := config.NewBigQueryClient(userAgent).Tables.Get(project, datasetID, tableID).Do()
1842+
client := config.NewBigQueryClient(userAgent).Tables.Get(project, datasetID, tableID)
1843+
if tableMetadataViewRaw, ok := d.GetOk("table_metadata_view"); ok {
1844+
client = client.View(tableMetadataViewRaw.(string))
1845+
}
1846+
res, err := client.Do()
1847+
18381848
if err != nil {
18391849
return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("BigQuery table %q", tableID))
18401850
}
@@ -2060,7 +2070,7 @@ type TableReference struct {
20602070

20612071
func resourceBigQueryTableUpdate(d *schema.ResourceData, meta interface{}) error {
20622072
// If only client-side fields were modified, short-circuit the Update function to avoid sending an update API request.
2063-
clientSideFields := map[string]bool{"deletion_protection": true}
2073+
clientSideFields := map[string]bool{"deletion_protection": true, "table_metadata_view": true}
20642074
clientSideOnly := true
20652075
for field := range ResourceBigQueryTable().Schema {
20662076
if d.HasChange(field) && !clientSideFields[field] {
@@ -2097,14 +2107,18 @@ func resourceBigQueryTableUpdate(d *schema.ResourceData, meta interface{}) error
20972107

20982108
datasetID := d.Get("dataset_id").(string)
20992109
tableID := d.Get("table_id").(string)
2110+
var tableMetadataView string
2111+
if tableMetadataViewRaw, ok := d.GetOk("table_metadata_view"); ok {
2112+
tableMetadataView = tableMetadataViewRaw.(string)
2113+
}
21002114

21012115
tableReference := &TableReference{
21022116
project: project,
21032117
datasetID: datasetID,
21042118
tableID: tableID,
21052119
}
21062120

2107-
if err = resourceBigQueryTableColumnDrop(config, userAgent, table, tableReference); err != nil {
2121+
if err = resourceBigQueryTableColumnDrop(config, userAgent, table, tableReference, tableMetadataView); err != nil {
21082122
return err
21092123
}
21102124

@@ -2115,8 +2129,13 @@ func resourceBigQueryTableUpdate(d *schema.ResourceData, meta interface{}) error
21152129
return resourceBigQueryTableRead(d, meta)
21162130
}
21172131

2118-
func resourceBigQueryTableColumnDrop(config *transport_tpg.Config, userAgent string, table *bigquery.Table, tableReference *TableReference) error {
2119-
oldTable, err := config.NewBigQueryClient(userAgent).Tables.Get(tableReference.project, tableReference.datasetID, tableReference.tableID).Do()
2132+
func resourceBigQueryTableColumnDrop(config *transport_tpg.Config, userAgent string, table *bigquery.Table, tableReference *TableReference, tableMetadataView string) error {
2133+
client := config.NewBigQueryClient(userAgent).Tables.Get(tableReference.project, tableReference.datasetID, tableReference.tableID)
2134+
if len(tableMetadataView) > 0 {
2135+
client = client.View(tableMetadataView)
2136+
}
2137+
oldTable, err := client.Do()
2138+
21202139
if err != nil {
21212140
return err
21222141
}
@@ -3382,6 +3401,8 @@ func resourceBigQueryTableImport(d *schema.ResourceData, meta interface{}) ([]*s
33823401
return nil, fmt.Errorf("Error setting deletion_protection: %s", err)
33833402
}
33843403

3404+
// Explicitly set virtual fields with default values to their default values on import
3405+
33853406
// Replace import id for the resource id
33863407
id, err := tpgresource.ReplaceVars(d, config, "projects/{{"{{"}}project{{"}}"}}/datasets/{{"{{"}}dataset_id{{"}}"}}/tables/{{"{{"}}table_id{{"}}"}}")
33873408
if err != nil {

mmv1/third_party/terraform/services/bigquery/resource_bigquery_table_test.go.tmpl

+49
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,39 @@ func TestAccBigQueryTable_Basic(t *testing.T) {
4545
})
4646
}
4747

48+
func TestAccBigQueryTable_TableMetadataView(t *testing.T) {
49+
t.Parallel()
50+
51+
datasetID := fmt.Sprintf("tf_test_%s", acctest.RandString(t, 10))
52+
tableID := fmt.Sprintf("tf_test_%s", acctest.RandString(t, 10))
53+
54+
acctest.VcrTest(t, resource.TestCase{
55+
PreCheck: func() { acctest.AccTestPreCheck(t) },
56+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
57+
CheckDestroy: testAccCheckBigQueryTableDestroyProducer(t),
58+
Steps: []resource.TestStep{
59+
{
60+
Config: testAccBigQueryTableBasicWithTableMetadataView(datasetID, tableID),
61+
},
62+
{
63+
ResourceName: "google_bigquery_table.test",
64+
ImportState: true,
65+
ImportStateVerify: true,
66+
ImportStateVerifyIgnore: []string{"deletion_protection", "last_modified_time", "table_metadata_view"},
67+
},
68+
{
69+
Config: testAccBigQueryTableUpdated(datasetID, tableID),
70+
},
71+
{
72+
ResourceName: "google_bigquery_table.test",
73+
ImportState: true,
74+
ImportStateVerify: true,
75+
ImportStateVerifyIgnore: []string{"deletion_protection", "last_modified_time", "table_metadata_view"},
76+
},
77+
},
78+
})
79+
}
80+
4881
func TestAccBigQueryTable_OnlyDeletionProtectionUpdate(t *testing.T) {
4982
t.Parallel()
5083

@@ -1950,6 +1983,22 @@ EOH
19501983
`, datasetID, tableID)
19511984
}
19521985

1986+
func testAccBigQueryTableBasicWithTableMetadataView(datasetID, tableID string) string {
1987+
return fmt.Sprintf(`
1988+
resource "google_bigquery_dataset" "test" {
1989+
dataset_id = "%s"
1990+
}
1991+
1992+
resource "google_bigquery_table" "test" {
1993+
deletion_protection = false
1994+
table_id = "%s"
1995+
dataset_id = google_bigquery_dataset.test.dataset_id
1996+
1997+
table_metadata_view = "BASIC"
1998+
}
1999+
`, datasetID, tableID)
2000+
}
2001+
19532002
func testAccBigQueryTableBasicSchemaWithDeletionProtection(datasetID, tableID string) string {
19542003
return fmt.Sprintf(`
19552004
resource "google_bigquery_dataset" "test" {

0 commit comments

Comments
 (0)