Skip to content

Commit

Permalink
feat(dds): support to get dds quotas
Browse files Browse the repository at this point in the history
  • Loading branch information
saf3dfsa committed Nov 18, 2024
1 parent 854990e commit 80f50cb
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 0 deletions.
43 changes: 43 additions & 0 deletions docs/data-sources/dds_quotas.md
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.

Check failure on line 23 in docs/data-sources/dds_quotas.md

View workflow job for this annotation

GitHub Actions / markdownlint

Line length [Expected: 120; Actual: 136]

## 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.
1 change: 1 addition & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,7 @@ func Provider() *schema.Provider {
"huaweicloud_dcs_accounts": dcs.DataSourceDcsAccounts(),
"huaweicloud_dcs_diagnosis_tasks": dcs.DataSourceDcsDiagnosisTasks(),

"huaweicloud_dds_quotas": dds.DataSourceDdsQuotas(),
"huaweicloud_dds_audit_logs": dds.DataSourceDdsAuditLogs(),
"huaweicloud_dds_audit_log_links": dds.DataSourceDdsAuditLogLinks(),
"huaweicloud_dds_database_versions": dds.DataSourceDdsDatabaseVersions(),
Expand Down
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 huaweicloud/services/dds/data_source_huaweicloud_dds_quotas.go
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

View workflow job for this annotation

GitHub Actions / golangci

File is not `gci`-ed with -s standard -s default -s prefix(github.com/chnsz/golangsdk) -s prefix(github.com/huaweicloud/huaweicloud-sdk-go-v3) -s prefix(github.com/huaweicloud/terraform-provider-huaweicloud) -s blank -s dot --custom-order (gci)
"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

View workflow job for this annotation

GitHub Actions / golangci

File is not `gci`-ed with -s standard -s default -s prefix(github.com/chnsz/golangsdk) -s prefix(github.com/huaweicloud/huaweicloud-sdk-go-v3) -s prefix(github.com/huaweicloud/terraform-provider-huaweicloud) -s blank -s dot --custom-order (gci)
"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())
}

0 comments on commit 80f50cb

Please sign in to comment.