Skip to content
Merged
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
3 changes: 3 additions & 0 deletions .changelog/3530.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-data-source
tencentcloud_tag_keys
```
1 change: 1 addition & 0 deletions tencentcloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,7 @@ func Provider() *schema.Provider {
"tencentcloud_oceanus_check_savepoint": oceanus.DataSourceTencentCloudOceanusCheckSavepoint(),
"tencentcloud_oceanus_job_events": oceanus.DataSourceTencentCloudOceanusJobEvents(),
"tencentcloud_oceanus_meta_table": oceanus.DataSourceTencentCloudOceanusMetaTable(),
"tencentcloud_tag_keys": tag.DataSourceTencentCloudTagKeys(),
"tencentcloud_vpn_customer_gateways": vpn.DataSourceTencentCloudVpnCustomerGateways(),
"tencentcloud_vpn_gateways": vpn.DataSourceTencentCloudVpnGateways(),
"tencentcloud_vpn_gateway_routes": vpn.DataSourceTencentCloudVpnGatewayRoutes(),
Expand Down
2 changes: 2 additions & 0 deletions tencentcloud/provider.md
Original file line number Diff line number Diff line change
Expand Up @@ -2105,6 +2105,8 @@ tencentcloud_clickhouse_keyval_config
tencentcloud_clickhouse_xml_config

Tag
Data Source
tencentcloud_tag_keys
Resource
tencentcloud_tag
tencentcloud_tag_attachment
Expand Down
104 changes: 104 additions & 0 deletions tencentcloud/services/tag/data_source_tc_tag_keys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package tag

import (
"context"

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

tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
)

func DataSourceTencentCloudTagKeys() *schema.Resource {
return &schema.Resource{
Read: dataSourceTencentCloudTagKeysRead,
Schema: map[string]*schema.Schema{
"create_uin": {
Type: schema.TypeInt,
Optional: true,
Description: "Creator `Uin`. If not specified, `Uin` is only used as the query condition.",
},

"show_project": {
Type: schema.TypeInt,
Optional: true,
Description: "Whether to show project. Allow values: 0: no, 1: yes.",
},

"category": {
Type: schema.TypeString,
Optional: true,
Description: "Tag type. Valid values: Custom: custom tag; System: system tag; All: all tags. Default value: All.",
},

"tags": {
Type: schema.TypeSet,
Computed: true,
Description: "Tag list.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},

"result_output_file": {
Type: schema.TypeString,
Optional: true,
Description: "Used to save results.",
},
},
}
}

func dataSourceTencentCloudTagKeysRead(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("data_source.tencentcloud_tag_keys.read")()
defer tccommon.InconsistentCheck(d, meta)()

var (
logId = tccommon.GetLogId(nil)
ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta)
service = TagService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
)

paramMap := make(map[string]interface{})
if v, ok := d.GetOkExists("create_uin"); ok {
paramMap["CreateUin"] = helper.IntUint64(v.(int))
}

if v, ok := d.GetOkExists("show_project"); ok {
paramMap["ShowProject"] = helper.IntUint64(v.(int))
}

if v, ok := d.GetOk("category"); ok {
paramMap["Category"] = helper.String(v.(string))
}

var respData []*string
reqErr := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
result, e := service.DescribeTagKeysByFilter(ctx, paramMap)
if e != nil {
return tccommon.RetryError(e)
}

respData = result
return nil
})

if reqErr != nil {
return reqErr
}

if respData != nil {
_ = d.Set("tags", respData)
}

d.SetId(helper.BuildToken())
output, ok := d.GetOk("result_output_file")
if ok && output.(string) != "" {
if e := tccommon.WriteToFile(output.(string), d); e != nil {
return e
}
}

return nil
}
19 changes: 19 additions & 0 deletions tencentcloud/services/tag/data_source_tc_tag_keys.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Use this data source to query detailed information of Tag keys

Example Usage

Qeury all tag keys

```hcl
data "tencentcloud_tag_keys" "tags" {}
```

Qeury tag keys by filter

```hcl
data "tencentcloud_tag_keys" "tags" {
create_uin = "1486445011341"
show_project = 1
category = "All"
}
```
29 changes: 29 additions & 0 deletions tencentcloud/services/tag/data_source_tc_tag_keys_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package tag_test

import (
"testing"

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

tcacctest "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/acctest"
)

func TestAccTencentCloudTagKeysDataSource_basic(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
PreCheck: func() {
tcacctest.AccPreCheck(t)
},
Providers: tcacctest.AccProviders,
Steps: []resource.TestStep{{
Config: testAccTagKeysDataSource,
Check: resource.ComposeTestCheckFunc(
tcacctest.AccCheckTencentCloudDataSourceID("data.tencentcloud_tag_keys.tags"),
),
}},
})
}

const testAccTagKeysDataSource = `
data "tencentcloud_tag_keys" "tags" {}
`
57 changes: 57 additions & 0 deletions tencentcloud/services/tag/service_tencentcloud_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,3 +417,60 @@ func (me *TagService) DeleteTagTagAttachmentById(ctx context.Context, tagKey str

return
}

func (me *TagService) DescribeTagKeysByFilter(ctx context.Context, param map[string]interface{}) (ret []*string, errRet error) {
var (
logId = tccommon.GetLogId(ctx)
request = tag.NewDescribeTagKeysRequest()
)

defer func() {
if errRet != nil {
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", logId, request.GetAction(), request.ToJsonString(), errRet.Error())
}
}()

for k, v := range param {
if k == "CreateUin" {
request.CreateUin = v.(*uint64)
}

if k == "ShowProject" {
request.ShowProject = v.(*uint64)
}

if k == "Category" {
request.Category = v.(*string)
}
}

var (
offset uint64 = 0
limit uint64 = 1000
)

for {
request.Offset = &offset
request.Limit = &limit
ratelimit.Check(request.GetAction())
response, err := me.client.UseTagClient().DescribeTagKeys(request)
if err != nil {
errRet = err
return
}

log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())
if response == nil || len(response.Response.Tags) < 1 {
break
}

ret = append(ret, response.Response.Tags...)
if len(response.Response.Tags) < int(limit) {
break
}

offset += limit
}

return
}
47 changes: 47 additions & 0 deletions website/docs/d/tag_keys.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
subcategory: "Tag"
layout: "tencentcloud"
page_title: "TencentCloud: tencentcloud_tag_keys"
sidebar_current: "docs-tencentcloud-datasource-tag_keys"
description: |-
Use this data source to query detailed information of Tag keys
---

# tencentcloud_tag_keys

Use this data source to query detailed information of Tag keys

## Example Usage

### Qeury all tag keys

```hcl
data "tencentcloud_tag_keys" "tags" {}
```

### Qeury tag keys by filter

```hcl
data "tencentcloud_tag_keys" "tags" {
create_uin = "1486445011341"
show_project = 1
category = "All"
}
```

## Argument Reference

The following arguments are supported:

* `category` - (Optional, String) Tag type. Valid values: Custom: custom tag; System: system tag; All: all tags. Default value: All.
* `create_uin` - (Optional, Int) Creator `Uin`. If not specified, `Uin` is only used as the query condition.
* `result_output_file` - (Optional, String) Used to save results.
* `show_project` - (Optional, Int) Whether to show project. Allow values: 0: no, 1: yes.

## Attributes Reference

In addition to all arguments above, the following attributes are exported:

* `tags` - Tag list.


9 changes: 8 additions & 1 deletion website/tencentcloud.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4600,7 +4600,14 @@
<li>
<a href="#">Tag</a>
<ul class="nav">

<li>
<a href="#">Data Sources</a>
<ul class="nav nav-auto-expand">
<li>
<a href="/docs/providers/tencentcloud/d/tag_keys.html">tencentcloud_tag_keys</a>
</li>
</ul>
</li>
<li>
<a href="#">Resources</a>
<ul class="nav nav-auto-expand">
Expand Down
Loading