11/*
22Provides a resource to create a CLB target group attachment is bound to the load balancing listener or forwarding rule.
33
4+ ~> **NOTE:** Required argument `targrt_group_id` is no longer supported, replace by `target_group_id`.
5+
46Example Usage
57
68```hcl
@@ -34,7 +36,7 @@ resource "tencentcloud_clb_target_group_attachment" "group" {
3436 clb_id = tencentcloud_clb_instance.clb_basic.id
3537 listener_id = tencentcloud_clb_listener.listener_basic.id
3638 rule_id = tencentcloud_clb_listener_rule.rule_basic.id
37- targrt_group_id = tencentcloud_clb_target_group.test.id
39+ target_group_id = tencentcloud_clb_target_group.test.id
3840}
3941```
4042
@@ -52,6 +54,7 @@ import (
5254 "context"
5355 "fmt"
5456 "strings"
57+ "time"
5558
5659 "github.com/hashicorp/terraform-plugin-sdk/helper/resource"
5760 "github.com/hashicorp/terraform-plugin-sdk/helper/schema"
@@ -80,41 +83,50 @@ func resourceTencentCloudClbTargetGroupAttachment() *schema.Resource {
8083 Description : "ID of the CLB listener." ,
8184 },
8285 "targrt_group_id" : {
86+ Type : schema .TypeString ,
87+ ForceNew : true ,
88+ Optional : true ,
89+ Description : "ID of the CLB target group." ,
90+ Deprecated : "It has been deprecated from version 1.47.1. Use `target_group_id` instead." ,
91+ },
92+ "target_group_id" : {
8393 Type : schema .TypeString ,
8494 ForceNew : true ,
8595 Required : true ,
8696 Description : "ID of the CLB target group." ,
8797 },
8898 "rule_id" : {
8999 Type : schema .TypeString ,
90- Required : true ,
100+ Optional : true ,
91101 ForceNew : true ,
92102 Description : "ID of the CLB listener rule." ,
93103 },
94104 },
95105 }
96106}
97-
98107func resourceTencentCloudClbTargetGroupAttachmentCreate (d * schema.ResourceData , meta interface {}) error {
99108 defer logElapsed ("resource.tencentcloud_clb_target_group_attachment.create" )()
100109
101110 var (
102111 clbService = ClbService {
103112 client : meta .(* TencentCloudClient ).apiV3Conn ,
104113 }
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
116- )
114+ logId = getLogId (contextNil )
115+ ctx = context .WithValue (context .TODO (), logIdKey , logId )
116+ locationId string
117+ listenerId = d .Get ("listener_id" ).(string )
118+ clbId = d .Get ("clb_id" ).(string )
119+ targetGroupId = d .Get ("target_group_id" ).(string )
117120
121+ targetInfos []* clb.TargetGroupInfo
122+ instance * clb.LoadBalancer
123+ //listener *clb.Listener
124+ has bool
125+ err error
126+ )
127+ if v , ok := d .GetOk ("rule_id" ); ok {
128+ locationId = v .(string )
129+ }
118130 //check listenerId
119131 checkErr := ListenerIdCheck (listenerId )
120132 if checkErr != nil {
@@ -125,26 +137,6 @@ func resourceTencentCloudClbTargetGroupAttachmentCreate(d *schema.ResourceData,
125137 if checkErr != nil {
126138 return checkErr
127139 }
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- }
148140
149141 //check target group
150142 err = resource .Retry (readRetryTimeout , func () * resource.RetryError {
@@ -158,7 +150,7 @@ func resourceTencentCloudClbTargetGroupAttachmentCreate(d *schema.ResourceData,
158150 return err
159151 }
160152 err = resource .Retry (readRetryTimeout , func () * resource.RetryError {
161- targetInfos , err = clbService .DescribeTargetGroups (ctx , targrtGroupId , nil )
153+ targetInfos , err = clbService .DescribeTargetGroups (ctx , targetGroupId , nil )
162154 if err != nil {
163155 return retryError (err , InternalError )
164156 }
@@ -171,22 +163,22 @@ func resourceTencentCloudClbTargetGroupAttachmentCreate(d *schema.ResourceData,
171163 return fmt .Errorf ("CLB instance needs to be in the same VPC as the backend target group" )
172164 }
173165
174- err = clbService .AssociateTargetGroups (ctx , listenerId , clbId , targrtGroupId , locationId )
166+ err = clbService .AssociateTargetGroups (ctx , listenerId , clbId , targetGroupId , locationId )
175167 if err != nil {
176168 return err
177169 }
178170
179171 // wait status
180- has , err = clbService .DescribeAssociateTargetGroups (ctx , []string {targrtGroupId , listenerId , clbId , locationId })
172+ has , err = clbService .DescribeAssociateTargetGroups (ctx , []string {targetGroupId , listenerId , clbId , locationId })
181173 if err != nil {
182174 return err
183175 }
184176 if ! has {
185- return fmt .Errorf ("AssociateTargetGroups faild, targrtGroupId = %s, listenerId = %s, clbId = %s, ruleId = %s" ,
186- targrtGroupId , listenerId , clbId , locationId )
177+ return fmt .Errorf ("AssociateTargetGroups faild, targetGroupId = %s, listenerId = %s, clbId = %s, ruleId = %s" ,
178+ targetGroupId , listenerId , clbId , locationId )
187179 }
188180
189- d .SetId (strings .Join ([]string {targrtGroupId , listenerId , clbId , locationId }, FILED_SP ))
181+ d .SetId (strings .Join ([]string {targetGroupId , listenerId , clbId , locationId }, FILED_SP ))
190182
191183 return resourceTencentCloudClbTargetGroupAttachmentRead (d , meta )
192184}
@@ -207,7 +199,7 @@ func resourceTencentCloudClbTargetGroupAttachmentRead(d *schema.ResourceData, me
207199
208200 ids := strings .Split (id , FILED_SP )
209201 if len (ids ) != 4 {
210- return fmt .Errorf ("CLB target group attachment id must contains clb_id, listernrt_id, targrt_group_id , rule_id" )
202+ return fmt .Errorf ("CLB target group attachment id must contains clb_id, listernrt_id, target_group_id , rule_id" )
211203 }
212204
213205 has , err := clbService .DescribeAssociateTargetGroups (ctx , ids )
@@ -219,10 +211,12 @@ func resourceTencentCloudClbTargetGroupAttachmentRead(d *schema.ResourceData, me
219211 return nil
220212 }
221213
222- _ = d .Set ("targrt_group_id " , ids [0 ])
214+ _ = d .Set ("target_group_id " , ids [0 ])
223215 _ = d .Set ("listener_id" , ids [1 ])
224216 _ = d .Set ("clb_id" , ids [2 ])
225- _ = d .Set ("rule_id" , ids [3 ])
217+ if ids [3 ] != "" {
218+ _ = d .Set ("rule_id" , ids [3 ])
219+ }
226220
227221 return nil
228222}
@@ -243,12 +237,13 @@ func resourceTencentCloudClbTargetGroupAttachmentDelete(d *schema.ResourceData,
243237
244238 ids := strings .Split (id , FILED_SP )
245239 if len (ids ) != 4 {
246- return fmt .Errorf ("CLB target group attachment id must contains clb_id, listernrt_id, targrt_group_id , rule_id" )
240+ return fmt .Errorf ("CLB target group attachment id must contains clb_id, listernrt_id, target_group_id , rule_id" )
247241 }
248242
249243 if err := clbService .DisassociateTargetGroups (ctx , ids [0 ], ids [1 ], ids [2 ], ids [3 ]); err != nil {
250244 return err
251245 }
246+ time .Sleep (10 * time .Second )
252247
253248 // check status
254249 err = resource .Retry (readRetryTimeout , func () * resource.RetryError {
@@ -265,7 +260,7 @@ func resourceTencentCloudClbTargetGroupAttachmentDelete(d *schema.ResourceData,
265260 originLocationId = * rule .LocationId
266261 }
267262
268- if originListenerId == ids [1 ] && originClbId == ids [2 ] && originLocationId == ids [3 ] {
263+ if originListenerId == ids [1 ] && originClbId == ids [2 ] || originLocationId == ids [3 ] {
269264 return resource .RetryableError (
270265 fmt .Errorf ("rule association target group instance still exist. [targetGroupId=%s, listenerId=%s, cldId=%s, ruleId=%s]" ,
271266 ids [0 ], ids [1 ], ids [2 ], ids [3 ]))
0 commit comments