diff --git a/.changelog/3530.txt b/.changelog/3530.txt new file mode 100644 index 0000000000..fe2245b1c8 --- /dev/null +++ b/.changelog/3530.txt @@ -0,0 +1,3 @@ +```release-note:new-data-source +tencentcloud_tag_keys +``` \ No newline at end of file diff --git a/tencentcloud/provider.go b/tencentcloud/provider.go index 139913345d..fc6d1b9d8d 100644 --- a/tencentcloud/provider.go +++ b/tencentcloud/provider.go @@ -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(), diff --git a/tencentcloud/provider.md b/tencentcloud/provider.md index 6377f9089b..75375800a2 100644 --- a/tencentcloud/provider.md +++ b/tencentcloud/provider.md @@ -2105,6 +2105,8 @@ tencentcloud_clickhouse_keyval_config tencentcloud_clickhouse_xml_config Tag +Data Source +tencentcloud_tag_keys Resource tencentcloud_tag tencentcloud_tag_attachment diff --git a/tencentcloud/services/tag/data_source_tc_tag_keys.go b/tencentcloud/services/tag/data_source_tc_tag_keys.go new file mode 100644 index 0000000000..618c2be64a --- /dev/null +++ b/tencentcloud/services/tag/data_source_tc_tag_keys.go @@ -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 +} diff --git a/tencentcloud/services/tag/data_source_tc_tag_keys.md b/tencentcloud/services/tag/data_source_tc_tag_keys.md new file mode 100644 index 0000000000..2979b0da97 --- /dev/null +++ b/tencentcloud/services/tag/data_source_tc_tag_keys.md @@ -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" +} +``` diff --git a/tencentcloud/services/tag/data_source_tc_tag_keys_test.go b/tencentcloud/services/tag/data_source_tc_tag_keys_test.go new file mode 100644 index 0000000000..f46d805f03 --- /dev/null +++ b/tencentcloud/services/tag/data_source_tc_tag_keys_test.go @@ -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" {} +` diff --git a/tencentcloud/services/tag/service_tencentcloud_tag.go b/tencentcloud/services/tag/service_tencentcloud_tag.go index 8e8d2c16e5..7421237e7b 100644 --- a/tencentcloud/services/tag/service_tencentcloud_tag.go +++ b/tencentcloud/services/tag/service_tencentcloud_tag.go @@ -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 +} diff --git a/website/docs/d/tag_keys.html.markdown b/website/docs/d/tag_keys.html.markdown new file mode 100644 index 0000000000..ee2216f9b3 --- /dev/null +++ b/website/docs/d/tag_keys.html.markdown @@ -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. + + diff --git a/website/tencentcloud.erb b/website/tencentcloud.erb index 712e9b6771..5af8f509d8 100644 --- a/website/tencentcloud.erb +++ b/website/tencentcloud.erb @@ -4600,7 +4600,14 @@