@@ -2,9 +2,13 @@ package clb
2
2
3
3
import (
4
4
"context"
5
+ "fmt"
6
+ "log"
5
7
6
8
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
9
+ "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
7
10
11
+ "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8
12
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9
13
clb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb/v20180317"
10
14
)
@@ -36,7 +40,7 @@ func ResourceTencentCloudClbTargetGroup() *schema.Resource {
36
40
Type : schema .TypeInt ,
37
41
Optional : true ,
38
42
ValidateFunc : tccommon .ValidatePort ,
39
- Description : "The default port of target group, add server after can use it." ,
43
+ Description : "The default port of target group, add server after can use it. If `full_listen_switch` is true, setting this parameter is not supported. " ,
40
44
},
41
45
"target_group_instances" : {
42
46
Type : schema .TypeList ,
@@ -72,6 +76,49 @@ func ResourceTencentCloudClbTargetGroup() *schema.Resource {
72
76
},
73
77
},
74
78
},
79
+ "type" : {
80
+ Type : schema .TypeString ,
81
+ Optional : true ,
82
+ Computed : true ,
83
+ ValidateFunc : tccommon .ValidateAllowedStringValue ([]string {"v1" , "v2" }),
84
+ Description : "Target group type, currently supports v1 (old version target group), v2 (new version target group), defaults to v1 (old version target group)." ,
85
+ },
86
+ "protocol" : {
87
+ Type : schema .TypeString ,
88
+ Optional : true ,
89
+ ValidateFunc : tccommon .ValidateAllowedStringValue ([]string {"TCP" , "UDP" }),
90
+ Description : "Target group backend forwarding protocol. This item is required for the v2 new version target group. Currently supports `TCP`, `UDP`." ,
91
+ },
92
+ "tags" : {
93
+ Optional : true ,
94
+ Type : schema .TypeList ,
95
+ Description : "Label." ,
96
+ Elem : & schema.Resource {
97
+ Schema : map [string ]* schema.Schema {
98
+ "tag_key" : {
99
+ Type : schema .TypeString ,
100
+ Required : true ,
101
+ Description : "Tag key." ,
102
+ },
103
+ "tag_value" : {
104
+ Type : schema .TypeString ,
105
+ Required : true ,
106
+ Description : "Tag value." ,
107
+ },
108
+ },
109
+ },
110
+ },
111
+ "weight" : {
112
+ Type : schema .TypeInt ,
113
+ Optional : true ,
114
+ ValidateFunc : tccommon .ValidateIntegerInRange (0 , 100 ),
115
+ Description : "Default weights for backend services. Value range [0, 100]. After setting this value, when adding backend services to the target group, if the backend services do not have separate weights set, the default weights here will be used." ,
116
+ },
117
+ "full_listen_switch" : {
118
+ Type : schema .TypeBool ,
119
+ Optional : true ,
120
+ Description : "Full listening target group identifier, true indicates full listening target group, false indicates not full listening target group." ,
121
+ },
75
122
},
76
123
}
77
124
}
@@ -80,40 +127,81 @@ func resourceTencentCloudClbTargetCreate(d *schema.ResourceData, meta interface{
80
127
defer tccommon .LogElapsed ("resource.tencentcloud_clb_target_group.create" )()
81
128
82
129
var (
83
- logId = tccommon .GetLogId (tccommon .ContextNil )
84
- ctx = context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
85
- clbService = ClbService {client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn ()}
86
- vpcId = d .Get ("vpc_id" ).(string )
87
- targetGroupName = d .Get ("target_group_name" ).(string )
88
- port = uint64 (d .Get ("port" ).(int ))
89
- insAttachments = make ([]* clb.TargetGroupInstance , 0 )
90
- targetGroupId string
91
- err error
130
+ logId = tccommon .GetLogId (tccommon .ContextNil )
131
+ request = clb .NewCreateTargetGroupRequest ()
132
+ response = clb .NewCreateTargetGroupResponse ()
92
133
)
93
134
94
- if v , ok := d .GetOk ("target_group_instances" ); ok {
95
- targetGroupInstances := v .([]interface {})
96
- for _ , v1 := range targetGroupInstances {
97
- value := v1 .(map [string ]interface {})
98
- bindIP := value ["bind_ip" ].(string )
99
- port := uint64 (value ["port" ].(int ))
100
- weight := uint64 (value ["weight" ].(int ))
101
- newPort := uint64 (value ["new_port" ].(int ))
102
- tgtGrp := & clb.TargetGroupInstance {
103
- BindIP : & bindIP ,
104
- Port : & port ,
105
- Weight : & weight ,
106
- NewPort : & newPort ,
135
+ if v , ok := d .GetOk ("target_group_name" ); ok {
136
+ request .TargetGroupName = helper .String (v .(string ))
137
+ }
138
+
139
+ if v , ok := d .GetOk ("vpc_id" ); ok {
140
+ request .VpcId = helper .String (v .(string ))
141
+ }
142
+
143
+ if v , ok := d .GetOkExists ("port" ); ok {
144
+ request .Port = helper .IntUint64 (v .(int ))
145
+ }
146
+
147
+ if v , ok := d .GetOk ("type" ); ok {
148
+ request .Type = helper .String (v .(string ))
149
+ }
150
+
151
+ if v , ok := d .GetOk ("protocol" ); ok {
152
+ request .Protocol = helper .String (v .(string ))
153
+ }
154
+
155
+ if v , ok := d .GetOk ("tags" ); ok {
156
+ for _ , item := range v .([]interface {}) {
157
+ dMap := item .(map [string ]interface {})
158
+ clbTags := clb.TagInfo {}
159
+ if v , ok := dMap ["tag_key" ]; ok {
160
+ clbTags .TagKey = helper .String (v .(string ))
161
+ }
162
+
163
+ if v , ok := dMap ["tag_value" ]; ok {
164
+ clbTags .TagValue = helper .String (v .(string ))
107
165
}
108
- insAttachments = append (insAttachments , tgtGrp )
166
+
167
+ request .Tags = append (request .Tags , & clbTags )
109
168
}
110
169
}
111
170
112
- targetGroupId , err = clbService .CreateTargetGroup (ctx , targetGroupName , vpcId , port , insAttachments )
171
+ if v , ok := d .GetOkExists ("weight" ); ok {
172
+ request .Weight = helper .IntUint64 (v .(int ))
173
+ }
174
+
175
+ if v , ok := d .GetOkExists ("full_listen_switch" ); ok {
176
+ request .FullListenSwitch = helper .Bool (v .(bool ))
177
+ }
178
+
179
+ err := resource .Retry (tccommon .WriteRetryTimeout , func () * resource.RetryError {
180
+ result , e := meta .(tccommon.ProviderMeta ).GetAPIV3Conn ().UseClbClient ().CreateTargetGroup (request )
181
+ if e != nil {
182
+ return tccommon .RetryError (e )
183
+ } else {
184
+ log .Printf ("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n " , logId , request .GetAction (), request .ToJsonString (), result .ToJsonString ())
185
+ }
186
+
187
+ if result == nil || result .Response == nil {
188
+ return resource .NonRetryableError (fmt .Errorf ("Create target group failed, Response is nil." ))
189
+ }
190
+
191
+ response = result
192
+ return nil
193
+ })
194
+
113
195
if err != nil {
196
+ log .Printf ("[CRITAL]%s create target group failed, reason:%+v" , logId , err )
114
197
return err
115
198
}
116
- d .SetId (targetGroupId )
199
+
200
+ if response .Response .TargetGroupId == nil {
201
+ return fmt .Errorf ("TargetGroupId is nil." )
202
+ }
203
+
204
+ d .SetId (* response .Response .TargetGroupId )
117
205
118
206
return resourceTencentCloudClbTargetRead (d , meta )
119
207
@@ -124,23 +212,64 @@ func resourceTencentCloudClbTargetRead(d *schema.ResourceData, meta interface{})
124
212
defer tccommon .InconsistentCheck (d , meta )()
125
213
126
214
var (
127
- logId = tccommon .GetLogId (tccommon .ContextNil )
128
- ctx = context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
129
- clbService = ClbService {client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn ()}
130
- id = d .Id ()
215
+ logId = tccommon .GetLogId (tccommon .ContextNil )
216
+ ctx = context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
217
+ clbService = ClbService {client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn ()}
218
+ targetGroupId = d .Id ()
131
219
)
220
+
132
221
filters := make (map [string ]string )
133
- targetGroupInfos , err := clbService .DescribeTargetGroups (ctx , id , filters )
222
+ targetGroupInfos , err := clbService .DescribeTargetGroups (ctx , targetGroupId , filters )
134
223
if err != nil {
135
224
return err
136
225
}
226
+
137
227
if len (targetGroupInfos ) < 1 {
138
228
d .SetId ("" )
139
229
return nil
140
230
}
141
- _ = d .Set ("target_group_name" , targetGroupInfos [0 ].TargetGroupName )
142
- _ = d .Set ("vpc_id" , targetGroupInfos [0 ].VpcId )
143
- _ = d .Set ("port" , targetGroupInfos [0 ].Port )
231
+
232
+ if targetGroupInfos [0 ].TargetGroupName != nil {
233
+ _ = d .Set ("target_group_name" , targetGroupInfos [0 ].TargetGroupName )
234
+ }
235
+
236
+ if targetGroupInfos [0 ].VpcId != nil {
237
+ _ = d .Set ("vpc_id" , targetGroupInfos [0 ].VpcId )
238
+ }
239
+
240
+ if targetGroupInfos [0 ].Port != nil {
241
+ _ = d .Set ("port" , targetGroupInfos [0 ].Port )
242
+ }
243
+
244
+ if targetGroupInfos [0 ].TargetGroupType != nil {
245
+ _ = d .Set ("type" , targetGroupInfos [0 ].TargetGroupType )
246
+ }
247
+
248
+ if targetGroupInfos [0 ].Tag != nil {
249
+ tagsList := make ([]interface {}, 0 , len (targetGroupInfos [0 ].Tag ))
250
+ for _ , tags := range targetGroupInfos [0 ].Tag {
251
+ tagsMap := map [string ]interface {}{}
252
+ if tags .TagKey != nil {
253
+ tagsMap ["tag_key" ] = tags .TagKey
254
+ }
255
+
256
+ if tags .TagValue != nil {
257
+ tagsMap ["tag_value" ] = tags .TagValue
258
+ }
259
+
260
+ tagsList = append (tagsList , tagsMap )
261
+ }
262
+
263
+ _ = d .Set ("tags" , tagsList )
264
+ }
265
+
266
+ if targetGroupInfos [0 ].Weight != nil {
267
+ _ = d .Set ("weight" , targetGroupInfos [0 ].Weight )
268
+ }
269
+
270
+ if targetGroupInfos [0 ].FullListenSwitch != nil {
271
+ _ = d .Set ("full_listen_switch" , targetGroupInfos [0 ].FullListenSwitch )
272
+ }
144
273
145
274
return nil
146
275
}
@@ -150,23 +279,44 @@ func resourceTencentCloudClbTargetUpdate(d *schema.ResourceData, meta interface{
150
279
151
280
var (
152
281
logId = tccommon .GetLogId (tccommon .ContextNil )
153
- ctx = context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
154
- clbService = ClbService {client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn ()}
155
282
targetGroupId = d .Id ()
156
- port uint64
157
- tgtGroupName string
158
283
)
159
284
160
- isChanged := false
161
- if d . HasChange ( "port" ) || d . HasChange ( "target_group_name" ) {
162
- isChanged = true
163
- port = uint64 ( d . Get ( "port" ).( int ) )
164
- tgtGroupName = d . Get ( "target_group_name" ).( string )
285
+ immutableArgs := [] string { "type" , "protocol" , "tags" , "full_listen_switch" }
286
+ for _ , v := range immutableArgs {
287
+ if d . HasChange ( v ) {
288
+ return fmt . Errorf ( "argument `%s` cannot be changed" , v )
289
+ }
165
290
}
166
291
167
- if isChanged {
168
- err := clbService .ModifyTargetGroup (ctx , targetGroupId , tgtGroupName , port )
292
+ if d .HasChange ("target_group_name" ) || d .HasChange ("port" ) || d .HasChange ("weight" ) {
293
+ request := clb .NewModifyTargetGroupAttributeRequest ()
294
+ if v , ok := d .GetOk ("target_group_name" ); ok {
295
+ request .TargetGroupName = helper .String (v .(string ))
296
+ }
297
+
298
+ if v , ok := d .GetOkExists ("port" ); ok {
299
+ request .Port = helper .IntUint64 (v .(int ))
300
+ }
301
+
302
+ if v , ok := d .GetOkExists ("weight" ); ok {
303
+ request .Weight = helper .IntUint64 (v .(int ))
304
+ }
305
+
306
+ request .TargetGroupId = & targetGroupId
307
+ err := resource .Retry (tccommon .WriteRetryTimeout , func () * resource.RetryError {
308
+ result , e := meta .(tccommon.ProviderMeta ).GetAPIV3Conn ().UseClbClient ().ModifyTargetGroupAttribute (request )
309
+ if e != nil {
310
+ return tccommon .RetryError (e )
311
+ } else {
312
+ log .Printf ("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n " , logId , request .GetAction (), request .ToJsonString (), result .ToJsonString ())
313
+ }
314
+
315
+ return nil
316
+ })
317
+
169
318
if err != nil {
319
+ log .Printf ("[CRITAL]%s modify target group failed, reason:%+v" , logId , err )
170
320
return err
171
321
}
172
322
}
@@ -185,9 +335,9 @@ func resourceTencentCloudClbTargetDelete(d *schema.ResourceData, meta interface{
185
335
)
186
336
187
337
err := clbService .DeleteTarget (ctx , targetGroupId )
188
-
189
338
if err != nil {
190
339
return err
191
340
}
341
+
192
342
return nil
193
343
}
0 commit comments