Skip to content

Commit 7963d74

Browse files
tongyimingmikatong
andauthored
fix: add kafka topic sweeper (#1165)
Co-authored-by: mikatong <mikatong@tencent.com>
1 parent 3ec36f5 commit 7963d74

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

tencentcloud/resource_tc_ckafka_topic_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,60 @@ package tencentcloud
33
import (
44
"context"
55
"fmt"
6+
"log"
67
"strings"
78
"testing"
9+
"time"
810

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

15+
func init() {
16+
// go test -v ./tencentcloud -sweep=ap-guangzhou -sweep-run=tencentcloud_ckafka_topic
17+
resource.AddTestSweepers("tencentcloud_ckafka_topic", &resource.Sweeper{
18+
Name: "tencentcloud_ckafka_topic",
19+
F: func(r string) error {
20+
logId := getLogId(contextNil)
21+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
22+
sharedClient, err := sharedClientForRegion(r)
23+
if err != nil {
24+
return fmt.Errorf("getting tencentcloud client error: %s", err.Error())
25+
}
26+
client := sharedClient.(*TencentCloudClient)
27+
ckafkcService := CkafkaService{
28+
client: client.apiV3Conn,
29+
}
30+
instanceId := defaultKafkaInstanceId
31+
topicDetails, err := ckafkcService.DescribeCkafkaTopics(ctx, instanceId, "")
32+
if err != nil {
33+
return err
34+
}
35+
for _, topicDetail := range topicDetails {
36+
log.Println(*topicDetail.TopicName)
37+
topicName := *topicDetail.TopicName
38+
now := time.Now()
39+
createTime := time.Unix(*topicDetail.CreateTime, 0)
40+
interval := now.Sub(createTime).Minutes()
41+
42+
if strings.HasPrefix(topicName, keepResource) || strings.HasPrefix(topicName, defaultResource) {
43+
continue
44+
}
45+
46+
if needProtect == 1 && int64(interval) < 30 {
47+
continue
48+
}
49+
err := ckafkcService.DeleteCkafkaTopic(ctx, instanceId, topicName)
50+
if err != nil {
51+
return err
52+
}
53+
}
54+
55+
return nil
56+
},
57+
})
58+
}
59+
1360
func TestAccTencentCloudCkafkaTopicResource(t *testing.T) {
1461
t.Parallel()
1562
resource.Test(t, resource.TestCase{

0 commit comments

Comments
 (0)