Skip to content

Commit 70d3e2f

Browse files
committed
feat(cts): add new data source to query resource tags
1 parent fe8d2ee commit 70d3e2f

File tree

4 files changed

+266
-1
lines changed

4 files changed

+266
-1
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
subcategory: "Cloud Trace Service (CTS)"
3+
layout: "huaweicloud"
4+
page_title: "HuaweiCloud: huaweicloud_cts_resource_tags"
5+
description: |-
6+
Use this data source to get resource tag list of CTS.
7+
---
8+
9+
# huaweicloud_cts_resource_tags
10+
11+
Use this data source to get resource tag list of CTS.
12+
13+
## Example Usage
14+
15+
```hcl
16+
resource "huaweicloud_obs_bucket" "test" {
17+
bucket = "tf-test-bucket"
18+
acl = "private"
19+
}
20+
21+
resource "huaweicloud_cts_tracker" "test" {
22+
bucket_name = huaweicloud_obs_bucket.test.bucket
23+
file_prefix = "cts"
24+
25+
tags = {
26+
foo1 = "bar1",
27+
foo2 = "bar2"
28+
}
29+
}
30+
31+
data "huaweicloud_cts_resource_tags" "test" {
32+
resource_id = huaweicloud_cts_tracker.test.id
33+
}
34+
```
35+
36+
## Argument Reference
37+
38+
The following arguments are supported:
39+
40+
* `region` - (Optional, String) Specifies the region in which to query the data source.
41+
If omitted, the provider-level region will be used.
42+
43+
* `resource_id` - (Required, String) Specifies the resource ID.
44+
45+
## Attribute Reference
46+
47+
In addition to all arguments above, the following attributes are exported:
48+
49+
* `id` - The resource ID.
50+
51+
* `tags` - The list of tags.
52+
The [tags](#cts_tags) structure is documented below.
53+
54+
<a name="cts_tags"></a>
55+
The `tags` block supports:
56+
57+
* `key` - The tag key.
58+
59+
* `value` - The tag value.

huaweicloud/provider.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,7 @@ func Provider() *schema.Provider {
745745
"huaweicloud_cts_trackers": cts.DataSourceCtsTrackers(),
746746
"huaweicloud_cts_operations": cts.DataSourceCtsOperations(),
747747
"huaweicloud_cts_quotas": cts.DataSourceCtsQuotas(),
748-
"huaweicloud_cts_resources": cts.DataSourceCtsResources(),
748+
"huaweicloud_cts_resource_tags": cts.DataSourceCtsResourcesTags(),
749749
"huaweicloud_cts_users": cts.DataSourceCtsUsers(),
750750

751751
"huaweicloud_cdm_clusters": cdm.DataSourceCdmClusters(),
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package cts
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
9+
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance"
10+
)
11+
12+
func TestAccDataSourceResourceTags_basic(t *testing.T) {
13+
dataSourceName := "data.huaweicloud_cts_resource_tags.test"
14+
dc := acceptance.InitDataSourceCheck(dataSourceName)
15+
16+
resource.ParallelTest(t, resource.TestCase{
17+
PreCheck: func() {
18+
acceptance.TestAccPreCheck(t)
19+
},
20+
ProviderFactories: acceptance.TestAccProviderFactories,
21+
Steps: []resource.TestStep{
22+
{
23+
Config: testAccDataSourceResourceTags_basic(),
24+
Check: resource.ComposeTestCheckFunc(
25+
dc.CheckResourceExists(),
26+
resource.TestCheckResourceAttrSet(dataSourceName, "region"),
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 testAccDataSourceResourceTags_base() string {
38+
return `
39+
resource "huaweicloud_obs_bucket" "test" {
40+
bucket = "tf-test-bucket"
41+
acl = "private"
42+
43+
tags = {
44+
foo = "bar"
45+
}
46+
}
47+
48+
resource "huaweicloud_cts_tracker" "test" {
49+
bucket_name = huaweicloud_obs_bucket.test.bucket
50+
file_prefix = "cts"
51+
52+
tags = {
53+
foo1 = "bar1",
54+
foo2 = "bar2"
55+
}
56+
}
57+
`
58+
}
59+
func testAccDataSourceResourceTags_basic() string {
60+
return fmt.Sprintf(`
61+
%s
62+
63+
data "huaweicloud_cts_resource_tags" "test" {
64+
resource_id = huaweicloud_cts_tracker.test.id
65+
}
66+
67+
locals {
68+
tags = data.huaweicloud_cts_resource_tags.test.tags
69+
}
70+
71+
output "is_tags_exist" {
72+
value = length(local.tags) > 0
73+
}
74+
75+
output "is_key_set" {
76+
value = alltrue([for v in local.tags[*].key : v != ""])
77+
}
78+
79+
output "is_values_set" {
80+
value = alltrue([for v in local.tags[*].value : v != ""])
81+
}
82+
`, testAccDataSourceResourceTags_base())
83+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package cts
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 CTS GET /v3/{project_id}/{resource_type}/{resource_id}/tags
19+
func DataSourceCtsResourcesTags() *schema.Resource {
20+
return &schema.Resource{
21+
ReadContext: dataSourceCtsResourcesTagsRead,
22+
23+
Schema: map[string]*schema.Schema{
24+
"region": {
25+
Type: schema.TypeString,
26+
Optional: true,
27+
Computed: true,
28+
Description: `Specifies the region in which to query the resource tags.`,
29+
},
30+
"resource_id": {
31+
Type: schema.TypeString,
32+
Required: true,
33+
Description: `Specifies the resource ID.`,
34+
},
35+
"tags": {
36+
Type: schema.TypeList,
37+
Computed: true,
38+
Description: `The list of resource tags.`,
39+
Elem: &schema.Resource{
40+
Schema: map[string]*schema.Schema{
41+
"key": {
42+
Type: schema.TypeString,
43+
Computed: true,
44+
Description: `The tag key.`,
45+
},
46+
"value": {
47+
Type: schema.TypeString,
48+
Computed: true,
49+
Description: `The tag value.`,
50+
},
51+
},
52+
},
53+
},
54+
},
55+
}
56+
}
57+
58+
func dataSourceCtsResourcesTagsRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
59+
cfg := meta.(*config.Config)
60+
region := cfg.GetRegion(d)
61+
client, err := cfg.NewServiceClient("cts", region)
62+
if err != nil {
63+
return diag.Errorf("error creating CTS client: %s", err)
64+
}
65+
66+
resourceId := d.Get("resource_id").(string)
67+
httpUrl := "v3/{project_id}/{resource_type}/{resource_id}/tags"
68+
path := client.Endpoint + httpUrl
69+
path = strings.ReplaceAll(path, "{project_id}", client.ProjectID)
70+
path = strings.ReplaceAll(path, "{resource_type}", "cts-tracker")
71+
path = strings.ReplaceAll(path, "{resource_id}", resourceId)
72+
73+
requestOpt := golangsdk.RequestOpts{
74+
KeepResponseBody: true,
75+
MoreHeaders: map[string]string{
76+
"Content-Type": "application/json",
77+
},
78+
}
79+
80+
response, err := client.Request("GET", path, &requestOpt)
81+
if err != nil {
82+
return diag.Errorf("error retrieving CTS resource tags: %s", err)
83+
}
84+
85+
respBody, err := utils.FlattenResponse(response)
86+
if err != nil {
87+
return diag.FromErr(err)
88+
}
89+
90+
randUUID, err := uuid.GenerateUUID()
91+
if err != nil {
92+
return diag.Errorf("unable to generate ID: %s", err)
93+
}
94+
d.SetId(randUUID)
95+
96+
mErr := multierror.Append(nil,
97+
d.Set("region", region),
98+
d.Set("tags", flattenResourceTags(respBody)),
99+
)
100+
101+
return diag.FromErr(mErr.ErrorOrNil())
102+
}
103+
104+
func flattenResourceTags(respBody interface{}) []interface{} {
105+
if respBody == nil {
106+
return nil
107+
}
108+
109+
curJson := utils.PathSearch("tags", respBody, make([]interface{}, 0))
110+
tagArray := curJson.([]interface{})
111+
if len(tagArray) == 0 {
112+
return nil
113+
}
114+
115+
result := make([]interface{}, 0, len(tagArray))
116+
for _, tag := range tagArray {
117+
result = append(result, map[string]interface{}{
118+
"key": utils.PathSearch("key", tag, nil),
119+
"value": utils.PathSearch("value", tag, nil),
120+
})
121+
}
122+
return result
123+
}

0 commit comments

Comments
 (0)