Skip to content

Commit 50a2be6

Browse files
feat: implemented google_bigquery_datasets data source (#14066) (#23059)
[upstream:133b02b1e0d4be02b8bfb78adb99b769757307fa] Signed-off-by: Modular Magician <magic-modules@google.com>
1 parent 1aec241 commit 50a2be6

File tree

5 files changed

+356
-0
lines changed

5 files changed

+356
-0
lines changed

.changelog/14066.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_datasets`
3+
```

google/provider/provider_mmv1_resources.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ var handwrittenDatasources = map[string]*schema.Resource{
201201
"google_bigquery_table": bigquery.DataSourceGoogleBigQueryTable(),
202202
"google_bigquery_tables": bigquery.DataSourceGoogleBigQueryTables(),
203203
"google_bigquery_dataset": bigquery.DataSourceGoogleBigqueryDataset(),
204+
"google_bigquery_datasets": bigquery.DataSourceGoogleBigqueryDatasets(),
204205
"google_bigquery_default_service_account": bigquery.DataSourceGoogleBigqueryDefaultServiceAccount(),
205206
"google_certificate_manager_certificates": certificatemanager.DataSourceGoogleCertificateManagerCertificates(),
206207
"google_certificate_manager_certificate_map": certificatemanager.DataSourceGoogleCertificateManagerCertificateMap(),
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
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_datasets.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 DataSourceGoogleBigqueryDatasets() *schema.Resource {
28+
dsSchema := map[string]*schema.Schema{
29+
"project": {
30+
Type: schema.TypeString,
31+
Optional: true,
32+
Description: "The ID of the project in which the datasets are located. If it is not provided, the provider project is used.",
33+
},
34+
"datasets": {
35+
Type: schema.TypeList,
36+
Computed: true,
37+
Elem: &schema.Resource{
38+
Schema: map[string]*schema.Schema{
39+
"labels": {
40+
Type: schema.TypeMap,
41+
Computed: true,
42+
Elem: &schema.Schema{
43+
Type: schema.TypeString,
44+
},
45+
Description: "The labels associated with this dataset. You can use these to organize and group your datasets.",
46+
},
47+
"friendly_name": {
48+
Type: schema.TypeString,
49+
Computed: true,
50+
Description: "A user-friendly name for the dataset.",
51+
},
52+
"dataset_id": {
53+
Type: schema.TypeString,
54+
Computed: true,
55+
Description: "A unique ID for this dataset, without the project name. The ID must contain only letters (a-z, A-Z), numbers (0-9), or underscores (_).",
56+
},
57+
"location": {
58+
Type: schema.TypeString,
59+
Computed: true,
60+
Description: "The geographic location where the dataset resides.",
61+
},
62+
},
63+
},
64+
},
65+
}
66+
67+
return &schema.Resource{
68+
Read: DataSourceGoogleBigQueryDatasetsRead,
69+
Schema: dsSchema,
70+
}
71+
}
72+
73+
func DataSourceGoogleBigQueryDatasetsRead(d *schema.ResourceData, meta interface{}) error {
74+
config := meta.(*transport_tpg.Config)
75+
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
76+
if err != nil {
77+
return err
78+
}
79+
80+
project, err := tpgresource.GetProject(d, config)
81+
82+
if err != nil {
83+
return fmt.Errorf("Error fetching project: %s", err)
84+
}
85+
86+
params := make(map[string]string)
87+
datasets := make([]map[string]interface{}, 0)
88+
89+
for {
90+
url, err := tpgresource.ReplaceVars(d, config, "{{BigQueryBasePath}}projects/{{project}}/datasets")
91+
if err != nil {
92+
return err
93+
}
94+
95+
url, err = transport_tpg.AddQueryParams(url, params)
96+
if err != nil {
97+
return err
98+
}
99+
100+
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
101+
Config: config,
102+
Method: "GET",
103+
RawURL: url,
104+
UserAgent: userAgent,
105+
})
106+
if err != nil {
107+
return fmt.Errorf("Error retrieving datasets: %s", err)
108+
}
109+
110+
pageDatasets := flattenDataSourceGoogleBigQueryDatasetsList(res["datasets"])
111+
datasets = append(datasets, pageDatasets...)
112+
113+
pToken, ok := res["nextPageToken"]
114+
if ok && pToken != nil && pToken.(string) != "" {
115+
params["pageToken"] = pToken.(string)
116+
} else {
117+
break
118+
}
119+
}
120+
121+
if err := d.Set("datasets", datasets); err != nil {
122+
return fmt.Errorf("Error retrieving datasets: %s", err)
123+
}
124+
125+
id := fmt.Sprintf("projects/%s/datasets", project)
126+
d.SetId(id)
127+
128+
return nil
129+
}
130+
131+
func flattenDataSourceGoogleBigQueryDatasetsList(res interface{}) []map[string]interface{} {
132+
if res == nil {
133+
return make([]map[string]interface{}, 0)
134+
}
135+
136+
ls := res.([]interface{})
137+
138+
datasets := make([]map[string]interface{}, 0, len(ls))
139+
140+
for _, raw := range ls {
141+
output := raw.(map[string]interface{})
142+
143+
var mLabels map[string]interface{}
144+
var mDatasetID string
145+
var mFriendlyName string
146+
var mLocation string
147+
148+
if oLabels, ok := output["labels"].(map[string]interface{}); ok {
149+
mLabels = oLabels
150+
} else {
151+
mLabels = make(map[string]interface{}) // Initialize as an empty map if labels are missing
152+
}
153+
154+
if oFriendlyName, ok := output["friendlyName"].(string); ok {
155+
mFriendlyName = oFriendlyName
156+
}
157+
158+
if oDatasetReference, ok := output["datasetReference"].(map[string]interface{}); ok {
159+
if datasetID, ok := oDatasetReference["datasetId"].(string); ok {
160+
mDatasetID = datasetID
161+
}
162+
}
163+
164+
if oLocation, ok := output["location"].(string); ok {
165+
mLocation = oLocation
166+
}
167+
168+
datasets = append(datasets, map[string]interface{}{
169+
"labels": mLabels,
170+
"friendly_name": mFriendlyName,
171+
"dataset_id": mDatasetID,
172+
"location": mLocation,
173+
})
174+
}
175+
176+
return datasets
177+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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_datasets_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+
"fmt"
21+
"regexp"
22+
"testing"
23+
24+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
25+
"github.com/hashicorp/terraform-provider-google/google/acctest"
26+
"github.com/hashicorp/terraform-provider-google/google/envvar"
27+
)
28+
29+
func TestAccDataSourceGoogleBigqueryDatasets_basic(t *testing.T) {
30+
t.Parallel()
31+
32+
randomSuffix := acctest.RandString(t, 10)
33+
projectID := envvar.GetTestProjectFromEnv()
34+
35+
expectedDatasetFoo := map[string]string{
36+
"dataset_id": fmt.Sprintf("tf_test_foo_%s", randomSuffix),
37+
"friendly_name": "Foo",
38+
"location": "US",
39+
"labels.%": "1",
40+
"labels.goog-terraform-provisioned": "true",
41+
}
42+
43+
expectedDatasetBar := map[string]string{
44+
"dataset_id": fmt.Sprintf("tf_test_bar_%s", randomSuffix),
45+
"friendly_name": "bar",
46+
"location": "EU",
47+
"labels.%": "1",
48+
"labels.goog-terraform-provisioned": "true",
49+
}
50+
51+
nonExpectedDataset := map[string]string{
52+
"dataset_id": "non_existent_dataset",
53+
"friendly_name": "I do not exist, and should throw an error",
54+
"location": "NON_EXIST",
55+
"labels.%": "8",
56+
"labels.goog-terraform-provisioned": "Nah",
57+
}
58+
59+
context := map[string]interface{}{
60+
"random_suffix": randomSuffix,
61+
"project_id": projectID,
62+
}
63+
64+
acctest.VcrTest(t, resource.TestCase{
65+
PreCheck: func() { acctest.AccTestPreCheck(t) },
66+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
67+
Steps: []resource.TestStep{
68+
{
69+
Config: testAccDataSourceGoogleBigqueryDatasets_basic(context),
70+
Check: resource.ComposeTestCheckFunc(
71+
resource.TestCheckTypeSetElemNestedAttrs("data.google_bigquery_datasets.example", "datasets.*", expectedDatasetFoo),
72+
resource.TestCheckTypeSetElemNestedAttrs("data.google_bigquery_datasets.example", "datasets.*", expectedDatasetBar),
73+
// this check is intended to throw an error, see ExpectError below
74+
resource.TestCheckTypeSetElemNestedAttrs("data.google_bigquery_datasets.example", "datasets.*", nonExpectedDataset),
75+
),
76+
ExpectError: regexp.MustCompile(".*no TypeSet element \"datasets.*\", with nested attrs.*non_existent_dataset.*I do not exist, and should throw an error.*"),
77+
},
78+
},
79+
})
80+
}
81+
82+
func testAccDataSourceGoogleBigqueryDatasets_basic(context map[string]interface{}) string {
83+
return acctest.Nprintf(`
84+
resource "google_bigquery_dataset" "foo" {
85+
dataset_id = "tf_test_foo_%{random_suffix}"
86+
friendly_name = "Foo"
87+
description = "This is a test description"
88+
location = "US"
89+
default_table_expiration_ms = 3600000
90+
91+
access {
92+
role = "OWNER"
93+
user_by_email = google_service_account.bqowner.email
94+
}
95+
}
96+
97+
resource "google_bigquery_dataset" "bar" {
98+
dataset_id = "tf_test_bar_%{random_suffix}"
99+
friendly_name = "bar"
100+
description = "This is a test description"
101+
location = "EU"
102+
default_table_expiration_ms = 3600000
103+
104+
access {
105+
role = "OWNER"
106+
user_by_email = google_service_account.bqowner.email
107+
}
108+
}
109+
110+
resource "google_service_account" "bqowner" {
111+
account_id = "tf-test-%{random_suffix}"
112+
}
113+
114+
data "google_bigquery_datasets" "example" {
115+
project = "%{project_id}"
116+
depends_on = [
117+
google_bigquery_dataset.foo,
118+
google_bigquery_dataset.bar,
119+
]
120+
}
121+
`, context)
122+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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_datasets.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 list of datasets in a project.
19+
---
20+
21+
# `google_bigquery_datasets`
22+
23+
Get a list of datasets in a GCP project. 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/datasets/list).
26+
27+
## Example Usage
28+
29+
```hcl
30+
data "google_bigquery_datasets" "datasets" {
31+
project = "my-project"
32+
}
33+
```
34+
35+
## Argument Reference
36+
37+
The following arguments are supported:
38+
39+
* `project` - (Optional) The ID of the project in which the resource belongs.
40+
If it is not provided, the provider project is used.
41+
42+
## Attributes Reference
43+
44+
The following attributes are exported:
45+
46+
* `datasets` - A list of all retrieved BigQuery datasets. Structure is [defined below](#nested_datasets).
47+
48+
<a name="nested_datasets"></a>The `datasets` block supports:
49+
50+
* `labels` - User-provided dataset labels, in key/value pairs.
51+
* `friendly_name` - The friendly name of the dataset.
52+
* `dataset_id` - The id of the dataset.
53+
* `location` - The geographic location of the dataset.

0 commit comments

Comments
 (0)