Skip to content

Commit 81f35b8

Browse files
committed
feat(dms): add new data source to get tags
1 parent c589c79 commit 81f35b8

File tree

4 files changed

+227
-0
lines changed

4 files changed

+227
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
subcategory: "Distributed Message Service (DMS)"
3+
layout: "huaweicloud"
4+
page_title: "HuaweiCloud: huaweicloud_dms_rocketmq_tags"
5+
description: |-
6+
Use this data source to get the list of RocketMQ tags within HuaweiCloud.
7+
---
8+
9+
# huaweicloud_dms_rocketmq_tags
10+
11+
Use this data source to get the list of RocketMQ tags within HuaweiCloud.
12+
13+
## Example Usage
14+
15+
```hcl
16+
data "huaweicloud_dms_rocketmq_tags" "test" {}
17+
```
18+
19+
## Argument Reference
20+
21+
The following arguments are supported:
22+
23+
* `region` - (Optional, String) Specifies the region in which to query the data source.
24+
If omitted, the provider-level region will be used.
25+
26+
## Attribute Reference
27+
28+
In addition to all arguments above, the following attributes are exported:
29+
30+
* `id` - The data source ID.
31+
32+
* `next_offset` - Indicates the offset for the next page of results.
33+
34+
* `previous_offset` - Indicates the offset for the previous page of results.
35+
36+
* `tags` - Indicates the list of tags.
37+
The [tags](#dms_rocketmq_tags) structure is documented below.
38+
39+
<a name="dms_rocketmq_tags"></a>
40+
The `tags` block supports:
41+
42+
* `key` - Indicates the tag key.
43+
44+
* `values` - Indicates the list of tag values.

huaweicloud/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,7 @@ func Provider() *schema.Provider {
949949
"huaweicloud_dms_rocketmq_message_traces": rocketmq.DataSourceDmsRocketmqMessageTraces(),
950950
"huaweicloud_dms_rocketmq_extend_flavors": rocketmq.DataSourceDmsRocketmqExtendFlavors(),
951951
"huaweicloud_dms_rocketmq_messages": rocketmq.DataSourceDmsRocketMQMessages(),
952+
"huaweicloud_dms_rocketmq_tags": rocketmq.DataSourceDmsRocketMQTags(),
952953

953954
"huaweicloud_dns_custom_lines": dns.DataSourceCustomLines(),
954955
"huaweicloud_dns_floating_ptrrecords": dns.DataSourceFloatingPtrRecords(),
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package rocketmq
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
8+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance"
9+
)
10+
11+
func TestAccDmsRocketMQTags_basic(t *testing.T) {
12+
var (
13+
dataSourceName = "data.huaweicloud_dms_rocketmq_tags.test"
14+
dc = acceptance.InitDataSourceCheck(dataSourceName)
15+
)
16+
17+
resource.ParallelTest(t, resource.TestCase{
18+
PreCheck: func() { acceptance.TestAccPreCheck(t) },
19+
ProviderFactories: acceptance.TestAccProviderFactories,
20+
Steps: []resource.TestStep{
21+
{
22+
Config: testAccDmsRocketMQTags_basic(),
23+
Check: resource.ComposeTestCheckFunc(
24+
dc.CheckResourceExists(),
25+
resource.TestCheckResourceAttrSet(dataSourceName, "next_offset"),
26+
resource.TestCheckResourceAttrSet(dataSourceName, "previous_offset"),
27+
resource.TestCheckResourceAttrSet(dataSourceName, "tags.#"),
28+
resource.TestCheckOutput("is_tags_exist", "true"),
29+
resource.TestCheckOutput("is_key_set", "true"),
30+
resource.TestCheckOutput("is_values_set", "true"),
31+
),
32+
},
33+
},
34+
})
35+
}
36+
37+
func testAccDmsRocketMQTags_basic() string {
38+
return `
39+
data "huaweicloud_dms_rocketmq_tags" "test" {}
40+
41+
locals {
42+
tags = data.huaweicloud_dms_rocketmq_tags.test.tags
43+
}
44+
45+
output "is_tags_exist" {
46+
value = length(local.tags) >= 0
47+
}
48+
49+
output "is_key_set" {
50+
value = length(local.tags) > 0 ? alltrue([for tag in local.tags : tag.key != ""]) : true
51+
}
52+
53+
output "is_values_set" {
54+
value = length(local.tags) > 0 ? alltrue([for tag in local.tags : length(tag.values) >= 0]) : true
55+
}
56+
`
57+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package rocketmq
2+
3+
import (
4+
"context"
5+
"strings"
6+
7+
"github.com/hashicorp/go-multierror"
8+
"github.com/hashicorp/go-uuid"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11+
12+
"github.com/chnsz/golangsdk"
13+
14+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
15+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
16+
)
17+
18+
// @API RocketMQ GET /v2/{project_id}/rocketmq/tags
19+
func DataSourceDmsRocketMQTags() *schema.Resource {
20+
return &schema.Resource{
21+
ReadContext: dataSourceDmsRocketMQTagsRead,
22+
Schema: map[string]*schema.Schema{
23+
"region": {
24+
Type: schema.TypeString,
25+
Optional: true,
26+
Computed: true,
27+
Description: `The region in which to query the resource.`,
28+
},
29+
30+
// Attributes
31+
"next_offset": {
32+
Type: schema.TypeInt,
33+
Computed: true,
34+
Description: `The offset for the next page of results.`,
35+
},
36+
"previous_offset": {
37+
Type: schema.TypeInt,
38+
Computed: true,
39+
Description: `The offset for the previous page of results.`,
40+
},
41+
"tags": {
42+
Type: schema.TypeList,
43+
Computed: true,
44+
Description: `The list of tags.`,
45+
Elem: &schema.Resource{
46+
Schema: map[string]*schema.Schema{
47+
"key": {
48+
Type: schema.TypeString,
49+
Computed: true,
50+
Description: `The tag key.`,
51+
},
52+
"values": {
53+
Type: schema.TypeList,
54+
Computed: true,
55+
Elem: &schema.Schema{Type: schema.TypeString},
56+
Description: `The list of tag values.`,
57+
},
58+
},
59+
},
60+
},
61+
},
62+
}
63+
}
64+
65+
func dataSourceDmsRocketMQTagsRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
66+
cfg := meta.(*config.Config)
67+
region := cfg.GetRegion(d)
68+
client, err := cfg.NewServiceClient("dmsv2", region)
69+
if err != nil {
70+
return diag.Errorf("error creating DMS RocketMQ client: %s", err)
71+
}
72+
73+
getTagsHttpUrl := "v2/{project_id}/rocketmq/tags"
74+
getTagsPath := client.Endpoint + getTagsHttpUrl
75+
getTagsPath = strings.ReplaceAll(getTagsPath, "{project_id}", client.ProjectID)
76+
77+
getTagsOpt := golangsdk.RequestOpts{
78+
KeepResponseBody: true,
79+
MoreHeaders: map[string]string{
80+
"Content-Type": "application/json",
81+
},
82+
}
83+
84+
getTagsResp, err := client.Request("GET", getTagsPath, &getTagsOpt)
85+
if err != nil {
86+
return diag.Errorf("error retrieving DMS RocketMQ tags: %s", err)
87+
}
88+
89+
getTagsRespBody, err := utils.FlattenResponse(getTagsResp)
90+
if err != nil {
91+
return diag.FromErr(err)
92+
}
93+
94+
randUUID, err := uuid.GenerateUUID()
95+
if err != nil {
96+
return diag.Errorf("unable to generate ID: %s", err)
97+
}
98+
d.SetId(randUUID)
99+
100+
mErr := multierror.Append(nil,
101+
d.Set("region", region),
102+
d.Set("next_offset", utils.PathSearch("next_offset", getTagsRespBody, nil)),
103+
d.Set("previous_offset", utils.PathSearch("previous_offset", getTagsRespBody, nil)),
104+
d.Set("tags", flattenRocketMQTags(getTagsRespBody)),
105+
)
106+
107+
return diag.FromErr(mErr.ErrorOrNil())
108+
}
109+
110+
func flattenRocketMQTags(resp interface{}) []interface{} {
111+
if resp == nil {
112+
return nil
113+
}
114+
115+
curJson := utils.PathSearch("tags", resp, make([]interface{}, 0))
116+
curArray := curJson.([]interface{})
117+
rst := make([]interface{}, 0, len(curArray))
118+
for _, v := range curArray {
119+
rst = append(rst, map[string]interface{}{
120+
"key": utils.PathSearch("key", v, nil),
121+
"values": utils.PathSearch("values", v, nil),
122+
})
123+
}
124+
return rst
125+
}

0 commit comments

Comments
 (0)