Skip to content

Commit 76b0e9e

Browse files
author
hellertang
authored
add vpc sweeper (#925)
1 parent 7ca2d40 commit 76b0e9e

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

tencentcloud/basic_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ the following must be changed to your resource id.
1212
var appid string = os.Getenv("TENCENTCLOUD_APPID")
1313
var ownerUin string = os.Getenv("TENCENTCLOUD_OWNER_UIN")
1414

15+
const (
16+
keepResource = "keep"
17+
defaultResource = "Default"
18+
)
19+
1520
const (
1621
defaultRegion = "ap-guangzhou"
1722
defaultVpcId = "vpc-86v957zb"
@@ -50,7 +55,7 @@ const (
5055
// Tke Exclusive Network Environment
5156
const (
5257
tkeExclusiveVpcId = "vpc-391sv4w3"
53-
tkeExclusiveVpcName = "tke_exclusive_vpc"
58+
tkeExclusiveVpcName = "keep_tke_exclusive_vpc"
5459
tkeExclusiveSubnetId = "subnet-ljyn7h30"
5560
defaultTkeClusterId = "cls-ely08ic4"
5661
defaultTkeClusterName = "preset_tke_cluster"

tencentcloud/common.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ func getEnvDefault(key string, defVal int) int {
8181
return int
8282
}
8383

84+
// string to time.Time
85+
func stringTotime(t string) time.Time {
86+
template := "2006-01-02 15:04:05"
87+
stamp, _ := time.ParseInLocation(template, t, time.Local)
88+
return stamp
89+
}
90+
8491
// getLogId get logId for trace, return a new logId if ctx is nil
8592
func getLogId(ctx context.Context) string {
8693
if ctx != nil {

tencentcloud/resource_tc_vpc_test.go

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

@@ -11,6 +13,56 @@ import (
1113
"github.com/hashicorp/terraform-plugin-sdk/terraform"
1214
)
1315

16+
func init() {
17+
resource.AddTestSweepers("tencentcloud_vpc", &resource.Sweeper{
18+
Name: "tencentcloud_vpc",
19+
F: testSweepVpcInstance,
20+
})
21+
}
22+
23+
func testSweepVpcInstance(region string) error {
24+
logId := getLogId(contextNil)
25+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
26+
27+
sharedClient, err := sharedClientForRegion(region)
28+
if err != nil {
29+
return fmt.Errorf("getting tencentcloud client error: %s", err.Error())
30+
}
31+
client := sharedClient.(*TencentCloudClient)
32+
33+
vpcService := VpcService{
34+
client: client.apiV3Conn,
35+
}
36+
37+
instances, err := vpcService.DescribeVpcs(ctx, "", "", 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+
instanceId := v.vpcId
44+
instanceName := v.name
45+
46+
now := time.Now()
47+
48+
createTime := stringTotime(v.createTime)
49+
interval := now.Sub(createTime).Minutes()
50+
if strings.HasPrefix(instanceName, keepResource) || strings.HasPrefix(instanceName, defaultResource) {
51+
continue
52+
}
53+
// less than 30 minute, not delete
54+
if int64(interval) < 30 {
55+
continue
56+
}
57+
58+
if err = vpcService.DeleteVpc(ctx, instanceId); err != nil {
59+
log.Printf("[ERROR] sweep instance %s error: %s", instanceId, err.Error())
60+
}
61+
}
62+
63+
return nil
64+
}
65+
1466
func TestAccTencentCloudVpcV3Basic(t *testing.T) {
1567
t.Parallel()
1668
resource.Test(t, resource.TestCase{

0 commit comments

Comments
 (0)