Skip to content

Commit 66c8944

Browse files
author
hellertang
authored
add clb sweeper (#929)
1 parent bd1bd77 commit 66c8944

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

tencentcloud/resource_tc_clb_instance_test.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import (
44
"context"
55
"fmt"
66
"log"
7+
"strings"
78
"testing"
9+
"time"
810

911
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
1012
"github.com/hashicorp/terraform-plugin-sdk/terraform"
@@ -34,14 +36,6 @@ func testSweepClbInstance(region string) error {
3436
}
3537
client := cli.(*TencentCloudClient).apiV3Conn
3638
service := ClbService{client: client}
37-
testCaseNames := []string{
38-
BasicClbName,
39-
InternalClbName,
40-
InternalClbNameUpdate,
41-
MyOpenClbName,
42-
OpenClbName,
43-
OpenClbNameUpdate,
44-
}
4539

4640
res, err := service.DescribeLoadBalancerByFilter(ctx, map[string]interface{}{})
4741
if err != nil {
@@ -51,9 +45,17 @@ func testSweepClbInstance(region string) error {
5145
if len(res) > 0 {
5246
for _, v := range res {
5347
id := *v.LoadBalancerId
48+
instanceName := *v.LoadBalancerName
49+
createTime := stringTotime(*v.CreateTime)
5450

55-
name := *v.LoadBalancerName
56-
if !IsContains(testCaseNames, name) {
51+
now := time.Now()
52+
interval := now.Sub(createTime).Minutes()
53+
// keep not delete
54+
if strings.HasPrefix(instanceName, keepResource) || strings.HasPrefix(instanceName, defaultResource) {
55+
continue
56+
}
57+
// less than 30 minute, not delete
58+
if int64(interval) < 30 {
5759
continue
5860
}
5961
if err := service.DeleteLoadBalancerById(ctx, id); err != nil {
@@ -62,7 +64,6 @@ func testSweepClbInstance(region string) error {
6264
}
6365
}
6466
}
65-
6667
return nil
6768
}
6869

0 commit comments

Comments
 (0)