Skip to content

Commit

Permalink
Add location to bigquery job self_link (#4043) (#7418)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Oct 2, 2020
1 parent 8163b44 commit 64779ae
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .changelog/4043.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
bigquery: fixed an issue in `google_bigquery_job` where non-US locations could not be read
```
7 changes: 5 additions & 2 deletions google/resource_big_query_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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<project>[^/]+)/jobs/(?P<job_id>[^/]+)/location/(?P<location>[^/]+)",
"projects/(?P<project>[^/]+)/jobs/(?P<job_id>[^/]+)",
"(?P<project>[^/]+)/(?P<job_id>[^/]+)/(?P<location>[^/]+)",
"(?P<job_id>[^/]+)/(?P<location>[^/]+)",
"(?P<project>[^/]+)/(?P<job_id>[^/]+)",
"(?P<job_id>[^/]+)",
}, d, config); err != nil {
Expand Down
83 changes: 83 additions & 0 deletions google/resource_bigquery_job_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
3 changes: 3 additions & 0 deletions website/docs/r/bigquery_job.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
```
Expand Down

0 comments on commit 64779ae

Please sign in to comment.