|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | +package cloudquotas |
| 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 DataSourceGoogleCloudQuotasQuotaInfos() *schema.Resource { |
| 14 | + return &schema.Resource{ |
| 15 | + Read: dataSourceGoogleCloudQuotasQuotaInfosRead, |
| 16 | + |
| 17 | + Schema: map[string]*schema.Schema{ |
| 18 | + "parent": { |
| 19 | + Type: schema.TypeString, |
| 20 | + Required: true, |
| 21 | + }, |
| 22 | + "service": { |
| 23 | + Type: schema.TypeString, |
| 24 | + Required: true, |
| 25 | + }, |
| 26 | + "quota_infos": { |
| 27 | + Type: schema.TypeList, |
| 28 | + Computed: true, |
| 29 | + Elem: &schema.Resource{ |
| 30 | + Schema: map[string]*schema.Schema{ |
| 31 | + "service": { |
| 32 | + Type: schema.TypeString, |
| 33 | + Computed: true, |
| 34 | + }, |
| 35 | + "quota_id": { |
| 36 | + Type: schema.TypeString, |
| 37 | + Computed: true, |
| 38 | + }, |
| 39 | + "name": { |
| 40 | + Type: schema.TypeString, |
| 41 | + Computed: true, |
| 42 | + }, |
| 43 | + "metric": { |
| 44 | + Type: schema.TypeString, |
| 45 | + Computed: true, |
| 46 | + }, |
| 47 | + "is_precise": { |
| 48 | + Type: schema.TypeBool, |
| 49 | + Computed: true, |
| 50 | + }, |
| 51 | + "refresh_interval": { |
| 52 | + Type: schema.TypeString, |
| 53 | + Computed: true, |
| 54 | + }, |
| 55 | + "container_type": { |
| 56 | + Type: schema.TypeString, |
| 57 | + Computed: true, |
| 58 | + }, |
| 59 | + "dimensions": { |
| 60 | + Type: schema.TypeList, |
| 61 | + Computed: true, |
| 62 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 63 | + }, |
| 64 | + "metric_display_name": { |
| 65 | + Type: schema.TypeString, |
| 66 | + Computed: true, |
| 67 | + }, |
| 68 | + "quota_display_name": { |
| 69 | + Type: schema.TypeString, |
| 70 | + Computed: true, |
| 71 | + }, |
| 72 | + "metric_unit": { |
| 73 | + Type: schema.TypeString, |
| 74 | + Computed: true, |
| 75 | + }, |
| 76 | + "quota_increase_eligibility": { |
| 77 | + Type: schema.TypeList, |
| 78 | + Computed: true, |
| 79 | + Elem: &schema.Resource{ |
| 80 | + Schema: map[string]*schema.Schema{ |
| 81 | + "is_eligible": { |
| 82 | + Type: schema.TypeBool, |
| 83 | + Computed: true, |
| 84 | + }, |
| 85 | + "ineligibility_reason": { |
| 86 | + Type: schema.TypeString, |
| 87 | + Computed: true, |
| 88 | + }, |
| 89 | + }, |
| 90 | + }, |
| 91 | + }, |
| 92 | + "is_fixed": { |
| 93 | + Type: schema.TypeBool, |
| 94 | + Computed: true, |
| 95 | + }, |
| 96 | + "dimensions_infos": { |
| 97 | + Type: schema.TypeList, |
| 98 | + Computed: true, |
| 99 | + Elem: &schema.Resource{ |
| 100 | + Schema: map[string]*schema.Schema{ |
| 101 | + "dimensions": { |
| 102 | + Type: schema.TypeMap, |
| 103 | + Computed: true, |
| 104 | + }, |
| 105 | + "details": { |
| 106 | + Type: schema.TypeList, |
| 107 | + Computed: true, |
| 108 | + Elem: &schema.Resource{ |
| 109 | + Schema: map[string]*schema.Schema{ |
| 110 | + "value": { |
| 111 | + Type: schema.TypeString, |
| 112 | + Computed: true, |
| 113 | + }, |
| 114 | + }, |
| 115 | + }, |
| 116 | + }, |
| 117 | + "applicable_locations": { |
| 118 | + Type: schema.TypeList, |
| 119 | + Computed: true, |
| 120 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 121 | + }, |
| 122 | + }, |
| 123 | + }, |
| 124 | + }, |
| 125 | + "is_concurrent": { |
| 126 | + Type: schema.TypeBool, |
| 127 | + Computed: true, |
| 128 | + }, |
| 129 | + "service_request_quota_uri": { |
| 130 | + Type: schema.TypeString, |
| 131 | + Computed: true, |
| 132 | + }, |
| 133 | + }, |
| 134 | + }, |
| 135 | + }, |
| 136 | + }, |
| 137 | + UseJSONNumber: true, |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +func dataSourceGoogleCloudQuotasQuotaInfosRead(d *schema.ResourceData, meta interface{}) error { |
| 142 | + config := meta.(*transport_tpg.Config) |
| 143 | + userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) |
| 144 | + if err != nil { |
| 145 | + return err |
| 146 | + } |
| 147 | + |
| 148 | + url, err := tpgresource.ReplaceVars(d, config, "{{CloudQuotasBasePath}}{{parent}}/locations/global/services/{{service}}/quotaInfos") |
| 149 | + if err != nil { |
| 150 | + return fmt.Errorf("error setting api endpoint") |
| 151 | + } |
| 152 | + |
| 153 | + res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ |
| 154 | + Config: config, |
| 155 | + Method: "GET", |
| 156 | + RawURL: url, |
| 157 | + UserAgent: userAgent, |
| 158 | + }) |
| 159 | + if err != nil { |
| 160 | + return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("CloudQuotasQuotaInfo %q", d.Id())) |
| 161 | + } |
| 162 | + |
| 163 | + var quotaInfos []map[string]interface{} |
| 164 | + for { |
| 165 | + fetchedQuotaInfos := res["quotaInfos"].([]interface{}) |
| 166 | + for _, rawQuotaInfo := range fetchedQuotaInfos { |
| 167 | + quotaInfos = append(quotaInfos, flattenCloudQuotasQuotaInfo(rawQuotaInfo.(map[string]interface{}), d, config)) |
| 168 | + } |
| 169 | + |
| 170 | + if res["nextPageToken"] == nil || res["nextPageToken"].(string) == "" { |
| 171 | + break |
| 172 | + } |
| 173 | + url, err = tpgresource.ReplaceVars(d, config, "{{CloudQuotasBasePath}}{{parent}}/locations/global/services/{{service}}/quotaInfos?pageToken="+res["nextPageToken"].(string)) |
| 174 | + if err != nil { |
| 175 | + return fmt.Errorf("error setting api endpoint") |
| 176 | + } |
| 177 | + res, err = transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ |
| 178 | + Config: config, |
| 179 | + Method: "GET", |
| 180 | + RawURL: url, |
| 181 | + UserAgent: userAgent, |
| 182 | + }) |
| 183 | + if err != nil { |
| 184 | + return transport_tpg.HandleDataSourceNotFoundError(err, d, fmt.Sprintf("CloudQuotasQuotaInfo %q", d.Id()), url) |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + if err := d.Set("quota_infos", quotaInfos); err != nil { |
| 189 | + return fmt.Errorf("error reading quota infos : %s", err) |
| 190 | + } |
| 191 | + |
| 192 | + d.SetId(url) |
| 193 | + return nil |
| 194 | +} |
| 195 | + |
| 196 | +func flattenCloudQuotasQuotaInfo(rawQuotaInfo map[string]interface{}, d *schema.ResourceData, config *transport_tpg.Config) map[string]interface{} { |
| 197 | + quotaInfo := make(map[string]interface{}) |
| 198 | + |
| 199 | + quotaInfo["name"] = rawQuotaInfo["name"] |
| 200 | + quotaInfo["quota_id"] = rawQuotaInfo["quotaId"] |
| 201 | + quotaInfo["metric"] = rawQuotaInfo["metric"] |
| 202 | + quotaInfo["service"] = rawQuotaInfo["service"] |
| 203 | + quotaInfo["is_precise"] = rawQuotaInfo["isPrecise"] |
| 204 | + quotaInfo["refresh_interval"] = rawQuotaInfo["refreshInterval"] |
| 205 | + quotaInfo["container_type"] = rawQuotaInfo["containerType"] |
| 206 | + quotaInfo["dimensions"] = rawQuotaInfo["dimensions"] |
| 207 | + quotaInfo["metric_display_name"] = rawQuotaInfo["metricDisplayName"] |
| 208 | + quotaInfo["quota_display_name"] = rawQuotaInfo["quotaDisplayName"] |
| 209 | + quotaInfo["metric_unit"] = rawQuotaInfo["metricUnit"] |
| 210 | + quotaInfo["quota_increase_eligibility"] = flattenCloudQuotasQuotaInfoQuotaIncreaseEligibility(rawQuotaInfo["quotaIncreaseEligibility"], d, config) |
| 211 | + quotaInfo["is_fixed"] = rawQuotaInfo["isFixed"] |
| 212 | + quotaInfo["dimensions_infos"] = flattenCloudQuotasQuotaInfoDimensionsInfos(rawQuotaInfo["dimensionsInfos"], d, config) |
| 213 | + quotaInfo["is_concurrent"] = rawQuotaInfo["isConcurrent"] |
| 214 | + quotaInfo["service_request_quota_uri"] = rawQuotaInfo["serviceRequestQuotaUri"] |
| 215 | + |
| 216 | + return quotaInfo |
| 217 | +} |
0 commit comments