Skip to content

Commit ec8ddad

Browse files
feat: added google_bigquery_table data source (#14026) (#22973)
[upstream:2a6c3c839011441a4cddfbf4e1250d0372f67763] Signed-off-by: Modular Magician <[email protected]>
1 parent 2e0e73c commit ec8ddad

File tree

5 files changed

+327
-0
lines changed

5 files changed

+327
-0
lines changed

.changelog/14026.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-datasource
2+
`google_bigquery_table`
3+
```

google/provider/provider_mmv1_resources.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ var handwrittenDatasources = map[string]*schema.Resource{
198198
"google_beyondcorp_app_gateway": beyondcorp.DataSourceGoogleBeyondcorpAppGateway(),
199199
"google_beyondcorp_security_gateway": beyondcorp.DataSourceGoogleBeyondcorpSecurityGateway(),
200200
"google_billing_account": billing.DataSourceGoogleBillingAccount(),
201+
"google_bigquery_table": bigquery.DataSourceGoogleBigQueryTable(),
201202
"google_bigquery_tables": bigquery.DataSourceGoogleBigQueryTables(),
202203
"google_bigquery_dataset": bigquery.DataSourceGoogleBigqueryDataset(),
203204
"google_bigquery_default_service_account": bigquery.DataSourceGoogleBigqueryDefaultServiceAccount(),
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
// ----------------------------------------------------------------------------
4+
//
5+
// *** AUTO GENERATED CODE *** Type: Handwritten ***
6+
//
7+
// ----------------------------------------------------------------------------
8+
//
9+
// This code is generated by Magic Modules using the following:
10+
//
11+
// Source file: https://github.com/GoogleCloudPlatform/magic-modules/tree/main/mmv1/third_party/terraform/services/bigquery/data_source_google_bigquery_table.go
12+
//
13+
// DO NOT EDIT this file directly. Any changes made to this file will be
14+
// overwritten during the next generation cycle.
15+
//
16+
// ----------------------------------------------------------------------------
17+
package bigquery
18+
19+
import (
20+
"fmt"
21+
22+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
23+
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
24+
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
25+
)
26+
27+
func DataSourceGoogleBigQueryTable() *schema.Resource {
28+
dsSchema := tpgresource.DatasourceSchemaFromResourceSchema(ResourceBigQueryTable().Schema)
29+
30+
tpgresource.AddRequiredFieldsToSchema(dsSchema, "dataset_id")
31+
tpgresource.AddRequiredFieldsToSchema(dsSchema, "table_id")
32+
tpgresource.AddOptionalFieldsToSchema(dsSchema, "project")
33+
34+
return &schema.Resource{
35+
Read: dataSourceBigQueryTableRead,
36+
Schema: dsSchema,
37+
}
38+
}
39+
40+
func dataSourceBigQueryTableRead(d *schema.ResourceData, meta interface{}) error {
41+
config := meta.(*transport_tpg.Config)
42+
project, err := tpgresource.GetProject(d, config)
43+
if err != nil {
44+
return fmt.Errorf("Error fetching project: %s", err)
45+
}
46+
47+
datasetID := d.Get("dataset_id").(string)
48+
tableID := d.Get("table_id").(string)
49+
50+
id := fmt.Sprintf("projects/%s/datasets/%s/tables/%s", project, datasetID, tableID)
51+
d.SetId(id)
52+
53+
err = resourceBigQueryTableRead(d, meta)
54+
if err != nil {
55+
return fmt.Errorf("Error retrieving table: %s", err)
56+
}
57+
58+
if err := tpgresource.SetDataSourceLabels(d); err != nil {
59+
return err
60+
}
61+
62+
if d.Id() == "" {
63+
return fmt.Errorf("%s not found", id)
64+
}
65+
66+
return nil
67+
}
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
// ----------------------------------------------------------------------------
4+
//
5+
// *** AUTO GENERATED CODE *** Type: Handwritten ***
6+
//
7+
// ----------------------------------------------------------------------------
8+
//
9+
// This code is generated by Magic Modules using the following:
10+
//
11+
// Source file: https://github.com/GoogleCloudPlatform/magic-modules/tree/main/mmv1/third_party/terraform/services/bigquery/data_source_google_bigquery_table_test.go
12+
//
13+
// DO NOT EDIT this file directly. Any changes made to this file will be
14+
// overwritten during the next generation cycle.
15+
//
16+
// ----------------------------------------------------------------------------
17+
package bigquery_test
18+
19+
import (
20+
"encoding/json"
21+
"fmt"
22+
"regexp"
23+
"testing"
24+
25+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
26+
"github.com/hashicorp/terraform-provider-google/google/acctest"
27+
"github.com/hashicorp/terraform-provider-google/google/envvar"
28+
)
29+
30+
func TestAccDataSourceGoogleBigqueryTable_basic(t *testing.T) {
31+
t.Parallel()
32+
33+
context := map[string]interface{}{
34+
"random_suffix": acctest.RandString(t, 10),
35+
}
36+
37+
expectedID := fmt.Sprintf("projects/%s/datasets/%s/tables/%s", envvar.GetTestProjectFromEnv(), fmt.Sprintf("tf_test_ds_%s", context["random_suffix"]), fmt.Sprintf("tf_test_table_%s", context["random_suffix"]))
38+
39+
acctest.VcrTest(t, resource.TestCase{
40+
PreCheck: func() { acctest.AccTestPreCheck(t) },
41+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
42+
CheckDestroy: testAccCheckBigQueryTableDestroyProducer(t),
43+
Steps: []resource.TestStep{
44+
{
45+
Config: testAccDataSourceGoogleBigqueryTable_basic(context),
46+
Check: resource.ComposeTestCheckFunc(
47+
resource.TestCheckResourceAttr("data.google_bigquery_table.example", "table_id", fmt.Sprintf("tf_test_table_%s", context["random_suffix"])),
48+
resource.TestCheckResourceAttr("data.google_bigquery_table.example", "dataset_id", fmt.Sprintf("tf_test_ds_%s", context["random_suffix"])),
49+
resource.TestCheckResourceAttrSet("data.google_bigquery_table.example", "schema"),
50+
resource.TestCheckResourceAttr("data.google_bigquery_table.example", "id", expectedID),
51+
resource.TestCheckResourceAttrWith("data.google_bigquery_table.example", "schema", func(schema string) error {
52+
var parsedSchema []map[string]interface{}
53+
54+
if err := json.Unmarshal([]byte(schema), &parsedSchema); err != nil {
55+
return fmt.Errorf("failed to parse schema JSON: %w", err)
56+
}
57+
58+
if len(parsedSchema) > 0 {
59+
if parsedSchema[0]["name"] != "name" {
60+
return fmt.Errorf("expected fields[0].name to be 'name', got '%v'", parsedSchema[0]["name"])
61+
}
62+
if parsedSchema[0]["type"] != "STRING" {
63+
return fmt.Errorf("expected fields[0].type to be 'STRING', got '%v'", parsedSchema[0]["type"])
64+
}
65+
if parsedSchema[0]["mode"] != "NULLABLE" {
66+
return fmt.Errorf("expected fields[0].mode to be 'NULLABLE', got '%v'", parsedSchema[0]["mode"])
67+
}
68+
}
69+
70+
if len(parsedSchema) > 2 {
71+
if parsedSchema[2]["name"] != "address" {
72+
return fmt.Errorf("expected fields[2].name to be 'address', got '%v'", parsedSchema[2]["name"])
73+
}
74+
if subFields, ok := parsedSchema[2]["fields"].([]interface{}); ok && len(subFields) > 1 {
75+
subField := subFields[1].(map[string]interface{})
76+
if subField["name"] != "zip" {
77+
return fmt.Errorf("expected fields[2].fields[1].name to be 'zip', got '%v'", subField["name"])
78+
}
79+
}
80+
}
81+
82+
if len(parsedSchema) > 4 {
83+
if parsedSchema[4]["name"] != "policy_tag_test" {
84+
return fmt.Errorf("expected fields[4].name to be 'policy_tag_test', got '%v'", parsedSchema[4]["name"])
85+
}
86+
if policyTags, ok := parsedSchema[4]["policyTags"].(map[string]interface{}); ok {
87+
if names, ok := policyTags["names"].([]interface{}); ok && len(names) > 0 {
88+
if !regexp.MustCompile("^projects/[^/]+/locations/us-central1/taxonomies/[^/]+/policyTags/[^/]+$").MatchString(names[0].(string)) {
89+
return fmt.Errorf("policy tag does not match expected pattern")
90+
}
91+
}
92+
}
93+
}
94+
95+
return nil
96+
}),
97+
),
98+
},
99+
},
100+
})
101+
}
102+
103+
func testAccDataSourceGoogleBigqueryTable_basic(context map[string]interface{}) string {
104+
return acctest.Nprintf(`
105+
106+
resource "google_data_catalog_policy_tag" "test" {
107+
taxonomy = google_data_catalog_taxonomy.test.id
108+
display_name = "Low security"
109+
description = "A policy tag normally associated with low security items"
110+
}
111+
112+
resource "google_data_catalog_taxonomy" "test" {
113+
region = "us-central1"
114+
display_name = "taxonomy_%{random_suffix}"
115+
description = "A collection of policy tags"
116+
activated_policy_types = ["FINE_GRAINED_ACCESS_CONTROL"]
117+
}
118+
119+
resource "google_bigquery_dataset" "test" {
120+
dataset_id = "tf_test_ds_%{random_suffix}"
121+
friendly_name = "testing"
122+
description = "This is a test description"
123+
location = "us-central1"
124+
default_table_expiration_ms = 3600000
125+
}
126+
127+
resource "google_bigquery_table" "test" {
128+
dataset_id = google_bigquery_dataset.test.dataset_id
129+
table_id = "tf_test_table_%{random_suffix}"
130+
deletion_protection = false
131+
depends_on = [google_data_catalog_policy_tag.test]
132+
schema = <<EOF
133+
[
134+
{
135+
"name": "name",
136+
"type": "STRING",
137+
"mode": "NULLABLE"
138+
},
139+
{
140+
"name": "age",
141+
"type": "INTEGER",
142+
"mode": "NULLABLE",
143+
"description": "Age of the person"
144+
},
145+
{
146+
"name": "address",
147+
"type": "RECORD",
148+
"mode": "NULLABLE",
149+
"fields": [
150+
{
151+
"name": "street",
152+
"type": "STRING",
153+
"mode": "NULLABLE"
154+
},
155+
{
156+
"name": "zip",
157+
"type": "STRING",
158+
"mode": "NULLABLE"
159+
},
160+
{
161+
"name": "city",
162+
"type": "STRING",
163+
"mode": "NULLABLE"
164+
}
165+
],
166+
"description": "Address of the person"
167+
},
168+
{
169+
"name": "phone_numbers",
170+
"type": "RECORD",
171+
"mode": "REPEATED",
172+
"fields": [
173+
{
174+
"name": "type",
175+
"type": "STRING",
176+
"mode": "NULLABLE"
177+
},
178+
{
179+
"name": "number",
180+
"type": "STRING",
181+
"mode": "NULLABLE"
182+
}
183+
],
184+
"description": "Phone numbers of the person"
185+
},
186+
{
187+
"name": "policy_tag_test",
188+
"type": "STRING",
189+
"mode": "NULLABLE",
190+
"description": "A test field with policy tags",
191+
"policyTags": {
192+
"names": [
193+
"${google_data_catalog_policy_tag.test.id}"
194+
]
195+
}
196+
}
197+
]
198+
EOF
199+
}
200+
201+
data "google_bigquery_table" "example" {
202+
dataset_id = google_bigquery_table.test.dataset_id
203+
table_id = google_bigquery_table.test.table_id
204+
}
205+
`, context)
206+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
# ----------------------------------------------------------------------------
3+
#
4+
# *** AUTO GENERATED CODE *** Type: Handwritten ***
5+
#
6+
# ----------------------------------------------------------------------------
7+
#
8+
# This code is generated by Magic Modules using the following:
9+
#
10+
# Source file: https://github.com/GoogleCloudPlatform/magic-modules/tree/main/mmv1/third_party/terraform/website/docs/d/bigquery_table.html.markdown
11+
#
12+
# DO NOT EDIT this file directly. Any changes made to this file will be
13+
# overwritten during the next generation cycle.
14+
#
15+
# ----------------------------------------------------------------------------
16+
subcategory: "BigQuery"
17+
description: |-
18+
A datasource to retrieve a specific table in a dataset.
19+
---
20+
21+
# `google_bigquery_table`
22+
23+
Get a specific table in a BigQuery dataset. For more information see
24+
the [official documentation](https://cloud.google.com/bigquery/docs)
25+
and [API](https://cloud.google.com/bigquery/docs/reference/rest/v2/tables/get).
26+
27+
## Example Usage
28+
29+
```hcl
30+
data "google_bigquery_table" "table" {
31+
project = "my-project"
32+
dataset_id = "my-bq-dataset"
33+
table_id = "my-table"
34+
}
35+
```
36+
37+
## Argument Reference
38+
39+
The following arguments are supported:
40+
41+
* `dataset_id` - (Required) The dataset ID.
42+
43+
* `table_id` - (Required) The table ID.
44+
45+
* `project` - (Optional) The ID of the project in which the resource belongs.
46+
If it is not provided, the provider project is used.
47+
48+
## Attributes Reference
49+
50+
See [google_bigquery_table](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/bigquery_table#attributes-reference) resource for details of the available attributes.

0 commit comments

Comments
 (0)