|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | +package bigquery |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 9 | + "github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource" |
| 10 | + transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport" |
| 11 | +) |
| 12 | + |
| 13 | +func DataSourceGoogleBigQueryTables() *schema.Resource { |
| 14 | + |
| 15 | + dsSchema := map[string]*schema.Schema{ |
| 16 | + "dataset_id": { |
| 17 | + Type: schema.TypeString, |
| 18 | + Required: true, |
| 19 | + Description: "The ID of the dataset containing the tables.", |
| 20 | + }, |
| 21 | + "project": { |
| 22 | + Type: schema.TypeString, |
| 23 | + Optional: true, |
| 24 | + Description: "The ID of the project in which the dataset is located. If it is not provided, the provider project is used.", |
| 25 | + }, |
| 26 | + "tables": { |
| 27 | + Type: schema.TypeList, |
| 28 | + Computed: true, |
| 29 | + Elem: &schema.Resource{ |
| 30 | + Schema: map[string]*schema.Schema{ |
| 31 | + "labels": { |
| 32 | + Type: schema.TypeMap, |
| 33 | + Computed: true, |
| 34 | + Elem: &schema.Schema{ |
| 35 | + Type: schema.TypeString, |
| 36 | + }, |
| 37 | + }, |
| 38 | + "table_id": { |
| 39 | + Type: schema.TypeString, |
| 40 | + Computed: true, |
| 41 | + }, |
| 42 | + }, |
| 43 | + }, |
| 44 | + }, |
| 45 | + } |
| 46 | + |
| 47 | + return &schema.Resource{ |
| 48 | + Read: DataSourceGoogleBigQueryTablesRead, |
| 49 | + Schema: dsSchema, |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func DataSourceGoogleBigQueryTablesRead(d *schema.ResourceData, meta interface{}) error { |
| 54 | + |
| 55 | + config := meta.(*transport_tpg.Config) |
| 56 | + userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) |
| 57 | + if err != nil { |
| 58 | + return err |
| 59 | + } |
| 60 | + |
| 61 | + datasetID := d.Get("dataset_id").(string) |
| 62 | + |
| 63 | + project, err := tpgresource.GetProject(d, config) |
| 64 | + |
| 65 | + if err != nil { |
| 66 | + return fmt.Errorf("Error fetching project: %s", err) |
| 67 | + } |
| 68 | + |
| 69 | + params := make(map[string]string) |
| 70 | + tables := make([]map[string]interface{}, 0) |
| 71 | + |
| 72 | + for { |
| 73 | + |
| 74 | + url, err := tpgresource.ReplaceVars(d, config, "{{BigQueryBasePath}}projects/{{project}}/datasets/{{dataset_id}}/tables") |
| 75 | + if err != nil { |
| 76 | + return err |
| 77 | + } |
| 78 | + |
| 79 | + url, err = transport_tpg.AddQueryParams(url, params) |
| 80 | + if err != nil { |
| 81 | + return err |
| 82 | + } |
| 83 | + |
| 84 | + res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ |
| 85 | + Config: config, |
| 86 | + Method: "GET", |
| 87 | + RawURL: url, |
| 88 | + UserAgent: userAgent, |
| 89 | + }) |
| 90 | + if err != nil { |
| 91 | + return fmt.Errorf("Error retrieving tables: %s", err) |
| 92 | + } |
| 93 | + |
| 94 | + pageTables := flattenDataSourceGoogleBigQueryTablesList(res["tables"]) |
| 95 | + tables = append(tables, pageTables...) |
| 96 | + |
| 97 | + pToken, ok := res["nextPageToken"] |
| 98 | + if ok && pToken != nil && pToken.(string) != "" { |
| 99 | + params["pageToken"] = pToken.(string) |
| 100 | + } else { |
| 101 | + break |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + if err := d.Set("tables", tables); err != nil { |
| 106 | + return fmt.Errorf("Error retrieving tables: %s", err) |
| 107 | + } |
| 108 | + |
| 109 | + id := fmt.Sprintf("projects/%s/datasets/%s/tables", project, datasetID) |
| 110 | + d.SetId(id) |
| 111 | + |
| 112 | + return nil |
| 113 | +} |
| 114 | + |
| 115 | +func flattenDataSourceGoogleBigQueryTablesList(res interface{}) []map[string]interface{} { |
| 116 | + |
| 117 | + if res == nil { |
| 118 | + return make([]map[string]interface{}, 0) |
| 119 | + } |
| 120 | + |
| 121 | + ls := res.([]interface{}) |
| 122 | + |
| 123 | + tables := make([]map[string]interface{}, 0, len(ls)) |
| 124 | + |
| 125 | + for _, raw := range ls { |
| 126 | + output := raw.(map[string]interface{}) |
| 127 | + |
| 128 | + var mLabels map[string]interface{} |
| 129 | + var mTableName string |
| 130 | + |
| 131 | + if oLabels, ok := output["labels"].(map[string]interface{}); ok { |
| 132 | + mLabels = oLabels |
| 133 | + } else { |
| 134 | + mLabels = make(map[string]interface{}) // Initialize as an empty map if labels are missing |
| 135 | + } |
| 136 | + |
| 137 | + if oTableReference, ok := output["tableReference"].(map[string]interface{}); ok { |
| 138 | + if tableID, ok := oTableReference["tableId"].(string); ok { |
| 139 | + mTableName = tableID |
| 140 | + } |
| 141 | + } |
| 142 | + tables = append(tables, map[string]interface{}{ |
| 143 | + "labels": mLabels, |
| 144 | + "table_id": mTableName, |
| 145 | + }) |
| 146 | + } |
| 147 | + |
| 148 | + return tables |
| 149 | +} |
0 commit comments