Skip to content

Commit 95a92f8

Browse files
author
hellertang
authored
add sweeper (#961)
1 parent 42a4889 commit 95a92f8

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,7 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.376/go.mod
516516
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.377/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
517517
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.378 h1:o+GKr3tHDivmoWxBXX5oeJC8EmmNV64g/yw5s0RjBdg=
518518
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.378/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
519+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.381/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
519520
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.383 h1:1TFCoXSDga/8jO66SGD7K7jMveZGVNJaD4IhX+vfvL0=
520521
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.383/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
521522
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.385 h1:bwgb0FZJRlPcDn3uRJfA7rI7dZejg7LZS0b8rUBE2CQ=

tencentcloud/resource_tc_postgresql_instance_test.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"strings"
77
"testing"
8+
"time"
89

910
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
1011
"github.com/hashicorp/terraform-plugin-sdk/terraform"
@@ -35,13 +36,25 @@ func init() {
3536
id := *v.DBInstanceId
3637
name := *v.DBInstanceName
3738
vpcId := *v.VpcId
39+
40+
now := time.Now()
41+
createTime := stringTotime(*v.CreateTime)
42+
interval := now.Sub(createTime).Minutes()
43+
3844
if strings.HasPrefix(name, keepResource) || strings.HasPrefix(name, defaultResource) {
3945
continue
4046
}
47+
48+
// less than 30 minute, not delete
49+
if needProtect == 1 && int64(interval) < 30 {
50+
continue
51+
}
52+
// isolate
4153
err := postgresqlService.IsolatePostgresqlInstance(ctx, id)
4254
if err != nil {
4355
continue
4456
}
57+
// describe status
4558
err = resource.Retry(readRetryTimeout, func() *resource.RetryError {
4659
instance, has, err := postgresqlService.DescribePostgresqlInstanceById(ctx, id)
4760
if err != nil {
@@ -58,13 +71,13 @@ func init() {
5871
if err != nil {
5972
continue
6073
}
74+
// delete
6175
err = postgresqlService.DeletePostgresqlInstance(ctx, id)
6276
if err != nil {
6377
continue
6478
}
6579
vpcs = append(vpcs, vpcId)
6680
}
67-
6881
for _, v := range vpcs {
6982
_ = vpcService.DeleteVpc(ctx, v)
7083
}

tencentcloud/resource_tc_sqlserver_instance_test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"sync"
77
"testing"
8+
"time"
89

910
sqlserver "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sqlserver/v20180328"
1011

@@ -24,7 +25,7 @@ func init() {
2425
cli, _ := sharedClientForRegion(r)
2526
client := cli.(*TencentCloudClient).apiV3Conn
2627
service := SqlserverService{client: client}
27-
instances, err := service.DescribeSqlserverInstances(ctx, "", "", -1, defaultVpcId, defaultSubnetId, 1)
28+
instances, err := service.DescribeSqlserverInstances(ctx, "", "", -1, "", "", 1)
2829

2930
if err != nil {
3031
return err
@@ -50,10 +51,20 @@ func batchDeleteSQLServerInstances(ctx context.Context, service SqlserverService
5051
defer wg.Done()
5152
id := *instances[i].InstanceId
5253
name := *instances[i].Name
54+
createTime := *instances[i].CreateTime
55+
now := time.Now()
56+
57+
interval := now.Sub(stringTotime(createTime)).Minutes()
58+
5359
if isResourcePersist(name, nil) {
5460
return
5561
}
5662

63+
// less than 30 minute, not delete
64+
if needProtect == 1 && int64(interval) < 30 {
65+
return
66+
}
67+
5768
var outErr, inErr error
5869
var has bool
5970

0 commit comments

Comments
 (0)