diff --git a/.changelog/4043.txt b/.changelog/4043.txt new file mode 100644 index 00000000000..6a7514de644 --- /dev/null +++ b/.changelog/4043.txt @@ -0,0 +1,3 @@ +```release-note:bug +bigquery: fixed an issue in `google_bigquery_job` where non-US locations could not be read +``` diff --git a/google/resource_big_query_job.go b/google/resource_big_query_job.go index 535141ea741..87ff297f89a 100644 --- a/google/resource_big_query_job.go +++ b/google/resource_big_query_job.go @@ -928,7 +928,7 @@ func resourceBigQueryJobPollRead(d *schema.ResourceData, meta interface{}) PollR return func() (map[string]interface{}, error) { config := meta.(*Config) - url, err := replaceVars(d, config, "{{BigQueryBasePath}}projects/{{project}}/jobs/{{job_id}}") + url, err := replaceVars(d, config, "{{BigQueryBasePath}}projects/{{project}}/jobs/{{job_id}}?location={{location}}") if err != nil { return nil, err } @@ -966,7 +966,7 @@ func resourceBigQueryJobRead(d *schema.ResourceData, meta interface{}) error { return err } - url, err := replaceVars(d, config, "{{BigQueryBasePath}}projects/{{project}}/jobs/{{job_id}}") + url, err := replaceVars(d, config, "{{BigQueryBasePath}}projects/{{project}}/jobs/{{job_id}}?location={{location}}") if err != nil { return err } @@ -1042,7 +1042,10 @@ func resourceBigQueryJobDelete(d *schema.ResourceData, meta interface{}) error { func resourceBigQueryJobImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { config := meta.(*Config) if err := parseImportId([]string{ + "projects/(?P[^/]+)/jobs/(?P[^/]+)/location/(?P[^/]+)", "projects/(?P[^/]+)/jobs/(?P[^/]+)", + "(?P[^/]+)/(?P[^/]+)/(?P[^/]+)", + "(?P[^/]+)/(?P[^/]+)", "(?P[^/]+)/(?P[^/]+)", "(?P[^/]+)", }, d, config); err != nil { diff --git a/google/resource_bigquery_job_test.go b/google/resource_bigquery_job_test.go new file mode 100644 index 00000000000..1ad5ac45607 --- /dev/null +++ b/google/resource_bigquery_job_test.go @@ -0,0 +1,83 @@ +package google + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func TestAccBigQueryJob_withLocation(t *testing.T) { + t.Parallel() + + context := map[string]interface{}{ + "random_suffix": randString(t, 10), + "location": "asia-northeast1", + } + + // Need to construct the import ID manually since the state ID will not contain the location + importID := fmt.Sprintf("projects/%s/jobs/tf_test_job_query%s/location/%s", getTestProjectFromEnv(), context["random_suffix"], context["location"]) + + vcrTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + ExternalProviders: map[string]resource.ExternalProvider{ + "random": {}, + }, + Steps: []resource.TestStep{ + { + Config: testAccBigQueryJob_withLocation(context), + }, + { + ResourceName: "google_bigquery_job.job", + ImportStateId: importID, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"etag"}, + }, + }, + }) +} + +func testAccBigQueryJob_withLocation(context map[string]interface{}) string { + return Nprintf(` +resource "google_bigquery_table" "foo" { + dataset_id = google_bigquery_dataset.bar.dataset_id + table_id = "tf_test_job_query%{random_suffix}_table" +} + +resource "google_bigquery_dataset" "bar" { + dataset_id = "tf_test_job_query%{random_suffix}_dataset" + friendly_name = "test" + description = "This is a test description" + location = "%{location}" +} + +resource "google_bigquery_job" "job" { + job_id = "tf_test_job_query%{random_suffix}" + + labels = { + "example-label" ="example-value" + } + + query { + query = "SELECT state FROM [lookerdata:cdc.project_tycho_reports]" + + destination_table { + project_id = google_bigquery_table.foo.project + dataset_id = google_bigquery_table.foo.dataset_id + table_id = google_bigquery_table.foo.table_id + } + + allow_large_results = true + flatten_results = true + + script_options { + key_result_statement = "LAST" + } + } + + location = "%{location}" +} +`, context) +} diff --git a/website/docs/r/bigquery_job.html.markdown b/website/docs/r/bigquery_job.html.markdown index 9ecbef7acf0..d528063ddd4 100644 --- a/website/docs/r/bigquery_job.html.markdown +++ b/website/docs/r/bigquery_job.html.markdown @@ -929,7 +929,10 @@ This resource provides the following Job can be imported using any of these accepted formats: ``` +$ terraform import google_bigquery_job.default projects/{{project}}/jobs/{{job_id}}/location/{{location}} $ terraform import google_bigquery_job.default projects/{{project}}/jobs/{{job_id}} +$ terraform import google_bigquery_job.default {{project}}/{{job_id}}/{{location}} +$ terraform import google_bigquery_job.default {{job_id}}/{{location}} $ terraform import google_bigquery_job.default {{project}}/{{job_id}} $ terraform import google_bigquery_job.default {{job_id}} ```