Skip to content
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

feat(cts): add data source to get the list of CTS quotas #6054

Merged
merged 1 commit into from
Dec 19, 2024
Merged
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
43 changes: 43 additions & 0 deletions docs/data-sources/cts_quotas.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
subcategory: "Cloud Trace Service (CTS)"
layout: "huaweicloud"
page_title: "HuaweiCloud: huaweicloud_cts_quotas"
description: |-
Use this data source to get the list of CTS quotas.
---

# huaweicloud_cts_quotas

Use this data source to get the list of CTS quotas.

## Example Usage

```hcl
data "huaweicloud_cts_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.

* `resources` - The quota information.

The [resources](#resources_struct) structure is documented below.

<a name="resources_struct"></a>
The `resources` block supports:

* `used` - The number of used resources.

* `quota` - The total number of resources.

* `type` - The resource type.
1 change: 1 addition & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,7 @@ func Provider() *schema.Provider {
"huaweicloud_cts_notifications": cts.DataSourceNotifications(),
"huaweicloud_cts_traces": cts.DataSourceCtsTraces(),
"huaweicloud_cts_trackers": cts.DataSourceCtsTrackers(),
"huaweicloud_cts_quotas": cts.DataSourceCtsQuotas(),

"huaweicloud_cdm_clusters": cdm.DataSourceCdmClusters(),
"huaweicloud_cdm_flavors": cdm.DataSourceCdmFlavors(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package cts

import (
"testing"

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

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

func TestAccDataSourceCtsQuotas_basic(t *testing.T) {
dataSource := "data.huaweicloud_cts_quotas.test"
dc := acceptance.InitDataSourceCheck(dataSource)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acceptance.TestAccPreCheck(t)
},
ProviderFactories: acceptance.TestAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testDataSourceCtsQuotas_basic,
Check: resource.ComposeTestCheckFunc(
dc.CheckResourceExists(),
resource.TestCheckResourceAttrSet(dataSource, "resources.0.type"),
resource.TestCheckResourceAttrSet(dataSource, "resources.0.quota"),
resource.TestCheckResourceAttrSet(dataSource, "resources.0.used"),
),
},
},
})
}

const testDataSourceCtsQuotas_basic = `data "huaweicloud_cts_quotas" "test" {}`
120 changes: 120 additions & 0 deletions huaweicloud/services/cts/data_source_huaweicloud_cts_quotas.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// Generated by PMS #481
package cts

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/tidwall/gjson"

"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/httphelper"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/helper/schemas"
)

func DataSourceCtsQuotas() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceCtsQuotasRead,

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.`,
},
"resources": {
Type: schema.TypeList,
Computed: true,
Description: `The quota information.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"used": {
Type: schema.TypeInt,
Computed: true,
Description: `The number of used resources.`,
},
"quota": {
Type: schema.TypeInt,
Computed: true,
Description: `The total number of resources.`,
},
"type": {
Type: schema.TypeString,
Computed: true,
Description: `The resource type.`,
},
},
},
},
},
}
}

type QuotasDSWrapper struct {
*schemas.ResourceDataWrapper
Config *config.Config
}

func newQuotasDSWrapper(d *schema.ResourceData, meta interface{}) *QuotasDSWrapper {
return &QuotasDSWrapper{
ResourceDataWrapper: schemas.NewSchemaWrapper(d),
Config: meta.(*config.Config),
}
}

func dataSourceCtsQuotasRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
wrapper := newQuotasDSWrapper(d, meta)
listQuotasRst, err := wrapper.ListQuotas()
if err != nil {
return diag.FromErr(err)
}

id, err := uuid.GenerateUUID()
if err != nil {
return diag.FromErr(err)
}
d.SetId(id)

err = wrapper.listQuotasToSchema(listQuotasRst)
if err != nil {
return diag.FromErr(err)
}

return nil
}

// @API CTS GET /v3/{project_id}/quotas
func (w *QuotasDSWrapper) ListQuotas() (*gjson.Result, error) {
client, err := w.NewClient(w.Config, "cts")
if err != nil {
return nil, err
}

uri := "/v3/{project_id}/quotas"
return httphelper.New(client).
Method("GET").
URI(uri).
Request().
Result()
}

func (w *QuotasDSWrapper) listQuotasToSchema(body *gjson.Result) error {
d := w.ResourceData
mErr := multierror.Append(nil,
d.Set("region", w.Config.GetRegion(w.ResourceData)),
d.Set("resources", schemas.SliceToList(body.Get("resources"),
func(resources gjson.Result) any {
return map[string]any{
"used": resources.Get("used").Value(),
"quota": resources.Get("quota").Value(),
"type": resources.Get("type").Value(),
}
},
)),
)
return mErr.ErrorOrNil()
}
Loading