@@ -7,6 +7,9 @@ Example Usage
77resource "tencentcloud_tdmq_instance" "foo" {
88 cluster_name = "example"
99 remark = "this is description."
10+ tags = {
11+ "createdBy" = "terraform"
12+ }
1013}
1114```
1215
@@ -23,10 +26,12 @@ package tencentcloud
2326import (
2427 "context"
2528 "fmt"
29+ "log"
2630
2731 "github.com/hashicorp/terraform-plugin-sdk/helper/resource"
2832 "github.com/hashicorp/terraform-plugin-sdk/helper/schema"
29- "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
33+ tdmq "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tdmq/v20200217"
34+ "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
3035)
3136
3237func resourceTencentCloudTdmqInstance () * schema.Resource {
@@ -55,6 +60,11 @@ func resourceTencentCloudTdmqInstance() *schema.Resource {
5560 Optional : true ,
5661 Description : "Description of the tdmq cluster." ,
5762 },
63+ "tags" : {
64+ Type : schema .TypeMap ,
65+ Optional : true ,
66+ Description : "Tag description list." ,
67+ },
5868 },
5969 }
6070}
@@ -65,30 +75,50 @@ func resourceTencentCloudTdmqCreate(d *schema.ResourceData, meta interface{}) er
6575 logId := getLogId (contextNil )
6676 ctx := context .WithValue (context .TODO (), logIdKey , logId )
6777
68- tdmqService := TdmqService {client : meta .(* TencentCloudClient ).apiV3Conn }
69-
7078 var (
71- clusterName string
72- bindClusterId uint64
73- remark string
79+ request = tdmq .NewCreateClusterRequest ()
80+ response * tdmq.CreateClusterResponse
7481 )
75- if temp , ok := d .GetOk ("cluster_name" ); ok {
76- clusterName = temp .(string )
77- if len (clusterName ) < 1 {
78- return fmt .Errorf ("cluster_name should be not empty string" )
79- }
82+ if v , ok := d .GetOk ("cluster_name" ); ok {
83+ request .ClusterName = helper .String (v .(string ))
8084 }
8185
82- bindClusterId = uint64 (d .Get ("bind_cluster_id" ).(int ))
86+ if v , ok := d .GetOk ("bind_cluster_id" ); ok {
87+ request .BindClusterId = helper .IntUint64 (v .(int ))
88+ }
8389
84- if temp , ok := d .GetOk ("remark" ); ok {
85- remark = temp .( string )
90+ if v , ok := d .GetOk ("remark" ); ok {
91+ request . Remark = helper . String ( v .( string ) )
8692 }
8793
88- clusterId , err := tdmqService .CreateTdmqInstance (ctx , clusterName , bindClusterId , remark )
94+ err := resource .Retry (writeRetryTimeout , func () * resource.RetryError {
95+ result , e := meta .(* TencentCloudClient ).apiV3Conn .UseTdmqClient ().CreateCluster (request )
96+ if e != nil {
97+ return retryError (e )
98+ } else {
99+ log .Printf ("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n " ,
100+ logId , request .GetAction (), request .ToJsonString (), result .ToJsonString ())
101+ }
102+ response = result
103+ return nil
104+ })
105+
89106 if err != nil {
107+ log .Printf ("[CRITAL]%s create cls logset failed, reason:%+v" , logId , err )
90108 return err
91109 }
110+
111+ clusterId := * response .Response .ClusterId
112+
113+ if tags := helper .GetTags (d , "tags" ); len (tags ) > 0 {
114+ tagService := TagService {client : meta .(* TencentCloudClient ).apiV3Conn }
115+ region := meta .(* TencentCloudClient ).apiV3Conn .Region
116+ resourceName := fmt .Sprintf ("qcs::tdmq:%s:uin/:cluster/%s" , region , clusterId )
117+ if err := tagService .ModifyTags (ctx , resourceName , tags , nil ); err != nil {
118+ return err
119+ }
120+ }
121+
92122 d .SetId (clusterId )
93123
94124 return resourceTencentCloudTdmqRead (d , meta )
@@ -122,6 +152,15 @@ func resourceTencentCloudTdmqRead(d *schema.ResourceData, meta interface{}) erro
122152 if err != nil {
123153 return err
124154 }
155+
156+ tcClient := meta .(* TencentCloudClient ).apiV3Conn
157+ tagService := & TagService {client : tcClient }
158+ tags , err := tagService .DescribeResourceTags (ctx , "tdmq" , "cluster" , tcClient .Region , d .Id ())
159+ if err != nil {
160+ return err
161+ }
162+ _ = d .Set ("tags" , tags )
163+
125164 return nil
126165}
127166
@@ -153,15 +192,20 @@ func resourceTencentCloudTdmqUpdate(d *schema.ResourceData, meta interface{}) er
153192 remark = old .(string )
154193 }
155194
156- d .Partial (true )
157-
158195 if err := service .ModifyTdmqInstanceAttribute (ctx , id , clusterName , remark ); err != nil {
159196 return err
160197 }
161- d .SetPartial ("cluster_name" )
162- d .SetPartial ("remark" )
163198
164- d .Partial (false )
199+ if d .HasChange ("tags" ) {
200+ tcClient := meta .(* TencentCloudClient ).apiV3Conn
201+ tagService := & TagService {client : tcClient }
202+ oldTags , newTags := d .GetChange ("tags" )
203+ replaceTags , deleteTags := diffTags (oldTags .(map [string ]interface {}), newTags .(map [string ]interface {}))
204+ resourceName := BuildTagResourceName ("tdmq" , "cluster" , tcClient .Region , d .Id ())
205+ if err := tagService .ModifyTags (ctx , resourceName , replaceTags , deleteTags ); err != nil {
206+ return err
207+ }
208+ }
165209 return resourceTencentCloudTdmqRead (d , meta )
166210}
167211
@@ -172,18 +216,11 @@ func resourceTencentCloudTdmqDelete(d *schema.ResourceData, meta interface{}) er
172216 ctx := context .WithValue (context .TODO (), logIdKey , logId )
173217
174218 service := TdmqService {client : meta .(* TencentCloudClient ).apiV3Conn }
219+ clusterId := d .Id ()
175220
176- err := resource .Retry (writeRetryTimeout , func () * resource.RetryError {
177- if err := service .DeleteTdmqInstance (ctx , d .Id ()); err != nil {
178- if sdkErr , ok := err .(* errors.TencentCloudSDKError ); ok {
179- if sdkErr .Code == VPCNotFound {
180- return nil
181- }
182- }
183- return resource .RetryableError (err )
184- }
185- return nil
186- })
221+ if err := service .DeleteTdmqInstance (ctx , clusterId ); err != nil {
222+ return err
223+ }
187224
188- return err
225+ return nil
189226}
0 commit comments