Skip to content

feat(dms): add new data source to get tags #7604

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
44 changes: 44 additions & 0 deletions docs/data-sources/dms_rocketmq_tags.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
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.

* `next_offset` - The offset for the next page of results.

* `previous_offset` - The offset for the previous page of results.

* `tags` - The list of tags.
The [tags](#dms_rocketmq_tags) structure is documented below.

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

* `key` - The tag key.

* `values` - The list of tag values.
1 change: 1 addition & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,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.DataSourceDmsRocketMQTags(),

"huaweicloud_dns_custom_lines": dns.DataSourceCustomLines(),
"huaweicloud_dns_floating_ptrrecords": dns.DataSourceFloatingPtrRecords(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package rocketmq

import (
"testing"

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

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

func TestAccDmsRocketMQTags_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: testAccDmsRocketMQTags_basic(),
Check: resource.ComposeTestCheckFunc(
dc.CheckResourceExists(),
resource.TestCheckResourceAttrSet(dataSourceName, "next_offset"),
resource.TestCheckResourceAttrSet(dataSourceName, "previous_offset"),
resource.TestCheckResourceAttrSet(dataSourceName, "tags.#"),
resource.TestCheckOutput("is_tags_exist", "true"),
resource.TestCheckOutput("is_key_set", "true"),
resource.TestCheckOutput("is_values_set", "true"),
),
},
},
})
}

func testAccDmsRocketMQTags_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
}
`
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
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 DataSourceDmsRocketMQTags() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceDmsRocketMQTagsRead,
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: `The region in which to query the resource.`,
},

// Attributes
"next_offset": {
Type: schema.TypeInt,
Computed: true,
Description: `The offset for the next page of results.`,
},
"previous_offset": {
Type: schema.TypeInt,
Computed: true,
Description: `The offset for the previous page of results.`,
},
"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 dataSourceDmsRocketMQTagsRead(_ 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("next_offset", utils.PathSearch("next_offset", getTagsRespBody, nil)),
d.Set("previous_offset", utils.PathSearch("previous_offset", getTagsRespBody, nil)),
d.Set("tags", flattenRocketMQTags(getTagsRespBody)),
)

return diag.FromErr(mErr.ErrorOrNil())
}

func flattenRocketMQTags(resp interface{}) []interface{} {
if resp == nil {
return nil
}

curJson := utils.PathSearch("tags", resp, make([]interface{}, 0))
curArray := curJson.([]interface{})
rst := make([]interface{}, 0, len(curArray))
for _, v := range curArray {
rst = append(rst, map[string]interface{}{
"key": utils.PathSearch("key", v, nil),
"values": utils.PathSearch("values", v, nil),
})
}

return rst
}
Loading