Skip to content

Commit 3ec36f5

Browse files
tongyimingmikatong
andauthored
fix: add as sweeper (#1164)
Co-authored-by: mikatong <mikatong@tencent.com>
1 parent 493265e commit 3ec36f5

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

tencentcloud/resource_tc_as_scaling_config_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,58 @@ package tencentcloud
33
import (
44
"context"
55
"fmt"
6+
"strings"
67
"testing"
8+
"time"
79

810
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
911
"github.com/hashicorp/terraform-plugin-sdk/terraform"
1012
)
1113

14+
func init() {
15+
// go test -v ./tencentcloud -sweep=ap-guangzhou -sweep-run=tencentcloud_as_scaling_config
16+
resource.AddTestSweepers("tencentcloud_as_scaling_config", &resource.Sweeper{
17+
Name: "tencentcloud_as_scaling_config",
18+
F: func(r string) error {
19+
logId := getLogId(contextNil)
20+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
21+
sharedClient, err := sharedClientForRegion(r)
22+
if err != nil {
23+
return fmt.Errorf("getting tencentcloud client error: %s", err.Error())
24+
}
25+
client := sharedClient.(*TencentCloudClient)
26+
asService := AsService{
27+
client: client.apiV3Conn,
28+
}
29+
configs, err := asService.DescribeLaunchConfigurationByFilter(ctx, "", "")
30+
if err != nil {
31+
return err
32+
}
33+
for _, config := range configs {
34+
instanceName := *config.LaunchConfigurationName
35+
now := time.Now()
36+
createTime := stringTotime(*config.CreatedTime)
37+
interval := now.Sub(createTime).Minutes()
38+
39+
if strings.HasPrefix(instanceName, keepResource) || strings.HasPrefix(instanceName, defaultResource) {
40+
continue
41+
}
42+
43+
if needProtect == 1 && int64(interval) < 30 {
44+
continue
45+
}
46+
47+
ee := asService.DeleteLaunchConfiguration(ctx, *config.LaunchConfigurationId)
48+
if ee != nil {
49+
continue
50+
}
51+
}
52+
53+
return nil
54+
},
55+
})
56+
}
57+
1258
func TestAccTencentCloudAsScalingConfig_basic(t *testing.T) {
1359
resource.Test(t, resource.TestCase{
1460
PreCheck: func() { testAccPreCheck(t) },

0 commit comments

Comments
 (0)