Skip to content

Commit 2395ac3

Browse files
feat(cbh/instance_quota): support instance quota dataSource
1 parent b52b574 commit 2395ac3

File tree

4 files changed

+159
-0
lines changed

4 files changed

+159
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
subcategory: "Cloud Bastion Host (CBH)"
3+
layout: "huaweicloud"
4+
page_title: "HuaweiCloud: huaweicloud_cbh_instance_quota"
5+
description: |-
6+
Use this data source to get CBH instance quota within HuaweiCloud.
7+
---
8+
9+
# huaweicloud_cbh_instance_quota
10+
11+
Use this data source to get CBH instance quota within HuaweiCloud.
12+
13+
## Example Usage
14+
15+
```hcl
16+
data "huaweicloud_cbh_instance_quota" "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 CBH instance quota.
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 in UUID format.
31+
32+
* `quota` - The maximum number of CBH instances that can be created.
33+
34+
* `quota_used` - The current number of CBH instances created.

huaweicloud/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,7 @@ func Provider() *schema.Provider {
491491
"huaweicloud_cbh_instances": cbh.DataSourceCbhInstances(),
492492
"huaweicloud_cbh_flavors": cbh.DataSourceCbhFlavors(),
493493
"huaweicloud_cbh_availability_zones": cbh.DataSourceAvailabilityZones(),
494+
"huaweicloud_cbh_instance_quota": cbh.DataSourceInstanceQuota(),
494495

495496
"huaweicloud_cc_authorizations": cc.DataSourceCcAuthorizations(),
496497
"huaweicloud_cc_bandwidth_packages": cc.DataSourceCcBandwidthPackages(),
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package cbh
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
8+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance"
9+
)
10+
11+
func TestAccDataSourceInstanceQuota_basic(t *testing.T) {
12+
var (
13+
dataSourceName = "data.huaweicloud_cbh_instance_quota.test"
14+
dc = acceptance.InitDataSourceCheck(dataSourceName)
15+
)
16+
17+
resource.ParallelTest(t, resource.TestCase{
18+
PreCheck: func() {
19+
acceptance.TestAccPreCheck(t)
20+
},
21+
ProviderFactories: acceptance.TestAccProviderFactories,
22+
Steps: []resource.TestStep{
23+
{
24+
Config: testAccDataSourceInstanceQuota_basic,
25+
Check: resource.ComposeTestCheckFunc(
26+
dc.CheckResourceExists(),
27+
resource.TestCheckResourceAttrSet(dataSourceName, "quota"),
28+
resource.TestCheckResourceAttrSet(dataSourceName, "quota_used"),
29+
),
30+
},
31+
},
32+
})
33+
}
34+
35+
const testAccDataSourceInstanceQuota_basic string = `
36+
data "huaweicloud_cbh_instance_quota" "test" {}
37+
`
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package cbh
2+
3+
import (
4+
"context"
5+
"strings"
6+
7+
"github.com/hashicorp/go-multierror"
8+
"github.com/hashicorp/go-uuid"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11+
12+
"github.com/chnsz/golangsdk"
13+
14+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
15+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
16+
)
17+
18+
// @API CBH GET /v2/{project_id}/cbs/instance/quota
19+
func DataSourceInstanceQuota() *schema.Resource {
20+
return &schema.Resource{
21+
ReadContext: dataSourceInstanceQuotaRead,
22+
23+
Schema: map[string]*schema.Schema{
24+
"region": {
25+
Type: schema.TypeString,
26+
Optional: true,
27+
Computed: true,
28+
},
29+
"quota": {
30+
Type: schema.TypeInt,
31+
Computed: true,
32+
},
33+
"quota_used": {
34+
Type: schema.TypeInt,
35+
Computed: true,
36+
},
37+
},
38+
}
39+
}
40+
41+
func dataSourceInstanceQuotaRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
42+
var (
43+
cfg = meta.(*config.Config)
44+
region = cfg.GetRegion(d)
45+
mErr *multierror.Error
46+
getHttpUrl = "v2/{project_id}/cbs/instance/quota"
47+
product = "cbh"
48+
)
49+
50+
client, err := cfg.NewServiceClient(product, region)
51+
if err != nil {
52+
return diag.Errorf("error creating CBH client: %s", err)
53+
}
54+
55+
getPath := client.Endpoint + getHttpUrl
56+
getPath = strings.ReplaceAll(getPath, "{project_id}", client.ProjectID)
57+
getOpt := golangsdk.RequestOpts{
58+
KeepResponseBody: true,
59+
MoreHeaders: map[string]string{"Content-Type": "application/json"},
60+
}
61+
62+
getResp, err := client.Request("GET", getPath, &getOpt)
63+
if err != nil {
64+
return diag.Errorf("error retrieving CBH instance quota, %s", err)
65+
}
66+
67+
getRespBody, err := utils.FlattenResponse(getResp)
68+
if err != nil {
69+
return diag.FromErr(err)
70+
}
71+
72+
dataSourceId, err := uuid.GenerateUUID()
73+
if err != nil {
74+
return diag.Errorf("unable to generate ID: %s", err)
75+
}
76+
77+
d.SetId(dataSourceId)
78+
79+
mErr = multierror.Append(
80+
mErr,
81+
d.Set("region", region),
82+
d.Set("quota", utils.PathSearch("quota", getRespBody, nil)),
83+
d.Set("quota_used", utils.PathSearch("quota_used", getRespBody, nil)),
84+
)
85+
86+
return diag.FromErr(mErr.ErrorOrNil())
87+
}

0 commit comments

Comments
 (0)