-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dds): support to get dds quotas
- Loading branch information
Showing
4 changed files
with
190 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
--- | ||
subcategory: "Document Database Service (DDS)" | ||
layout: "huaweicloud" | ||
page_title: "HuaweiCloud: huaweicloud_dds_quotas" | ||
description: |- | ||
Use this data source to get the list of DDS quotas. | ||
--- | ||
|
||
# huaweicloud_dds_quotas | ||
|
||
Use this data source to get the list of DDS quotas. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
data "huaweicloud_dds_quotas" "test" {} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `region` - (Optional, String) Specifies the region in which to query the resource. 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` - Indicates the quotas information. | ||
The [quotas](#attrblock--quotas) structure is documented below. | ||
|
||
<a name="attrblock--quotas"></a> | ||
The `quotas` block supports: | ||
|
||
* `mode` - Indicates the instance type. | ||
|
||
* `quota` - Indicates the existing quota. | ||
|
||
* `type` - Indicates the quota resource type. | ||
|
||
* `used` - Indicates the number of the used instances. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
huaweicloud/services/acceptance/dds/data_source_huaweicloud_dds_quotas_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package dds | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
|
||
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance" | ||
) | ||
|
||
func TestAccDataSourceDdsQuotas_basic(t *testing.T) { | ||
dataSource := "data.huaweicloud_dds_quotas.test" | ||
dc := acceptance.InitDataSourceCheck(dataSource) | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { | ||
acceptance.TestAccPreCheck(t) | ||
}, | ||
ProviderFactories: acceptance.TestAccProviderFactories, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testDataSourceDdsQuotas_basic, | ||
Check: resource.ComposeTestCheckFunc( | ||
dc.CheckResourceExists(), | ||
resource.TestCheckResourceAttrSet(dataSource, "quotas.#"), | ||
resource.TestCheckResourceAttrSet(dataSource, "quotas.0.type"), | ||
resource.TestCheckResourceAttrSet(dataSource, "quotas.0.used"), | ||
resource.TestCheckResourceAttrSet(dataSource, "quotas.0.mode"), | ||
resource.TestCheckResourceAttrSet(dataSource, "quotas.0.quota"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
const testDataSourceDdsQuotas_basic string = `data "huaweicloud_dds_quotas" "test" {}` |
110 changes: 110 additions & 0 deletions
110
huaweicloud/services/dds/data_source_huaweicloud_dds_quotas.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package dds | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
|
||
"github.com/chnsz/golangsdk" | ||
Check failure on line 7 in huaweicloud/services/dds/data_source_huaweicloud_dds_quotas.go GitHub Actions / golangci
|
||
"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" | ||
Check failure on line 11 in huaweicloud/services/dds/data_source_huaweicloud_dds_quotas.go GitHub Actions / golangci
|
||
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config" | ||
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils" | ||
) | ||
|
||
// @API DDS GET /v3/{project_id}/quotas | ||
func DataSourceDdsQuotas() *schema.Resource { | ||
return &schema.Resource{ | ||
ReadContext: dataSourceDdsQuotasRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"region": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
Description: `Specifies the region in which to query the resource. If omitted, the provider-level region will be used.`, | ||
}, | ||
"quotas": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Description: `Indicates the quotas information.`, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"used": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
Description: `Indicates the number of the used instances.`, | ||
}, | ||
"type": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: `Indicates the quota resource type.`, | ||
}, | ||
"mode": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: `Indicates the instance type.`, | ||
}, | ||
"quota": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
Description: `Indicates the existing quota.`, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceDdsQuotasRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
cfg := meta.(*config.Config) | ||
region := cfg.GetRegion(d) | ||
client, err := cfg.NewServiceClient("dds", region) | ||
if err != nil { | ||
return diag.Errorf("error creating DDS client: %s", err) | ||
} | ||
|
||
httpUrl := "v3/{project_id}/quotas" | ||
getPath := client.Endpoint + httpUrl | ||
getPath = strings.ReplaceAll(getPath, "{project_id}", client.ProjectID) | ||
getOpt := golangsdk.RequestOpts{ | ||
KeepResponseBody: true, | ||
MoreHeaders: map[string]string{ | ||
"Content-Type": "application/json", | ||
}, | ||
} | ||
|
||
getResp, err := client.Request("GET", getPath, &getOpt) | ||
if err != nil { | ||
return diag.Errorf("error retrieving DDS quotas: %s", err) | ||
} | ||
getRespBody, err := utils.FlattenResponse(getResp) | ||
if err != nil { | ||
return diag.Errorf("error flattening response: %s", err) | ||
} | ||
|
||
resources := utils.PathSearch("quotas.resources", getRespBody, make([]interface{}, 0)).([]interface{}) | ||
rst := make([]map[string]interface{}, 0, len(resources)) | ||
for _, resource := range resources { | ||
rst = append(rst, map[string]interface{}{ | ||
"type": utils.PathSearch("type", resource, nil), | ||
"mode": utils.PathSearch("mode", resource, nil), | ||
"quota": utils.PathSearch("quota", resource, nil), | ||
"used": utils.PathSearch("used", resource, nil), | ||
}) | ||
} | ||
|
||
id, err := uuid.GenerateUUID() | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
d.SetId(id) | ||
|
||
mErr := multierror.Append(nil, | ||
d.Set("region", region), | ||
d.Set("quotas", rst), | ||
) | ||
return diag.FromErr(mErr.ErrorOrNil()) | ||
} |