Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added CloudQuotas service and Create QuotaInfo Datasource #17564

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/10071.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
google_cloud_quotas_quota_info
```
7 changes: 6 additions & 1 deletion .teamcity/components/inputs/services_beta.kt
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,11 @@ var ServicesListBeta = mapOf(
"displayName" to "Cloudids",
"path" to "./google-beta/services/cloudids"
),
"cloudquotas" to mapOf(
"name" to "cloudquotas",
"displayName" to "Cloudquotas",
"path" to "./google-beta/services/cloudquotas"
),
"cloudrun" to mapOf(
"name" to "cloudrun",
"displayName" to "Cloudrun",
Expand Down Expand Up @@ -693,4 +698,4 @@ var ServicesListBeta = mapOf(
"displayName" to "Workstations",
"path" to "./google-beta/services/workstations"
)
)
)
7 changes: 6 additions & 1 deletion .teamcity/components/inputs/services_ga.kt
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,11 @@ var ServicesListGa = mapOf(
"displayName" to "Cloudids",
"path" to "./google/services/cloudids"
),
"cloudquotas" to mapOf(
"name" to "cloudquotas",
"displayName" to "Cloudquotas",
"path" to "./google/services/cloudquotas"
),
"cloudrun" to mapOf(
"name" to "cloudrun",
"displayName" to "Cloudrun",
Expand Down Expand Up @@ -688,4 +693,4 @@ var ServicesListGa = mapOf(
"displayName" to "Workstations",
"path" to "./google/services/workstations"
)
)
)
1 change: 1 addition & 0 deletions google/fwmodels/provider_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type ProviderModel struct {
Cloudfunctions2CustomEndpoint types.String `tfsdk:"cloudfunctions2_custom_endpoint"`
CloudIdentityCustomEndpoint types.String `tfsdk:"cloud_identity_custom_endpoint"`
CloudIdsCustomEndpoint types.String `tfsdk:"cloud_ids_custom_endpoint"`
CloudQuotasCustomEndpoint types.String `tfsdk:"cloud_quotas_custom_endpoint"`
CloudRunCustomEndpoint types.String `tfsdk:"cloud_run_custom_endpoint"`
CloudRunV2CustomEndpoint types.String `tfsdk:"cloud_run_v2_custom_endpoint"`
CloudSchedulerCustomEndpoint types.String `tfsdk:"cloud_scheduler_custom_endpoint"`
Expand Down
6 changes: 6 additions & 0 deletions google/fwprovider/framework_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,12 @@ func (p *FrameworkProvider) Schema(_ context.Context, _ provider.SchemaRequest,
transport_tpg.CustomEndpointValidator(),
},
},
"cloud_quotas_custom_endpoint": &schema.StringAttribute{
Optional: true,
Validators: []validator.String{
transport_tpg.CustomEndpointValidator(),
},
},
"cloud_run_custom_endpoint": &schema.StringAttribute{
Optional: true,
Validators: []validator.String{
Expand Down
10 changes: 10 additions & 0 deletions google/fwtransport/framework_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ type FrameworkProviderConfig struct {
Cloudfunctions2BasePath string
CloudIdentityBasePath string
CloudIdsBasePath string
CloudQuotasBasePath string
CloudRunBasePath string
CloudRunV2BasePath string
CloudSchedulerBasePath string
Expand Down Expand Up @@ -233,6 +234,7 @@ func (p *FrameworkProviderConfig) LoadAndValidateFramework(ctx context.Context,
p.Cloudfunctions2BasePath = data.Cloudfunctions2CustomEndpoint.ValueString()
p.CloudIdentityBasePath = data.CloudIdentityCustomEndpoint.ValueString()
p.CloudIdsBasePath = data.CloudIdsCustomEndpoint.ValueString()
p.CloudQuotasBasePath = data.CloudQuotasCustomEndpoint.ValueString()
p.CloudRunBasePath = data.CloudRunCustomEndpoint.ValueString()
p.CloudRunV2BasePath = data.CloudRunV2CustomEndpoint.ValueString()
p.CloudSchedulerBasePath = data.CloudSchedulerCustomEndpoint.ValueString()
Expand Down Expand Up @@ -683,6 +685,14 @@ func (p *FrameworkProviderConfig) HandleDefaults(ctx context.Context, data *fwmo
data.CloudIdsCustomEndpoint = types.StringValue(customEndpoint.(string))
}
}
if data.CloudQuotasCustomEndpoint.IsNull() {
customEndpoint := transport_tpg.MultiEnvDefault([]string{
"GOOGLE_CLOUD_QUOTAS_CUSTOM_ENDPOINT",
}, transport_tpg.DefaultBasePaths[transport_tpg.CloudQuotasBasePathKey])
if customEndpoint != nil {
data.CloudQuotasCustomEndpoint = types.StringValue(customEndpoint.(string))
}
}
if data.CloudRunCustomEndpoint.IsNull() {
customEndpoint := transport_tpg.MultiEnvDefault([]string{
"GOOGLE_CLOUD_RUN_CUSTOM_ENDPOINT",
Expand Down
6 changes: 6 additions & 0 deletions google/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,11 @@ func Provider() *schema.Provider {
Optional: true,
ValidateFunc: transport_tpg.ValidateCustomEndpoint,
},
"cloud_quotas_custom_endpoint": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: transport_tpg.ValidateCustomEndpoint,
},
"cloud_run_custom_endpoint": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -954,6 +959,7 @@ func ProviderConfigure(ctx context.Context, d *schema.ResourceData, p *schema.Pr
config.Cloudfunctions2BasePath = d.Get("cloudfunctions2_custom_endpoint").(string)
config.CloudIdentityBasePath = d.Get("cloud_identity_custom_endpoint").(string)
config.CloudIdsBasePath = d.Get("cloud_ids_custom_endpoint").(string)
config.CloudQuotasBasePath = d.Get("cloud_quotas_custom_endpoint").(string)
config.CloudRunBasePath = d.Get("cloud_run_custom_endpoint").(string)
config.CloudRunV2BasePath = d.Get("cloud_run_v2_custom_endpoint").(string)
config.CloudSchedulerBasePath = d.Get("cloud_scheduler_custom_endpoint").(string)
Expand Down
3 changes: 3 additions & 0 deletions google/provider/provider_mmv1_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ import (
"github.com/hashicorp/terraform-provider-google/google/services/dataflow"
"github.com/hashicorp/terraform-provider-google/google/services/servicenetworking"
"github.com/hashicorp/terraform-provider-google/google/tpgiamresource"
// https://github.com/hashicorp/terraform-provider-google/issues/15633 for details
"github.com/hashicorp/terraform-provider-google/google/services/cloudquotas"
)

// Datasources
Expand Down Expand Up @@ -153,6 +155,7 @@ var handwrittenDatasources = map[string]*schema.Resource{
"google_cloud_identity_groups": cloudidentity.DataSourceGoogleCloudIdentityGroups(),
"google_cloud_identity_group_memberships": cloudidentity.DataSourceGoogleCloudIdentityGroupMemberships(),
"google_cloud_identity_group_lookup": cloudidentity.DataSourceGoogleCloudIdentityGroupLookup(),
"google_cloud_quotas_quota_info": cloudquotas.DataSourceGoogleCloudQuotasQuotaInfo(),
"google_cloud_run_locations": cloudrun.DataSourceGoogleCloudRunLocations(),
"google_cloud_run_service": cloudrun.DataSourceGoogleCloudRunService(),
"google_cloud_run_v2_job": cloudrunv2.DataSourceGoogleCloudRunV2Job(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package cloudquotas

import (
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
)

func DataSourceGoogleCloudQuotasQuotaInfo() *schema.Resource {
return &schema.Resource{
Read: dataSourceGoogleCloudQuotasQuotaInfoRead,

Schema: map[string]*schema.Schema{
"parent": {
Type: schema.TypeString,
Required: true,
},
"service": {
Type: schema.TypeString,
Required: true,
},
"quota_id": {
Type: schema.TypeString,
Required: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"metric": {
Type: schema.TypeString,
Computed: true,
},
"is_precise": {
Type: schema.TypeBool,
Computed: true,
},
"refresh_interval": {
Type: schema.TypeString,
Computed: true,
},
"container_type": {
Type: schema.TypeString,
Computed: true,
},
"dimensions": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"metric_display_name": {
Type: schema.TypeString,
Computed: true,
},
"quota_display_name": {
Type: schema.TypeString,
Computed: true,
},
"metric_unit": {
Type: schema.TypeString,
Computed: true,
},
"quota_increase_eligibility": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"is_eligible": {
Type: schema.TypeBool,
Computed: true,
},
"ineligibility_reason": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"is_fixed": {
Type: schema.TypeBool,
Computed: true,
},
"dimensions_infos": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"dimensions": {
Type: schema.TypeMap,
Computed: true,
},
"details": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"value": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"applicable_locations": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
},
"is_concurrent": {
Type: schema.TypeBool,
Computed: true,
},
"service_request_quota_uri": {
Type: schema.TypeString,
Computed: true,
},
},
UseJSONNumber: true,
}
}

func dataSourceGoogleCloudQuotasQuotaInfoRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*transport_tpg.Config)
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
if err != nil {
return err
}

url, err := tpgresource.ReplaceVars(d, config, "{{CloudQuotasBasePath}}{{parent}}/locations/global/services/{{service}}/quotaInfos/{{quota_id}}")
if err != nil {
return fmt.Errorf("error setting api endpoint")
}

res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "GET",
RawURL: url,
UserAgent: userAgent,
})

if err != nil {
return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("CloudQuotasQuotaInfo %q", d.Id()))
}

if err := d.Set("name", res["name"]); err != nil {
return fmt.Errorf("error reading QuotaInfo name: %s", err)
}
if err := d.Set("quota_id", res["quotaId"]); err != nil {
return fmt.Errorf("error reading QuotaInfo quota_id: %s", err)
}
if err := d.Set("metric", res["metric"]); err != nil {
return fmt.Errorf("error reading QuotaInfo metric: %s", err)
}
if err := d.Set("service", res["service"]); err != nil {
return fmt.Errorf("error reading QuotaInfo service: %s", err)
}
if err := d.Set("is_precise", res["isPrecise"]); err != nil {
return fmt.Errorf("error reading QuotaInfo is_precise: %s", err)
}
if err := d.Set("refresh_interval", res["refreshInterval"]); err != nil {
return fmt.Errorf("error reading QuotaInfo refresh_interval: %s", err)
}
if err := d.Set("container_type", res["containerType"]); err != nil {
return fmt.Errorf("error reading QuotaInfo container_type: %s", err)
}
if err := d.Set("dimensions", res["dimensions"]); err != nil {
return fmt.Errorf("error reading QuotaInfo dimensions: %s", err)
}
if err := d.Set("metric_display_name", res["metricDisplayName"]); err != nil {
return fmt.Errorf("error reading QuotaInfo metric_display_name: %s", err)
}
if err := d.Set("quota_display_name", res["quotaDisplayName"]); err != nil {
return fmt.Errorf("error reading QuotaInfo quota_display_name: %s", err)
}
if err := d.Set("metric_unit", res["metricUnit"]); err != nil {
return fmt.Errorf("error reading QuotaInfo metric_unit: %s", err)
}
if err := d.Set("quota_increase_eligibility", flattenCloudQuotasQuotaInfoQuotaIncreaseEligibility(res["quotaIncreaseEligibility"], d, config)); err != nil {
return fmt.Errorf("error reading QuotaInfo quota_increase_eligibility: %s", err)
}
if err := d.Set("is_fixed", res["isFixed"]); err != nil {
return fmt.Errorf("error reading QuotaInfo is_fixed: %s", err)
}
if err := d.Set("dimensions_infos", flattenCloudQuotasQuotaInfoDimensionsInfos(res["dimensionsInfos"], d, config)); err != nil {
return fmt.Errorf("error reading QuotaInfo dimensions_infos: %s", err)
}
if err := d.Set("is_concurrent", res["isConcurrent"]); err != nil {
return fmt.Errorf("error reading QuotaInfo is_concurrent: %s", err)
}
if err := d.Set("service_request_quota_uri", res["serviceRequestQuotaUri"]); err != nil {
return fmt.Errorf("error reading QuotaInfo service_request_quota_uri: %s", err)
}

d.SetId(res["name"].(string))
return nil
}

func flattenCloudQuotasQuotaInfoQuotaIncreaseEligibility(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
if v == nil {
return nil
}
original := v.(map[string]interface{})
if len(original) == 0 {
return nil
}
transformed := make(map[string]interface{})
transformed["is_eligible"] = original["is_eligible"]
transformed["ineligibility_reason"] = original["ineligibility_reason"]
return []interface{}{transformed}
}

func flattenCloudQuotasQuotaInfoDimensionsInfos(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) []interface{} {
if v == nil {
return make([]interface{}, 0)
}

original := v.([]interface{})
dimensionsInfos := make([]interface{}, 0, len(original))

for _, raw := range original {
data := make(map[string]interface{})
data["details"] = flattenCloudQuotasQuotaInfoDetails(raw.(map[string]interface{})["details"], d, config)
data["applicable_locations"] = raw.(map[string]interface{})["applicableLocations"]
data["dimensions"] = raw.(map[string]interface{})["dimensions"]

dimensionsInfos = append(dimensionsInfos, data)
}
return dimensionsInfos
}

func flattenCloudQuotasQuotaInfoDetails(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
original, ok := v.(map[string]interface{})
if !ok || len(original) == 0 {
return nil
}

return []interface{}{
map[string]interface{}{"value": original["value"]},
}
}