Skip to content

Commit a6c67cc

Browse files
feat: added data source for bigquery dataset (#9352) (#16368)
[upstream:7d76e3b6f834bcea8f0edf086207af30da5e4665] Signed-off-by: Modular Magician <[email protected]>
1 parent 095dcdd commit a6c67cc

File tree

5 files changed

+136
-0
lines changed

5 files changed

+136
-0
lines changed

.changelog/9352.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-datasource
2+
google_bigquery_dataset
3+
```

google/provider/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,7 @@ func DatasourceMapWithErrors() (map[string]*schema.Resource, error) {
801801
"google_beyondcorp_app_connector": beyondcorp.DataSourceGoogleBeyondcorpAppConnector(),
802802
"google_beyondcorp_app_gateway": beyondcorp.DataSourceGoogleBeyondcorpAppGateway(),
803803
"google_billing_account": billing.DataSourceGoogleBillingAccount(),
804+
"google_bigquery_dataset": bigquery.DataSourceGoogleBigqueryDataset(),
804805
"google_bigquery_default_service_account": bigquery.DataSourceGoogleBigqueryDefaultServiceAccount(),
805806
"google_certificate_manager_certificate_map": certificatemanager.DataSourceGoogleCertificateManagerCertificateMap(),
806807
"google_cloudbuild_trigger": cloudbuild.DataSourceGoogleCloudBuildTrigger(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
// Copyright (c) HashiCorp, Inc.
4+
// SPDX-License-Identifier: MPL-2.0
5+
package bigquery
6+
7+
import (
8+
"fmt"
9+
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11+
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
12+
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
13+
)
14+
15+
func DataSourceGoogleBigqueryDataset() *schema.Resource {
16+
dsSchema := tpgresource.DatasourceSchemaFromResourceSchema(ResourceBigQueryDataset().Schema)
17+
tpgresource.AddRequiredFieldsToSchema(dsSchema, "dataset_id")
18+
tpgresource.AddOptionalFieldsToSchema(dsSchema, "project")
19+
20+
return &schema.Resource{
21+
Read: dataSourceGoogleBigqueryDatasetRead,
22+
Schema: dsSchema,
23+
}
24+
}
25+
26+
func dataSourceGoogleBigqueryDatasetRead(d *schema.ResourceData, meta interface{}) error {
27+
config := meta.(*transport_tpg.Config)
28+
29+
dataset_id := d.Get("dataset_id").(string)
30+
31+
project, err := tpgresource.GetProject(d, config)
32+
if err != nil {
33+
return fmt.Errorf("Error fetching project: %s", err)
34+
}
35+
36+
id := fmt.Sprintf("projects/%s/datasets/%s", project, dataset_id)
37+
d.SetId(id)
38+
err = resourceBigQueryDatasetRead(d, meta)
39+
if err != nil {
40+
return err
41+
}
42+
43+
if d.Id() == "" {
44+
return fmt.Errorf("%s not found", id)
45+
}
46+
47+
return nil
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
// Copyright (c) HashiCorp, Inc.
4+
// SPDX-License-Identifier: MPL-2.0
5+
package bigquery_test
6+
7+
import (
8+
"testing"
9+
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
11+
"github.com/hashicorp/terraform-provider-google/google/acctest"
12+
)
13+
14+
func TestAccDataSourceGoogleBigqueryDataset_basic(t *testing.T) {
15+
t.Parallel()
16+
17+
context := map[string]interface{}{
18+
"random_suffix": acctest.RandString(t, 10),
19+
}
20+
21+
acctest.VcrTest(t, resource.TestCase{
22+
PreCheck: func() { acctest.AccTestPreCheck(t) },
23+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
24+
CheckDestroy: testAccCheckBigQueryDatasetDestroyProducer(t),
25+
Steps: []resource.TestStep{
26+
{
27+
Config: testAccDataSourceGoogleBigqueryDataset_basic(context),
28+
Check: resource.ComposeTestCheckFunc(
29+
acctest.CheckDataSourceStateMatchesResourceState("data.google_bigquery_dataset.bar", "google_bigquery_dataset.foo"),
30+
),
31+
},
32+
},
33+
})
34+
}
35+
36+
func testAccDataSourceGoogleBigqueryDataset_basic(context map[string]interface{}) string {
37+
return acctest.Nprintf(`
38+
39+
resource "google_bigquery_dataset" "foo" {
40+
dataset_id = "tf_test_ds_%{random_suffix}"
41+
friendly_name = "testing"
42+
description = "This is a test description"
43+
location = "US"
44+
default_table_expiration_ms = 3600000
45+
}
46+
47+
data "google_bigquery_dataset" "bar" {
48+
dataset_id = google_bigquery_dataset.foo.dataset_id
49+
}
50+
`, context)
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
subcategory: "BigQuery"
3+
description: |-
4+
A datasource to retrieve information about a BigQuery dataset.
5+
---
6+
7+
# `google_bigquery_dataset`
8+
9+
Get information about a BigQuery dataset. For more information see
10+
the [official documentation](https://cloud.google.com/bigquery/docs)
11+
and [API](https://cloud.google.com/bigquery/docs/reference/rest/v2/datasets).
12+
13+
## Example Usage
14+
15+
```hcl
16+
data "google_bigquery_dataset" "dataset" {
17+
dataset_id = "my-bq-dataset"
18+
project = "my-project"
19+
}
20+
```
21+
22+
## Argument Reference
23+
24+
The following arguments are supported:
25+
26+
* `dataset_id` - (Required) The dataset ID.
27+
28+
* `project` - (Optional) The ID of the project in which the resource belongs.
29+
If it is not provided, the provider project is used.
30+
31+
## Attributes Reference
32+
33+
See [google_bigquery_dataset](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/bigquery_dataset) resource for details of the available attributes.

0 commit comments

Comments
 (0)