Skip to content

Commit 92e743a

Browse files
tongyimingmikatong
andauthored
fix clb target (#2148)
* fix clb target * add changelog * update unit test --------- Co-authored-by: mikatong <mikatong@tencent.com>
1 parent cdaa060 commit 92e743a

File tree

6 files changed

+157
-102
lines changed

6 files changed

+157
-102
lines changed

.changelog/2148.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
resource/tencentcloud_clb_attachment: fix http backend delete problem.
3+
```

tencentcloud/basic_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,3 +1120,21 @@ variable "instance_id" {
11201120
`
11211121

11221122
// End of Clickhouse
1123+
1124+
// CLB
1125+
const clbTargetEniTestCase = instanceCommonTestCase + `
1126+
resource "tencentcloud_eni" "clb_eni_target" {
1127+
name = "ci-test-eni"
1128+
vpc_id = var.cvm_vpc_id
1129+
subnet_id = var.cvm_subnet_id
1130+
description = "clb eni backend"
1131+
ipv4_count = 1
1132+
}
1133+
1134+
resource "tencentcloud_eni_attachment" "foo" {
1135+
eni_id = tencentcloud_eni.clb_eni_target.id
1136+
instance_id = tencentcloud_instance.default.id
1137+
}
1138+
`
1139+
1140+
//End of Clb

tencentcloud/extension_clb.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,8 @@ const (
124124
CLB_TARGET_TYPE_NODE = "NODE"
125125
CLB_TARGET_TYPE_TARGETGROUP = "TARGETGROUP"
126126
)
127+
128+
const (
129+
CLB_BACKEND_TYPE_CVM = "CVM"
130+
CLB_BACKEND_TYPE_ENI = "ENI"
131+
)

tencentcloud/resource_tc_clb_attachment.go

Lines changed: 23 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -455,51 +455,25 @@ func resourceTencentCloudClbServerAttachmentRead(d *schema.ResourceData, meta in
455455
} else if instance.Targets != nil {
456456
onlineTargets = instance.Targets
457457
}
458-
459-
//this may cause problems when there are members in two dimensions array
460-
//need to read state of the tfstate file to clear the relationships
461-
//in this situation, import action is not supported
462-
// TL,DR: just update partial targets which this resource declared.
463-
stateTargets := d.Get("targets").(*schema.Set)
464-
if stateTargets.Len() != 0 {
465-
//the old state exist
466-
//create a new attachment with state
467-
exactTargets := make([]interface{}, 0)
468-
for i := range onlineTargets {
469-
v := onlineTargets[i]
470-
if *v.Type == "CVM" && v.InstanceId != nil {
471-
target := map[string]interface{}{
472-
"weight": int(*v.Weight),
473-
"port": int(*v.Port),
474-
"instance_id": *v.InstanceId,
475-
}
476-
if stateTargets.Contains(target) {
477-
exactTargets = append(exactTargets, map[string]interface{}{
478-
"weight": int(*v.Weight),
479-
"port": int(*v.Port),
480-
"instance_id": *v.InstanceId,
481-
})
482-
}
483-
484-
} else if len(v.PrivateIpAddresses) > 0 && *v.PrivateIpAddresses[0] != "" {
485-
target := map[string]interface{}{
486-
"weight": int(*v.Weight),
487-
"port": int(*v.Port),
488-
"eni_ip": *v.PrivateIpAddresses[0],
489-
}
490-
if stateTargets.Contains(target) {
491-
exactTargets = append(exactTargets, map[string]interface{}{
492-
"weight": int(*v.Weight),
493-
"port": int(*v.Port),
494-
"eni_ip": *v.PrivateIpAddresses[0],
495-
})
496-
}
458+
targets := make([]interface{}, 0)
459+
for _, onlineTarget := range onlineTargets {
460+
if *onlineTarget.Type == CLB_BACKEND_TYPE_CVM {
461+
target := map[string]interface{}{
462+
"weight": int(*onlineTarget.Weight),
463+
"port": int(*onlineTarget.Port),
464+
"instance_id": *onlineTarget.InstanceId,
497465
}
466+
targets = append(targets, target)
467+
} else if *onlineTarget.Type == CLB_BACKEND_TYPE_ENI {
468+
target := map[string]interface{}{
469+
"weight": int(*onlineTarget.Weight),
470+
"port": int(*onlineTarget.Port),
471+
"eni_ip": *onlineTarget.PrivateIpAddresses[0],
472+
}
473+
targets = append(targets, target)
498474
}
499-
_ = d.Set("targets", exactTargets)
500-
} else {
501-
_ = d.Set("targets", flattenBackendList(onlineTargets))
502475
}
476+
_ = d.Set("targets", targets)
503477

504478
return nil
505479
}
@@ -508,11 +482,15 @@ func resourceTencentCloudClbServerAttachmentRead(d *schema.ResourceData, meta in
508482
// If remove diffs created, check existing cvm instance first, filter target groups which already deregister
509483
func getRemoveCandidates(ctx context.Context, clbService ClbService, clbId string, listenerId string, locationId string, remove []interface{}) []interface{} {
510484
removeCandidates := make([]interface{}, 0)
511-
existAttachments, err := clbService.DescribeAttachmentByPara(ctx, clbId, listenerId, locationId)
485+
listenerBackend, err := clbService.DescribeAttachmentByPara(ctx, clbId, listenerId, locationId)
512486
if err != nil {
513487
return removeCandidates
514488
}
515-
existTargetGroups := existAttachments.Targets
489+
existTargetGroups := make([]*clb.Backend, 0)
490+
existTargetGroups = append(existTargetGroups, listenerBackend.Targets...)
491+
if len(listenerBackend.Rules) > 0 {
492+
existTargetGroups = append(existTargetGroups, listenerBackend.Rules[0].Targets...)
493+
}
516494

517495
for _, item := range remove {
518496
target := item.(map[string]interface{})
@@ -551,7 +529,7 @@ func targetGroupContainsEni(targets []*clb.Backend, eniIp interface{}) (contains
551529
return
552530
}
553531
for _, target := range targets {
554-
if len(target.PrivateIpAddresses) > 0 && target.PrivateIpAddresses[0] != nil {
532+
if len(target.PrivateIpAddresses) == 0 || target.PrivateIpAddresses[0] == nil {
555533
continue
556534
}
557535
if ip == *target.PrivateIpAddresses[0] {

0 commit comments

Comments
 (0)