Skip to content

Commit

Permalink
fix: add updateMask to bqtransfer config updateurl (#8433) (#15359)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Aug 1, 2023
1 parent c7fe758 commit 26b073d
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 73 deletions.
3 changes: 3 additions & 0 deletions .changelog/8433.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
bigquery: fixed a bug in update support for several fields in `google_bigquery_data_transfer_config`
```
33 changes: 5 additions & 28 deletions google/resource_bigquery_data_transfer_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,6 @@ func testAccCheckBigqueryDataTransferConfigDestroyProducer(t *testing.T) func(s
func testAccBigqueryDataTransferConfig_scheduledQuery_update_service_account(t *testing.T) {
random_suffix1 := acctest.RandString(t, 10)
random_suffix2 := acctest.RandString(t, 10)
transferConfigID := ""

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
Expand All @@ -397,7 +396,6 @@ func testAccBigqueryDataTransferConfig_scheduledQuery_update_service_account(t *
Steps: []resource.TestStep{
{
Config: testAccBigqueryDataTransferConfig_scheduledQuery_updateServiceAccount(random_suffix1, random_suffix1),
Check: testAccCheckDataTransferConfigID("google_bigquery_data_transfer_config.query_config", &transferConfigID),
},
{
ResourceName: "google_bigquery_data_transfer_config.query_config",
Expand All @@ -407,7 +405,7 @@ func testAccBigqueryDataTransferConfig_scheduledQuery_update_service_account(t *
},
{
Config: testAccBigqueryDataTransferConfig_scheduledQuery_updateServiceAccount(random_suffix1, random_suffix2),
Check: testAccCheckDataTransferConfigIDChange("google_bigquery_data_transfer_config.query_config", &transferConfigID),
Check: testAccCheckDataTransferServiceAccountNamePrefix("google_bigquery_data_transfer_config.query_config", random_suffix2),
},
{
ResourceName: "google_bigquery_data_transfer_config.query_config",
Expand All @@ -419,40 +417,19 @@ func testAccBigqueryDataTransferConfig_scheduledQuery_update_service_account(t *
})
}

// Retrieve transfer config ID and stores it in transferConfigID
func testAccCheckDataTransferConfigID(resourceName string, transferConfigID *string) resource.TestCheckFunc {
// Check if transfer config service account name starts with given prefix
func testAccCheckDataTransferServiceAccountNamePrefix(resourceName string, prefix string) resource.TestCheckFunc {
return func(s *terraform.State) error {

rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("Not found: %s", resourceName)
}

if rs.Primary.ID == "" {
return fmt.Errorf("Transfer config ID is not set")
if !strings.HasPrefix(rs.Primary.Attributes["service_account_name"], "bqwriter"+prefix) {
return fmt.Errorf("Transfer config service account not updated")
}

*transferConfigID = rs.Primary.ID
return nil
}
}

// Check if transfer config ID matches the one stored in transferConfigID
func testAccCheckDataTransferConfigIDChange(resourceName string, transferConfigID *string) resource.TestCheckFunc {
return func(s *terraform.State) error {

rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("Not found: %s", resourceName)
}

if rs.Primary.ID == "" {
return fmt.Errorf("Transfer config ID is not set")
}

if *transferConfigID != rs.Primary.ID {
return fmt.Errorf("Transfer config was recreated after changing service account")
}
return nil
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"fmt"
"log"
"reflect"
"strings"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
Expand Down Expand Up @@ -578,55 +577,12 @@ func resourceBigqueryDataTransferConfigUpdate(d *schema.ResourceData, meta inter
return err
}

url, err := tpgresource.ReplaceVars(d, config, "{{BigqueryDataTransferBasePath}}{{name}}?serviceAccountName={{service_account_name}}")
url, err := tpgresource.ReplaceVars(d, config, "{{BigqueryDataTransferBasePath}}{{name}}?serviceAccountName={{service_account_name}}&updateMask=serviceAccountName,displayName,destinationDatasetId,schedule,scheduleOptions,emailPreferences,notificationPubsubTopic,dataRefreshWindowDays,disabled,params")
if err != nil {
return err
}

log.Printf("[DEBUG] Updating Config %q: %#v", d.Id(), obj)
updateMask := []string{}

if d.HasChange("display_name") {
updateMask = append(updateMask, "displayName")
}

if d.HasChange("destination_dataset_id") {
updateMask = append(updateMask, "destinationDatasetId")
}

if d.HasChange("schedule") {
updateMask = append(updateMask, "schedule")
}

if d.HasChange("schedule_options") {
updateMask = append(updateMask, "scheduleOptions")
}

if d.HasChange("email_preferences") {
updateMask = append(updateMask, "emailPreferences")
}

if d.HasChange("notification_pubsub_topic") {
updateMask = append(updateMask, "notificationPubsubTopic")
}

if d.HasChange("data_refresh_window_days") {
updateMask = append(updateMask, "dataRefreshWindowDays")
}

if d.HasChange("disabled") {
updateMask = append(updateMask, "disabled")
}

if d.HasChange("params") {
updateMask = append(updateMask, "params")
}
// updateMask is a URL parameter but not present in the schema, so ReplaceVars
// won't set it
url, err = transport_tpg.AddQueryParams(url, map[string]string{"updateMask": strings.Join(updateMask, ",")})
if err != nil {
return err
}

// err == nil indicates that the billing_project value was found
if bp, err := tpgresource.GetBillingProject(d, config); err == nil {
Expand Down

0 comments on commit 26b073d

Please sign in to comment.