Skip to content

Commit d607fff

Browse files
authored
feat(tag): [127517088] add new data source (#3530)
* add * add
1 parent 2bd57e0 commit d607fff

File tree

9 files changed

+270
-1
lines changed

9 files changed

+270
-1
lines changed

.changelog/3530.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-data-source
2+
tencentcloud_tag_keys
3+
```

tencentcloud/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,7 @@ func Provider() *schema.Provider {
536536
"tencentcloud_oceanus_check_savepoint": oceanus.DataSourceTencentCloudOceanusCheckSavepoint(),
537537
"tencentcloud_oceanus_job_events": oceanus.DataSourceTencentCloudOceanusJobEvents(),
538538
"tencentcloud_oceanus_meta_table": oceanus.DataSourceTencentCloudOceanusMetaTable(),
539+
"tencentcloud_tag_keys": tag.DataSourceTencentCloudTagKeys(),
539540
"tencentcloud_vpn_customer_gateways": vpn.DataSourceTencentCloudVpnCustomerGateways(),
540541
"tencentcloud_vpn_gateways": vpn.DataSourceTencentCloudVpnGateways(),
541542
"tencentcloud_vpn_gateway_routes": vpn.DataSourceTencentCloudVpnGatewayRoutes(),

tencentcloud/provider.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2105,6 +2105,8 @@ tencentcloud_clickhouse_keyval_config
21052105
tencentcloud_clickhouse_xml_config
21062106

21072107
Tag
2108+
Data Source
2109+
tencentcloud_tag_keys
21082110
Resource
21092111
tencentcloud_tag
21102112
tencentcloud_tag_attachment
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package tag
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
9+
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
10+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
11+
)
12+
13+
func DataSourceTencentCloudTagKeys() *schema.Resource {
14+
return &schema.Resource{
15+
Read: dataSourceTencentCloudTagKeysRead,
16+
Schema: map[string]*schema.Schema{
17+
"create_uin": {
18+
Type: schema.TypeInt,
19+
Optional: true,
20+
Description: "Creator `Uin`. If not specified, `Uin` is only used as the query condition.",
21+
},
22+
23+
"show_project": {
24+
Type: schema.TypeInt,
25+
Optional: true,
26+
Description: "Whether to show project. Allow values: 0: no, 1: yes.",
27+
},
28+
29+
"category": {
30+
Type: schema.TypeString,
31+
Optional: true,
32+
Description: "Tag type. Valid values: Custom: custom tag; System: system tag; All: all tags. Default value: All.",
33+
},
34+
35+
"tags": {
36+
Type: schema.TypeSet,
37+
Computed: true,
38+
Description: "Tag list.",
39+
Elem: &schema.Schema{
40+
Type: schema.TypeString,
41+
},
42+
},
43+
44+
"result_output_file": {
45+
Type: schema.TypeString,
46+
Optional: true,
47+
Description: "Used to save results.",
48+
},
49+
},
50+
}
51+
}
52+
53+
func dataSourceTencentCloudTagKeysRead(d *schema.ResourceData, meta interface{}) error {
54+
defer tccommon.LogElapsed("data_source.tencentcloud_tag_keys.read")()
55+
defer tccommon.InconsistentCheck(d, meta)()
56+
57+
var (
58+
logId = tccommon.GetLogId(nil)
59+
ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta)
60+
service = TagService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
61+
)
62+
63+
paramMap := make(map[string]interface{})
64+
if v, ok := d.GetOkExists("create_uin"); ok {
65+
paramMap["CreateUin"] = helper.IntUint64(v.(int))
66+
}
67+
68+
if v, ok := d.GetOkExists("show_project"); ok {
69+
paramMap["ShowProject"] = helper.IntUint64(v.(int))
70+
}
71+
72+
if v, ok := d.GetOk("category"); ok {
73+
paramMap["Category"] = helper.String(v.(string))
74+
}
75+
76+
var respData []*string
77+
reqErr := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
78+
result, e := service.DescribeTagKeysByFilter(ctx, paramMap)
79+
if e != nil {
80+
return tccommon.RetryError(e)
81+
}
82+
83+
respData = result
84+
return nil
85+
})
86+
87+
if reqErr != nil {
88+
return reqErr
89+
}
90+
91+
if respData != nil {
92+
_ = d.Set("tags", respData)
93+
}
94+
95+
d.SetId(helper.BuildToken())
96+
output, ok := d.GetOk("result_output_file")
97+
if ok && output.(string) != "" {
98+
if e := tccommon.WriteToFile(output.(string), d); e != nil {
99+
return e
100+
}
101+
}
102+
103+
return nil
104+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Use this data source to query detailed information of Tag keys
2+
3+
Example Usage
4+
5+
Qeury all tag keys
6+
7+
```hcl
8+
data "tencentcloud_tag_keys" "tags" {}
9+
```
10+
11+
Qeury tag keys by filter
12+
13+
```hcl
14+
data "tencentcloud_tag_keys" "tags" {
15+
create_uin = "1486445011341"
16+
show_project = 1
17+
category = "All"
18+
}
19+
```
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package tag_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
8+
tcacctest "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/acctest"
9+
)
10+
11+
func TestAccTencentCloudTagKeysDataSource_basic(t *testing.T) {
12+
t.Parallel()
13+
resource.Test(t, resource.TestCase{
14+
PreCheck: func() {
15+
tcacctest.AccPreCheck(t)
16+
},
17+
Providers: tcacctest.AccProviders,
18+
Steps: []resource.TestStep{{
19+
Config: testAccTagKeysDataSource,
20+
Check: resource.ComposeTestCheckFunc(
21+
tcacctest.AccCheckTencentCloudDataSourceID("data.tencentcloud_tag_keys.tags"),
22+
),
23+
}},
24+
})
25+
}
26+
27+
const testAccTagKeysDataSource = `
28+
data "tencentcloud_tag_keys" "tags" {}
29+
`

tencentcloud/services/tag/service_tencentcloud_tag.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,3 +417,60 @@ func (me *TagService) DeleteTagTagAttachmentById(ctx context.Context, tagKey str
417417

418418
return
419419
}
420+
421+
func (me *TagService) DescribeTagKeysByFilter(ctx context.Context, param map[string]interface{}) (ret []*string, errRet error) {
422+
var (
423+
logId = tccommon.GetLogId(ctx)
424+
request = tag.NewDescribeTagKeysRequest()
425+
)
426+
427+
defer func() {
428+
if errRet != nil {
429+
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", logId, request.GetAction(), request.ToJsonString(), errRet.Error())
430+
}
431+
}()
432+
433+
for k, v := range param {
434+
if k == "CreateUin" {
435+
request.CreateUin = v.(*uint64)
436+
}
437+
438+
if k == "ShowProject" {
439+
request.ShowProject = v.(*uint64)
440+
}
441+
442+
if k == "Category" {
443+
request.Category = v.(*string)
444+
}
445+
}
446+
447+
var (
448+
offset uint64 = 0
449+
limit uint64 = 1000
450+
)
451+
452+
for {
453+
request.Offset = &offset
454+
request.Limit = &limit
455+
ratelimit.Check(request.GetAction())
456+
response, err := me.client.UseTagClient().DescribeTagKeys(request)
457+
if err != nil {
458+
errRet = err
459+
return
460+
}
461+
462+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())
463+
if response == nil || len(response.Response.Tags) < 1 {
464+
break
465+
}
466+
467+
ret = append(ret, response.Response.Tags...)
468+
if len(response.Response.Tags) < int(limit) {
469+
break
470+
}
471+
472+
offset += limit
473+
}
474+
475+
return
476+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
subcategory: "Tag"
3+
layout: "tencentcloud"
4+
page_title: "TencentCloud: tencentcloud_tag_keys"
5+
sidebar_current: "docs-tencentcloud-datasource-tag_keys"
6+
description: |-
7+
Use this data source to query detailed information of Tag keys
8+
---
9+
10+
# tencentcloud_tag_keys
11+
12+
Use this data source to query detailed information of Tag keys
13+
14+
## Example Usage
15+
16+
### Qeury all tag keys
17+
18+
```hcl
19+
data "tencentcloud_tag_keys" "tags" {}
20+
```
21+
22+
### Qeury tag keys by filter
23+
24+
```hcl
25+
data "tencentcloud_tag_keys" "tags" {
26+
create_uin = "1486445011341"
27+
show_project = 1
28+
category = "All"
29+
}
30+
```
31+
32+
## Argument Reference
33+
34+
The following arguments are supported:
35+
36+
* `category` - (Optional, String) Tag type. Valid values: Custom: custom tag; System: system tag; All: all tags. Default value: All.
37+
* `create_uin` - (Optional, Int) Creator `Uin`. If not specified, `Uin` is only used as the query condition.
38+
* `result_output_file` - (Optional, String) Used to save results.
39+
* `show_project` - (Optional, Int) Whether to show project. Allow values: 0: no, 1: yes.
40+
41+
## Attributes Reference
42+
43+
In addition to all arguments above, the following attributes are exported:
44+
45+
* `tags` - Tag list.
46+
47+

website/tencentcloud.erb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4600,7 +4600,14 @@
46004600
<li>
46014601
<a href="#">Tag</a>
46024602
<ul class="nav">
4603-
4603+
<li>
4604+
<a href="#">Data Sources</a>
4605+
<ul class="nav nav-auto-expand">
4606+
<li>
4607+
<a href="/docs/providers/tencentcloud/d/tag_keys.html">tencentcloud_tag_keys</a>
4608+
</li>
4609+
</ul>
4610+
</li>
46044611
<li>
46054612
<a href="#">Resources</a>
46064613
<ul class="nav nav-auto-expand">

0 commit comments

Comments
 (0)