Skip to content

Commit 017f37f

Browse files
authored
add machine group sweeper (#1149)
1 parent b3d3788 commit 017f37f

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

tencentcloud/resource_tc_cls_machine_group_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,65 @@ package tencentcloud
33
import (
44
"context"
55
"fmt"
6+
"log"
7+
"strings"
68
"testing"
9+
"time"
710

811
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
912
"github.com/hashicorp/terraform-plugin-sdk/terraform"
1013
)
1114

15+
func init() {
16+
resource.AddTestSweepers("tencentcloud_cls_machine_group", &resource.Sweeper{
17+
Name: "tencentcloud_cls_machine_group",
18+
F: testSweepMachineGroup,
19+
})
20+
}
21+
22+
func testSweepMachineGroup(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+
clsService := ClsService{
33+
client: client.apiV3Conn,
34+
}
35+
36+
instances, err := clsService.DescribeClsMachineGroupByFilter(ctx, nil)
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.GroupId
43+
instanceName := v.GroupName
44+
45+
now := time.Now()
46+
47+
createTime := stringTotime(*v.CreateTime)
48+
interval := now.Sub(createTime).Minutes()
49+
if strings.HasPrefix(*instanceName, keepResource) || strings.HasPrefix(*instanceName, defaultResource) {
50+
continue
51+
}
52+
// less than 30 minute, not delete
53+
if needProtect == 1 && int64(interval) < 30 {
54+
continue
55+
}
56+
57+
if err = clsService.DeleteClsMachineGroup(ctx, *instanceId); err != nil {
58+
log.Printf("[ERROR] sweep instance %s error: %s", *instanceId, err.Error())
59+
}
60+
}
61+
62+
return nil
63+
}
64+
1265
func TestAccTencentCloudClsMachineGroup_basic(t *testing.T) {
1366
t.Parallel()
1467

0 commit comments

Comments
 (0)