Skip to content

Commit d0a0220

Browse files
author
hellertang
authored
Feat/fix sweeper instance (#990)
* fix cvm sweeper * add subnet/routetable sweeper
1 parent ad2fd90 commit d0a0220

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

tencentcloud/data_source_tc_route_table_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,66 @@
11
package tencentcloud
22

33
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"strings"
48
"testing"
9+
"time"
510

611
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
712
)
813

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

tencentcloud/resource_tc_subnet_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,66 @@ 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_subnet", &resource.Sweeper{
17+
Name: "tencentcloud_subnet",
18+
F: testSweepSubnet,
19+
})
20+
}
21+
22+
func testSweepSubnet(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.DescribeSubnets(ctx, "", "", "", "",
37+
nil, nil, nil, "", "")
38+
if err != nil {
39+
return fmt.Errorf("get instance list error: %s", err.Error())
40+
}
41+
42+
for _, v := range instances {
43+
44+
instanceId := v.subnetId
45+
instanceName := v.name
46+
now := time.Now()
47+
createTime := stringTotime(v.createTime)
48+
interval := now.Sub(createTime).Minutes()
49+
50+
if strings.HasPrefix(instanceName, keepResource) || strings.HasPrefix(instanceName, defaultResource) {
51+
continue
52+
}
53+
54+
if needProtect == 1 && int64(interval) < 30 {
55+
continue
56+
}
57+
58+
if err = vpcService.DeleteSubnet(ctx, instanceId); err != nil {
59+
log.Printf("[ERROR] sweep instance %s error: %s", instanceId, err.Error())
60+
}
61+
}
62+
63+
return nil
64+
}
65+
1366
func TestAccTencentCloudVpcV3SubnetBasic(t *testing.T) {
1467
t.Parallel()
1568
resource.Test(t, resource.TestCase{

0 commit comments

Comments
 (0)