Skip to content

Commit 61c8433

Browse files
committed
feat(eps): add new data source to query eps quotas
1 parent 8adda6e commit 61c8433

File tree

4 files changed

+231
-2
lines changed

4 files changed

+231
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
subcategory: "Enterprise Project Management Service (EPS)"
3+
layout: "huaweicloud"
4+
page_title: "HuaweiCloud: huaweicloud_enterprise_project_quotas"
5+
description: |-
6+
Use this data source to get the list of quotas of EPS resources within HuaweiCloud.
7+
---
8+
9+
# huaweicloud_enterprise_project_quotas
10+
11+
Use this data source to get the list of quotas of EPS resources within HuaweiCloud.
12+
13+
## Example Usage
14+
15+
```hcl
16+
data "huaweicloud_enterprise_project_quotas" "test" {}
17+
```
18+
19+
## Argument Reference
20+
21+
The following arguments are supported:
22+
23+
* `region` - (Optional, String) Specifies the region in which to query the resource quotas.
24+
If omitted, the provider-level region will be used.
25+
26+
## Attribute Reference
27+
28+
In addition to all arguments above, the following attributes are exported:
29+
30+
* `id` - The data source ID.
31+
32+
* `quotas` - The list of the resource quotas.
33+
The [quotas](#eps_quotas) structure is documented below.
34+
35+
<a name="eps_quotas"></a>
36+
The `quotas` block supports:
37+
38+
* `quota` - The total number of the resource quota.
39+
40+
* `type` - The resource type corresponding to quota.
41+
42+
* `used` - The used quota number.

huaweicloud/provider.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -846,8 +846,9 @@ func Provider() *schema.Provider {
846846
"huaweicloud_eg_event_channels": eg.DataSourceEventChannels(),
847847
"huaweicloud_eg_event_sources": eg.DataSourceEventSources(),
848848

849-
"huaweicloud_enterprise_project": eps.DataSourceEnterpriseProject(),
850-
"huaweicloud_enterprise_projects": eps.DataSourceEnterpriseProjects(),
849+
"huaweicloud_enterprise_project": eps.DataSourceEnterpriseProject(),
850+
"huaweicloud_enterprise_projects": eps.DataSourceEnterpriseProjects(),
851+
"huaweicloud_enterprise_project_quotas": eps.DataSourceQuotas(),
851852

852853
"huaweicloud_er_associations": er.DataSourceAssociations(),
853854
"huaweicloud_er_attachments": er.DataSourceAttachments(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package eps
2+
3+
import (
4+
"regexp"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
9+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance"
10+
)
11+
12+
func TestAccDataEnterpriseProjectQuotas_basic(t *testing.T) {
13+
all := "data.huaweicloud_enterprise_project_quotas.test"
14+
dc := acceptance.InitDataSourceCheck(all)
15+
16+
resource.ParallelTest(t, resource.TestCase{
17+
PreCheck: func() { acceptance.TestAccPreCheck(t) },
18+
ProviderFactories: acceptance.TestAccProviderFactories,
19+
Steps: []resource.TestStep{
20+
{
21+
Config: testAccDataEnterpriseProjectQuotas_basic,
22+
Check: resource.ComposeTestCheckFunc(
23+
dc.CheckResourceExists(),
24+
resource.TestMatchResourceAttr(all, "quotas.#", regexp.MustCompile(`^[1-9]([0-9]*)?$`)),
25+
resource.TestCheckOutput("is_eps_quota_configured", "true"),
26+
resource.TestCheckOutput("is_type_configured", "true"),
27+
resource.TestCheckOutput("is_quota_used_number_configured", "true"),
28+
),
29+
},
30+
},
31+
})
32+
}
33+
34+
const testAccDataEnterpriseProjectQuotas_basic = `
35+
data "huaweicloud_enterprise_project_quotas" "test" {}
36+
37+
locals {
38+
enterprise_project_quota = try(element([for o in data.huaweicloud_enterprise_project_quotas.test.quotas: o if
39+
o.type == "enterprise_project"], 0), null)
40+
}
41+
42+
output "is_eps_quota_configured" {
43+
value = local.enterprise_project_quota != null
44+
}
45+
46+
output "is_total_quota_number_configured" {
47+
value = lookup(local.enterprise_project_quota, "quota") > 0
48+
}
49+
50+
output "is_type_configured" {
51+
value = lookup(local.enterprise_project_quota, "type") != ""
52+
}
53+
54+
output "is_quota_used_number_configured" {
55+
value = lookup(local.enterprise_project_quota, "used") > 0
56+
}
57+
`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package eps
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/go-multierror"
7+
"github.com/hashicorp/go-uuid"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
11+
"github.com/chnsz/golangsdk"
12+
13+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
14+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
15+
)
16+
17+
// @API EPS GET /v1.0/enterprise-projects/quotas
18+
func DataSourceQuotas() *schema.Resource {
19+
return &schema.Resource{
20+
ReadContext: dataSourceQuotasRead,
21+
22+
Schema: map[string]*schema.Schema{
23+
"region": {
24+
Type: schema.TypeString,
25+
Optional: true,
26+
Computed: true,
27+
Description: `The region in which to query the resource quotas.`,
28+
},
29+
30+
// Attributes.
31+
"quotas": {
32+
Type: schema.TypeList,
33+
Computed: true,
34+
Elem: &schema.Resource{
35+
Schema: map[string]*schema.Schema{
36+
"quota": {
37+
Type: schema.TypeInt,
38+
Computed: true,
39+
Description: `The total number of the resource quota.`,
40+
},
41+
"type": {
42+
Type: schema.TypeString,
43+
Computed: true,
44+
Description: `The resource type corresponding to quota.`,
45+
},
46+
"used": {
47+
Type: schema.TypeInt,
48+
Computed: true,
49+
Description: `The used quota number.`,
50+
},
51+
},
52+
},
53+
Description: `The list of the resource quotas.`,
54+
},
55+
},
56+
}
57+
}
58+
59+
func listResourceQuotas(client *golangsdk.ServiceClient, d *schema.ResourceData) ([]interface{}, error) {
60+
httpUrl := "v1.0/enterprise-projects/quotas"
61+
listPath := client.Endpoint + httpUrl
62+
63+
opt := golangsdk.RequestOpts{
64+
KeepResponseBody: true,
65+
MoreHeaders: map[string]string{
66+
"Content-Type": "application/json",
67+
},
68+
}
69+
70+
requestResp, err := client.Request("GET", listPath, &opt)
71+
if err != nil {
72+
return nil, err
73+
}
74+
respBody, err := utils.FlattenResponse(requestResp)
75+
if err != nil {
76+
return nil, err
77+
}
78+
79+
return utils.PathSearch("quotas.resources", respBody, make([]interface{}, 0)).([]interface{}), nil
80+
}
81+
82+
func flattenResourceQuotas(resources []interface{}) []map[string]interface{} {
83+
if len(resources) < 1 {
84+
return nil
85+
}
86+
87+
result := make([]map[string]interface{}, 0, len(resources))
88+
for _, resource := range resources {
89+
result = append(result, map[string]interface{}{
90+
"quota": utils.PathSearch("quota", resource, nil),
91+
"type": utils.PathSearch("type", resource, nil),
92+
"used": utils.PathSearch("used", resource, nil),
93+
})
94+
}
95+
96+
return result
97+
}
98+
99+
func dataSourceQuotasRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
100+
var (
101+
cfg = meta.(*config.Config)
102+
region = cfg.GetRegion(d)
103+
)
104+
client, err := cfg.NewServiceClient("eps", region)
105+
if err != nil {
106+
return diag.Errorf("error creating EPS client: %s", err)
107+
}
108+
109+
staticRoutes, err := listResourceQuotas(client, d)
110+
if err != nil {
111+
return diag.Errorf("error querying resource quotas: %s", err)
112+
}
113+
114+
randUUID, err := uuid.GenerateUUID()
115+
if err != nil {
116+
return diag.Errorf("unable to generate ID: %s", err)
117+
}
118+
d.SetId(randUUID)
119+
120+
mErr := multierror.Append(nil,
121+
d.Set("region", region),
122+
d.Set("quotas", flattenResourceQuotas(staticRoutes)),
123+
)
124+
125+
if mErr.ErrorOrNil() != nil {
126+
return diag.Errorf("error saving data source fields of the EPS resource quotas: %s", mErr)
127+
}
128+
return nil
129+
}

0 commit comments

Comments
 (0)