Skip to content

Commit

Permalink
Create quota infos datasource (#10074) (#17617)
Browse files Browse the repository at this point in the history
* add quota info data source

* add test and documention for quota_info data source

* fix lint error

* add quota infos datasource

* update read test

* remove duplicate import

* update formatting and documentation

* remove pagination parameters, concat and return all qualified entries

* remove specific check for number of entries

[upstream:55deca52e291505ba1692b3fea275116b2adfe85]

Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician committed Mar 19, 2024
1 parent 8cd88af commit c146e74
Show file tree
Hide file tree
Showing 6 changed files with 336 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .changelog/10074.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-datasource
`google_cloud_quotas_quota_infos`
```
1 change: 1 addition & 0 deletions google/provider/provider_mmv1_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ var handwrittenDatasources = map[string]*schema.Resource{
"google_cloud_identity_group_memberships": cloudidentity.DataSourceGoogleCloudIdentityGroupMemberships(),
"google_cloud_identity_group_lookup": cloudidentity.DataSourceGoogleCloudIdentityGroupLookup(),
"google_cloud_quotas_quota_info": cloudquotas.DataSourceGoogleCloudQuotasQuotaInfo(),
"google_cloud_quotas_quota_infos": cloudquotas.DataSourceGoogleCloudQuotasQuotaInfos(),
"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,217 @@
// 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 DataSourceGoogleCloudQuotasQuotaInfos() *schema.Resource {
return &schema.Resource{
Read: dataSourceGoogleCloudQuotasQuotaInfosRead,

Schema: map[string]*schema.Schema{
"parent": {
Type: schema.TypeString,
Required: true,
},
"service": {
Type: schema.TypeString,
Required: true,
},
"quota_infos": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"service": {
Type: schema.TypeString,
Computed: true,
},
"quota_id": {
Type: schema.TypeString,
Computed: 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 dataSourceGoogleCloudQuotasQuotaInfosRead(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")
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()))
}

var quotaInfos []map[string]interface{}
for {
fetchedQuotaInfos := res["quotaInfos"].([]interface{})
for _, rawQuotaInfo := range fetchedQuotaInfos {
quotaInfos = append(quotaInfos, flattenCloudQuotasQuotaInfo(rawQuotaInfo.(map[string]interface{}), d, config))
}

if res["nextPageToken"] == nil || res["nextPageToken"].(string) == "" {
break
}
url, err = tpgresource.ReplaceVars(d, config, "{{CloudQuotasBasePath}}{{parent}}/locations/global/services/{{service}}/quotaInfos?pageToken="+res["nextPageToken"].(string))
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.HandleDataSourceNotFoundError(err, d, fmt.Sprintf("CloudQuotasQuotaInfo %q", d.Id()), url)
}
}

if err := d.Set("quota_infos", quotaInfos); err != nil {
return fmt.Errorf("error reading quota infos : %s", err)
}

d.SetId(url)
return nil
}

func flattenCloudQuotasQuotaInfo(rawQuotaInfo map[string]interface{}, d *schema.ResourceData, config *transport_tpg.Config) map[string]interface{} {
quotaInfo := make(map[string]interface{})

quotaInfo["name"] = rawQuotaInfo["name"]
quotaInfo["quota_id"] = rawQuotaInfo["quotaId"]
quotaInfo["metric"] = rawQuotaInfo["metric"]
quotaInfo["service"] = rawQuotaInfo["service"]
quotaInfo["is_precise"] = rawQuotaInfo["isPrecise"]
quotaInfo["refresh_interval"] = rawQuotaInfo["refreshInterval"]
quotaInfo["container_type"] = rawQuotaInfo["containerType"]
quotaInfo["dimensions"] = rawQuotaInfo["dimensions"]
quotaInfo["metric_display_name"] = rawQuotaInfo["metricDisplayName"]
quotaInfo["quota_display_name"] = rawQuotaInfo["quotaDisplayName"]
quotaInfo["metric_unit"] = rawQuotaInfo["metricUnit"]
quotaInfo["quota_increase_eligibility"] = flattenCloudQuotasQuotaInfoQuotaIncreaseEligibility(rawQuotaInfo["quotaIncreaseEligibility"], d, config)
quotaInfo["is_fixed"] = rawQuotaInfo["isFixed"]
quotaInfo["dimensions_infos"] = flattenCloudQuotasQuotaInfoDimensionsInfos(rawQuotaInfo["dimensionsInfos"], d, config)
quotaInfo["is_concurrent"] = rawQuotaInfo["isConcurrent"]
quotaInfo["service_request_quota_uri"] = rawQuotaInfo["serviceRequestQuotaUri"]

return quotaInfo
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package cloudquotas_test

import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-provider-google/google/acctest"
"github.com/hashicorp/terraform-provider-google/google/envvar"
)

func TestAccDataSourceGoogleQuotaInfos_basic(t *testing.T) {
t.Parallel()

resourceName := "data.google_cloud_quotas_quota_infos.my_quota_infos"
service := "compute.googleapis.com"

context := map[string]interface{}{
"project": envvar.GetTestProjectFromEnv(),
"service": service,
}

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
Steps: []resource.TestStep{
{
Config: testAccDataSourceGoogleQuotaInfos_basic(context),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(resourceName, "quota_infos.0.name"),
resource.TestCheckResourceAttrSet(resourceName, "quota_infos.0.quota_id"),
resource.TestCheckResourceAttrSet(resourceName, "quota_infos.0.metric"),
resource.TestCheckResourceAttr(resourceName, "quota_infos.0.service", service),
resource.TestCheckResourceAttrSet(resourceName, "quota_infos.0.is_precise"),
resource.TestCheckResourceAttrSet(resourceName, "quota_infos.0.container_type"),
resource.TestCheckResourceAttrSet(resourceName, "quota_infos.0.quota_increase_eligibility.0.is_eligible"),
resource.TestCheckResourceAttrSet(resourceName, "quota_infos.0.dimensions_infos.0.details.0.value"),
resource.TestCheckResourceAttrSet(resourceName, "quota_infos.0.dimensions_infos.0.applicable_locations.0"),
),
},
},
})
}

func testAccDataSourceGoogleQuotaInfos_basic(context map[string]interface{}) string {
return acctest.Nprintf(`
data "google_cloud_quotas_quota_infos" "my_quota_infos" {
parent = "projects/%{project}"
service = "%{service}"
}
`, context)
}
1 change: 0 additions & 1 deletion website/docs/d/cloud_quotas_quota_info.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ The following attributes are exported:
<a name="nested_dimensions_infos"></a> The `dimensions_infos` block supports:
* `dimensions` - The map of dimensions for this dimensions info. The key of a map entry is "region", "zone" or the name of a service specific dimension, and the value of a map entry is the value of the dimension. If a dimension does not appear in the map of dimensions, the dimensions info applies to all the dimension values except for those that have another DimenisonInfo instance configured for the specific value. Example: {"provider" : "Foo Inc"} where "provider" is a service specific dimension of a quota.

An object containing a list of "key": value pairs, for example: `{ "name": "wrench", "mass": "1.3kg", "count": "3" }`.
* `details` - The quota details for a map of dimensions.
* `applicable_locations` - The applicable regions or zones of this dimensions info. The field will be set to `['global']` for quotas that are not per region or per zone. Otherwise, it will be set to the list of locations this dimension info is applicable to.

Expand Down
62 changes: 62 additions & 0 deletions website/docs/d/cloud_quotas_quota_infos.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
---
subcategory: "Cloud Quotas"
---

# google\_cloud\_quotas\_quota\_infos

Provides information about all quotas for a given project, folder or organization.

## Example Usage

```hcl
data "google_cloud_quotas_quota_infos" "my_quota_infos" {
parent = "projects/my-project"
service = "compute.googleapis.com"
}
```

## Argument Reference

The following arguments are supported:

* `parent` - (Required) Parent value of QuotaInfo resources. Listing across different resource containers (such as 'projects/-') is not allowed. Allowed parents are "projects/[project-id / number]" or "folders/[folder-id / number]" or "organizations/[org-id / number].

* `service` - (Required) The name of the service in which the quotas are defined.


## Attributes Reference

The following attributes are exported:

* `quota_infos` - (Output) The list of QuotaInfo.

<a name="nested_quota_infos"></a> The `quota_infos` block supports:

* `name` - (Output) Resource name of this QuotaInfo, for example: `projects/123/locations/global/services/compute.googleapis.com/quotaInfos/CpusPerProjectPerRegion`.
* `metric` - (Output) The metric of the quota. It specifies the resources consumption the quota is defined for, for example: `compute.googleapis.com/cpus`.
* `is_precise` - (Output) Whether this is a precise quota. A precise quota is tracked with absolute precision. In contrast, an imprecise quota is not tracked with precision.
* `refresh_interval` - (Output) The reset time interval for the quota. Refresh interval applies to rate quota only. Example: "minute" for per minute, "day" for per day, or "10 seconds" for every 10 seconds.
* `container_type` - (Output) The container type of the QuotaInfo.
* `dimensions` - (Output) The dimensions the quota is defined on.
* `metric_display_name` - (Output) The display name of the quota metric.
* `quota_display_name` - (Output) The display name of the quota.
* `metric_unit` - (Output) The unit in which the metric value is reported, e.g., `MByte`.
* `quota_increase_eligibility` - (Output) Whether it is eligible to request a higher quota value for this quota.
* `is_fixed` - (Output) Whether the quota value is fixed or adjustable.
* `dimensions_infos` - (Output) The collection of dimensions info ordered by their dimensions from more specific ones to less specific ones.
* `is_concurrent` - (Output) Whether the quota is a concurrent quota. Concurrent quotas are enforced on the total number of concurrent operations in flight at any given time.
* `service_request_quota_uri` - (Output) URI to the page where users can request more quota for the cloud service, for example: `https://console.cloud.google.com/iam-admin/quotas`.

<a name="nested_quota_increase_eligibility"></a> The `quota_increase_eligibility` block supports:

* `is_eligible` - Whether a higher quota value can be requested for the quota.
* `ineligibility_reason` - The enumeration of reasons when it is ineligible to request increase adjustment.

<a name="nested_dimensions_infos"></a> The `dimensions_infos` block supports:
* `dimensions` - The map of dimensions for this dimensions info. The key of a map entry is "region", "zone" or the name of a service specific dimension, and the value of a map entry is the value of the dimension. If a dimension does not appear in the map of dimensions, the dimensions info applies to all the dimension values except for those that have another DimenisonInfo instance configured for the specific value. Example: {"provider" : "Foo Inc"} where "provider" is a service specific dimension of a quota.

* `details` - The quota details for a map of dimensions.
* `applicable_locations` - The applicable regions or zones of this dimensions info. The field will be set to `['global']` for quotas that are not per region or per zone. Otherwise, it will be set to the list of locations this dimension info is applicable to.

<a name="nested_details"></a> The `details` block supports:
* `value` - The value currently in effect and being enforced.

0 comments on commit c146e74

Please sign in to comment.