Skip to content

impl(google_bigquery_table): exposing TableMetadataView query param #9530

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/13240.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
bigquery: added `table_metadata_view` query param to `google_bigquery_table`
```
31 changes: 26 additions & 5 deletions google-beta/services/bigquery/resource_bigquery_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -1439,6 +1439,11 @@ func ResourceBigQueryTable() *schema.Resource {
},
},
},
"table_metadata_view": {
Type: schema.TypeString,
Optional: true,
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`,
},
// TableReplicationInfo: [Optional] Replication info of a table created using `AS REPLICA` DDL like: `CREATE MATERIALIZED VIEW mv1 AS REPLICA OF src_mv`.
"table_replication_info": {
Type: schema.TypeList,
Expand Down Expand Up @@ -1816,7 +1821,12 @@ func resourceBigQueryTableRead(d *schema.ResourceData, meta interface{}) error {
datasetID := d.Get("dataset_id").(string)
tableID := d.Get("table_id").(string)

res, err := config.NewBigQueryClient(userAgent).Tables.Get(project, datasetID, tableID).Do()
client := config.NewBigQueryClient(userAgent).Tables.Get(project, datasetID, tableID)
if tableMetadataViewRaw, ok := d.GetOk("table_metadata_view"); ok {
client = client.View(tableMetadataViewRaw.(string))
}
res, err := client.Do()

if err != nil {
return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("BigQuery table %q", tableID))
}
Expand Down Expand Up @@ -2036,7 +2046,7 @@ type TableReference struct {

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

datasetID := d.Get("dataset_id").(string)
tableID := d.Get("table_id").(string)
var tableMetadataView string
if tableMetadataViewRaw, ok := d.GetOk("table_metadata_view"); ok {
tableMetadataView = tableMetadataViewRaw.(string)
}

tableReference := &TableReference{
project: project,
datasetID: datasetID,
tableID: tableID,
}

if err = resourceBigQueryTableColumnDrop(config, userAgent, table, tableReference); err != nil {
if err = resourceBigQueryTableColumnDrop(config, userAgent, table, tableReference, tableMetadataView); err != nil {
return err
}

Expand All @@ -2091,8 +2105,13 @@ func resourceBigQueryTableUpdate(d *schema.ResourceData, meta interface{}) error
return resourceBigQueryTableRead(d, meta)
}

func resourceBigQueryTableColumnDrop(config *transport_tpg.Config, userAgent string, table *bigquery.Table, tableReference *TableReference) error {
oldTable, err := config.NewBigQueryClient(userAgent).Tables.Get(tableReference.project, tableReference.datasetID, tableReference.tableID).Do()
func resourceBigQueryTableColumnDrop(config *transport_tpg.Config, userAgent string, table *bigquery.Table, tableReference *TableReference, tableMetadataView string) error {
client := config.NewBigQueryClient(userAgent).Tables.Get(tableReference.project, tableReference.datasetID, tableReference.tableID)
if len(tableMetadataView) > 0 {
client = client.View(tableMetadataView)
}
oldTable, err := client.Do()

if err != nil {
return err
}
Expand Down Expand Up @@ -3351,6 +3370,8 @@ func resourceBigQueryTableImport(d *schema.ResourceData, meta interface{}) ([]*s
return nil, fmt.Errorf("Error setting deletion_protection: %s", err)
}

// Explicitly set virtual fields with default values to their default values on import

// Replace import id for the resource id
id, err := tpgresource.ReplaceVars(d, config, "projects/{{project}}/datasets/{{dataset_id}}/tables/{{table_id}}")
if err != nil {
Expand Down
49 changes: 49 additions & 0 deletions google-beta/services/bigquery/resource_bigquery_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,39 @@ func TestAccBigQueryTable_Basic(t *testing.T) {
})
}

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

datasetID := fmt.Sprintf("tf_test_%s", acctest.RandString(t, 10))
tableID := fmt.Sprintf("tf_test_%s", acctest.RandString(t, 10))

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckBigQueryTableDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccBigQueryTableBasicWithTableMetadataView(datasetID, tableID),
},
{
ResourceName: "google_bigquery_table.test",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"deletion_protection", "last_modified_time", "table_metadata_view"},
},
{
Config: testAccBigQueryTableUpdated(datasetID, tableID),
},
{
ResourceName: "google_bigquery_table.test",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"deletion_protection", "last_modified_time", "table_metadata_view"},
},
},
})
}

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

Expand Down Expand Up @@ -1948,6 +1981,22 @@ EOH
`, datasetID, tableID)
}

func testAccBigQueryTableBasicWithTableMetadataView(datasetID, tableID string) string {
return fmt.Sprintf(`
resource "google_bigquery_dataset" "test" {
dataset_id = "%s"
}

resource "google_bigquery_table" "test" {
deletion_protection = false
table_id = "%s"
dataset_id = google_bigquery_dataset.test.dataset_id

table_metadata_view = "BASIC"
}
`, datasetID, tableID)
}

func testAccBigQueryTableBasicSchemaWithDeletionProtection(datasetID, tableID string) string {
return fmt.Sprintf(`
resource "google_bigquery_dataset" "test" {
Expand Down
Loading