-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…19402) [upstream:6231f4d544bcad3612ba6834c211f6e29c8f0cb4] Signed-off-by: Modular Magician <magic-modules@google.com>
- Loading branch information
1 parent
7e071a5
commit 520045c
Showing
5 changed files
with
262 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:new-datasource | ||
`google_bigquery_tables` | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
google/services/bigquery/data_source_google_bigquery_tables.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
package bigquery | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-provider-google/google/tpgresource" | ||
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport" | ||
) | ||
|
||
func DataSourceGoogleBigQueryTables() *schema.Resource { | ||
|
||
dsSchema := map[string]*schema.Schema{ | ||
"dataset_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: "The ID of the dataset containing the tables.", | ||
}, | ||
"project": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Description: "The ID of the project in which the dataset is located. If it is not provided, the provider project is used.", | ||
}, | ||
"tables": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"labels": { | ||
Type: schema.TypeMap, | ||
Computed: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
}, | ||
"table_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
return &schema.Resource{ | ||
Read: DataSourceGoogleBigQueryTablesRead, | ||
Schema: dsSchema, | ||
} | ||
} | ||
|
||
func DataSourceGoogleBigQueryTablesRead(d *schema.ResourceData, meta interface{}) error { | ||
|
||
config := meta.(*transport_tpg.Config) | ||
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
datasetID := d.Get("dataset_id").(string) | ||
|
||
project, err := tpgresource.GetProject(d, config) | ||
|
||
if err != nil { | ||
return fmt.Errorf("Error fetching project: %s", err) | ||
} | ||
|
||
params := make(map[string]string) | ||
tables := make([]map[string]interface{}, 0) | ||
|
||
for { | ||
|
||
url, err := tpgresource.ReplaceVars(d, config, "{{BigQueryBasePath}}projects/{{project}}/datasets/{{dataset_id}}/tables") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
url, err = transport_tpg.AddQueryParams(url, params) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ | ||
Config: config, | ||
Method: "GET", | ||
RawURL: url, | ||
UserAgent: userAgent, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("Error retrieving tables: %s", err) | ||
} | ||
|
||
pageTables := flattenDataSourceGoogleBigQueryTablesList(res["tables"]) | ||
tables = append(tables, pageTables...) | ||
|
||
pToken, ok := res["nextPageToken"] | ||
if ok && pToken != nil && pToken.(string) != "" { | ||
params["pageToken"] = pToken.(string) | ||
} else { | ||
break | ||
} | ||
} | ||
|
||
if err := d.Set("tables", tables); err != nil { | ||
return fmt.Errorf("Error retrieving tables: %s", err) | ||
} | ||
|
||
id := fmt.Sprintf("projects/%s/datasets/%s/tables", project, datasetID) | ||
d.SetId(id) | ||
|
||
return nil | ||
} | ||
|
||
func flattenDataSourceGoogleBigQueryTablesList(res interface{}) []map[string]interface{} { | ||
|
||
if res == nil { | ||
return make([]map[string]interface{}, 0) | ||
} | ||
|
||
ls := res.([]interface{}) | ||
|
||
tables := make([]map[string]interface{}, 0, len(ls)) | ||
|
||
for _, raw := range ls { | ||
output := raw.(map[string]interface{}) | ||
|
||
var mLabels map[string]interface{} | ||
var mTableName string | ||
|
||
if oLabels, ok := output["labels"].(map[string]interface{}); ok { | ||
mLabels = oLabels | ||
} else { | ||
mLabels = make(map[string]interface{}) // Initialize as an empty map if labels are missing | ||
} | ||
|
||
if oTableReference, ok := output["tableReference"].(map[string]interface{}); ok { | ||
if tableID, ok := oTableReference["tableId"].(string); ok { | ||
mTableName = tableID | ||
} | ||
} | ||
tables = append(tables, map[string]interface{}{ | ||
"labels": mLabels, | ||
"table_id": mTableName, | ||
}) | ||
} | ||
|
||
return tables | ||
} |
68 changes: 68 additions & 0 deletions
68
google/services/bigquery/data_source_google_bigquery_tables_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
package bigquery_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
"github.com/hashicorp/terraform-provider-google/google/acctest" | ||
) | ||
|
||
func TestAccDataSourceGoogleBigqueryTables_basic(t *testing.T) { | ||
t.Parallel() | ||
|
||
context := map[string]interface{}{ | ||
"random_suffix": acctest.RandString(t, 10), | ||
} | ||
|
||
acctest.VcrTest(t, resource.TestCase{ | ||
PreCheck: func() { acctest.AccTestPreCheck(t) }, | ||
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), | ||
CheckDestroy: testAccCheckBigQueryTableDestroyProducer(t), | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataSourceGoogleBigqueryTables_basic(context), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr("data.google_bigquery_tables.example", "tables.#", "1"), | ||
resource.TestCheckResourceAttr("data.google_bigquery_tables.example", "tables.0.table_id", fmt.Sprintf("tf_test_table_%s", context["random_suffix"])), | ||
resource.TestCheckResourceAttr("data.google_bigquery_tables.example", "tables.0.labels.%", "1"), | ||
resource.TestCheckResourceAttr("data.google_bigquery_tables.example", "tables.0.labels.goog-terraform-provisioned", "true"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccDataSourceGoogleBigqueryTables_basic(context map[string]interface{}) string { | ||
return acctest.Nprintf(` | ||
resource "google_bigquery_dataset" "test" { | ||
dataset_id = "tf_test_ds_%{random_suffix}" | ||
friendly_name = "testing" | ||
description = "This is a test description" | ||
location = "US" | ||
default_table_expiration_ms = 3600000 | ||
} | ||
resource "google_bigquery_table" "test" { | ||
dataset_id = google_bigquery_dataset.test.dataset_id | ||
table_id = "tf_test_table_%{random_suffix}" | ||
deletion_protection = false | ||
schema = <<EOF | ||
[ | ||
{ | ||
"name": "name", | ||
"type": "STRING", | ||
"mode": "NULLABLE" | ||
} | ||
] | ||
EOF | ||
} | ||
data "google_bigquery_tables" "example" { | ||
dataset_id = google_bigquery_table.test.dataset_id | ||
} | ||
`, context) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
--- | ||
subcategory: "BigQuery" | ||
description: |- | ||
A datasource to retrieve a list of tables in a dataset. | ||
--- | ||
|
||
# `google_bigquery_tables` | ||
|
||
Get a list of tables in a BigQuery dataset. For more information see | ||
the [official documentation](https://cloud.google.com/bigquery/docs) | ||
and [API](https://cloud.google.com/bigquery/docs/reference/rest/v2/tables). | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
data "google_bigquery_tables" "tables" { | ||
dataset_id = "my-bq-dataset" | ||
project = "my-project" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `dataset_id` - (Required) The dataset ID. | ||
|
||
* `project` - (Optional) The ID of the project in which the resource belongs. | ||
If it is not provided, the provider project is used. | ||
|
||
## Attributes Reference | ||
|
||
The following attributes are exported: | ||
|
||
* `tables` - A list of all retrieved BigQuery tables. Structure is [defined below](#nested_tables). | ||
|
||
<a name="nested_tables"></a>The `tables` block supports: | ||
|
||
* `labels` - User-provided table labels, in key/value pairs. | ||
* `table_id` - The name of the table. | ||
|