@@ -34,7 +34,7 @@ resource "tencentcloud_clb_target_group_attachment" "group" {
3434 clb_id = tencentcloud_clb_instance.clb_basic.id
3535 listener_id = tencentcloud_clb_listener.listener_basic.id
3636 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
3838}
3939```
4040
@@ -62,6 +62,7 @@ func resourceTencentCloudClbTargetGroupAttachment() *schema.Resource {
6262 return & schema.Resource {
6363 Create : resourceTencentCloudClbTargetGroupAttachmentCreate ,
6464 Read : resourceTencentCloudClbTargetGroupAttachmentRead ,
65+ Update : resourceTencentCloudClbTargetGroupAttachmentUpdate ,
6566 Delete : resourceTencentCloudClbTargetGroupAttachmentDelete ,
6667 Importer : & schema.ResourceImporter {
6768 State : schema .ImportStatePassthrough ,
@@ -80,40 +81,61 @@ func resourceTencentCloudClbTargetGroupAttachment() *schema.Resource {
8081 Description : "ID of the CLB listener." ,
8182 },
8283 "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" : {
8390 Type : schema .TypeString ,
8491 ForceNew : true ,
85- Required : true ,
92+ Optional : true ,
8693 Description : "ID of the CLB target group." ,
8794 },
8895 "rule_id" : {
8996 Type : schema .TypeString ,
90- Required : true ,
97+ Optional : true ,
9198 ForceNew : true ,
9299 Description : "ID of the CLB listener rule." ,
93100 },
94101 },
95102 }
96103}
97-
98104func resourceTencentCloudClbTargetGroupAttachmentCreate (d * schema.ResourceData , meta interface {}) error {
99105 defer logElapsed ("resource.tencentcloud_clb_target_group_attachment.create" )()
100106
101107 var (
102108 clbService = ClbService {
103109 client : meta .(* TencentCloudClient ).apiV3Conn ,
104110 }
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
116122 )
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+ }
117139
118140 //check listenerId
119141 checkErr := ListenerIdCheck (listenerId )
@@ -125,26 +147,6 @@ func resourceTencentCloudClbTargetGroupAttachmentCreate(d *schema.ResourceData,
125147 if checkErr != nil {
126148 return checkErr
127149 }
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- }
148150
149151 //check target group
150152 err = resource .Retry (readRetryTimeout , func () * resource.RetryError {
@@ -158,7 +160,7 @@ func resourceTencentCloudClbTargetGroupAttachmentCreate(d *schema.ResourceData,
158160 return err
159161 }
160162 err = resource .Retry (readRetryTimeout , func () * resource.RetryError {
161- targetInfos , err = clbService .DescribeTargetGroups (ctx , targrtGroupId , nil )
163+ targetInfos , err = clbService .DescribeTargetGroups (ctx , targetGroupId , nil )
162164 if err != nil {
163165 return retryError (err , InternalError )
164166 }
@@ -171,22 +173,22 @@ func resourceTencentCloudClbTargetGroupAttachmentCreate(d *schema.ResourceData,
171173 return fmt .Errorf ("CLB instance needs to be in the same VPC as the backend target group" )
172174 }
173175
174- err = clbService .AssociateTargetGroups (ctx , listenerId , clbId , targrtGroupId , locationId )
176+ err = clbService .AssociateTargetGroups (ctx , listenerId , clbId , targetGroupId , locationId )
175177 if err != nil {
176178 return err
177179 }
178180
179181 // wait status
180- has , err = clbService .DescribeAssociateTargetGroups (ctx , []string {targrtGroupId , listenerId , clbId , locationId })
182+ has , err = clbService .DescribeAssociateTargetGroups (ctx , []string {targetGroupId , listenerId , clbId , locationId })
181183 if err != nil {
182184 return err
183185 }
184186 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 )
187189 }
188190
189- d .SetId (strings .Join ([]string {targrtGroupId , listenerId , clbId , locationId }, FILED_SP ))
191+ d .SetId (strings .Join ([]string {targetGroupId , listenerId , clbId , locationId }, FILED_SP ))
190192
191193 return resourceTencentCloudClbTargetGroupAttachmentRead (d , meta )
192194}
@@ -207,7 +209,7 @@ func resourceTencentCloudClbTargetGroupAttachmentRead(d *schema.ResourceData, me
207209
208210 ids := strings .Split (id , FILED_SP )
209211 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) " )
211213 }
212214
213215 has , err := clbService .DescribeAssociateTargetGroups (ctx , ids )
@@ -219,14 +221,21 @@ func resourceTencentCloudClbTargetGroupAttachmentRead(d *schema.ResourceData, me
219221 return nil
220222 }
221223
222- _ = d .Set ("targrt_group_id " , ids [0 ])
224+ _ = d .Set ("target_group_id " , ids [0 ])
223225 _ = d .Set ("listener_id" , ids [1 ])
224226 _ = 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+ }
226230
227231 return nil
228232}
229233
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+
230239func resourceTencentCloudClbTargetGroupAttachmentDelete (d * schema.ResourceData , meta interface {}) error {
231240 defer logElapsed ("resource.tencentcloud_clb_target_group_attachment.delete" )()
232241
@@ -243,7 +252,7 @@ func resourceTencentCloudClbTargetGroupAttachmentDelete(d *schema.ResourceData,
243252
244253 ids := strings .Split (id , FILED_SP )
245254 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) " )
247256 }
248257
249258 if err := clbService .DisassociateTargetGroups (ctx , ids [0 ], ids [1 ], ids [2 ], ids [3 ]); err != nil {
@@ -264,12 +273,18 @@ func resourceTencentCloudClbTargetGroupAttachmentDelete(d *schema.ResourceData,
264273 if rule .LocationId != nil {
265274 originLocationId = * rule .LocationId
266275 }
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 ] {
269283 return resource .RetryableError (
270284 fmt .Errorf ("rule association target group instance still exist. [targetGroupId=%s, listenerId=%s, cldId=%s, ruleId=%s]" ,
271285 ids [0 ], ids [1 ], ids [2 ], ids [3 ]))
272286 }
287+
273288 }
274289 }
275290 return nil
0 commit comments