@@ -30,19 +30,10 @@ func ResourceTencentCloudAsLoadBalancer() *schema.Resource {
3030 Description : "ID of a scaling group." ,
3131 },
3232
33- "load_balancer_ids" : {
34- Optional : true ,
35- Type : schema .TypeSet ,
36- Elem : & schema.Schema {
37- Type : schema .TypeString ,
38- },
39- Description : "List of traditional load balancer IDs. The maximum number of traditional load balancers bound to each scaling group is 20. Both LoadBalancerIds and ForwardLoadBalancers can specify at most one at the same time." ,
40- },
41-
4233 "forward_load_balancers" : {
4334 Optional : true ,
4435 Type : schema .TypeList ,
45- Description : "List of application load balancers. The maximum number of application-type load balancers bound to each scaling group is 100. Both LoadBalancerIds and ForwardLoadBalancers can specify at most one at the same time. " ,
36+ Description : "List of application load balancers. The maximum number of application-type load balancers bound to each scaling group is 100." ,
4637 Elem : & schema.Resource {
4738 Schema : map [string ]* schema.Schema {
4839 "load_balancer_id" : {
@@ -82,6 +73,7 @@ func ResourceTencentCloudAsLoadBalancer() *schema.Resource {
8273 "region" : {
8374 Type : schema .TypeString ,
8475 Optional : true ,
76+ Computed : true ,
8577 Description : "Load balancer instance region. Default value is the region of current auto scaling group. The format is the same as the public parameter Region, for example: ap-guangzhou." ,
8678 },
8779 },
@@ -95,23 +87,15 @@ func resourceTencentCloudAsLoadBalancerCreate(d *schema.ResourceData, meta inter
9587 defer tccommon .LogElapsed ("resource.tencentcloud_as_load_balancer.create" )()
9688 defer tccommon .InconsistentCheck (d , meta )()
9789
98- logId := tccommon .GetLogId (tccommon .ContextNil )
99-
10090 var (
91+ logId = tccommon .GetLogId (tccommon .ContextNil )
10192 request = as .NewAttachLoadBalancersRequest ()
10293 autoScalingGroupId string
10394 )
95+
10496 if v , ok := d .GetOk ("auto_scaling_group_id" ); ok {
105- autoScalingGroupId = v .(string )
10697 request .AutoScalingGroupId = helper .String (v .(string ))
107- }
108-
109- if v , ok := d .GetOk ("load_balancer_ids" ); ok {
110- loadBalancerIdsSet := v .(* schema.Set ).List ()
111- for i := range loadBalancerIdsSet {
112- loadBalancerIds := loadBalancerIdsSet [i ].(string )
113- request .LoadBalancerIds = append (request .LoadBalancerIds , & loadBalancerIds )
114- }
98+ autoScalingGroupId = v .(string )
11599 }
116100
117101 if v , ok := d .GetOk ("forward_load_balancers" ); ok {
@@ -121,28 +105,35 @@ func resourceTencentCloudAsLoadBalancerCreate(d *schema.ResourceData, meta inter
121105 if v , ok := dMap ["load_balancer_id" ]; ok {
122106 forwardLoadBalancer .LoadBalancerId = helper .String (v .(string ))
123107 }
108+
124109 if v , ok := dMap ["listener_id" ]; ok {
125110 forwardLoadBalancer .ListenerId = helper .String (v .(string ))
126111 }
112+
127113 if v , ok := dMap ["target_attributes" ]; ok {
128114 for _ , item := range v .([]interface {}) {
129115 targetAttributesMap := item .(map [string ]interface {})
130116 targetAttribute := as.TargetAttribute {}
131117 if v , ok := targetAttributesMap ["port" ]; ok {
132118 targetAttribute .Port = helper .IntUint64 (v .(int ))
133119 }
120+
134121 if v , ok := targetAttributesMap ["weight" ]; ok {
135122 targetAttribute .Weight = helper .IntUint64 (v .(int ))
136123 }
124+
137125 forwardLoadBalancer .TargetAttributes = append (forwardLoadBalancer .TargetAttributes , & targetAttribute )
138126 }
139127 }
128+
140129 if v , ok := dMap ["location_id" ]; ok {
141130 forwardLoadBalancer .LocationId = helper .String (v .(string ))
142131 }
132+
143133 if v , ok := dMap ["region" ]; ok {
144134 forwardLoadBalancer .Region = helper .String (v .(string ))
145135 }
136+
146137 request .ForwardLoadBalancers = append (request .ForwardLoadBalancers , & forwardLoadBalancer )
147138 }
148139 }
@@ -154,8 +145,10 @@ func resourceTencentCloudAsLoadBalancerCreate(d *schema.ResourceData, meta inter
154145 } else {
155146 log .Printf ("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n " , logId , request .GetAction (), request .ToJsonString (), result .ToJsonString ())
156147 }
148+
157149 return nil
158150 })
151+
159152 if err != nil {
160153 log .Printf ("[CRITAL]%s create as loadBalancer failed, reason:%+v" , logId , err )
161154 return err
@@ -170,13 +163,12 @@ func resourceTencentCloudAsLoadBalancerRead(d *schema.ResourceData, meta interfa
170163 defer tccommon .LogElapsed ("resource.tencentcloud_as_load_balancer.read" )()
171164 defer tccommon .InconsistentCheck (d , meta )()
172165
173- logId := tccommon .GetLogId (tccommon .ContextNil )
174-
175- ctx := context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
176-
177- service := AsService {client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn ()}
178-
179- autoScalingGroupId := d .Id ()
166+ var (
167+ logId = tccommon .GetLogId (tccommon .ContextNil )
168+ ctx = context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
169+ service = AsService {client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn ()}
170+ autoScalingGroupId = d .Id ()
171+ )
180172
181173 loadBalancer , err := service .DescribeAsLoadBalancerById (ctx , autoScalingGroupId )
182174 if err != nil {
@@ -193,15 +185,10 @@ func resourceTencentCloudAsLoadBalancerRead(d *schema.ResourceData, meta interfa
193185 _ = d .Set ("auto_scaling_group_id" , loadBalancer .AutoScalingGroupId )
194186 }
195187
196- if loadBalancer .LoadBalancerIdSet != nil {
197- _ = d .Set ("load_balancer_ids" , loadBalancer .LoadBalancerIdSet )
198- }
199-
200188 if loadBalancer .ForwardLoadBalancerSet != nil {
201- forwardLoadBalancersList := [] interface {}{}
189+ forwardLoadBalancersList := make ([] map [ string ] interface {}, 0 , len ( loadBalancer . ForwardLoadBalancerSet ))
202190 for _ , forwardLoadBalancers := range loadBalancer .ForwardLoadBalancerSet {
203191 forwardLoadBalancersMap := map [string ]interface {}{}
204-
205192 if forwardLoadBalancers .LoadBalancerId != nil {
206193 forwardLoadBalancersMap ["load_balancer_id" ] = forwardLoadBalancers .LoadBalancerId
207194 }
@@ -211,10 +198,9 @@ func resourceTencentCloudAsLoadBalancerRead(d *schema.ResourceData, meta interfa
211198 }
212199
213200 if forwardLoadBalancers .TargetAttributes != nil {
214- targetAttributesList := [] interface {}{}
201+ targetAttributesList := make ([] map [ string ] interface {}, 0 , len ( forwardLoadBalancers . TargetAttributes ))
215202 for _ , targetAttributes := range forwardLoadBalancers .TargetAttributes {
216203 targetAttributesMap := map [string ]interface {}{}
217-
218204 if targetAttributes .Port != nil {
219205 targetAttributesMap ["port" ] = targetAttributes .Port
220206 }
@@ -226,7 +212,7 @@ func resourceTencentCloudAsLoadBalancerRead(d *schema.ResourceData, meta interfa
226212 targetAttributesList = append (targetAttributesList , targetAttributesMap )
227213 }
228214
229- forwardLoadBalancersMap ["target_attributes" ] = [] interface {}{ targetAttributesList }
215+ forwardLoadBalancersMap ["target_attributes" ] = targetAttributesList
230216 }
231217
232218 if forwardLoadBalancers .LocationId != nil {
@@ -241,7 +227,6 @@ func resourceTencentCloudAsLoadBalancerRead(d *schema.ResourceData, meta interfa
241227 }
242228
243229 _ = d .Set ("forward_load_balancers" , forwardLoadBalancersList )
244-
245230 }
246231
247232 return nil
@@ -251,69 +236,76 @@ func resourceTencentCloudAsLoadBalancerUpdate(d *schema.ResourceData, meta inter
251236 defer tccommon .LogElapsed ("resource.tencentcloud_as_load_balancer.update" )()
252237 defer tccommon .InconsistentCheck (d , meta )()
253238
254- logId := tccommon .GetLogId (tccommon .ContextNil )
255-
256- request := as .NewModifyLoadBalancerTargetAttributesRequest ()
257-
258- autoScalingGroupId := d .Id ()
259-
260- request .AutoScalingGroupId = & autoScalingGroupId
261-
262- immutableArgs := []string {"load_balancer_ids" }
239+ var (
240+ logId = tccommon .GetLogId (tccommon .ContextNil )
241+ request = as .NewModifyLoadBalancerTargetAttributesRequest ()
242+ autoScalingGroupId = d .Id ()
243+ )
263244
245+ immutableArgs := []string {"auto_scaling_group_id" }
264246 for _ , v := range immutableArgs {
265247 if d .HasChange (v ) {
266248 return fmt .Errorf ("argument `%s` cannot be changed" , v )
267249 }
268250 }
269251
270252 if d .HasChange ("forward_load_balancers" ) {
253+ request .AutoScalingGroupId = & autoScalingGroupId
271254 if v , ok := d .GetOk ("forward_load_balancers" ); ok {
272255 for _ , item := range v .([]interface {}) {
273256 forwardLoadBalancer := as.ForwardLoadBalancer {}
274257 dMap := item .(map [string ]interface {})
275258 if v , ok := dMap ["load_balancer_id" ]; ok {
276259 forwardLoadBalancer .LoadBalancerId = helper .String (v .(string ))
277260 }
261+
278262 if v , ok := dMap ["listener_id" ]; ok {
279263 forwardLoadBalancer .ListenerId = helper .String (v .(string ))
280264 }
265+
281266 if v , ok := dMap ["target_attributes" ]; ok {
282267 for _ , item := range v .([]interface {}) {
283268 targetAttributesMap := item .(map [string ]interface {})
284269 targetAttribute := as.TargetAttribute {}
285270 if v , ok := targetAttributesMap ["port" ]; ok {
286271 targetAttribute .Port = helper .IntUint64 (v .(int ))
287272 }
273+
288274 if v , ok := targetAttributesMap ["weight" ]; ok {
289275 targetAttribute .Weight = helper .IntUint64 (v .(int ))
290276 }
277+
291278 forwardLoadBalancer .TargetAttributes = append (forwardLoadBalancer .TargetAttributes , & targetAttribute )
292279 }
293280 }
281+
294282 if v , ok := dMap ["location_id" ]; ok {
295283 forwardLoadBalancer .LocationId = helper .String (v .(string ))
296284 }
285+
297286 if v , ok := dMap ["region" ]; ok {
298287 forwardLoadBalancer .Region = helper .String (v .(string ))
299288 }
289+
300290 request .ForwardLoadBalancers = append (request .ForwardLoadBalancers , & forwardLoadBalancer )
301291 }
302292 }
303- }
304293
305- err := resource .Retry (tccommon .WriteRetryTimeout , func () * resource.RetryError {
306- result , e := meta .(tccommon.ProviderMeta ).GetAPIV3Conn ().UseAsClient ().ModifyLoadBalancerTargetAttributes (request )
307- if e != nil {
308- return tccommon .RetryError (e )
309- } else {
310- log .Printf ("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n " , logId , request .GetAction (), request .ToJsonString (), result .ToJsonString ())
294+ err := resource .Retry (tccommon .WriteRetryTimeout , func () * resource.RetryError {
295+ result , e := meta .(tccommon.ProviderMeta ).GetAPIV3Conn ().UseAsClient ().ModifyLoadBalancerTargetAttributes (request )
296+ if e != nil {
297+ return tccommon .RetryError (e )
298+ } else {
299+ log .Printf ("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n " , logId , request .GetAction (), request .ToJsonString (), result .ToJsonString ())
300+ }
301+
302+ return nil
303+ })
304+
305+ if err != nil {
306+ log .Printf ("[CRITAL]%s update as loadBalancer failed, reason:%+v" , logId , err )
307+ return err
311308 }
312- return nil
313- })
314- if err != nil {
315- log .Printf ("[CRITAL]%s update as loadBalancer failed, reason:%+v" , logId , err )
316- return err
317309 }
318310
319311 return resourceTencentCloudAsLoadBalancerRead (d , meta )
@@ -323,11 +315,12 @@ func resourceTencentCloudAsLoadBalancerDelete(d *schema.ResourceData, meta inter
323315 defer tccommon .LogElapsed ("resource.tencentcloud_as_load_balancer.delete" )()
324316 defer tccommon .InconsistentCheck (d , meta )()
325317
326- logId := tccommon .GetLogId (tccommon .ContextNil )
327- ctx := context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
328-
329- service := AsService {client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn ()}
330- autoScalingGroupId := d .Id ()
318+ var (
319+ logId = tccommon .GetLogId (tccommon .ContextNil )
320+ ctx = context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
321+ service = AsService {client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn ()}
322+ autoScalingGroupId = d .Id ()
323+ )
331324
332325 if err := service .DeleteAsLoadBalancerById (ctx , autoScalingGroupId ); err != nil {
333326 return err
0 commit comments