Skip to content

Commit c27476a

Browse files
authored
fix: clb - attach data conflict and add target group sweeper (#1162)
1 parent b58e026 commit c27476a

File tree

4 files changed

+52
-5
lines changed

4 files changed

+52
-5
lines changed

tencentcloud/common.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ const (
3232
PROVIDER_READ_RETRY_TIMEOUT = "TENCENTCLOUD_READ_RETRY_TIMEOUT"
3333
PROVIDER_WRITE_RETRY_TIMEOUT = "TENCENTCLOUD_WRITE_RETRY_TIMEOUT"
3434

35-
SWEEPER_NEED_PROTECT = "SWEEPER_NEED_PROTECT"
35+
SWEEPER_NEED_PROTECT = "SWEEPER_NEED_PROTECT"
36+
TENCENTCLOUD_COMMON_TIME_LAYOUT = "2006-01-02 15:04:05"
3637
)
3738

3839
var logFirstTime = ""
@@ -88,11 +89,22 @@ func getEnvDefault(key string, defVal int) int {
8889

8990
// string to time.Time
9091
func stringTotime(t string) time.Time {
91-
template := "2006-01-02 15:04:05"
92+
template := TENCENTCLOUD_COMMON_TIME_LAYOUT
9293
stamp, _ := time.ParseInLocation(template, t, time.Local)
9394
return stamp
9495
}
9596

97+
func parseTimeFromCommonLayout(t *string) time.Time {
98+
if t == nil {
99+
return time.Time{}
100+
}
101+
result, err := time.Parse(TENCENTCLOUD_COMMON_TIME_LAYOUT, *t)
102+
if err != nil {
103+
return time.Time{}
104+
}
105+
return result
106+
}
107+
96108
// getLogId get logId for trace, return a new logId if ctx is nil
97109
func getLogId(ctx context.Context) string {
98110
if ctx != nil {

tencentcloud/data_source_tc_clb_attachments_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
77
)
88

9-
func TestAccTencentCloudDataSourceClbServerAttachments(t *testing.T) {
9+
func TestAccTencentCloudClbServerAttachmentsDataSource(t *testing.T) {
1010
t.Parallel()
1111

1212
resource.Test(t, resource.TestCase{

tencentcloud/data_source_tc_clb_target_groups_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ func TestAccTencentCloudClbTargetGroupDataSource(t *testing.T) {
5656
const tareGetGroupBase = defaultVpcSubnets + `
5757
resource "tencentcloud_clb_instance" "clb_basic" {
5858
network_type = "OPEN"
59-
clb_name = "tf-clb-rule-basic"
60-
vpc_id = local.vpc_id
59+
clb_name = "tf-clb-rule-data"
60+
vpc_id = local.vpc_id
6161
}
6262
6363
resource "tencentcloud_clb_listener" "listener_basic" {

tencentcloud/resource_tc_clb_target_group_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,48 @@ package tencentcloud
33
import (
44
"context"
55
"fmt"
6+
"log"
67
"testing"
78
"time"
89

910
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
1011
"github.com/hashicorp/terraform-plugin-sdk/terraform"
1112
)
1213

14+
func init() {
15+
// go test -v ./tencentcloud -sweep=ap-guangzhou -sweep-run=tencentcloud_clb_target_group
16+
resource.AddTestSweepers("tencentcloud_clb_target_group", &resource.Sweeper{
17+
Name: "tencentcloud_clb_target_group",
18+
F: func(r string) error {
19+
logId := getLogId(contextNil)
20+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
21+
cli, _ := sharedClientForRegion(r)
22+
client := cli.(*TencentCloudClient).apiV3Conn
23+
service := ClbService{client}
24+
25+
tgs, err := service.DescribeTargetGroups(ctx, "", nil)
26+
if err != nil {
27+
return err
28+
}
29+
30+
for i := range tgs {
31+
tg := tgs[i]
32+
created := parseTimeFromCommonLayout(tg.CreatedTime)
33+
if isResourcePersist(*tg.TargetGroupName, &created) {
34+
continue
35+
}
36+
log.Printf("%s will be remvoed", *tg.TargetGroupName)
37+
err = service.DeleteTarget(ctx, *tg.TargetGroupId)
38+
if err != nil {
39+
continue
40+
}
41+
}
42+
43+
return nil
44+
},
45+
})
46+
}
47+
1348
func TestAccTencentCloudClbTargetGroup_basic(t *testing.T) {
1449
t.Parallel()
1550
resource.Test(t, resource.TestCase{

0 commit comments

Comments
 (0)