Skip to content

Commit 487d8e9

Browse files
Add status to bigquery_job (#4455) (#8377)
* Add `status` field to `google_bigquery_job` fixes #8357 * fixup! ignore `.status.0.state` in tests Signed-off-by: Modular Magician <magic-modules@google.com>
1 parent e60b797 commit 487d8e9

File tree

5 files changed

+209
-9
lines changed

5 files changed

+209
-9
lines changed

.changelog/4455.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
bigquery: added `status` field to `google_bigquery_job`
3+
```

google/resource_big_query_job.go

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,70 @@ Creation, truncation and append actions occur as one atomic update upon job comp
842842
Default: "US",
843843
},
844844

845+
"status": {
846+
Type: schema.TypeList,
847+
Computed: true,
848+
Description: `The status of this job. Examine this value when polling an asynchronous job to see if the job is complete.`,
849+
Elem: &schema.Resource{
850+
Schema: map[string]*schema.Schema{
851+
"error_result": {
852+
Type: schema.TypeList,
853+
Computed: true,
854+
Description: `Final error result of the job. If present, indicates that the job has completed and was unsuccessful.`,
855+
Elem: &schema.Resource{
856+
Schema: map[string]*schema.Schema{
857+
"location": {
858+
Type: schema.TypeString,
859+
Optional: true,
860+
Description: `Specifies where the error occurred, if present.`,
861+
},
862+
"message": {
863+
Type: schema.TypeString,
864+
Optional: true,
865+
Description: `A human-readable description of the error.`,
866+
},
867+
"reason": {
868+
Type: schema.TypeString,
869+
Optional: true,
870+
Description: `A short error code that summarizes the error.`,
871+
},
872+
},
873+
},
874+
},
875+
"errors": {
876+
Type: schema.TypeList,
877+
Computed: true,
878+
Description: `The first errors encountered during the running of the job. The final message
879+
includes the number of errors that caused the process to stop. Errors here do
880+
not necessarily mean that the job has not completed or was unsuccessful.`,
881+
Elem: &schema.Resource{
882+
Schema: map[string]*schema.Schema{
883+
"location": {
884+
Type: schema.TypeString,
885+
Optional: true,
886+
Description: `Specifies where the error occurred, if present.`,
887+
},
888+
"message": {
889+
Type: schema.TypeString,
890+
Optional: true,
891+
Description: `A human-readable description of the error.`,
892+
},
893+
"reason": {
894+
Type: schema.TypeString,
895+
Optional: true,
896+
Description: `A short error code that summarizes the error.`,
897+
},
898+
},
899+
},
900+
},
901+
"state": {
902+
Type: schema.TypeString,
903+
Computed: true,
904+
Description: `Running state of the job. Valid states include 'PENDING', 'RUNNING', and 'DONE'.`,
905+
},
906+
},
907+
},
908+
},
845909
"user_email": {
846910
Type: schema.TypeString,
847911
Computed: true,
@@ -1027,6 +1091,9 @@ func resourceBigQueryJobRead(d *schema.ResourceData, meta interface{}) error {
10271091
}
10281092
}
10291093
}
1094+
if err := d.Set("status", flattenBigQueryJobStatus(res["status"], d, config)); err != nil {
1095+
return fmt.Errorf("Error reading Job: %s", err)
1096+
}
10301097

10311098
return nil
10321099
}
@@ -1744,6 +1811,88 @@ func flattenBigQueryJobJobReferenceLocation(v interface{}, d *schema.ResourceDat
17441811
return v
17451812
}
17461813

1814+
func flattenBigQueryJobStatus(v interface{}, d *schema.ResourceData, config *Config) interface{} {
1815+
if v == nil {
1816+
return nil
1817+
}
1818+
original := v.(map[string]interface{})
1819+
if len(original) == 0 {
1820+
return nil
1821+
}
1822+
transformed := make(map[string]interface{})
1823+
transformed["error_result"] =
1824+
flattenBigQueryJobStatusErrorResult(original["errorResult"], d, config)
1825+
transformed["errors"] =
1826+
flattenBigQueryJobStatusErrors(original["errors"], d, config)
1827+
transformed["state"] =
1828+
flattenBigQueryJobStatusState(original["state"], d, config)
1829+
return []interface{}{transformed}
1830+
}
1831+
func flattenBigQueryJobStatusErrorResult(v interface{}, d *schema.ResourceData, config *Config) interface{} {
1832+
if v == nil {
1833+
return nil
1834+
}
1835+
original := v.(map[string]interface{})
1836+
if len(original) == 0 {
1837+
return nil
1838+
}
1839+
transformed := make(map[string]interface{})
1840+
transformed["reason"] =
1841+
flattenBigQueryJobStatusErrorResultReason(original["reason"], d, config)
1842+
transformed["location"] =
1843+
flattenBigQueryJobStatusErrorResultLocation(original["location"], d, config)
1844+
transformed["message"] =
1845+
flattenBigQueryJobStatusErrorResultMessage(original["message"], d, config)
1846+
return []interface{}{transformed}
1847+
}
1848+
func flattenBigQueryJobStatusErrorResultReason(v interface{}, d *schema.ResourceData, config *Config) interface{} {
1849+
return v
1850+
}
1851+
1852+
func flattenBigQueryJobStatusErrorResultLocation(v interface{}, d *schema.ResourceData, config *Config) interface{} {
1853+
return v
1854+
}
1855+
1856+
func flattenBigQueryJobStatusErrorResultMessage(v interface{}, d *schema.ResourceData, config *Config) interface{} {
1857+
return v
1858+
}
1859+
1860+
func flattenBigQueryJobStatusErrors(v interface{}, d *schema.ResourceData, config *Config) interface{} {
1861+
if v == nil {
1862+
return v
1863+
}
1864+
l := v.([]interface{})
1865+
transformed := make([]interface{}, 0, len(l))
1866+
for _, raw := range l {
1867+
original := raw.(map[string]interface{})
1868+
if len(original) < 1 {
1869+
// Do not include empty json objects coming back from the api
1870+
continue
1871+
}
1872+
transformed = append(transformed, map[string]interface{}{
1873+
"reason": flattenBigQueryJobStatusErrorsReason(original["reason"], d, config),
1874+
"location": flattenBigQueryJobStatusErrorsLocation(original["location"], d, config),
1875+
"message": flattenBigQueryJobStatusErrorsMessage(original["message"], d, config),
1876+
})
1877+
}
1878+
return transformed
1879+
}
1880+
func flattenBigQueryJobStatusErrorsReason(v interface{}, d *schema.ResourceData, config *Config) interface{} {
1881+
return v
1882+
}
1883+
1884+
func flattenBigQueryJobStatusErrorsLocation(v interface{}, d *schema.ResourceData, config *Config) interface{} {
1885+
return v
1886+
}
1887+
1888+
func flattenBigQueryJobStatusErrorsMessage(v interface{}, d *schema.ResourceData, config *Config) interface{} {
1889+
return v
1890+
}
1891+
1892+
func flattenBigQueryJobStatusState(v interface{}, d *schema.ResourceData, config *Config) interface{} {
1893+
return v
1894+
}
1895+
17471896
func expandBigQueryJobConfiguration(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
17481897
transformed := make(map[string]interface{})
17491898
transformedJobType, err := expandBigQueryJobConfigurationJobType(d.Get("job_type"), d, config)

google/resource_big_query_job_generated_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func TestAccBigQueryJob_bigqueryJobQueryExample(t *testing.T) {
4141
ResourceName: "google_bigquery_job.job",
4242
ImportState: true,
4343
ImportStateVerify: true,
44-
ImportStateVerifyIgnore: []string{"etag"},
44+
ImportStateVerifyIgnore: []string{"etag", "status.0.state"},
4545
},
4646
},
4747
})
@@ -109,7 +109,7 @@ func TestAccBigQueryJob_bigqueryJobQueryTableReferenceExample(t *testing.T) {
109109
ResourceName: "google_bigquery_job.job",
110110
ImportState: true,
111111
ImportStateVerify: true,
112-
ImportStateVerifyIgnore: []string{"etag", "query.0.default_dataset.0.dataset_id", "query.0.destination_table.0.table_id"},
112+
ImportStateVerifyIgnore: []string{"etag", "query.0.default_dataset.0.dataset_id", "query.0.destination_table.0.table_id", "status.0.state"},
113113
},
114114
},
115115
})
@@ -179,7 +179,7 @@ func TestAccBigQueryJob_bigqueryJobLoadExample(t *testing.T) {
179179
ResourceName: "google_bigquery_job.job",
180180
ImportState: true,
181181
ImportStateVerify: true,
182-
ImportStateVerifyIgnore: []string{"etag"},
182+
ImportStateVerifyIgnore: []string{"etag", "status.0.state"},
183183
},
184184
},
185185
})
@@ -248,7 +248,7 @@ func TestAccBigQueryJob_bigqueryJobLoadTableReferenceExample(t *testing.T) {
248248
ResourceName: "google_bigquery_job.job",
249249
ImportState: true,
250250
ImportStateVerify: true,
251-
ImportStateVerifyIgnore: []string{"etag", "load.0.destination_table.0.table_id"},
251+
ImportStateVerifyIgnore: []string{"etag", "load.0.destination_table.0.table_id", "status.0.state"},
252252
},
253253
},
254254
})
@@ -316,7 +316,7 @@ func TestAccBigQueryJob_bigqueryJobCopyExample(t *testing.T) {
316316
ResourceName: "google_bigquery_job.job",
317317
ImportState: true,
318318
ImportStateVerify: true,
319-
ImportStateVerifyIgnore: []string{"etag"},
319+
ImportStateVerifyIgnore: []string{"etag", "status.0.state"},
320320
},
321321
},
322322
})
@@ -471,7 +471,7 @@ func TestAccBigQueryJob_bigqueryJobCopyTableReferenceExample(t *testing.T) {
471471
ResourceName: "google_bigquery_job.job",
472472
ImportState: true,
473473
ImportStateVerify: true,
474-
ImportStateVerifyIgnore: []string{"etag", "copy.0.destination_table.0.table_id", "copy.0.source_tables.0.table_id", "copy.0.source_tables.1.table_id"},
474+
ImportStateVerifyIgnore: []string{"etag", "copy.0.destination_table.0.table_id", "copy.0.source_tables.0.table_id", "copy.0.source_tables.1.table_id", "status.0.state"},
475475
},
476476
},
477477
})
@@ -619,7 +619,7 @@ func TestAccBigQueryJob_bigqueryJobExtractExample(t *testing.T) {
619619
ResourceName: "google_bigquery_job.job",
620620
ImportState: true,
621621
ImportStateVerify: true,
622-
ImportStateVerifyIgnore: []string{"etag"},
622+
ImportStateVerifyIgnore: []string{"etag", "status.0.state"},
623623
},
624624
},
625625
})
@@ -705,7 +705,7 @@ func TestAccBigQueryJob_bigqueryJobExtractTableReferenceExample(t *testing.T) {
705705
ResourceName: "google_bigquery_job.job",
706706
ImportState: true,
707707
ImportStateVerify: true,
708-
ImportStateVerifyIgnore: []string{"etag", "extract.0.source_table.0.table_id"},
708+
ImportStateVerifyIgnore: []string{"etag", "extract.0.source_table.0.table_id", "status.0.state"},
709709
},
710710
},
711711
})

google/resource_bigquery_job_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func TestAccBigQueryJob_withLocation(t *testing.T) {
3333
ImportStateId: importID,
3434
ImportState: true,
3535
ImportStateVerify: true,
36-
ImportStateVerifyIgnore: []string{"etag"},
36+
ImportStateVerifyIgnore: []string{"etag", "status.0.state"},
3737
},
3838
},
3939
})

website/docs/r/bigquery_job.html.markdown

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,54 @@ In addition to the arguments listed above, the following computed attributes are
920920
* `job_type` -
921921
The type of the job.
922922

923+
* `status` -
924+
The status of this job. Examine this value when polling an asynchronous job to see if the job is complete.
925+
Structure is documented below.
926+
927+
928+
The `status` block contains:
929+
930+
* `error_result` -
931+
Final error result of the job. If present, indicates that the job has completed and was unsuccessful.
932+
Structure is documented below.
933+
934+
* `errors` -
935+
The first errors encountered during the running of the job. The final message
936+
includes the number of errors that caused the process to stop. Errors here do
937+
not necessarily mean that the job has not completed or was unsuccessful.
938+
Structure is documented below.
939+
940+
* `state` -
941+
Running state of the job. Valid states include 'PENDING', 'RUNNING', and 'DONE'.
942+
943+
944+
The `error_result` block contains:
945+
946+
* `reason` -
947+
(Optional)
948+
A short error code that summarizes the error.
949+
950+
* `location` -
951+
(Optional)
952+
Specifies where the error occurred, if present.
953+
954+
* `message` -
955+
(Optional)
956+
A human-readable description of the error.
957+
958+
The `errors` block contains:
959+
960+
* `reason` -
961+
(Optional)
962+
A short error code that summarizes the error.
963+
964+
* `location` -
965+
(Optional)
966+
Specifies where the error occurred, if present.
967+
968+
* `message` -
969+
(Optional)
970+
A human-readable description of the error.
923971

924972
## Timeouts
925973

0 commit comments

Comments
 (0)