Skip to content

Commit 95ef0fb

Browse files
author
hellertang
authored
fix sg sweeper (#958)
1 parent 20b5283 commit 95ef0fb

8 files changed

+74
-38
lines changed

tencentcloud/common.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ const logIdKey = contextLogId("logId")
3131
const (
3232
PROVIDER_READ_RETRY_TIMEOUT = "TENCENTCLOUD_READ_RETRY_TIMEOUT"
3333
PROVIDER_WRITE_RETRY_TIMEOUT = "TENCENTCLOUD_WRITE_RETRY_TIMEOUT"
34+
35+
SWEEPER_NEED_PROTECT = "SWEEPER_NEED_PROTECT"
3436
)
3537

3638
var logFirstTime = ""
@@ -46,6 +48,9 @@ var readRetryTimeout = time.Duration(readRetry) * time.Minute
4648
var writeRetry = getEnvDefault(PROVIDER_WRITE_RETRY_TIMEOUT, 5)
4749
var writeRetryTimeout = time.Duration(writeRetry) * time.Minute
4850

51+
//const writeRetryTimeout = 5 * time.Minute
52+
var needProtect = getEnvDefault(SWEEPER_NEED_PROTECT, 0)
53+
4954
// InternalError common internalError, do not add in retryableErrorCode,
5055
// because when some product return this error, retry won't fix anything.
5156
const InternalError = "InternalError"

tencentcloud/resource_tc_ccn_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,67 @@ package tencentcloud
33
import (
44
"context"
55
"fmt"
6+
"log"
7+
"strings"
68
"testing"
79
"time"
810

911
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
1012
"github.com/hashicorp/terraform-plugin-sdk/terraform"
1113
)
1214

15+
func init() {
16+
resource.AddTestSweepers("tencentcloud_ccn", &resource.Sweeper{
17+
Name: "tencentcloud_ccn",
18+
F: testSweepCcnInstance,
19+
})
20+
}
21+
22+
func testSweepCcnInstance(region string) error {
23+
logId := getLogId(contextNil)
24+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
25+
26+
sharedClient, err := sharedClientForRegion(region)
27+
if err != nil {
28+
return fmt.Errorf("getting tencentcloud client error: %s", err.Error())
29+
}
30+
client := sharedClient.(*TencentCloudClient)
31+
32+
vpcService := VpcService{
33+
client: client.apiV3Conn,
34+
}
35+
36+
instances, err := vpcService.DescribeCcns(ctx, "", "")
37+
if err != nil {
38+
return fmt.Errorf("get instance list error: %s", err.Error())
39+
}
40+
41+
for _, v := range instances {
42+
instanceId := v.ccnId
43+
instanceName := v.name
44+
45+
now := time.Now()
46+
47+
createTime := stringTotime(v.createTime)
48+
interval := now.Sub(createTime).Minutes()
49+
if instanceName != "" {
50+
if strings.HasPrefix(instanceName, keepResource) || strings.HasPrefix(instanceName, defaultResource) {
51+
continue
52+
}
53+
}
54+
55+
// less than 30 minute, not delete
56+
if needProtect == 1 && int64(interval) < 30 {
57+
continue
58+
}
59+
60+
if err = vpcService.DeleteCcn(ctx, instanceId); err != nil {
61+
log.Printf("[ERROR] sweep instance %s error: %s", instanceId, err.Error())
62+
}
63+
}
64+
return nil
65+
}
66+
1367
func TestAccTencentCloudCcnV3Basic(t *testing.T) {
1468
t.Parallel()
1569
keyName := "tencentcloud_ccn.main"

tencentcloud/resource_tc_clb_instance_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func testSweepClbInstance(region string) error {
5656
continue
5757
}
5858
// less than 30 minute, not delete
59-
if int64(interval) < 30 {
59+
if needProtect == 1 && int64(interval) < 30 {
6060
continue
6161
}
6262
if err := service.DeleteLoadBalancerById(ctx, id); err != nil {

tencentcloud/resource_tc_eip_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func testSweepEipInstance(region string) error {
5555
}
5656

5757
// less than 30 minute, not delete
58-
if int64(interval) < 30 {
58+
if needProtect == 1 && int64(interval) < 30 {
5959
continue
6060
}
6161

tencentcloud/resource_tc_eni_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func testSweepEniInstance(region string) error {
5454
}
5555

5656
// less than 30 minute, not delete
57-
if int64(interval) < 30 {
57+
if needProtect == 1 && int64(interval) < 30 {
5858
continue
5959
}
6060

tencentcloud/resource_tc_nat_gateway_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func testSweepNatInstance(region string) error {
5454
}
5555

5656
// less than 30 minute, not delete
57-
if int64(interval) < 30 {
57+
if needProtect == 1 && int64(interval) < 30 {
5858
continue
5959
}
6060

tencentcloud/resource_tc_security_group_test.go

Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ import (
66
"log"
77
"strings"
88
"testing"
9-
10-
vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"
11-
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
9+
"time"
1210

1311
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
1412
"github.com/hashicorp/terraform-plugin-sdk/terraform"
@@ -35,10 +33,7 @@ func testSweepSecurityGroups(region string) error {
3533
client: client.apiV3Conn,
3634
}
3735

38-
sgs, err := service.DescribeSecurityGroups(ctx, nil, helper.String(NamePrefix), nil, nil)
39-
var sgIds []*string
40-
var candidates []string
41-
var referredSgs = make(map[string][]*string, 0)
36+
sgs, err := service.DescribeSecurityGroups(ctx, nil, nil, nil, nil)
4237

4338
if err != nil {
4439
return fmt.Errorf("DescribeSecurityGroups error: %s", err.Error())
@@ -47,43 +42,25 @@ func testSweepSecurityGroups(region string) error {
4742
for _, v := range sgs {
4843
name := *v.SecurityGroupName
4944
id := *v.SecurityGroupId
50-
sgIds = append(sgIds, v.SecurityGroupId)
51-
if !strings.HasPrefix(name, NamePrefix) {
52-
continue
53-
}
54-
candidates = append(candidates, id)
55-
56-
}
5745

58-
refReq := vpc.NewDescribeSecurityGroupReferencesRequest()
59-
refReq.SecurityGroupIds = sgIds
46+
now := time.Now()
47+
createTime := stringTotime(*v.CreatedTime)
48+
interval := now.Sub(createTime).Minutes()
6049

61-
refRes, err := client.apiV3Conn.UseVpcClient().DescribeSecurityGroupReferences(refReq)
62-
if err != nil {
63-
return fmt.Errorf("DescribeSecurityGroupReferences error: %s", err.Error())
64-
}
65-
for _, v := range refRes.Response.ReferredSecurityGroupSet {
66-
if len(v.ReferredSecurityGroupIds) > 0 {
67-
referredSgs[*v.SecurityGroupId] = v.ReferredSecurityGroupIds
50+
if strings.HasPrefix(name, keepResource) || strings.HasPrefix(name, defaultResource) {
51+
continue
6852
}
69-
}
70-
71-
res, err := service.DescribeSecurityGroupsAssociate(ctx, candidates)
72-
if err != nil {
73-
return fmt.Errorf("DescribeSecurityGroupsAssociate error: %s", err.Error())
74-
}
7553

76-
for _, v := range res {
77-
id := *v.SecurityGroupId
78-
79-
if *v.TotalCount > 0 || len(referredSgs[id]) > 0 {
54+
// less than 30 minute, not delete
55+
if needProtect == 1 && int64(interval) < 30 {
8056
continue
8157
}
8258

8359
if err := service.DeleteSecurityGroup(ctx, id); err != nil {
8460
log.Printf("[ERROR] sweep security group %s error: %s", id, err.Error())
8561
}
8662
}
63+
8764
return nil
8865
}
8966

tencentcloud/resource_tc_vpc_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func testSweepVpcInstance(region string) error {
5151
continue
5252
}
5353
// less than 30 minute, not delete
54-
if int64(interval) < 30 {
54+
if needProtect == 1 && int64(interval) < 30 {
5555
continue
5656
}
5757

0 commit comments

Comments
 (0)