From 9dd8fdd2eb4eb6057005d9cb6f9ba3a9959eed94 Mon Sep 17 00:00:00 2001 From: Zippo-Wang <852420284@qq.com> Date: Thu, 21 Aug 2025 19:04:31 +0800 Subject: [PATCH] feat(dms): add new data source to get tags --- docs/data-sources/dms_rocketmq_tags.md | 40 +++++++ huaweicloud/provider.go | 1 + ...urce_huaweicloud_dms_rocketmq_tags_test.go | 55 +++++++++ ...ta_source_huaweicloud_dms_rocketmq_tags.go | 113 ++++++++++++++++++ 4 files changed, 209 insertions(+) create mode 100644 docs/data-sources/dms_rocketmq_tags.md create mode 100644 huaweicloud/services/acceptance/rocketmq/data_source_huaweicloud_dms_rocketmq_tags_test.go create mode 100644 huaweicloud/services/rocketmq/data_source_huaweicloud_dms_rocketmq_tags.go diff --git a/docs/data-sources/dms_rocketmq_tags.md b/docs/data-sources/dms_rocketmq_tags.md new file mode 100644 index 00000000000..90bc982bf4e --- /dev/null +++ b/docs/data-sources/dms_rocketmq_tags.md @@ -0,0 +1,40 @@ +--- +subcategory: "Distributed Message Service (DMS)" +layout: "huaweicloud" +page_title: "HuaweiCloud: huaweicloud_dms_rocketmq_tags" +description: |- + Use this data source to get the list of DMS RocketMQ tags within HuaweiCloud. +--- + +# huaweicloud_dms_rocketmq_tags + +Use this data source to get the list of DMS RocketMQ tags within HuaweiCloud. + +## Example Usage + +```hcl +data "huaweicloud_dms_rocketmq_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 list of tags. + The [tags](#dms_rocketmq_tags_attr) structure is documented below. + + +The `tags` block supports: + +* `key` - The tag key. + +* `values` - The list of tag values. diff --git a/huaweicloud/provider.go b/huaweicloud/provider.go index 17bd49abb84..4f849612fc2 100644 --- a/huaweicloud/provider.go +++ b/huaweicloud/provider.go @@ -958,6 +958,7 @@ func Provider() *schema.Provider { "huaweicloud_dms_rocketmq_message_traces": rocketmq.DataSourceDmsRocketmqMessageTraces(), "huaweicloud_dms_rocketmq_extend_flavors": rocketmq.DataSourceDmsRocketmqExtendFlavors(), "huaweicloud_dms_rocketmq_messages": rocketmq.DataSourceDmsRocketMQMessages(), + "huaweicloud_dms_rocketmq_tags": rocketmq.DataSourceTags(), "huaweicloud_dns_custom_lines": dns.DataSourceCustomLines(), "huaweicloud_dns_floating_ptrrecords": dns.DataSourceFloatingPtrRecords(), diff --git a/huaweicloud/services/acceptance/rocketmq/data_source_huaweicloud_dms_rocketmq_tags_test.go b/huaweicloud/services/acceptance/rocketmq/data_source_huaweicloud_dms_rocketmq_tags_test.go new file mode 100644 index 00000000000..b183d502096 --- /dev/null +++ b/huaweicloud/services/acceptance/rocketmq/data_source_huaweicloud_dms_rocketmq_tags_test.go @@ -0,0 +1,55 @@ +package rocketmq + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance" +) + +func TestAccTags_basic(t *testing.T) { + var ( + dataSourceName = "data.huaweicloud_dms_rocketmq_tags.test" + dc = acceptance.InitDataSourceCheck(dataSourceName) + ) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acceptance.TestAccPreCheck(t) }, + ProviderFactories: acceptance.TestAccProviderFactories, + Steps: []resource.TestStep{ + { + Config: testAccTags_basic(), + Check: resource.ComposeTestCheckFunc( + dc.CheckResourceExists(), + resource.TestCheckResourceAttrSet(dataSourceName, "tags.#"), + resource.TestCheckOutput("is_tags_exist", "true"), + resource.TestCheckOutput("is_key_set", "true"), + resource.TestCheckOutput("is_values_set", "true"), + ), + }, + }, + }) +} + +func testAccTags_basic() string { + return ` +data "huaweicloud_dms_rocketmq_tags" "test" {} + +locals { + tags = data.huaweicloud_dms_rocketmq_tags.test.tags +} + +output "is_tags_exist" { + value = length(local.tags) >= 0 +} + +output "is_key_set" { + value = length(local.tags) > 0 ? alltrue([for tag in local.tags : tag.key != ""]) : true +} + +output "is_values_set" { + value = length(local.tags) > 0 ? alltrue([for tag in local.tags : length(tag.values) >= 0]) : true +} +` +} diff --git a/huaweicloud/services/rocketmq/data_source_huaweicloud_dms_rocketmq_tags.go b/huaweicloud/services/rocketmq/data_source_huaweicloud_dms_rocketmq_tags.go new file mode 100644 index 00000000000..565684f4f42 --- /dev/null +++ b/huaweicloud/services/rocketmq/data_source_huaweicloud_dms_rocketmq_tags.go @@ -0,0 +1,113 @@ +package rocketmq + +import ( + "context" + "strings" + + "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/chnsz/golangsdk" + + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config" + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils" +) + +// @API RocketMQ GET /v2/{project_id}/rocketmq/tags +func DataSourceTags() *schema.Resource { + return &schema.Resource{ + ReadContext: dataSourceTagsRead, + Schema: map[string]*schema.Schema{ + "region": { + Type: schema.TypeString, + Optional: true, + Computed: true, + Description: `The region in which to query the resource.`, + }, + + // Attributes + "tags": { + Type: schema.TypeList, + Computed: true, + Description: `The list of tags.`, + 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 list of tag values.`, + }, + }, + }, + }, + }, + } +} + +func dataSourceTagsRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + cfg := meta.(*config.Config) + region := cfg.GetRegion(d) + client, err := cfg.NewServiceClient("dmsv2", region) + if err != nil { + return diag.Errorf("error creating DMS RocketMQ client: %s", err) + } + + getTagsHttpUrl := "v2/{project_id}/rocketmq/tags" + getTagsPath := client.Endpoint + getTagsHttpUrl + getTagsPath = strings.ReplaceAll(getTagsPath, "{project_id}", client.ProjectID) + + getTagsOpt := golangsdk.RequestOpts{ + KeepResponseBody: true, + MoreHeaders: map[string]string{ + "Content-Type": "application/json", + }, + } + + getTagsResp, err := client.Request("GET", getTagsPath, &getTagsOpt) + if err != nil { + return diag.Errorf("error retrieving DMS RocketMQ tags: %s", err) + } + + getTagsRespBody, err := utils.FlattenResponse(getTagsResp) + if err != nil { + return diag.FromErr(err) + } + + randUUID, err := uuid.GenerateUUID() + if err != nil { + return diag.Errorf("unable to generate ID: %s", err) + } + d.SetId(randUUID) + + mErr := multierror.Append(nil, + d.Set("region", region), + d.Set("tags", flattenTags( + utils.PathSearch("tags", getTagsRespBody, make([]interface{}, 0)).([]interface{}))), + ) + + return diag.FromErr(mErr.ErrorOrNil()) +} + +func flattenTags(tags []interface{}) []interface{} { + if len(tags) == 0 { + return nil + } + + rst := make([]interface{}, 0, len(tags)) + for _, v := range tags { + rst = append(rst, map[string]interface{}{ + "key": utils.PathSearch("key", v, nil), + "values": utils.PathSearch("values", v, nil), + }) + } + + return rst +}