Skip to content

Commit 14636e2

Browse files
authored
fix: tcaplus_table - add sweeper (#1072)
* fix: tcaplus_table - add sweeper * fix: tcaplus_table - sweeper add offline recycle step
1 parent 59472c3 commit 14636e2

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

tencentcloud/resource_tc_tcaplus_idl_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func TestAccTencentCloudTcaplusIdlResource(t *testing.T) {
3636
resource.TestCheckResourceAttrSet(testTcaplusIdlResourceNameResourceKey, "table_infos.0.value_fields"),
3737
resource.TestCheckResourceAttrSet(testTcaplusIdlResourceNameResourceKey, "table_infos.0.sum_value_field_size"),
3838
resource.TestCheckResourceAttrSet(testTcaplusIdlResourceNameResourceKey, "table_infos.0.index_key_set"),
39-
resource.TestCheckResourceAttr(testTcaplusIdlResourceNameResourceKey, "table_infos.0.table_name", "tb_online_guagua"),
39+
resource.TestCheckResourceAttr(testTcaplusIdlResourceNameResourceKey, "table_infos.0.table_name", "tb_idle_test"),
4040
resource.TestCheckResourceAttr(testTcaplusIdlResourceNameResourceKey, "table_infos.0.error", ""),
4141
),
4242
},
@@ -112,7 +112,7 @@ resource "tencentcloud_tcaplus_idl" "test_idl" {
112112
syntax = "proto2";
113113
package myTcaplusTable;
114114
import "tcaplusservice.optionv1.proto";
115-
message tb_online_guagua {
115+
message tb_idle_test {
116116
option(tcaplusservice.tcaplus_primary_key) = "uin,name,region";
117117
required int64 uin = 1;
118118
required string name = 2;

tencentcloud/resource_tc_tcaplus_table_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"testing"
7+
"time"
78

89
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
910
"github.com/hashicorp/terraform-plugin-sdk/terraform"
@@ -12,6 +13,76 @@ import (
1213
var testTcaplusTableResourceName = "tencentcloud_tcaplus_table"
1314
var testTcaplusTableResourceNameResourceKey = testTcaplusTableResourceName + ".test_table"
1415

16+
func init() {
17+
// go test -v ./tencentcloud -sweep=ap-guangzhou -sweep-run=tencentcloud_tcaplus_table
18+
resource.AddTestSweepers("tencentcloud_tcaplus_table", &resource.Sweeper{
19+
Name: "tencentcloud_tcaplus_table",
20+
F: func(r string) error {
21+
logId := getLogId(contextNil)
22+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
23+
cli, _ := sharedClientForRegion(r)
24+
client := cli.(*TencentCloudClient).apiV3Conn
25+
service := TcaplusService{client}
26+
27+
clusters, err := service.DescribeClusters(ctx, "", defaultTcaPlusClusterName)
28+
29+
if err != nil {
30+
return err
31+
}
32+
33+
if len(clusters) == 0 {
34+
return fmt.Errorf("no cluster named %s", defaultTcaPlusClusterName)
35+
}
36+
37+
clusterId := *clusters[0].ClusterId
38+
39+
tables, err := service.DescribeTables(ctx, clusterId, "", "", "")
40+
41+
for _, table := range tables {
42+
name := *table.TableName
43+
// legacy bad argument
44+
gId := clusterId + ":" + *table.TableGroupId
45+
insId := *table.TableInstanceId
46+
created := time.Time{}
47+
48+
if isResourcePersist(name, &created) {
49+
continue
50+
}
51+
52+
taskId, err := service.DeleteTable(ctx, clusterId, gId, insId, name)
53+
if err != nil {
54+
continue
55+
}
56+
57+
err = resource.Retry(readRetryTimeout*3, func() *resource.RetryError {
58+
info, has, err := service.DescribeTask(ctx, clusterId, taskId)
59+
if err != nil {
60+
return retryError(err)
61+
}
62+
if !has {
63+
return nil
64+
}
65+
if *info.Progress < 100 {
66+
return resource.RetryableError(fmt.Errorf("running delete task %s, table: %s -> %s", taskId, clusterId, name))
67+
}
68+
return nil
69+
})
70+
71+
if err != nil {
72+
continue
73+
}
74+
75+
_, err = service.DeleteTable(ctx, clusterId, gId, insId, name)
76+
if err != nil {
77+
continue
78+
}
79+
}
80+
81+
return nil
82+
},
83+
})
84+
}
85+
1586
func TestAccTencentCloudTcaplusTableResource(t *testing.T) {
1687
t.Parallel()
1788
resource.Test(t, resource.TestCase{

0 commit comments

Comments
 (0)