From 8130f8c636c60f916bc817868b2b29ada36fa62a Mon Sep 17 00:00:00 2001 From: mikatong Date: Mon, 8 May 2023 16:33:02 +0800 Subject: [PATCH] add lighthouse sweeper --- .../resource_tc_lighthouse_instance_test.go | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/tencentcloud/resource_tc_lighthouse_instance_test.go b/tencentcloud/resource_tc_lighthouse_instance_test.go index a0a87db7bf..b923053d87 100644 --- a/tencentcloud/resource_tc_lighthouse_instance_test.go +++ b/tencentcloud/resource_tc_lighthouse_instance_test.go @@ -1,11 +1,49 @@ package tencentcloud import ( + "context" "testing" + "time" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + lighthouse "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/lighthouse/v20200324" ) +func init() { + // go test -v ./tencentcloud -sweep=ap-guangzhou -sweep-run=tencentcloud_lighthouse_instance + resource.AddTestSweepers("tencentcloud_lighthouse_instance", &resource.Sweeper{ + Name: "tencentcloud_lighthouse_instance", + F: func(r string) error { + logId := getLogId(contextNil) + ctx := context.WithValue(context.TODO(), logIdKey, logId) + cli, _ := sharedClientForRegion(r) + + request := lighthouse.NewDescribeInstancesRequest() + response, err := cli.(*TencentCloudClient).apiV3Conn.UseLighthouseClient().DescribeInstances(request) + if err != nil { + return err + } + instances := response.Response.InstanceSet + service := LightHouseService{client: cli.(*TencentCloudClient).apiV3Conn} + + for _, instance := range instances { + name := *instance.InstanceName + created, err := time.Parse("2006-01-02 15:04:05", *instance.CreatedTime) + if err != nil { + continue + } + if isResourcePersist(name, &created) { + continue + } + if innerErr := service.DeleteLighthouseInstanceById(ctx, *instance.InstanceId); innerErr != nil { + continue + } + } + return nil + }, + }) +} + func TestAccTencentCloudLighthouseInstance_basic(t *testing.T) { t.Parallel()