Skip to content

Commit e44dc0c

Browse files
authored
Merge pull request #664 from tencentcloudstack/fix/clb-target-groups-sync
fix: clb - target group check before deregister
2 parents 65ff495 + ad6c4b8 commit e44dc0c

File tree

3 files changed

+162
-12
lines changed

3 files changed

+162
-12
lines changed

go.sum

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Z
173173
github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
174174
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
175175
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
176-
github.com/google/go-cmp v0.3.1 h1:Xye71clBPdm5HgqGwUkwhbynsUJZhDbS20FvLhQ2izg=
177176
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
178177
github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM=
179178
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
@@ -468,8 +467,6 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb v1.0.199/go.mod h1:
468467
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cloudaudit v1.0.199 h1:o41qFAFJGPDTLNWXs7nLw4fsDxFUCe5gkO2YXI9Ye6Q=
469468
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cloudaudit v1.0.199/go.mod h1:b7dNjabPys0/iLwRFd8MVE5EkJTNAh4qtaHQOOLchx4=
470469
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.194/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
471-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.199 h1:TeLKOemumLTjWpkRKNVNhpb7VMDlOPaVEuukrWmab30=
472-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.199/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
473470
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.234 h1:yAtw4jVBsQZ/KcM2nMHRzcpIfSXRw0Alt7wVTR9OodM=
474471
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.234/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
475472
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.199 h1:ajgJogYSIQ5u1PIbiV5nsvr5K0fYpm1/T7Dy+mxEM6U=
@@ -511,8 +508,6 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tcaplusdb v1.0.199 h1:i
511508
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tcaplusdb v1.0.199/go.mod h1:PUgbrkzA9IaKBj1urk+W4L6Jr5TuBhQ4xB/96QvLf/U=
512509
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tcr v1.0.199 h1:ku4oDXWK1rzfwFXd9q8eB3DktWNTHnkTiX9z5Ay8hqs=
513510
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tcr v1.0.199/go.mod h1:SEUO10oGtg+4AGCfpJDn9ynf47P+ZiyvhzOyXLt0mOY=
514-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke v1.0.199 h1:AHlrfOhAZnmwQOZ4aitzOb3Tzzw3LBKViPtI40F+ldI=
515-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke v1.0.199/go.mod h1:ij3CHdPvqI2aSMcl7+jdI0yCO7oOiywKTAa55qmO2iI=
516511
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke v1.0.234 h1:bJU0a3yEir4BHTiIHgLvsVqDAFeuHe/r3PML3V92R/o=
517512
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke v1.0.234/go.mod h1:ij3CHdPvqI2aSMcl7+jdI0yCO7oOiywKTAa55qmO2iI=
518513
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vod v1.0.199 h1:6Yt74l4pA5QtzhwMNIEUt0spXdSBKH744DDqTHJOCP0=
@@ -521,8 +516,6 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc v1.0.199 h1:UDZ59pv
521516
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc v1.0.199/go.mod h1:SKgeSsIfPEM6BeoIFiGHsWG9UsEXzkK0SkWx51H/OS8=
522517
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/wss v1.0.199 h1:hMBLtiJPnZ9GvA677cTB6ELBR6B68wCR2QY1sNoGQc4=
523518
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/wss v1.0.199/go.mod h1:nnY91/H3j/Gu7V/oCA6Zeg8T5D3q36EUdBh4EjmHwqY=
524-
github.com/tencentyun/cos-go-sdk-v5 v0.7.29 h1:uwRBzc70Wgtc5iQQCowqecfRT0OpCXUOZzodZHOOEDs=
525-
github.com/tencentyun/cos-go-sdk-v5 v0.7.29/go.mod h1:4E4+bQ2gBVJcgEC9Cufwylio4mXOct2iu05WjgEBx1o=
526519
github.com/tencentyun/cos-go-sdk-v5 v0.7.31-0.20210902132439-360bc9b1be6b h1:rLl5sAeLt382023Kd3X4TaOEaT2hdgXWwTGyKiy16Zo=
527520
github.com/tencentyun/cos-go-sdk-v5 v0.7.31-0.20210902132439-360bc9b1be6b/go.mod h1:4E4+bQ2gBVJcgEC9Cufwylio4mXOct2iu05WjgEBx1o=
528521
github.com/tetafro/godot v0.3.7 h1:+mecr7RKrUKB5UQ1gwqEMn13sDKTyDR8KNIquB9mm+8=

tencentcloud/resource_tc_clb_attachment.go

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ func resourceTencentCloudClbServerAttachmentDelete(d *schema.ResourceData, meta
250250
return nil
251251
}
252252

253-
func resourceTencentCloudClbServerAttachementRemove(d *schema.ResourceData, meta interface{}, remove []interface{}) error {
253+
func resourceTencentCloudClbServerAttachmentRemove(d *schema.ResourceData, meta interface{}, remove []interface{}) error {
254254
defer logElapsed("resource.tencentcloud_clb_attachment.remove")()
255255

256256
logId := getLogId(contextNil)
@@ -276,7 +276,11 @@ func resourceTencentCloudClbServerAttachementRemove(d *schema.ResourceData, meta
276276
}
277277

278278
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
279-
e := clbService.DeleteAttachmentById(ctx, clbId, listenerId, locationId, remove)
279+
removeCandidates := getRemoveCandidates(ctx, clbService, clbId, listenerId, locationId, remove)
280+
if len(removeCandidates) == 0 {
281+
return nil
282+
}
283+
e := clbService.DeleteAttachmentById(ctx, clbId, listenerId, locationId, removeCandidates)
280284
if e != nil {
281285
return retryError(e)
282286
}
@@ -291,7 +295,7 @@ func resourceTencentCloudClbServerAttachementRemove(d *schema.ResourceData, meta
291295
return nil
292296
}
293297

294-
func resourceTencentCloudClbServerAttachementAdd(d *schema.ResourceData, meta interface{}, add []interface{}) error {
298+
func resourceTencentCloudClbServerAttachmentAdd(d *schema.ResourceData, meta interface{}, add []interface{}) error {
295299
defer logElapsed("resource.tencentcloud_clb_attachment.add")()
296300
logId := getLogId(contextNil)
297301

@@ -349,13 +353,13 @@ func resourceTencentCloudClbServerAttachmentUpdate(d *schema.ResourceData, meta
349353
add := ns.Difference(os).List()
350354
remove := os.Difference(ns).List()
351355
if len(remove) > 0 {
352-
err := resourceTencentCloudClbServerAttachementRemove(d, meta, remove)
356+
err := resourceTencentCloudClbServerAttachmentRemove(d, meta, remove)
353357
if err != nil {
354358
return err
355359
}
356360
}
357361
if len(add) > 0 {
358-
err := resourceTencentCloudClbServerAttachementAdd(d, meta, add)
362+
err := resourceTencentCloudClbServerAttachmentAdd(d, meta, add)
359363
if err != nil {
360364
return err
361365
}
@@ -443,3 +447,35 @@ func resourceTencentCloudClbServerAttachmentRead(d *schema.ResourceData, meta in
443447

444448
return nil
445449
}
450+
451+
// Destroy CVM instance will dispatch async task to deregister target group from cloudApi backend. Duplicate deregister target groups here will cause Error response.
452+
// If remove diffs created, check existing cvm instance first, filter target groups which already deregister
453+
func getRemoveCandidates(ctx context.Context, clbService ClbService, clbId string, listenerId string, locationId string, remove []interface{}) []interface{} {
454+
removeCandidates := make([]interface{}, 0)
455+
existAttachments, err := clbService.DescribeAttachmentByPara(ctx, clbId, listenerId, locationId)
456+
if err != nil {
457+
return removeCandidates
458+
}
459+
existTargetGroups := existAttachments.Targets
460+
461+
for _, item := range remove {
462+
target := item.(map[string]interface{})
463+
if targetGroupContainsInstance(existTargetGroups, target["instance_id"].(string)) {
464+
removeCandidates = append(removeCandidates, target)
465+
}
466+
}
467+
468+
return removeCandidates
469+
}
470+
471+
func targetGroupContainsInstance(targets []*clb.Backend, instanceId string) (contains bool) {
472+
contains = false
473+
for _, target := range targets {
474+
if instanceId == *target.InstanceId {
475+
log.Printf("[WARN] Instance %s applied.", instanceId)
476+
return true
477+
}
478+
}
479+
log.Printf("[WARN] Instance %s not exist, skip deregister.", instanceId)
480+
return
481+
}

tencentcloud/resource_tc_clb_attachment_test.go

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,32 @@ func testAccCheckClbServerAttachmentDestroy(s *terraform.State) error {
9292
return nil
9393
}
9494

95+
func testAccCheckClbServerAttachmentTargetGroups(t *testing.T) {
96+
resource.Test(t, resource.TestCase{
97+
PreCheck: func() {
98+
testAccPreCheck(t)
99+
},
100+
Providers: testAccProviders,
101+
CheckDestroy: testAccCheckClbServerAttachmentDestroy,
102+
Steps: []resource.TestStep{
103+
{
104+
Config: fmt.Sprintf(testAccClbServerAttachment_multiple, defaultSshCertificate),
105+
Check: resource.ComposeTestCheckFunc(
106+
testAccCheckClbServerAttachmentExists("tencentcloud_clb_attachment.foo"),
107+
resource.TestCheckResourceAttr("tencentcloud_clb_attachment.foo", "targets.#", "2"),
108+
),
109+
},
110+
{
111+
Config: fmt.Sprintf(testAccClbServerAttachment_multiple_update, defaultSshCertificate),
112+
Check: resource.ComposeTestCheckFunc(
113+
testAccCheckClbServerAttachmentExists("tencentcloud_clb_attachment.foo"),
114+
resource.TestCheckResourceAttr("tencentcloud_clb_attachment.foo", "targets.#", "1"),
115+
),
116+
},
117+
},
118+
})
119+
}
120+
95121
func testAccCheckClbServerAttachmentExists(n string) resource.TestCheckFunc {
96122
return func(s *terraform.State) error {
97123
logId := getLogId(contextNil)
@@ -228,3 +254,98 @@ resource "tencentcloud_clb_attachment" "foo" {
228254
}
229255
}
230256
`
257+
258+
const testAccClbServerAttachment_multiple = instanceCommonTestCase + `
259+
resource "tencentcloud_instance" "update" {
260+
instance_name = var.instance_name_update
261+
availability_zone = data.tencentcloud_availability_zones.default.zones.0.name
262+
image_id = data.tencentcloud_images.default.images.0.image_id
263+
instance_type = data.tencentcloud_instance_types.default.instance_types.0.instance_type
264+
system_disk_type = "CLOUD_PREMIUM"
265+
system_disk_size = 50
266+
allocate_public_ip = true
267+
internet_max_bandwidth_out = 10
268+
vpc_id = var.vpc_id
269+
subnet_id = var.subnet_id
270+
}
271+
272+
resource "tencentcloud_clb_instance" "foo" {
273+
network_type = "OPEN"
274+
clb_name = var.instance_name
275+
vpc_id = var.vpc_id
276+
}
277+
278+
resource "tencentcloud_clb_listener" "foo" {
279+
clb_id = tencentcloud_clb_instance.foo.id
280+
listener_name = var.instance_name
281+
port = 77
282+
protocol = "HTTPS"
283+
certificate_ssl_mode = "UNIDIRECTIONAL"
284+
certificate_id = "%s"
285+
}
286+
287+
resource "tencentcloud_clb_listener_rule" "foo" {
288+
clb_id = tencentcloud_clb_instance.foo.id
289+
listener_id = tencentcloud_clb_listener.foo.id
290+
domain = "abc.com"
291+
url = "/"
292+
session_expire_time = 30
293+
scheduler = "WRR"
294+
}
295+
296+
resource "tencentcloud_clb_attachment" "foo" {
297+
clb_id = tencentcloud_clb_instance.foo.id
298+
listener_id = tencentcloud_clb_listener.foo.id
299+
rule_id = tencentcloud_clb_listener_rule.foo.id
300+
301+
targets {
302+
instance_id = tencentcloud_instance.default.id
303+
port = 23
304+
weight = 10
305+
}
306+
targets {
307+
instance_id = tencentcloud_instance.update.id
308+
port = 24
309+
weight = 10
310+
}
311+
}
312+
`
313+
314+
const testAccClbServerAttachment_multiple_update = instanceCommonTestCase + `
315+
316+
resource "tencentcloud_clb_instance" "foo" {
317+
network_type = "OPEN"
318+
clb_name = var.instance_name
319+
vpc_id = var.vpc_id
320+
}
321+
322+
resource "tencentcloud_clb_listener" "foo" {
323+
clb_id = tencentcloud_clb_instance.foo.id
324+
listener_name = var.instance_name
325+
port = 77
326+
protocol = "HTTPS"
327+
certificate_ssl_mode = "UNIDIRECTIONAL"
328+
certificate_id = "%s"
329+
}
330+
331+
resource "tencentcloud_clb_listener_rule" "foo" {
332+
clb_id = tencentcloud_clb_instance.foo.id
333+
listener_id = tencentcloud_clb_listener.foo.id
334+
domain = "abc.com"
335+
url = "/"
336+
session_expire_time = 30
337+
scheduler = "WRR"
338+
}
339+
340+
resource "tencentcloud_clb_attachment" "foo" {
341+
clb_id = tencentcloud_clb_instance.foo.id
342+
listener_id = tencentcloud_clb_listener.foo.id
343+
rule_id = tencentcloud_clb_listener_rule.foo.id
344+
345+
targets {
346+
instance_id = tencentcloud_instance.default.id
347+
port = 23
348+
weight = 10
349+
}
350+
}
351+
`

0 commit comments

Comments
 (0)