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(cfw): add this data source to get the list of CFW tags #6160

Merged
merged 1 commit into from
Jan 9, 2025
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
41 changes: 41 additions & 0 deletions docs/data-sources/cfw_tags.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
subcategory: "Cloud Firewall (CFW)"
layout: "huaweicloud"
page_title: "HuaweiCloud: huaweicloud_cfw_tags"
description: |-
Use this data source to get the list of CFW tags.
---

# huaweicloud_cfw_tags

Use this data source to get the list of CFW tags.

## Example Usage

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

* `tags` - The tag list.

The [tags](#tags_struct) structure is documented below.

<a name="tags_struct"></a>
The `tags` block supports:

* `key` - The tag key.

* `values` - The tag values.
1 change: 1 addition & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,7 @@ func Provider() *schema.Provider {
"huaweicloud_cfw_flow_logs": cfw.DataSourceCfwFlowLogs(),
"huaweicloud_cfw_regions": cfw.DataSourceCfwRegions(),
"huaweicloud_cfw_ips_rules": cfw.DataSourceCfwIpsRules(),
"huaweicloud_cfw_tags": cfw.DataSourceCfwTags(),

"huaweicloud_cnad_advanced_instances": cnad.DataSourceInstances(),
"huaweicloud_cnad_advanced_available_objects": cnad.DataSourceAvailableProtectedObjects(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package cfw

import (
"testing"

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

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

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

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acceptance.TestAccPreCheck(t)
acceptance.TestAccPreCheckCfw(t)
},
ProviderFactories: acceptance.TestAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testDataSourceDataSourceCfwTags_basic,
Check: resource.ComposeTestCheckFunc(
dc.CheckResourceExists(),
resource.TestCheckResourceAttrSet(dataSource, "tags.0.key"),
resource.TestCheckResourceAttrSet(dataSource, "tags.0.values.#"),
),
},
},
})
}

const testDataSourceDataSourceCfwTags_basic = `data "huaweicloud_cfw_tags" "test" {}`
116 changes: 116 additions & 0 deletions huaweicloud/services/cfw/data_source_huaweicloud_cfw_tags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// Generated by PMS #514
package cfw

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 DataSourceCfwTags() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceCfwTagsRead,

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.`,
},
"tags": {
Type: schema.TypeList,
Computed: true,
Description: `The tag list.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"key": {
Type: schema.TypeString,
Computed: true,
Description: `The tag key.`,
},
"values": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: `The tag values.`,
},
},
},
},
},
}
}

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

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

func dataSourceCfwTagsRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
wrapper := newTagsDSWrapper(d, meta)
listProjectTagsRst, err := wrapper.ListProjectTags()
if err != nil {
return diag.FromErr(err)
}

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

err = wrapper.listProjectTagsToSchema(listProjectTagsRst)
if err != nil {
return diag.FromErr(err)
}

return nil
}

// @API CFW GET /v2/{project_id}/cfw-cfw/tags
func (w *TagsDSWrapper) ListProjectTags() (*gjson.Result, error) {
client, err := w.NewClient(w.Config, "cfw")
if err != nil {
return nil, err
}

uri := "/v2/{project_id}/cfw-cfw/tags"
return httphelper.New(client).
Method("GET").
URI(uri).
OffsetPager("tags", "offset", "limit", 1000).
Request().
Result()
}

func (w *TagsDSWrapper) listProjectTagsToSchema(body *gjson.Result) error {
d := w.ResourceData
mErr := multierror.Append(nil,
d.Set("region", w.Config.GetRegion(w.ResourceData)),
d.Set("tags", schemas.SliceToList(body.Get("tags"),
func(tags gjson.Result) any {
return map[string]any{
"key": tags.Get("key").Value(),
"values": schemas.SliceToStrList(tags.Get("values")),
}
},
)),
)
return mErr.ErrorOrNil()
}
Loading