Skip to content

Commit 30ba6a7

Browse files
bindermuehleBBBmau
authored andcommitted
improve big query job error messages (GoogleCloudPlatform#12157)
1 parent 018a85a commit 30ba6a7

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

mmv1/templates/terraform/custom_expand/bigquery_table_ref.go.tmpl

+14
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,19 @@ func expand{{$.GetPrefix}}{{$.TitlelizeProperty}}(v interface{}, d tpgresource.T
4040
transformed["tableId"] = parts[3]
4141
}
4242

43+
configError := "Invalid BigQuery job destination_table configuration. You must either:\n" +
44+
"1. Set all of project_id, dataset_id, and table_id separately, or\n" +
45+
"2. Provide table_id in the form '{{`projects/{{project}}/datasets/{{dataset_id}}/tables/{{table_id}}`}}'"
46+
47+
// Validate required fields
48+
if projectId, ok := transformed["projectId"]; !ok || projectId == nil ||
49+
reflect.ValueOf(projectId).IsZero() {
50+
return nil, fmt.Errorf("%s\nMissing or empty projectId", configError)
51+
}
52+
53+
if datasetId, ok := transformed["datasetId"]; !ok || datasetId == nil ||
54+
reflect.ValueOf(datasetId).IsZero() {
55+
return nil, fmt.Errorf("%s\nMissing or empty datasetId", configError)
56+
}
4357
return transformed, nil
4458
}

mmv1/third_party/terraform/services/bigquery/resource_bigquery_job_test.go

+62
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package bigquery_test
22

33
import (
44
"fmt"
5+
"regexp"
56
"testing"
67

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

0 commit comments

Comments
 (0)