Skip to content

feat(eps): add new data source to query eps quotas #6732

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions docs/data-sources/enterprise_project_quotas.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
subcategory: "Enterprise Project Management Service (EPS)"
layout: "huaweicloud"
page_title: "HuaweiCloud: huaweicloud_enterprise_project_quotas"
description: |-
Use this data source to get the list of quotas of EPS resources within HuaweiCloud.
---

# huaweicloud_enterprise_project_quotas

Use this data source to get the list of quotas of EPS resources within HuaweiCloud.

## Example Usage

```hcl
data "huaweicloud_enterprise_project_quotas" "test" {}
```

## Argument Reference

The following arguments are supported:

* `region` - (Optional, String) Specifies the region in which to query the resource quotas.
If omitted, the provider-level region will be used.

## Attribute Reference

In addition to all arguments above, the following attributes are exported:

* `id` - The data source ID.

* `quotas` - The list of the resource quotas.
The [quotas](#eps_quotas) structure is documented below.

<a name="eps_quotas"></a>
The `quotas` block supports:

* `quota` - The total number of the resource quota.

* `type` - The resource type corresponding to quota.

* `used` - The used quota number.
5 changes: 3 additions & 2 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -846,8 +846,9 @@ func Provider() *schema.Provider {
"huaweicloud_eg_event_channels": eg.DataSourceEventChannels(),
"huaweicloud_eg_event_sources": eg.DataSourceEventSources(),

"huaweicloud_enterprise_project": eps.DataSourceEnterpriseProject(),
"huaweicloud_enterprise_projects": eps.DataSourceEnterpriseProjects(),
"huaweicloud_enterprise_project": eps.DataSourceEnterpriseProject(),
"huaweicloud_enterprise_projects": eps.DataSourceEnterpriseProjects(),
"huaweicloud_enterprise_project_quotas": eps.DataSourceQuotas(),

"huaweicloud_er_associations": er.DataSourceAssociations(),
"huaweicloud_er_attachments": er.DataSourceAttachments(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package eps

import (
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"

"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance"
)

func TestAccDataEnterpriseProjectQuotas_basic(t *testing.T) {
all := "data.huaweicloud_enterprise_project_quotas.test"
dc := acceptance.InitDataSourceCheck(all)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.TestAccPreCheck(t) },
ProviderFactories: acceptance.TestAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccDataEnterpriseProjectQuotas_basic,
Check: resource.ComposeTestCheckFunc(
dc.CheckResourceExists(),
resource.TestMatchResourceAttr(all, "quotas.#", regexp.MustCompile(`^[1-9]([0-9]*)?$`)),
resource.TestCheckOutput("is_eps_quota_configured", "true"),
resource.TestCheckOutput("is_type_configured", "true"),
resource.TestCheckOutput("is_quota_used_number_configured", "true"),
),
},
},
})
}

const testAccDataEnterpriseProjectQuotas_basic = `
data "huaweicloud_enterprise_project_quotas" "test" {}

locals {
enterprise_project_quota = try(element([for o in data.huaweicloud_enterprise_project_quotas.test.quotas: o if
o.type == "enterprise_project"], 0), null)
}

output "is_eps_quota_configured" {
value = local.enterprise_project_quota != null
}

output "is_total_quota_number_configured" {
value = lookup(local.enterprise_project_quota, "quota") > 0
}

output "is_type_configured" {
value = lookup(local.enterprise_project_quota, "type") != ""
}

output "is_quota_used_number_configured" {
value = lookup(local.enterprise_project_quota, "used") > 0
}
`
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package eps

import (
"context"

"github.com/hashicorp/go-multierror"
"github.com/hashicorp/go-uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/chnsz/golangsdk"

"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
)

// @API EPS GET /v1.0/enterprise-projects/quotas
func DataSourceQuotas() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceQuotasRead,

Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: `The region in which to query the resource quotas.`,
},

// Attributes.
"quotas": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"quota": {
Type: schema.TypeInt,
Computed: true,
Description: `The total number of the resource quota.`,
},
"type": {
Type: schema.TypeString,
Computed: true,
Description: `The resource type corresponding to quota.`,
},
"used": {
Type: schema.TypeInt,
Computed: true,
Description: `The used quota number.`,
},
},
},
Description: `The list of the resource quotas.`,
},
},
}
}

func listResourceQuotas(client *golangsdk.ServiceClient) ([]interface{}, error) {
httpUrl := "v1.0/enterprise-projects/quotas"
listPath := client.Endpoint + httpUrl

opt := golangsdk.RequestOpts{
KeepResponseBody: true,
MoreHeaders: map[string]string{
"Content-Type": "application/json",
},
}

requestResp, err := client.Request("GET", listPath, &opt)
if err != nil {
return nil, err
}
respBody, err := utils.FlattenResponse(requestResp)
if err != nil {
return nil, err
}

return utils.PathSearch("quotas.resources", respBody, make([]interface{}, 0)).([]interface{}), nil
}

func flattenResourceQuotas(resources []interface{}) []map[string]interface{} {
if len(resources) < 1 {
return nil
}

result := make([]map[string]interface{}, 0, len(resources))
for _, resource := range resources {
result = append(result, map[string]interface{}{
"quota": utils.PathSearch("quota", resource, nil),
"type": utils.PathSearch("type", resource, nil),
"used": utils.PathSearch("used", resource, nil),
})
}

return result
}

func dataSourceQuotasRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var (
cfg = meta.(*config.Config)
region = cfg.GetRegion(d)
)
client, err := cfg.NewServiceClient("eps", region)
if err != nil {
return diag.Errorf("error creating EPS client: %s", err)
}

staticRoutes, err := listResourceQuotas(client)
if err != nil {
return diag.Errorf("error querying resource quotas: %s", err)
}

randUUID, err := uuid.GenerateUUID()
if err != nil {
return diag.Errorf("unable to generate ID: %s", err)
}
d.SetId(randUUID)

mErr := multierror.Append(nil,
d.Set("region", region),
d.Set("quotas", flattenResourceQuotas(staticRoutes)),
)

if mErr.ErrorOrNil() != nil {
return diag.Errorf("error saving data source fields of the EPS resource quotas: %s", mErr)
}
return nil
}
Loading