From 520045cab00f875f776a8981ad456f9aba64ea99 Mon Sep 17 00:00:00 2001 From: The Magician Date: Fri, 6 Sep 2024 11:37:19 -0700 Subject: [PATCH] Feat: Add new files for datasources google_bigquery_tables (#11552) (#19402) [upstream:6231f4d544bcad3612ba6834c211f6e29c8f0cb4] Signed-off-by: Modular Magician --- .changelog/11552.txt | 3 + google/provider/provider_mmv1_resources.go | 1 + .../data_source_google_bigquery_tables.go | 149 ++++++++++++++++++ ...data_source_google_bigquery_tables_test.go | 68 ++++++++ website/docs/d/bigquery_tables.html.markdown | 41 +++++ 5 files changed, 262 insertions(+) create mode 100644 .changelog/11552.txt create mode 100644 google/services/bigquery/data_source_google_bigquery_tables.go create mode 100644 google/services/bigquery/data_source_google_bigquery_tables_test.go create mode 100644 website/docs/d/bigquery_tables.html.markdown diff --git a/.changelog/11552.txt b/.changelog/11552.txt new file mode 100644 index 00000000000..1af351c2cc7 --- /dev/null +++ b/.changelog/11552.txt @@ -0,0 +1,3 @@ +```release-note:new-datasource +`google_bigquery_tables` +``` \ No newline at end of file diff --git a/google/provider/provider_mmv1_resources.go b/google/provider/provider_mmv1_resources.go index 1ee760606af..7650aed1665 100644 --- a/google/provider/provider_mmv1_resources.go +++ b/google/provider/provider_mmv1_resources.go @@ -152,6 +152,7 @@ var handwrittenDatasources = map[string]*schema.Resource{ "google_beyondcorp_app_connector": beyondcorp.DataSourceGoogleBeyondcorpAppConnector(), "google_beyondcorp_app_gateway": beyondcorp.DataSourceGoogleBeyondcorpAppGateway(), "google_billing_account": billing.DataSourceGoogleBillingAccount(), + "google_bigquery_tables": bigquery.DataSourceGoogleBigQueryTables(), "google_bigquery_dataset": bigquery.DataSourceGoogleBigqueryDataset(), "google_bigquery_default_service_account": bigquery.DataSourceGoogleBigqueryDefaultServiceAccount(), "google_certificate_manager_certificates": certificatemanager.DataSourceGoogleCertificateManagerCertificates(), diff --git a/google/services/bigquery/data_source_google_bigquery_tables.go b/google/services/bigquery/data_source_google_bigquery_tables.go new file mode 100644 index 00000000000..ecc163985f7 --- /dev/null +++ b/google/services/bigquery/data_source_google_bigquery_tables.go @@ -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 +} diff --git a/google/services/bigquery/data_source_google_bigquery_tables_test.go b/google/services/bigquery/data_source_google_bigquery_tables_test.go new file mode 100644 index 00000000000..bf2c8dad366 --- /dev/null +++ b/google/services/bigquery/data_source_google_bigquery_tables_test.go @@ -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 = <The `tables` block supports: + +* `labels` - User-provided table labels, in key/value pairs. +* `table_id` - The name of the table. +