@@ -34,7 +34,7 @@ resource "tencentcloud_clb_target_group_attachment" "group" {
34
34
clb_id = tencentcloud_clb_instance.clb_basic.id
35
35
listener_id = tencentcloud_clb_listener.listener_basic.id
36
36
rule_id = tencentcloud_clb_listener_rule.rule_basic.id
37
- targrt_group_id = tencentcloud_clb_target_group.test.id
37
+ target_group_id = tencentcloud_clb_target_group.test.id
38
38
}
39
39
```
40
40
@@ -62,6 +62,7 @@ func resourceTencentCloudClbTargetGroupAttachment() *schema.Resource {
62
62
return & schema.Resource {
63
63
Create : resourceTencentCloudClbTargetGroupAttachmentCreate ,
64
64
Read : resourceTencentCloudClbTargetGroupAttachmentRead ,
65
+ Update : resourceTencentCloudClbTargetGroupAttachmentUpdate ,
65
66
Delete : resourceTencentCloudClbTargetGroupAttachmentDelete ,
66
67
Importer : & schema.ResourceImporter {
67
68
State : schema .ImportStatePassthrough ,
@@ -80,40 +81,61 @@ func resourceTencentCloudClbTargetGroupAttachment() *schema.Resource {
80
81
Description : "ID of the CLB listener." ,
81
82
},
82
83
"targrt_group_id" : {
84
+ Type : schema .TypeString ,
85
+ Optional : true ,
86
+ Description : "ID of the CLB target group." ,
87
+ Deprecated : "It has been deprecated from version 1.47.1. Use `target_group_id` instead." ,
88
+ },
89
+ "target_group_id" : {
83
90
Type : schema .TypeString ,
84
91
ForceNew : true ,
85
- Required : true ,
92
+ Optional : true ,
86
93
Description : "ID of the CLB target group." ,
87
94
},
88
95
"rule_id" : {
89
96
Type : schema .TypeString ,
90
- Required : true ,
97
+ Optional : true ,
91
98
ForceNew : true ,
92
99
Description : "ID of the CLB listener rule." ,
93
100
},
94
101
},
95
102
}
96
103
}
97
-
98
104
func resourceTencentCloudClbTargetGroupAttachmentCreate (d * schema.ResourceData , meta interface {}) error {
99
105
defer logElapsed ("resource.tencentcloud_clb_target_group_attachment.create" )()
100
106
101
107
var (
102
108
clbService = ClbService {
103
109
client : meta .(* TencentCloudClient ).apiV3Conn ,
104
110
}
105
- logId = getLogId (contextNil )
106
- ctx = context .WithValue (context .TODO (), logIdKey , logId )
107
- listenerId = d . Get ( "listener_id" ).( string )
108
- clbId = d .Get ("clb_id " ).(string )
109
- targrtGroupId = d .Get ("targrt_group_id " ).(string )
110
- locationId = d . Get ( "rule_id" ).( string )
111
- targetInfos [] * clb. TargetGroupInfo
112
- instance * clb.LoadBalancer
113
- listener * clb.Listener
114
- isRuleExist , has bool
115
- err error
111
+ logId = getLogId (contextNil )
112
+ ctx = context .WithValue (context .TODO (), logIdKey , logId )
113
+ locationId string
114
+ listenerId = d .Get ("listener_id " ).(string )
115
+ clbId = d .Get ("clb_id " ).(string )
116
+ targetGroupId string
117
+
118
+ targetInfos [] * clb.TargetGroupInfo
119
+ instance * clb.LoadBalancer
120
+ has bool
121
+ err error
116
122
)
123
+ if v , ok := d .GetOk ("rule_id" ); ok {
124
+ locationId = v .(string )
125
+ }
126
+ vTarget , eHas := d .GetOk ("target_group_id" )
127
+ vTargrt , rHas := d .GetOk ("targrt_group_id" )
128
+
129
+ if eHas || rHas {
130
+ if rHas {
131
+ targetGroupId = vTargrt .(string )
132
+ }
133
+ if eHas {
134
+ targetGroupId = vTarget .(string )
135
+ }
136
+ } else {
137
+ return fmt .Errorf ("'target_group_id' or 'targrt_group_id' at least set one, please use 'target_group_id'" )
138
+ }
117
139
118
140
//check listenerId
119
141
checkErr := ListenerIdCheck (listenerId )
@@ -125,26 +147,6 @@ func resourceTencentCloudClbTargetGroupAttachmentCreate(d *schema.ResourceData,
125
147
if checkErr != nil {
126
148
return checkErr
127
149
}
128
- //check rule
129
- err = resource .Retry (readRetryTimeout , func () * resource.RetryError {
130
- listener , err = clbService .DescribeListenerById (ctx , listenerId , clbId )
131
- if err != nil {
132
- return retryError (err , InternalError )
133
- }
134
- return nil
135
- })
136
- if err != nil {
137
- return err
138
- }
139
- for _ , rule := range listener .Rules {
140
- if locationId == * rule .LocationId && (rule .TargetType != nil && * rule .TargetType == CLB_TARGET_TYPE_TARGETGROUP ) {
141
- isRuleExist = true
142
- break
143
- }
144
- }
145
- if ! isRuleExist {
146
- return fmt .Errorf ("rule bound to the listener of the CLB instance does not exist or the rule not in targetgroup mode" )
147
- }
148
150
149
151
//check target group
150
152
err = resource .Retry (readRetryTimeout , func () * resource.RetryError {
@@ -158,7 +160,7 @@ func resourceTencentCloudClbTargetGroupAttachmentCreate(d *schema.ResourceData,
158
160
return err
159
161
}
160
162
err = resource .Retry (readRetryTimeout , func () * resource.RetryError {
161
- targetInfos , err = clbService .DescribeTargetGroups (ctx , targrtGroupId , nil )
163
+ targetInfos , err = clbService .DescribeTargetGroups (ctx , targetGroupId , nil )
162
164
if err != nil {
163
165
return retryError (err , InternalError )
164
166
}
@@ -171,22 +173,22 @@ func resourceTencentCloudClbTargetGroupAttachmentCreate(d *schema.ResourceData,
171
173
return fmt .Errorf ("CLB instance needs to be in the same VPC as the backend target group" )
172
174
}
173
175
174
- err = clbService .AssociateTargetGroups (ctx , listenerId , clbId , targrtGroupId , locationId )
176
+ err = clbService .AssociateTargetGroups (ctx , listenerId , clbId , targetGroupId , locationId )
175
177
if err != nil {
176
178
return err
177
179
}
178
180
179
181
// wait status
180
- has , err = clbService .DescribeAssociateTargetGroups (ctx , []string {targrtGroupId , listenerId , clbId , locationId })
182
+ has , err = clbService .DescribeAssociateTargetGroups (ctx , []string {targetGroupId , listenerId , clbId , locationId })
181
183
if err != nil {
182
184
return err
183
185
}
184
186
if ! has {
185
- return fmt .Errorf ("AssociateTargetGroups faild, targrtGroupId = %s, listenerId = %s, clbId = %s, ruleId = %s" ,
186
- targrtGroupId , listenerId , clbId , locationId )
187
+ return fmt .Errorf ("AssociateTargetGroups faild, targetGroupId = %s, listenerId = %s, clbId = %s, ruleId = %s" ,
188
+ targetGroupId , listenerId , clbId , locationId )
187
189
}
188
190
189
- d .SetId (strings .Join ([]string {targrtGroupId , listenerId , clbId , locationId }, FILED_SP ))
191
+ d .SetId (strings .Join ([]string {targetGroupId , listenerId , clbId , locationId }, FILED_SP ))
190
192
191
193
return resourceTencentCloudClbTargetGroupAttachmentRead (d , meta )
192
194
}
@@ -207,7 +209,7 @@ func resourceTencentCloudClbTargetGroupAttachmentRead(d *schema.ResourceData, me
207
209
208
210
ids := strings .Split (id , FILED_SP )
209
211
if len (ids ) != 4 {
210
- return fmt .Errorf ("CLB target group attachment id must contains clb_id, listernrt_id, targrt_group_id, rule_id " )
212
+ return fmt .Errorf ("CLB target group attachment id is clb_id#listener_id#target_group_id#rule_id(only required for 7 layer CLB) " )
211
213
}
212
214
213
215
has , err := clbService .DescribeAssociateTargetGroups (ctx , ids )
@@ -219,14 +221,21 @@ func resourceTencentCloudClbTargetGroupAttachmentRead(d *schema.ResourceData, me
219
221
return nil
220
222
}
221
223
222
- _ = d .Set ("targrt_group_id " , ids [0 ])
224
+ _ = d .Set ("target_group_id " , ids [0 ])
223
225
_ = d .Set ("listener_id" , ids [1 ])
224
226
_ = d .Set ("clb_id" , ids [2 ])
225
- _ = d .Set ("rule_id" , ids [3 ])
227
+ if ids [3 ] != "" {
228
+ _ = d .Set ("rule_id" , ids [3 ])
229
+ }
226
230
227
231
return nil
228
232
}
229
233
234
+ func resourceTencentCloudClbTargetGroupAttachmentUpdate (d * schema.ResourceData , meta interface {}) error {
235
+ defer logElapsed ("resource.tencentcloud_clb_target_group_attachment.update" )()
236
+ return resourceTencentCloudClbTargetGroupAttachmentRead (d , meta )
237
+ }
238
+
230
239
func resourceTencentCloudClbTargetGroupAttachmentDelete (d * schema.ResourceData , meta interface {}) error {
231
240
defer logElapsed ("resource.tencentcloud_clb_target_group_attachment.delete" )()
232
241
@@ -243,7 +252,7 @@ func resourceTencentCloudClbTargetGroupAttachmentDelete(d *schema.ResourceData,
243
252
244
253
ids := strings .Split (id , FILED_SP )
245
254
if len (ids ) != 4 {
246
- return fmt .Errorf ("CLB target group attachment id must contains clb_id, listernrt_id, targrt_group_id, rule_id " )
255
+ return fmt .Errorf ("CLB target group attachment id is clb_id#listener_id#target_group_id#rule_id(only required for 7 layer CLB) " )
247
256
}
248
257
249
258
if err := clbService .DisassociateTargetGroups (ctx , ids [0 ], ids [1 ], ids [2 ], ids [3 ]); err != nil {
@@ -264,12 +273,18 @@ func resourceTencentCloudClbTargetGroupAttachmentDelete(d *schema.ResourceData,
264
273
if rule .LocationId != nil {
265
274
originLocationId = * rule .LocationId
266
275
}
267
-
268
- if originListenerId == ids [1 ] && originClbId == ids [2 ] && originLocationId == ids [3 ] {
276
+ if * rule .Protocol == CLB_LISTENER_PROTOCOL_TCP || * rule .Protocol == CLB_LISTENER_PROTOCOL_UDP || * rule .Protocol == CLB_LISTENER_PROTOCOL_TCPSSL {
277
+ if originListenerId == ids [1 ] && originClbId == ids [2 ] {
278
+ return resource .RetryableError (
279
+ fmt .Errorf ("rule association target group instance still exist. [targetGroupId=%s, listenerId=%s, cldId=%s]" ,
280
+ ids [0 ], ids [1 ], ids [2 ]))
281
+ }
282
+ } else if originListenerId == ids [1 ] && originClbId == ids [2 ] && originLocationId == ids [3 ] {
269
283
return resource .RetryableError (
270
284
fmt .Errorf ("rule association target group instance still exist. [targetGroupId=%s, listenerId=%s, cldId=%s, ruleId=%s]" ,
271
285
ids [0 ], ids [1 ], ids [2 ], ids [3 ]))
272
286
}
287
+
273
288
}
274
289
}
275
290
return nil
0 commit comments