|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | +// Copyright (c) HashiCorp, Inc. |
| 4 | +// SPDX-License-Identifier: MPL-2.0 |
| 5 | +package cloudquotas |
| 6 | + |
| 7 | +import ( |
| 8 | + "fmt" |
| 9 | + |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 11 | + "github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource" |
| 12 | + transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport" |
| 13 | +) |
| 14 | + |
| 15 | +func DataSourceGoogleCloudQuotasQuotaInfo() *schema.Resource { |
| 16 | + return &schema.Resource{ |
| 17 | + Read: dataSourceGoogleCloudQuotasQuotaInfoRead, |
| 18 | + |
| 19 | + Schema: map[string]*schema.Schema{ |
| 20 | + "parent": { |
| 21 | + Type: schema.TypeString, |
| 22 | + Required: true, |
| 23 | + }, |
| 24 | + "service": { |
| 25 | + Type: schema.TypeString, |
| 26 | + Required: true, |
| 27 | + }, |
| 28 | + "quota_id": { |
| 29 | + Type: schema.TypeString, |
| 30 | + Required: true, |
| 31 | + }, |
| 32 | + "name": { |
| 33 | + Type: schema.TypeString, |
| 34 | + Computed: true, |
| 35 | + }, |
| 36 | + "metric": { |
| 37 | + Type: schema.TypeString, |
| 38 | + Computed: true, |
| 39 | + }, |
| 40 | + "is_precise": { |
| 41 | + Type: schema.TypeBool, |
| 42 | + Computed: true, |
| 43 | + }, |
| 44 | + "refresh_interval": { |
| 45 | + Type: schema.TypeString, |
| 46 | + Computed: true, |
| 47 | + }, |
| 48 | + "container_type": { |
| 49 | + Type: schema.TypeString, |
| 50 | + Computed: true, |
| 51 | + }, |
| 52 | + "dimensions": { |
| 53 | + Type: schema.TypeList, |
| 54 | + Computed: true, |
| 55 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 56 | + }, |
| 57 | + "metric_display_name": { |
| 58 | + Type: schema.TypeString, |
| 59 | + Computed: true, |
| 60 | + }, |
| 61 | + "quota_display_name": { |
| 62 | + Type: schema.TypeString, |
| 63 | + Computed: true, |
| 64 | + }, |
| 65 | + "metric_unit": { |
| 66 | + Type: schema.TypeString, |
| 67 | + Computed: true, |
| 68 | + }, |
| 69 | + "quota_increase_eligibility": { |
| 70 | + Type: schema.TypeList, |
| 71 | + Computed: true, |
| 72 | + Elem: &schema.Resource{ |
| 73 | + Schema: map[string]*schema.Schema{ |
| 74 | + "is_eligible": { |
| 75 | + Type: schema.TypeBool, |
| 76 | + Computed: true, |
| 77 | + }, |
| 78 | + "ineligibility_reason": { |
| 79 | + Type: schema.TypeString, |
| 80 | + Computed: true, |
| 81 | + }, |
| 82 | + }, |
| 83 | + }, |
| 84 | + }, |
| 85 | + "is_fixed": { |
| 86 | + Type: schema.TypeBool, |
| 87 | + Computed: true, |
| 88 | + }, |
| 89 | + "dimensions_infos": { |
| 90 | + Type: schema.TypeList, |
| 91 | + Computed: true, |
| 92 | + Elem: &schema.Resource{ |
| 93 | + Schema: map[string]*schema.Schema{ |
| 94 | + "dimensions": { |
| 95 | + Type: schema.TypeMap, |
| 96 | + Computed: true, |
| 97 | + }, |
| 98 | + "details": { |
| 99 | + Type: schema.TypeList, |
| 100 | + Computed: true, |
| 101 | + Elem: &schema.Resource{ |
| 102 | + Schema: map[string]*schema.Schema{ |
| 103 | + "value": { |
| 104 | + Type: schema.TypeString, |
| 105 | + Computed: true, |
| 106 | + }, |
| 107 | + }, |
| 108 | + }, |
| 109 | + }, |
| 110 | + "applicable_locations": { |
| 111 | + Type: schema.TypeList, |
| 112 | + Computed: true, |
| 113 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 114 | + }, |
| 115 | + }, |
| 116 | + }, |
| 117 | + }, |
| 118 | + "is_concurrent": { |
| 119 | + Type: schema.TypeBool, |
| 120 | + Computed: true, |
| 121 | + }, |
| 122 | + "service_request_quota_uri": { |
| 123 | + Type: schema.TypeString, |
| 124 | + Computed: true, |
| 125 | + }, |
| 126 | + }, |
| 127 | + UseJSONNumber: true, |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +func dataSourceGoogleCloudQuotasQuotaInfoRead(d *schema.ResourceData, meta interface{}) error { |
| 132 | + config := meta.(*transport_tpg.Config) |
| 133 | + userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) |
| 134 | + if err != nil { |
| 135 | + return err |
| 136 | + } |
| 137 | + |
| 138 | + url, err := tpgresource.ReplaceVars(d, config, "{{CloudQuotasBasePath}}{{parent}}/locations/global/services/{{service}}/quotaInfos/{{quota_id}}") |
| 139 | + if err != nil { |
| 140 | + return fmt.Errorf("error setting api endpoint") |
| 141 | + } |
| 142 | + |
| 143 | + res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ |
| 144 | + Config: config, |
| 145 | + Method: "GET", |
| 146 | + RawURL: url, |
| 147 | + UserAgent: userAgent, |
| 148 | + }) |
| 149 | + |
| 150 | + if err != nil { |
| 151 | + return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("CloudQuotasQuotaInfo %q", d.Id())) |
| 152 | + } |
| 153 | + |
| 154 | + if err := d.Set("name", res["name"]); err != nil { |
| 155 | + return fmt.Errorf("error reading QuotaInfo name: %s", err) |
| 156 | + } |
| 157 | + if err := d.Set("quota_id", res["quotaId"]); err != nil { |
| 158 | + return fmt.Errorf("error reading QuotaInfo quota_id: %s", err) |
| 159 | + } |
| 160 | + if err := d.Set("metric", res["metric"]); err != nil { |
| 161 | + return fmt.Errorf("error reading QuotaInfo metric: %s", err) |
| 162 | + } |
| 163 | + if err := d.Set("service", res["service"]); err != nil { |
| 164 | + return fmt.Errorf("error reading QuotaInfo service: %s", err) |
| 165 | + } |
| 166 | + if err := d.Set("is_precise", res["isPrecise"]); err != nil { |
| 167 | + return fmt.Errorf("error reading QuotaInfo is_precise: %s", err) |
| 168 | + } |
| 169 | + if err := d.Set("refresh_interval", res["refreshInterval"]); err != nil { |
| 170 | + return fmt.Errorf("error reading QuotaInfo refresh_interval: %s", err) |
| 171 | + } |
| 172 | + if err := d.Set("container_type", res["containerType"]); err != nil { |
| 173 | + return fmt.Errorf("error reading QuotaInfo container_type: %s", err) |
| 174 | + } |
| 175 | + if err := d.Set("dimensions", res["dimensions"]); err != nil { |
| 176 | + return fmt.Errorf("error reading QuotaInfo dimensions: %s", err) |
| 177 | + } |
| 178 | + if err := d.Set("metric_display_name", res["metricDisplayName"]); err != nil { |
| 179 | + return fmt.Errorf("error reading QuotaInfo metric_display_name: %s", err) |
| 180 | + } |
| 181 | + if err := d.Set("quota_display_name", res["quotaDisplayName"]); err != nil { |
| 182 | + return fmt.Errorf("error reading QuotaInfo quota_display_name: %s", err) |
| 183 | + } |
| 184 | + if err := d.Set("metric_unit", res["metricUnit"]); err != nil { |
| 185 | + return fmt.Errorf("error reading QuotaInfo metric_unit: %s", err) |
| 186 | + } |
| 187 | + if err := d.Set("quota_increase_eligibility", flattenCloudQuotasQuotaInfoQuotaIncreaseEligibility(res["quotaIncreaseEligibility"], d, config)); err != nil { |
| 188 | + return fmt.Errorf("error reading QuotaInfo quota_increase_eligibility: %s", err) |
| 189 | + } |
| 190 | + if err := d.Set("is_fixed", res["isFixed"]); err != nil { |
| 191 | + return fmt.Errorf("error reading QuotaInfo is_fixed: %s", err) |
| 192 | + } |
| 193 | + if err := d.Set("dimensions_infos", flattenCloudQuotasQuotaInfoDimensionsInfos(res["dimensionsInfos"], d, config)); err != nil { |
| 194 | + return fmt.Errorf("error reading QuotaInfo dimensions_infos: %s", err) |
| 195 | + } |
| 196 | + if err := d.Set("is_concurrent", res["isConcurrent"]); err != nil { |
| 197 | + return fmt.Errorf("error reading QuotaInfo is_concurrent: %s", err) |
| 198 | + } |
| 199 | + if err := d.Set("service_request_quota_uri", res["serviceRequestQuotaUri"]); err != nil { |
| 200 | + return fmt.Errorf("error reading QuotaInfo service_request_quota_uri: %s", err) |
| 201 | + } |
| 202 | + |
| 203 | + d.SetId(res["name"].(string)) |
| 204 | + return nil |
| 205 | +} |
| 206 | + |
| 207 | +func flattenCloudQuotasQuotaInfoQuotaIncreaseEligibility(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| 208 | + if v == nil { |
| 209 | + return nil |
| 210 | + } |
| 211 | + original := v.(map[string]interface{}) |
| 212 | + if len(original) == 0 { |
| 213 | + return nil |
| 214 | + } |
| 215 | + transformed := make(map[string]interface{}) |
| 216 | + transformed["is_eligible"] = original["is_eligible"] |
| 217 | + transformed["ineligibility_reason"] = original["ineligibility_reason"] |
| 218 | + return []interface{}{transformed} |
| 219 | +} |
| 220 | + |
| 221 | +func flattenCloudQuotasQuotaInfoDimensionsInfos(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) []interface{} { |
| 222 | + if v == nil { |
| 223 | + return make([]interface{}, 0) |
| 224 | + } |
| 225 | + |
| 226 | + original := v.([]interface{}) |
| 227 | + dimensionsInfos := make([]interface{}, 0, len(original)) |
| 228 | + |
| 229 | + for _, raw := range original { |
| 230 | + data := make(map[string]interface{}) |
| 231 | + data["details"] = flattenCloudQuotasQuotaInfoDetails(raw.(map[string]interface{})["details"], d, config) |
| 232 | + data["applicable_locations"] = raw.(map[string]interface{})["applicableLocations"] |
| 233 | + data["dimensions"] = raw.(map[string]interface{})["dimensions"] |
| 234 | + |
| 235 | + dimensionsInfos = append(dimensionsInfos, data) |
| 236 | + } |
| 237 | + return dimensionsInfos |
| 238 | +} |
| 239 | + |
| 240 | +func flattenCloudQuotasQuotaInfoDetails(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { |
| 241 | + original, ok := v.(map[string]interface{}) |
| 242 | + if !ok || len(original) == 0 { |
| 243 | + return nil |
| 244 | + } |
| 245 | + |
| 246 | + return []interface{}{ |
| 247 | + map[string]interface{}{"value": original["value"]}, |
| 248 | + } |
| 249 | +} |
0 commit comments