@@ -3,20 +3,23 @@ package clb
3
3
import (
4
4
"context"
5
5
"fmt"
6
+ "log"
6
7
"strings"
7
8
9
+ "github.com/pkg/errors"
8
10
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
11
+ "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
9
12
10
13
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
11
14
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12
15
clb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb/v20180317"
16
+ sdkErrors "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
13
17
)
14
18
15
19
func ResourceTencentCloudClbTargetGroupAttachment () * schema.Resource {
16
20
return & schema.Resource {
17
21
Create : resourceTencentCloudClbTargetGroupAttachmentCreate ,
18
22
Read : resourceTencentCloudClbTargetGroupAttachmentRead ,
19
- Update : resourceTencentCloudClbTargetGroupAttachmentUpdate ,
20
23
Delete : resourceTencentCloudClbTargetGroupAttachmentDelete ,
21
24
Importer : & schema.ResourceImporter {
22
25
State : schema .ImportStatePassthrough ,
@@ -28,23 +31,17 @@ func ResourceTencentCloudClbTargetGroupAttachment() *schema.Resource {
28
31
Required : true ,
29
32
Description : "ID of the CLB." ,
30
33
},
31
- "listener_id " : {
34
+ "target_group_id " : {
32
35
Type : schema .TypeString ,
33
36
ForceNew : true ,
34
37
Required : true ,
35
- Description : "ID of the CLB listener." ,
36
- },
37
- "targrt_group_id" : {
38
- Type : schema .TypeString ,
39
- Optional : true ,
40
38
Description : "ID of the CLB target group." ,
41
- Deprecated : "It has been deprecated from version 1.47.1. Use `target_group_id` instead." ,
42
39
},
43
- "target_group_id " : {
40
+ "listener_id " : {
44
41
Type : schema .TypeString ,
45
42
ForceNew : true ,
46
43
Optional : true ,
47
- Description : "ID of the CLB target group ." ,
44
+ Description : "ID of the CLB listener ." ,
48
45
},
49
46
"rule_id" : {
50
47
Type : schema .TypeString ,
@@ -59,88 +56,122 @@ func resourceTencentCloudClbTargetGroupAttachmentCreate(d *schema.ResourceData,
59
56
defer tccommon .LogElapsed ("resource.tencentcloud_clb_target_group_attachment.create" )()
60
57
61
58
var (
62
- clbService = ClbService {
63
- client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn (),
64
- }
59
+ clbService = ClbService {client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn ()}
65
60
logId = tccommon .GetLogId (tccommon .ContextNil )
66
61
ctx = context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
67
- locationId string
68
- listenerId = d .Get ("listener_id" ).(string )
69
- clbId = d .Get ("clb_id" ).(string )
62
+ request = clb .NewAssociateTargetGroupsRequest ()
63
+ targetInfos []* clb.TargetGroupInfo
64
+ instance * clb.LoadBalancer
65
+ clbId string
66
+ listenerId string
70
67
targetGroupId string
71
-
72
- targetInfos []* clb.TargetGroupInfo
73
- instance * clb.LoadBalancer
74
- has bool
75
- err error
68
+ locationId string
76
69
)
77
- if v , ok := d .GetOk ("rule_id" ); ok {
78
- locationId = v .(string )
70
+
71
+ targetGroupAssociation := clb.TargetGroupAssociation {}
72
+ if v , ok := d .GetOk ("clb_id" ); ok {
73
+ targetGroupAssociation .LoadBalancerId = helper .String (v .(string ))
74
+ clbId = v .(string )
79
75
}
80
- vTarget , eHas := d .GetOk ("target_group_id" )
81
- vTargrt , rHas := d .GetOk ("targrt_group_id" )
82
76
83
- if eHas || rHas {
84
- if rHas {
85
- targetGroupId = vTargrt .(string )
86
- }
87
- if eHas {
88
- targetGroupId = vTarget .(string )
89
- }
90
- } else {
91
- return fmt .Errorf ("'target_group_id' or 'targrt_group_id' at least set one, please use 'target_group_id'" )
77
+ if v , ok := d .GetOk ("listener_id" ); ok {
78
+ targetGroupAssociation .ListenerId = helper .String (v .(string ))
79
+ listenerId = v .(string )
80
+ }
81
+
82
+ if v , ok := d .GetOk ("target_group_id" ); ok {
83
+ targetGroupAssociation .TargetGroupId = helper .String (v .(string ))
84
+ targetGroupId = v .(string )
85
+ }
86
+
87
+ if v , ok := d .GetOk ("rule_id" ); ok {
88
+ targetGroupAssociation .LocationId = helper .String (v .(string ))
89
+ locationId = v .(string )
92
90
}
93
91
94
92
//check listenerId
95
93
checkErr := ListenerIdCheck (listenerId )
96
94
if checkErr != nil {
97
95
return checkErr
98
96
}
97
+
99
98
//check ruleId
100
99
checkErr = RuleIdCheck (locationId )
101
100
if checkErr != nil {
102
101
return checkErr
103
102
}
104
103
105
104
//check target group
106
- err = resource .Retry (tccommon .ReadRetryTimeout , func () * resource.RetryError {
107
- instance , err = clbService .DescribeLoadBalancerById (ctx , clbId )
108
- if err != nil {
109
- return tccommon .RetryError (err , tccommon .InternalError )
105
+ err : = resource .Retry (tccommon .ReadRetryTimeout , func () * resource.RetryError {
106
+ result , e : = clbService .DescribeLoadBalancerById (ctx , clbId )
107
+ if e != nil {
108
+ return tccommon .RetryError (e , tccommon .InternalError )
110
109
}
110
+
111
+ if result == nil {
112
+ return resource .NonRetryableError (fmt .Errorf ("DescribeLoadBalancers response is nil." ))
113
+ }
114
+
115
+ instance = result
111
116
return nil
112
117
})
118
+
113
119
if err != nil {
114
120
return err
115
121
}
122
+
116
123
err = resource .Retry (tccommon .ReadRetryTimeout , func () * resource.RetryError {
117
- targetInfos , err = clbService .DescribeTargetGroups (ctx , targetGroupId , nil )
118
- if err != nil {
119
- return tccommon .RetryError (err , tccommon .InternalError )
124
+ result , e : = clbService .DescribeTargetGroups (ctx , targetGroupId , nil )
125
+ if e != nil {
126
+ return tccommon .RetryError (e , tccommon .InternalError )
120
127
}
128
+
129
+ if result == nil {
130
+ return resource .NonRetryableError (fmt .Errorf ("DescribeTargetGroups response is nil." ))
131
+ }
132
+
133
+ targetInfos = result
121
134
return nil
122
135
})
136
+
123
137
if err != nil {
124
138
return err
125
139
}
140
+
126
141
if len (targetInfos ) > 0 && (* targetInfos [0 ].VpcId != * instance .TargetRegionInfo .VpcId ) {
127
142
return fmt .Errorf ("CLB instance needs to be in the same VPC as the backend target group" )
128
143
}
129
144
130
- err = clbService .AssociateTargetGroups (ctx , listenerId , clbId , targetGroupId , locationId )
131
- if err != nil {
132
- return err
133
- }
145
+ request .Associations = []* clb.TargetGroupAssociation {& targetGroupAssociation }
146
+ err = resource .Retry (tccommon .WriteRetryTimeout , func () * resource.RetryError {
147
+ result , e := meta .(tccommon.ProviderMeta ).GetAPIV3Conn ().UseClbClient ().AssociateTargetGroups (request )
148
+ if e != nil {
149
+ if sdkError , ok := e .(* sdkErrors.TencentCloudSDKError ); ok {
150
+ if sdkError .Code == "FailedOperation.ResourceInOperating" {
151
+ return resource .RetryableError (e )
152
+ }
153
+ }
154
+
155
+ return tccommon .RetryError (e )
156
+ } else {
157
+ log .Printf ("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n " , logId , request .GetAction (), request .ToJsonString (), result .ToJsonString ())
158
+ if result == nil || result .Response == nil || result .Response .RequestId == nil {
159
+ return resource .NonRetryableError (fmt .Errorf ("AssociateTargetGroups response is nil." ))
160
+ }
161
+
162
+ requestId := * result .Response .RequestId
163
+ retryErr := waitForTaskFinish (requestId , meta .(tccommon.ProviderMeta ).GetAPIV3Conn ().UseClbClient ())
164
+ if retryErr != nil {
165
+ return tccommon .RetryError (errors .WithStack (retryErr ))
166
+ }
167
+ }
168
+
169
+ return nil
170
+ })
134
171
135
- // wait status
136
- has , err = clbService .DescribeAssociateTargetGroups (ctx , []string {targetGroupId , listenerId , clbId , locationId })
137
172
if err != nil {
138
173
return err
139
174
}
140
- if ! has {
141
- return fmt .Errorf ("AssociateTargetGroups faild, targetGroupId = %s, listenerId = %s, clbId = %s, ruleId = %s" ,
142
- targetGroupId , listenerId , clbId , locationId )
143
- }
144
175
145
176
d .SetId (strings .Join ([]string {targetGroupId , listenerId , clbId , locationId }, tccommon .FILED_SP ))
146
177
@@ -152,13 +183,11 @@ func resourceTencentCloudClbTargetGroupAttachmentRead(d *schema.ResourceData, me
152
183
defer tccommon .InconsistentCheck (d , meta )()
153
184
154
185
var (
155
- clbService = ClbService {
156
- client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn (),
157
- }
158
- logId = tccommon .GetLogId (tccommon .ContextNil )
159
- ctx = context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
160
- id = d .Id ()
161
- has bool
186
+ logId = tccommon .GetLogId (tccommon .ContextNil )
187
+ ctx = context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
188
+ clbService = ClbService {client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn ()}
189
+ id = d .Id ()
190
+ has bool
162
191
)
163
192
164
193
ids := strings .Split (id , tccommon .FILED_SP )
@@ -170,6 +199,7 @@ func resourceTencentCloudClbTargetGroupAttachmentRead(d *schema.ResourceData, me
170
199
if err != nil {
171
200
return err
172
201
}
202
+
173
203
if ! has {
174
204
d .SetId ("" )
175
205
return nil
@@ -185,20 +215,14 @@ func resourceTencentCloudClbTargetGroupAttachmentRead(d *schema.ResourceData, me
185
215
return nil
186
216
}
187
217
188
- func resourceTencentCloudClbTargetGroupAttachmentUpdate (d * schema.ResourceData , meta interface {}) error {
189
- defer tccommon .LogElapsed ("resource.tencentcloud_clb_target_group_attachment.update" )()
190
- return resourceTencentCloudClbTargetGroupAttachmentRead (d , meta )
191
- }
192
-
193
218
func resourceTencentCloudClbTargetGroupAttachmentDelete (d * schema.ResourceData , meta interface {}) error {
194
219
defer tccommon .LogElapsed ("resource.tencentcloud_clb_target_group_attachment.delete" )()
195
220
196
221
var (
197
- clbService = ClbService {
198
- client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn (),
199
- }
200
222
logId = tccommon .GetLogId (tccommon .ContextNil )
201
223
ctx = context .WithValue (context .TODO (), tccommon .LogIdKey , logId )
224
+ clbService = ClbService {client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn ()}
225
+ request = clb .NewDisassociateTargetGroupsRequest ()
202
226
id = d .Id ()
203
227
targetInfos []* clb.TargetGroupInfo
204
228
err error
@@ -209,7 +233,41 @@ func resourceTencentCloudClbTargetGroupAttachmentDelete(d *schema.ResourceData,
209
233
return fmt .Errorf ("CLB target group attachment id is clb_id#listener_id#target_group_id#rule_id(only required for 7 layer CLB)" )
210
234
}
211
235
212
- if err := clbService .DisassociateTargetGroups (ctx , ids [0 ], ids [1 ], ids [2 ], ids [3 ]); err != nil {
236
+ request .Associations = []* clb.TargetGroupAssociation {
237
+ {
238
+ TargetGroupId : & ids [0 ],
239
+ ListenerId : & ids [1 ],
240
+ LoadBalancerId : & ids [2 ],
241
+ LocationId : & ids [3 ],
242
+ },
243
+ }
244
+ err = resource .Retry (tccommon .WriteRetryTimeout , func () * resource.RetryError {
245
+ result , e := meta .(tccommon.ProviderMeta ).GetAPIV3Conn ().UseClbClient ().DisassociateTargetGroups (request )
246
+ if e != nil {
247
+ if sdkError , ok := e .(* sdkErrors.TencentCloudSDKError ); ok {
248
+ if sdkError .Code == "FailedOperation.ResourceInOperating" {
249
+ return resource .RetryableError (e )
250
+ }
251
+ }
252
+
253
+ return tccommon .RetryError (e )
254
+ } else {
255
+ log .Printf ("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n " , logId , request .GetAction (), request .ToJsonString (), result .ToJsonString ())
256
+ if result == nil || result .Response == nil || result .Response .RequestId == nil {
257
+ return resource .NonRetryableError (fmt .Errorf ("DisassociateTargetGroups response is nil." ))
258
+ }
259
+
260
+ requestId := * result .Response .RequestId
261
+ retryErr := waitForTaskFinish (requestId , meta .(tccommon.ProviderMeta ).GetAPIV3Conn ().UseClbClient ())
262
+ if retryErr != nil {
263
+ return tccommon .RetryError (errors .WithStack (retryErr ))
264
+ }
265
+ }
266
+
267
+ return nil
268
+ })
269
+
270
+ if err != nil {
213
271
return err
214
272
}
215
273
@@ -219,6 +277,7 @@ func resourceTencentCloudClbTargetGroupAttachmentDelete(d *schema.ResourceData,
219
277
if err != nil {
220
278
return tccommon .RetryError (err , tccommon .InternalError )
221
279
}
280
+
222
281
for _ , info := range targetInfos {
223
282
for _ , rule := range info .AssociatedRule {
224
283
var originLocationId string
@@ -227,6 +286,7 @@ func resourceTencentCloudClbTargetGroupAttachmentDelete(d *schema.ResourceData,
227
286
if rule .LocationId != nil {
228
287
originLocationId = * rule .LocationId
229
288
}
289
+
230
290
if * rule .Protocol == CLB_LISTENER_PROTOCOL_TCP || * rule .Protocol == CLB_LISTENER_PROTOCOL_UDP ||
231
291
* rule .Protocol == CLB_LISTENER_PROTOCOL_TCPSSL || * rule .Protocol == CLB_LISTENER_PROTOCOL_QUIC {
232
292
if originListenerId == ids [1 ] && originClbId == ids [2 ] {
@@ -242,8 +302,10 @@ func resourceTencentCloudClbTargetGroupAttachmentDelete(d *schema.ResourceData,
242
302
243
303
}
244
304
}
305
+
245
306
return nil
246
307
})
308
+
247
309
if err != nil {
248
310
return err
249
311
}
0 commit comments