diff --git a/tencentcloud/resource_tc_cls_machine_group_test.go b/tencentcloud/resource_tc_cls_machine_group_test.go index c2a2e80096..f75ddde0cc 100644 --- a/tencentcloud/resource_tc_cls_machine_group_test.go +++ b/tencentcloud/resource_tc_cls_machine_group_test.go @@ -3,12 +3,65 @@ package tencentcloud import ( "context" "fmt" + "log" + "strings" "testing" + "time" "github.com/hashicorp/terraform-plugin-sdk/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/terraform" ) +func init() { + resource.AddTestSweepers("tencentcloud_cls_machine_group", &resource.Sweeper{ + Name: "tencentcloud_cls_machine_group", + F: testSweepMachineGroup, + }) +} + +func testSweepMachineGroup(region string) error { + logId := getLogId(contextNil) + ctx := context.WithValue(context.TODO(), logIdKey, logId) + + sharedClient, err := sharedClientForRegion(region) + if err != nil { + return fmt.Errorf("getting tencentcloud client error: %s", err.Error()) + } + client := sharedClient.(*TencentCloudClient) + + clsService := ClsService{ + client: client.apiV3Conn, + } + + instances, err := clsService.DescribeClsMachineGroupByFilter(ctx, nil) + if err != nil { + return fmt.Errorf("get instance list error: %s", err.Error()) + } + + for _, v := range instances { + instanceId := v.GroupId + instanceName := v.GroupName + + now := time.Now() + + createTime := stringTotime(*v.CreateTime) + interval := now.Sub(createTime).Minutes() + if strings.HasPrefix(*instanceName, keepResource) || strings.HasPrefix(*instanceName, defaultResource) { + continue + } + // less than 30 minute, not delete + if needProtect == 1 && int64(interval) < 30 { + continue + } + + if err = clsService.DeleteClsMachineGroup(ctx, *instanceId); err != nil { + log.Printf("[ERROR] sweep instance %s error: %s", *instanceId, err.Error()) + } + } + + return nil +} + func TestAccTencentCloudClsMachineGroup_basic(t *testing.T) { t.Parallel()