Skip to content

Commit cc1b867

Browse files
authored
add cls sweeper (#1100)
1 parent cb5100c commit cc1b867

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

tencentcloud/resource_tc_cls_logset_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_logset", &resource.Sweeper{
17+
Name: "tencentcloud_cls_logset",
18+
F: testSweepClsLogset,
19+
})
20+
}
21+
22+
func testSweepClsLogset(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.DescribeClsLogsetByFilter(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.LogsetId
43+
instanceName := v.LogsetName
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.DeleteClsLogset(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 TestAccTencentCloudClsLogset_basic(t *testing.T) {
1366
t.Parallel()
1467

tencentcloud/resource_tc_cls_topic_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,64 @@ 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_topic", &resource.Sweeper{
17+
Name: "tencentcloud_cls_topic",
18+
F: testSweepClsTopic,
19+
})
20+
}
21+
22+
func testSweepClsTopic(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.DescribeClsTopicByFilter(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.TopicId
43+
instanceName := v.TopicName
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.DeleteClsTopic(ctx, *instanceId); err != nil {
58+
log.Printf("[ERROR] sweep instance %s error: %s", *instanceId, err.Error())
59+
}
60+
}
61+
return nil
62+
}
63+
1264
func TestAccTencentCloudClsTopic_basic(t *testing.T) {
1365
t.Parallel()
1466

0 commit comments

Comments
 (0)