|
| 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 | +} |
0 commit comments