From 5b236012b48a4c77ba020318401a669123cf6efd Mon Sep 17 00:00:00 2001 From: wj-chen Date: Wed, 30 Oct 2024 10:20:10 -0700 Subject: [PATCH] Remove unnecessary update API call for client-side-only field changes in google_bigquery_table (#12174) --- .../bigquery/resource_bigquery_table.go | 13 ++ .../bigquery/resource_bigquery_table_test.go | 151 +++++++++++++++++- 2 files changed, 163 insertions(+), 1 deletion(-) diff --git a/mmv1/third_party/terraform/services/bigquery/resource_bigquery_table.go b/mmv1/third_party/terraform/services/bigquery/resource_bigquery_table.go index 83234d895710..8528c5fb14d8 100644 --- a/mmv1/third_party/terraform/services/bigquery/resource_bigquery_table.go +++ b/mmv1/third_party/terraform/services/bigquery/resource_bigquery_table.go @@ -1903,6 +1903,19 @@ 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} + clientSideOnly := true + for field := range ResourceBigQueryTable().Schema { + if d.HasChange(field) && !clientSideFields[field] { + clientSideOnly = false + break + } + } + if clientSideOnly { + return resourceBigQueryTableRead(d, meta) + } + config := meta.(*transport_tpg.Config) userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) if err != nil { diff --git a/mmv1/third_party/terraform/services/bigquery/resource_bigquery_table_test.go b/mmv1/third_party/terraform/services/bigquery/resource_bigquery_table_test.go index 81e638036609..484f1a21a21e 100644 --- a/mmv1/third_party/terraform/services/bigquery/resource_bigquery_table_test.go +++ b/mmv1/third_party/terraform/services/bigquery/resource_bigquery_table_test.go @@ -45,6 +45,72 @@ func TestAccBigQueryTable_Basic(t *testing.T) { }) } +func TestAccBigQueryTable_OnlyDeletionProtectionUpdate(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: testAccBigQueryTableBasicSchemaWithDeletionProtection(datasetID, tableID), + }, + { + ResourceName: "google_bigquery_table.test", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"deletion_protection"}, + }, + { + Config: testAccBigQueryTableBasicSchema(datasetID, tableID), + }, + { + ResourceName: "google_bigquery_table.test", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"deletion_protection"}, + }, + }, + }) +} + +func TestAccBigQueryTable_OnlyNestedFieldUpdate(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: testAccBigQueryTableTimePartitioningWithExpirationMs(datasetID, tableID, 1000), + }, + { + ResourceName: "google_bigquery_table.test", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"deletion_protection"}, + }, + { + Config: testAccBigQueryTableTimePartitioningWithExpirationMs(datasetID, tableID, 2000), + }, + { + ResourceName: "google_bigquery_table.test", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"deletion_protection"}, + }, + }, + }) +} + func TestAccBigQueryTable_DropColumns(t *testing.T) { t.Parallel() @@ -1818,6 +1884,30 @@ EOH `, datasetID, tableID) } +func testAccBigQueryTableBasicSchemaWithDeletionProtection(datasetID, tableID string) string { + return fmt.Sprintf(` +resource "google_bigquery_dataset" "test" { + dataset_id = "%s" +} + +resource "google_bigquery_table" "test" { + deletion_protection = true + table_id = "%s" + dataset_id = google_bigquery_dataset.test.dataset_id + + schema = <