@@ -2,9 +2,13 @@ package clb
22
33import (
44 "context"
5+ "fmt"
6+ "log"
57
68 tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
9+ "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
710
11+ "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
812 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
913 clb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb/v20180317"
1014)
@@ -36,7 +40,7 @@ func ResourceTencentCloudClbTargetGroup() *schema.Resource {
3640 Type : schema .TypeInt ,
3741 Optional : true ,
3842 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. " ,
4044 },
4145 "target_group_instances" : {
4246 Type : schema .TypeList ,
@@ -72,6 +76,49 @@ func ResourceTencentCloudClbTargetGroup() *schema.Resource {
7276 },
7377 },
7478 },
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+ },
75122 },
76123 }
77124}
@@ -80,40 +127,81 @@ func resourceTencentCloudClbTargetCreate(d *schema.ResourceData, meta interface{
80127 defer tccommon .LogElapsed ("resource.tencentcloud_clb_target_group.create" )()
81128
82129 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 ()
92133 )
93134
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 ))
107165 }
108- insAttachments = append (insAttachments , tgtGrp )
166+
167+ request .Tags = append (request .Tags , & clbTags )
109168 }
110169 }
111170
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+
113195 if err != nil {
196+ log .Printf ("[CRITAL]%s create target group failed, reason:%+v" , logId , err )
114197 return err
115198 }
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 )
117205
118206 return resourceTencentCloudClbTargetRead (d , meta )
119207
@@ -124,23 +212,64 @@ func resourceTencentCloudClbTargetRead(d *schema.ResourceData, meta interface{})
124212 defer tccommon .InconsistentCheck (d , meta )()
125213
126214 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 ()
131219 )
220+
132221 filters := make (map [string ]string )
133- targetGroupInfos , err := clbService .DescribeTargetGroups (ctx , id , filters )
222+ targetGroupInfos , err := clbService .DescribeTargetGroups (ctx , targetGroupId , filters )
134223 if err != nil {
135224 return err
136225 }
226+
137227 if len (targetGroupInfos ) < 1 {
138228 d .SetId ("" )
139229 return nil
140230 }
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+ }
144273
145274 return nil
146275}
@@ -150,23 +279,44 @@ func resourceTencentCloudClbTargetUpdate(d *schema.ResourceData, meta interface{
150279
151280 var (
152281 logId = tccommon .GetLogId (tccommon .ContextNil )
153- ctx = context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
154- clbService = ClbService {client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn ()}
155282 targetGroupId = d .Id ()
156- port uint64
157- tgtGroupName string
158283 )
159284
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+ }
165290 }
166291
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+
169318 if err != nil {
319+ log .Printf ("[CRITAL]%s modify target group failed, reason:%+v" , logId , err )
170320 return err
171321 }
172322 }
@@ -185,9 +335,9 @@ func resourceTencentCloudClbTargetDelete(d *schema.ResourceData, meta interface{
185335 )
186336
187337 err := clbService .DeleteTarget (ctx , targetGroupId )
188-
189338 if err != nil {
190339 return err
191340 }
341+
192342 return nil
193343}
0 commit comments