Skip to content

Commit 39d359d

Browse files
Added CloudQuotas service and Create QuotaInfo Datasource (#10071) (#7092)
* add quota info data source * add test and documention for quota_info data source * fix lint error * clean up * update test * manually import cloudquotas to terraform provider * Update mmv1/third_party/terraform/provider/provider_mmv1_resources.go.erb * Update mmv1/third_party/terraform/provider/provider_mmv1_resources.go.erb --------- [upstream:e3a06e371bbcd7ccd50dd832b96f4108d27d97bc] Signed-off-by: Modular Magician <[email protected]>
1 parent 162f91d commit 39d359d

11 files changed

+411
-0
lines changed

.changelog/10071.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-resource
2+
google_cloud_quotas_quota_info
3+
```

google-beta/fwmodels/provider_model.go

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ type ProviderModel struct {
6060
Cloudfunctions2CustomEndpoint types.String `tfsdk:"cloudfunctions2_custom_endpoint"`
6161
CloudIdentityCustomEndpoint types.String `tfsdk:"cloud_identity_custom_endpoint"`
6262
CloudIdsCustomEndpoint types.String `tfsdk:"cloud_ids_custom_endpoint"`
63+
CloudQuotasCustomEndpoint types.String `tfsdk:"cloud_quotas_custom_endpoint"`
6364
CloudRunCustomEndpoint types.String `tfsdk:"cloud_run_custom_endpoint"`
6465
CloudRunV2CustomEndpoint types.String `tfsdk:"cloud_run_v2_custom_endpoint"`
6566
CloudSchedulerCustomEndpoint types.String `tfsdk:"cloud_scheduler_custom_endpoint"`

google-beta/fwprovider/framework_provider.go

+6
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,12 @@ func (p *FrameworkProvider) Schema(_ context.Context, _ provider.SchemaRequest,
335335
transport_tpg.CustomEndpointValidator(),
336336
},
337337
},
338+
"cloud_quotas_custom_endpoint": &schema.StringAttribute{
339+
Optional: true,
340+
Validators: []validator.String{
341+
transport_tpg.CustomEndpointValidator(),
342+
},
343+
},
338344
"cloud_run_custom_endpoint": &schema.StringAttribute{
339345
Optional: true,
340346
Validators: []validator.String{

google-beta/fwtransport/framework_config.go

+10
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ type FrameworkProviderConfig struct {
8383
Cloudfunctions2BasePath string
8484
CloudIdentityBasePath string
8585
CloudIdsBasePath string
86+
CloudQuotasBasePath string
8687
CloudRunBasePath string
8788
CloudRunV2BasePath string
8889
CloudSchedulerBasePath string
@@ -248,6 +249,7 @@ func (p *FrameworkProviderConfig) LoadAndValidateFramework(ctx context.Context,
248249
p.Cloudfunctions2BasePath = data.Cloudfunctions2CustomEndpoint.ValueString()
249250
p.CloudIdentityBasePath = data.CloudIdentityCustomEndpoint.ValueString()
250251
p.CloudIdsBasePath = data.CloudIdsCustomEndpoint.ValueString()
252+
p.CloudQuotasBasePath = data.CloudQuotasCustomEndpoint.ValueString()
251253
p.CloudRunBasePath = data.CloudRunCustomEndpoint.ValueString()
252254
p.CloudRunV2BasePath = data.CloudRunV2CustomEndpoint.ValueString()
253255
p.CloudSchedulerBasePath = data.CloudSchedulerCustomEndpoint.ValueString()
@@ -725,6 +727,14 @@ func (p *FrameworkProviderConfig) HandleDefaults(ctx context.Context, data *fwmo
725727
data.CloudIdsCustomEndpoint = types.StringValue(customEndpoint.(string))
726728
}
727729
}
730+
if data.CloudQuotasCustomEndpoint.IsNull() {
731+
customEndpoint := transport_tpg.MultiEnvDefault([]string{
732+
"GOOGLE_CLOUD_QUOTAS_CUSTOM_ENDPOINT",
733+
}, transport_tpg.DefaultBasePaths[transport_tpg.CloudQuotasBasePathKey])
734+
if customEndpoint != nil {
735+
data.CloudQuotasCustomEndpoint = types.StringValue(customEndpoint.(string))
736+
}
737+
}
728738
if data.CloudRunCustomEndpoint.IsNull() {
729739
customEndpoint := transport_tpg.MultiEnvDefault([]string{
730740
"GOOGLE_CLOUD_RUN_CUSTOM_ENDPOINT",

google-beta/provider/provider.go

+6
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,11 @@ func Provider() *schema.Provider {
305305
Optional: true,
306306
ValidateFunc: transport_tpg.ValidateCustomEndpoint,
307307
},
308+
"cloud_quotas_custom_endpoint": {
309+
Type: schema.TypeString,
310+
Optional: true,
311+
ValidateFunc: transport_tpg.ValidateCustomEndpoint,
312+
},
308313
"cloud_run_custom_endpoint": {
309314
Type: schema.TypeString,
310315
Optional: true,
@@ -1022,6 +1027,7 @@ func ProviderConfigure(ctx context.Context, d *schema.ResourceData, p *schema.Pr
10221027
config.Cloudfunctions2BasePath = d.Get("cloudfunctions2_custom_endpoint").(string)
10231028
config.CloudIdentityBasePath = d.Get("cloud_identity_custom_endpoint").(string)
10241029
config.CloudIdsBasePath = d.Get("cloud_ids_custom_endpoint").(string)
1030+
config.CloudQuotasBasePath = d.Get("cloud_quotas_custom_endpoint").(string)
10251031
config.CloudRunBasePath = d.Get("cloud_run_custom_endpoint").(string)
10261032
config.CloudRunV2BasePath = d.Get("cloud_run_v2_custom_endpoint").(string)
10271033
config.CloudSchedulerBasePath = d.Get("cloud_scheduler_custom_endpoint").(string)

google-beta/provider/provider_mmv1_resources.go

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
"github.com/hashicorp/terraform-provider-google-beta/google-beta/services/cloudfunctions2"
3838
"github.com/hashicorp/terraform-provider-google-beta/google-beta/services/cloudidentity"
3939
"github.com/hashicorp/terraform-provider-google-beta/google-beta/services/cloudids"
40+
"github.com/hashicorp/terraform-provider-google-beta/google-beta/services/cloudquotas"
4041
"github.com/hashicorp/terraform-provider-google-beta/google-beta/services/cloudrun"
4142
"github.com/hashicorp/terraform-provider-google-beta/google-beta/services/cloudrunv2"
4243
"github.com/hashicorp/terraform-provider-google-beta/google-beta/services/cloudscheduler"
@@ -169,6 +170,7 @@ var handwrittenDatasources = map[string]*schema.Resource{
169170
"google_cloud_identity_groups": cloudidentity.DataSourceGoogleCloudIdentityGroups(),
170171
"google_cloud_identity_group_memberships": cloudidentity.DataSourceGoogleCloudIdentityGroupMemberships(),
171172
"google_cloud_identity_group_lookup": cloudidentity.DataSourceGoogleCloudIdentityGroupLookup(),
173+
"google_cloud_quotas_quota_info": cloudquotas.DataSourceGoogleCloudQuotasQuotaInfo(),
172174
"google_cloud_run_locations": cloudrun.DataSourceGoogleCloudRunLocations(),
173175
"google_cloud_run_service": cloudrun.DataSourceGoogleCloudRunService(),
174176
"google_cloud_run_v2_job": cloudrunv2.DataSourceGoogleCloudRunV2Job(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
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

Comments
 (0)