Skip to content

Commit 211dfe8

Browse files
improve big query job error messages (#12157) (#8542)
[upstream:9323d5fb8acdf32fb92f08e21af26cb0e2d1feb2] Signed-off-by: Modular Magician <[email protected]>
1 parent 3653493 commit 211dfe8

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed

.changelog/12157.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
bigquery: added descriptive validation errors for missing required fields in `google_bigquery_job` destination table configuration
3+
```

google-beta/services/bigquery/resource_bigquery_job.go

+56
Original file line numberDiff line numberDiff line change
@@ -2331,6 +2331,20 @@ func expandBigQueryJobConfigurationQueryDestinationTable(v interface{}, d tpgres
23312331
transformed["tableId"] = parts[3]
23322332
}
23332333

2334+
configError := "Invalid BigQuery job destination_table configuration. You must either:\n" +
2335+
"1. Set all of project_id, dataset_id, and table_id separately, or\n" +
2336+
"2. Provide table_id in the form 'projects/{{project}}/datasets/{{dataset_id}}/tables/{{table_id}}'"
2337+
2338+
// Validate required fields
2339+
if projectId, ok := transformed["projectId"]; !ok || projectId == nil ||
2340+
reflect.ValueOf(projectId).IsZero() {
2341+
return nil, fmt.Errorf("%s\nMissing or empty projectId", configError)
2342+
}
2343+
2344+
if datasetId, ok := transformed["datasetId"]; !ok || datasetId == nil ||
2345+
reflect.ValueOf(datasetId).IsZero() {
2346+
return nil, fmt.Errorf("%s\nMissing or empty datasetId", configError)
2347+
}
23342348
return transformed, nil
23352349
}
23362350

@@ -2714,6 +2728,20 @@ func expandBigQueryJobConfigurationLoadDestinationTable(v interface{}, d tpgreso
27142728
transformed["tableId"] = parts[3]
27152729
}
27162730

2731+
configError := "Invalid BigQuery job destination_table configuration. You must either:\n" +
2732+
"1. Set all of project_id, dataset_id, and table_id separately, or\n" +
2733+
"2. Provide table_id in the form 'projects/{{project}}/datasets/{{dataset_id}}/tables/{{table_id}}'"
2734+
2735+
// Validate required fields
2736+
if projectId, ok := transformed["projectId"]; !ok || projectId == nil ||
2737+
reflect.ValueOf(projectId).IsZero() {
2738+
return nil, fmt.Errorf("%s\nMissing or empty projectId", configError)
2739+
}
2740+
2741+
if datasetId, ok := transformed["datasetId"]; !ok || datasetId == nil ||
2742+
reflect.ValueOf(datasetId).IsZero() {
2743+
return nil, fmt.Errorf("%s\nMissing or empty datasetId", configError)
2744+
}
27172745
return transformed, nil
27182746
}
27192747

@@ -3008,6 +3036,20 @@ func expandBigQueryJobConfigurationCopyDestinationTable(v interface{}, d tpgreso
30083036
transformed["tableId"] = parts[3]
30093037
}
30103038

3039+
configError := "Invalid BigQuery job destination_table configuration. You must either:\n" +
3040+
"1. Set all of project_id, dataset_id, and table_id separately, or\n" +
3041+
"2. Provide table_id in the form 'projects/{{project}}/datasets/{{dataset_id}}/tables/{{table_id}}'"
3042+
3043+
// Validate required fields
3044+
if projectId, ok := transformed["projectId"]; !ok || projectId == nil ||
3045+
reflect.ValueOf(projectId).IsZero() {
3046+
return nil, fmt.Errorf("%s\nMissing or empty projectId", configError)
3047+
}
3048+
3049+
if datasetId, ok := transformed["datasetId"]; !ok || datasetId == nil ||
3050+
reflect.ValueOf(datasetId).IsZero() {
3051+
return nil, fmt.Errorf("%s\nMissing or empty datasetId", configError)
3052+
}
30113053
return transformed, nil
30123054
}
30133055

@@ -3175,6 +3217,20 @@ func expandBigQueryJobConfigurationExtractSourceTable(v interface{}, d tpgresour
31753217
transformed["tableId"] = parts[3]
31763218
}
31773219

3220+
configError := "Invalid BigQuery job destination_table configuration. You must either:\n" +
3221+
"1. Set all of project_id, dataset_id, and table_id separately, or\n" +
3222+
"2. Provide table_id in the form 'projects/{{project}}/datasets/{{dataset_id}}/tables/{{table_id}}'"
3223+
3224+
// Validate required fields
3225+
if projectId, ok := transformed["projectId"]; !ok || projectId == nil ||
3226+
reflect.ValueOf(projectId).IsZero() {
3227+
return nil, fmt.Errorf("%s\nMissing or empty projectId", configError)
3228+
}
3229+
3230+
if datasetId, ok := transformed["datasetId"]; !ok || datasetId == nil ||
3231+
reflect.ValueOf(datasetId).IsZero() {
3232+
return nil, fmt.Errorf("%s\nMissing or empty datasetId", configError)
3233+
}
31783234
return transformed, nil
31793235
}
31803236

google-beta/services/bigquery/resource_bigquery_job_test.go

+62
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package bigquery_test
44

55
import (
66
"fmt"
7+
"regexp"
78
"testing"
89

910
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
@@ -83,3 +84,64 @@ resource "google_bigquery_job" "job" {
8384
}
8485
`, context)
8586
}
87+
88+
func TestAccBigQueryJob_validationErrors(t *testing.T) {
89+
t.Parallel()
90+
context := map[string]interface{}{
91+
"random_suffix": acctest.RandString(t, 10),
92+
"project": envvar.GetTestProjectFromEnv(),
93+
}
94+
95+
acctest.VcrTest(t, resource.TestCase{
96+
PreCheck: func() { acctest.AccTestPreCheck(t) },
97+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
98+
Steps: []resource.TestStep{
99+
{
100+
Config: testAccBigQueryJob_missingProjectId(context),
101+
ExpectError: regexp.MustCompile(
102+
`(?s)Invalid BigQuery job destination_table configuration\. You must either:.*Missing or empty projectId`,
103+
),
104+
},
105+
{
106+
Config: testAccBigQueryJob_missingDatasetId(context),
107+
ExpectError: regexp.MustCompile(
108+
`(?s)Invalid BigQuery job destination_table configuration\. You must either:.*Missing or empty datasetId`,
109+
),
110+
},
111+
},
112+
})
113+
}
114+
115+
func testAccBigQueryJob_missingProjectId(context map[string]interface{}) string {
116+
return acctest.Nprintf(`
117+
resource "google_bigquery_job" "job" {
118+
job_id = "tf-test-job-%{random_suffix}"
119+
120+
query {
121+
query = "SELECT state FROM [lookerdata:cdc.project_tycho_reports]"
122+
destination_table {
123+
dataset_id = "example_dataset"
124+
table_id = "example_table"
125+
# project_id intentionally omitted
126+
}
127+
}
128+
}
129+
`, context)
130+
}
131+
132+
func testAccBigQueryJob_missingDatasetId(context map[string]interface{}) string {
133+
return acctest.Nprintf(`
134+
resource "google_bigquery_job" "job" {
135+
job_id = "tf-test-job-%{random_suffix}"
136+
137+
query {
138+
query = "SELECT state FROM [lookerdata:cdc.project_tycho_reports]"
139+
destination_table {
140+
project_id = "%{project}"
141+
table_id = "example_table"
142+
# dataset_id intentionally omitted
143+
}
144+
}
145+
}
146+
`, context)
147+
}

0 commit comments

Comments
 (0)