Skip to content

Commit cdafb1e

Browse files
authored
fix(clb): [136875225] tencentcloud_clb_target_group_attachment update code (#3090)
* add * add
1 parent a55c835 commit cdafb1e

File tree

4 files changed

+228
-110
lines changed

4 files changed

+228
-110
lines changed

.changelog/3090.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
resource/tencentcloud_clb_target_group_attachment: update code
3+
```

tencentcloud/services/clb/resource_tc_clb_target_group_attachment.go

Lines changed: 129 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,23 @@ package clb
33
import (
44
"context"
55
"fmt"
6+
"log"
67
"strings"
78

9+
"github.com/pkg/errors"
810
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
11+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
912

1013
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1114
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1215
clb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb/v20180317"
16+
sdkErrors "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
1317
)
1418

1519
func ResourceTencentCloudClbTargetGroupAttachment() *schema.Resource {
1620
return &schema.Resource{
1721
Create: resourceTencentCloudClbTargetGroupAttachmentCreate,
1822
Read: resourceTencentCloudClbTargetGroupAttachmentRead,
19-
Update: resourceTencentCloudClbTargetGroupAttachmentUpdate,
2023
Delete: resourceTencentCloudClbTargetGroupAttachmentDelete,
2124
Importer: &schema.ResourceImporter{
2225
State: schema.ImportStatePassthrough,
@@ -28,23 +31,17 @@ func ResourceTencentCloudClbTargetGroupAttachment() *schema.Resource {
2831
Required: true,
2932
Description: "ID of the CLB.",
3033
},
31-
"listener_id": {
34+
"target_group_id": {
3235
Type: schema.TypeString,
3336
ForceNew: true,
3437
Required: true,
35-
Description: "ID of the CLB listener.",
36-
},
37-
"targrt_group_id": {
38-
Type: schema.TypeString,
39-
Optional: true,
4038
Description: "ID of the CLB target group.",
41-
Deprecated: "It has been deprecated from version 1.47.1. Use `target_group_id` instead.",
4239
},
43-
"target_group_id": {
40+
"listener_id": {
4441
Type: schema.TypeString,
4542
ForceNew: true,
4643
Optional: true,
47-
Description: "ID of the CLB target group.",
44+
Description: "ID of the CLB listener.",
4845
},
4946
"rule_id": {
5047
Type: schema.TypeString,
@@ -59,88 +56,122 @@ func resourceTencentCloudClbTargetGroupAttachmentCreate(d *schema.ResourceData,
5956
defer tccommon.LogElapsed("resource.tencentcloud_clb_target_group_attachment.create")()
6057

6158
var (
62-
clbService = ClbService{
63-
client: meta.(tccommon.ProviderMeta).GetAPIV3Conn(),
64-
}
59+
clbService = ClbService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
6560
logId = tccommon.GetLogId(tccommon.ContextNil)
6661
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
7067
targetGroupId string
71-
72-
targetInfos []*clb.TargetGroupInfo
73-
instance *clb.LoadBalancer
74-
has bool
75-
err error
68+
locationId string
7669
)
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)
7975
}
80-
vTarget, eHas := d.GetOk("target_group_id")
81-
vTargrt, rHas := d.GetOk("targrt_group_id")
8276

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)
9290
}
9391

9492
//check listenerId
9593
checkErr := ListenerIdCheck(listenerId)
9694
if checkErr != nil {
9795
return checkErr
9896
}
97+
9998
//check ruleId
10099
checkErr = RuleIdCheck(locationId)
101100
if checkErr != nil {
102101
return checkErr
103102
}
104103

105104
//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)
110109
}
110+
111+
if result == nil {
112+
return resource.NonRetryableError(fmt.Errorf("DescribeLoadBalancers response is nil."))
113+
}
114+
115+
instance = result
111116
return nil
112117
})
118+
113119
if err != nil {
114120
return err
115121
}
122+
116123
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)
120127
}
128+
129+
if result == nil {
130+
return resource.NonRetryableError(fmt.Errorf("DescribeTargetGroups response is nil."))
131+
}
132+
133+
targetInfos = result
121134
return nil
122135
})
136+
123137
if err != nil {
124138
return err
125139
}
140+
126141
if len(targetInfos) > 0 && (*targetInfos[0].VpcId != *instance.TargetRegionInfo.VpcId) {
127142
return fmt.Errorf("CLB instance needs to be in the same VPC as the backend target group")
128143
}
129144

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+
})
134171

135-
// wait status
136-
has, err = clbService.DescribeAssociateTargetGroups(ctx, []string{targetGroupId, listenerId, clbId, locationId})
137172
if err != nil {
138173
return err
139174
}
140-
if !has {
141-
return fmt.Errorf("AssociateTargetGroups faild, targetGroupId = %s, listenerId = %s, clbId = %s, ruleId = %s",
142-
targetGroupId, listenerId, clbId, locationId)
143-
}
144175

145176
d.SetId(strings.Join([]string{targetGroupId, listenerId, clbId, locationId}, tccommon.FILED_SP))
146177

@@ -152,13 +183,11 @@ func resourceTencentCloudClbTargetGroupAttachmentRead(d *schema.ResourceData, me
152183
defer tccommon.InconsistentCheck(d, meta)()
153184

154185
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
162191
)
163192

164193
ids := strings.Split(id, tccommon.FILED_SP)
@@ -170,6 +199,7 @@ func resourceTencentCloudClbTargetGroupAttachmentRead(d *schema.ResourceData, me
170199
if err != nil {
171200
return err
172201
}
202+
173203
if !has {
174204
d.SetId("")
175205
return nil
@@ -185,20 +215,14 @@ func resourceTencentCloudClbTargetGroupAttachmentRead(d *schema.ResourceData, me
185215
return nil
186216
}
187217

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-
193218
func resourceTencentCloudClbTargetGroupAttachmentDelete(d *schema.ResourceData, meta interface{}) error {
194219
defer tccommon.LogElapsed("resource.tencentcloud_clb_target_group_attachment.delete")()
195220

196221
var (
197-
clbService = ClbService{
198-
client: meta.(tccommon.ProviderMeta).GetAPIV3Conn(),
199-
}
200222
logId = tccommon.GetLogId(tccommon.ContextNil)
201223
ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
224+
clbService = ClbService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
225+
request = clb.NewDisassociateTargetGroupsRequest()
202226
id = d.Id()
203227
targetInfos []*clb.TargetGroupInfo
204228
err error
@@ -209,7 +233,41 @@ func resourceTencentCloudClbTargetGroupAttachmentDelete(d *schema.ResourceData,
209233
return fmt.Errorf("CLB target group attachment id is clb_id#listener_id#target_group_id#rule_id(only required for 7 layer CLB)")
210234
}
211235

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 {
213271
return err
214272
}
215273

@@ -219,6 +277,7 @@ func resourceTencentCloudClbTargetGroupAttachmentDelete(d *schema.ResourceData,
219277
if err != nil {
220278
return tccommon.RetryError(err, tccommon.InternalError)
221279
}
280+
222281
for _, info := range targetInfos {
223282
for _, rule := range info.AssociatedRule {
224283
var originLocationId string
@@ -227,6 +286,7 @@ func resourceTencentCloudClbTargetGroupAttachmentDelete(d *schema.ResourceData,
227286
if rule.LocationId != nil {
228287
originLocationId = *rule.LocationId
229288
}
289+
230290
if *rule.Protocol == CLB_LISTENER_PROTOCOL_TCP || *rule.Protocol == CLB_LISTENER_PROTOCOL_UDP ||
231291
*rule.Protocol == CLB_LISTENER_PROTOCOL_TCPSSL || *rule.Protocol == CLB_LISTENER_PROTOCOL_QUIC {
232292
if originListenerId == ids[1] && originClbId == ids[2] {
@@ -242,8 +302,10 @@ func resourceTencentCloudClbTargetGroupAttachmentDelete(d *schema.ResourceData,
242302

243303
}
244304
}
305+
245306
return nil
246307
})
308+
247309
if err != nil {
248310
return err
249311
}

0 commit comments

Comments
 (0)