@@ -30,19 +30,10 @@ func ResourceTencentCloudAsLoadBalancer() *schema.Resource {
30
30
Description : "ID of a scaling group." ,
31
31
},
32
32
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
-
42
33
"forward_load_balancers" : {
43
34
Optional : true ,
44
35
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." ,
46
37
Elem : & schema.Resource {
47
38
Schema : map [string ]* schema.Schema {
48
39
"load_balancer_id" : {
@@ -82,6 +73,7 @@ func ResourceTencentCloudAsLoadBalancer() *schema.Resource {
82
73
"region" : {
83
74
Type : schema .TypeString ,
84
75
Optional : true ,
76
+ Computed : true ,
85
77
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." ,
86
78
},
87
79
},
@@ -95,23 +87,15 @@ func resourceTencentCloudAsLoadBalancerCreate(d *schema.ResourceData, meta inter
95
87
defer tccommon .LogElapsed ("resource.tencentcloud_as_load_balancer.create" )()
96
88
defer tccommon .InconsistentCheck (d , meta )()
97
89
98
- logId := tccommon .GetLogId (tccommon .ContextNil )
99
-
100
90
var (
91
+ logId = tccommon .GetLogId (tccommon .ContextNil )
101
92
request = as .NewAttachLoadBalancersRequest ()
102
93
autoScalingGroupId string
103
94
)
95
+
104
96
if v , ok := d .GetOk ("auto_scaling_group_id" ); ok {
105
- autoScalingGroupId = v .(string )
106
97
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 )
115
99
}
116
100
117
101
if v , ok := d .GetOk ("forward_load_balancers" ); ok {
@@ -121,28 +105,35 @@ func resourceTencentCloudAsLoadBalancerCreate(d *schema.ResourceData, meta inter
121
105
if v , ok := dMap ["load_balancer_id" ]; ok {
122
106
forwardLoadBalancer .LoadBalancerId = helper .String (v .(string ))
123
107
}
108
+
124
109
if v , ok := dMap ["listener_id" ]; ok {
125
110
forwardLoadBalancer .ListenerId = helper .String (v .(string ))
126
111
}
112
+
127
113
if v , ok := dMap ["target_attributes" ]; ok {
128
114
for _ , item := range v .([]interface {}) {
129
115
targetAttributesMap := item .(map [string ]interface {})
130
116
targetAttribute := as.TargetAttribute {}
131
117
if v , ok := targetAttributesMap ["port" ]; ok {
132
118
targetAttribute .Port = helper .IntUint64 (v .(int ))
133
119
}
120
+
134
121
if v , ok := targetAttributesMap ["weight" ]; ok {
135
122
targetAttribute .Weight = helper .IntUint64 (v .(int ))
136
123
}
124
+
137
125
forwardLoadBalancer .TargetAttributes = append (forwardLoadBalancer .TargetAttributes , & targetAttribute )
138
126
}
139
127
}
128
+
140
129
if v , ok := dMap ["location_id" ]; ok {
141
130
forwardLoadBalancer .LocationId = helper .String (v .(string ))
142
131
}
132
+
143
133
if v , ok := dMap ["region" ]; ok {
144
134
forwardLoadBalancer .Region = helper .String (v .(string ))
145
135
}
136
+
146
137
request .ForwardLoadBalancers = append (request .ForwardLoadBalancers , & forwardLoadBalancer )
147
138
}
148
139
}
@@ -154,8 +145,10 @@ func resourceTencentCloudAsLoadBalancerCreate(d *schema.ResourceData, meta inter
154
145
} else {
155
146
log .Printf ("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n " , logId , request .GetAction (), request .ToJsonString (), result .ToJsonString ())
156
147
}
148
+
157
149
return nil
158
150
})
151
+
159
152
if err != nil {
160
153
log .Printf ("[CRITAL]%s create as loadBalancer failed, reason:%+v" , logId , err )
161
154
return err
@@ -170,13 +163,12 @@ func resourceTencentCloudAsLoadBalancerRead(d *schema.ResourceData, meta interfa
170
163
defer tccommon .LogElapsed ("resource.tencentcloud_as_load_balancer.read" )()
171
164
defer tccommon .InconsistentCheck (d , meta )()
172
165
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
+ )
180
172
181
173
loadBalancer , err := service .DescribeAsLoadBalancerById (ctx , autoScalingGroupId )
182
174
if err != nil {
@@ -193,15 +185,10 @@ func resourceTencentCloudAsLoadBalancerRead(d *schema.ResourceData, meta interfa
193
185
_ = d .Set ("auto_scaling_group_id" , loadBalancer .AutoScalingGroupId )
194
186
}
195
187
196
- if loadBalancer .LoadBalancerIdSet != nil {
197
- _ = d .Set ("load_balancer_ids" , loadBalancer .LoadBalancerIdSet )
198
- }
199
-
200
188
if loadBalancer .ForwardLoadBalancerSet != nil {
201
- forwardLoadBalancersList := [] interface {}{}
189
+ forwardLoadBalancersList := make ([] map [ string ] interface {}, 0 , len ( loadBalancer . ForwardLoadBalancerSet ))
202
190
for _ , forwardLoadBalancers := range loadBalancer .ForwardLoadBalancerSet {
203
191
forwardLoadBalancersMap := map [string ]interface {}{}
204
-
205
192
if forwardLoadBalancers .LoadBalancerId != nil {
206
193
forwardLoadBalancersMap ["load_balancer_id" ] = forwardLoadBalancers .LoadBalancerId
207
194
}
@@ -211,10 +198,9 @@ func resourceTencentCloudAsLoadBalancerRead(d *schema.ResourceData, meta interfa
211
198
}
212
199
213
200
if forwardLoadBalancers .TargetAttributes != nil {
214
- targetAttributesList := [] interface {}{}
201
+ targetAttributesList := make ([] map [ string ] interface {}, 0 , len ( forwardLoadBalancers . TargetAttributes ))
215
202
for _ , targetAttributes := range forwardLoadBalancers .TargetAttributes {
216
203
targetAttributesMap := map [string ]interface {}{}
217
-
218
204
if targetAttributes .Port != nil {
219
205
targetAttributesMap ["port" ] = targetAttributes .Port
220
206
}
@@ -226,7 +212,7 @@ func resourceTencentCloudAsLoadBalancerRead(d *schema.ResourceData, meta interfa
226
212
targetAttributesList = append (targetAttributesList , targetAttributesMap )
227
213
}
228
214
229
- forwardLoadBalancersMap ["target_attributes" ] = [] interface {}{ targetAttributesList }
215
+ forwardLoadBalancersMap ["target_attributes" ] = targetAttributesList
230
216
}
231
217
232
218
if forwardLoadBalancers .LocationId != nil {
@@ -241,7 +227,6 @@ func resourceTencentCloudAsLoadBalancerRead(d *schema.ResourceData, meta interfa
241
227
}
242
228
243
229
_ = d .Set ("forward_load_balancers" , forwardLoadBalancersList )
244
-
245
230
}
246
231
247
232
return nil
@@ -251,69 +236,76 @@ func resourceTencentCloudAsLoadBalancerUpdate(d *schema.ResourceData, meta inter
251
236
defer tccommon .LogElapsed ("resource.tencentcloud_as_load_balancer.update" )()
252
237
defer tccommon .InconsistentCheck (d , meta )()
253
238
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
+ )
263
244
245
+ immutableArgs := []string {"auto_scaling_group_id" }
264
246
for _ , v := range immutableArgs {
265
247
if d .HasChange (v ) {
266
248
return fmt .Errorf ("argument `%s` cannot be changed" , v )
267
249
}
268
250
}
269
251
270
252
if d .HasChange ("forward_load_balancers" ) {
253
+ request .AutoScalingGroupId = & autoScalingGroupId
271
254
if v , ok := d .GetOk ("forward_load_balancers" ); ok {
272
255
for _ , item := range v .([]interface {}) {
273
256
forwardLoadBalancer := as.ForwardLoadBalancer {}
274
257
dMap := item .(map [string ]interface {})
275
258
if v , ok := dMap ["load_balancer_id" ]; ok {
276
259
forwardLoadBalancer .LoadBalancerId = helper .String (v .(string ))
277
260
}
261
+
278
262
if v , ok := dMap ["listener_id" ]; ok {
279
263
forwardLoadBalancer .ListenerId = helper .String (v .(string ))
280
264
}
265
+
281
266
if v , ok := dMap ["target_attributes" ]; ok {
282
267
for _ , item := range v .([]interface {}) {
283
268
targetAttributesMap := item .(map [string ]interface {})
284
269
targetAttribute := as.TargetAttribute {}
285
270
if v , ok := targetAttributesMap ["port" ]; ok {
286
271
targetAttribute .Port = helper .IntUint64 (v .(int ))
287
272
}
273
+
288
274
if v , ok := targetAttributesMap ["weight" ]; ok {
289
275
targetAttribute .Weight = helper .IntUint64 (v .(int ))
290
276
}
277
+
291
278
forwardLoadBalancer .TargetAttributes = append (forwardLoadBalancer .TargetAttributes , & targetAttribute )
292
279
}
293
280
}
281
+
294
282
if v , ok := dMap ["location_id" ]; ok {
295
283
forwardLoadBalancer .LocationId = helper .String (v .(string ))
296
284
}
285
+
297
286
if v , ok := dMap ["region" ]; ok {
298
287
forwardLoadBalancer .Region = helper .String (v .(string ))
299
288
}
289
+
300
290
request .ForwardLoadBalancers = append (request .ForwardLoadBalancers , & forwardLoadBalancer )
301
291
}
302
292
}
303
- }
304
293
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
311
308
}
312
- return nil
313
- })
314
- if err != nil {
315
- log .Printf ("[CRITAL]%s update as loadBalancer failed, reason:%+v" , logId , err )
316
- return err
317
309
}
318
310
319
311
return resourceTencentCloudAsLoadBalancerRead (d , meta )
@@ -323,11 +315,12 @@ func resourceTencentCloudAsLoadBalancerDelete(d *schema.ResourceData, meta inter
323
315
defer tccommon .LogElapsed ("resource.tencentcloud_as_load_balancer.delete" )()
324
316
defer tccommon .InconsistentCheck (d , meta )()
325
317
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
+ )
331
324
332
325
if err := service .DeleteAsLoadBalancerById (ctx , autoScalingGroupId ); err != nil {
333
326
return err
0 commit comments