diff --git a/docs/data-sources/cfw_tags.md b/docs/data-sources/cfw_tags.md new file mode 100644 index 0000000000..c1434222ff --- /dev/null +++ b/docs/data-sources/cfw_tags.md @@ -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. + + +The `tags` block supports: + +* `key` - The tag key. + +* `values` - The tag values. diff --git a/huaweicloud/provider.go b/huaweicloud/provider.go index c1ec92f6a3..aaef6d0e3c 100644 --- a/huaweicloud/provider.go +++ b/huaweicloud/provider.go @@ -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(), diff --git a/huaweicloud/services/acceptance/cfw/data_source_huaweicloud_cfw_tags_test.go b/huaweicloud/services/acceptance/cfw/data_source_huaweicloud_cfw_tags_test.go new file mode 100644 index 0000000000..26a3581a5a --- /dev/null +++ b/huaweicloud/services/acceptance/cfw/data_source_huaweicloud_cfw_tags_test.go @@ -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" {}` diff --git a/huaweicloud/services/cfw/data_source_huaweicloud_cfw_tags.go b/huaweicloud/services/cfw/data_source_huaweicloud_cfw_tags.go new file mode 100644 index 0000000000..936773f9bf --- /dev/null +++ b/huaweicloud/services/cfw/data_source_huaweicloud_cfw_tags.go @@ -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() +}