@@ -3,15 +3,80 @@ package tencentcloud
3
3
import (
4
4
"context"
5
5
"fmt"
6
+ "log"
6
7
"strings"
7
8
"testing"
9
+ "time"
10
+
11
+ "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
8
12
9
13
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
10
14
"github.com/hashicorp/terraform-plugin-sdk/terraform"
11
15
cdb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cdb/v20170320"
12
16
sdkError "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
13
17
)
14
18
19
+ func init () {
20
+ // go test -v ./tencentcloud -sweep=ap-guangzhou -sweep-run=tencentcloud_mysql_account
21
+ resource .AddTestSweepers ("tencentcloud_mysql_account" , & resource.Sweeper {
22
+ Name : "tencentcloud_mysql_account" ,
23
+ F : func (r string ) error {
24
+ logId := getLogId (contextNil )
25
+ ctx := context .WithValue (context .TODO (), logIdKey , logId )
26
+ cli , _ := sharedClientForRegion (r )
27
+ client := cli .(* TencentCloudClient ).apiV3Conn
28
+
29
+ service := MysqlService {client }
30
+
31
+ request := cdb .NewDescribeDBInstancesRequest ()
32
+ request .InstanceNames = []* string {
33
+ helper .String (defaultMySQLName ),
34
+ }
35
+ response , err := client .UseMysqlClient ().DescribeDBInstances (request )
36
+ if err != nil {
37
+ log .Printf ("[CRITICAL] [%s] fail, request: %s, reason: %s" , request .GetAction (), request .ToJsonString (), err .Error ())
38
+ return err
39
+ }
40
+
41
+ if len (response .Response .Items ) == 0 {
42
+ return nil
43
+ }
44
+
45
+ instance := response .Response .Items [0 ]
46
+ id := instance .InstanceId
47
+
48
+ accounts , err := service .DescribeAccounts (ctx , * id )
49
+
50
+ if err != nil {
51
+ return err
52
+ }
53
+
54
+ for i := range accounts {
55
+ item := accounts [i ]
56
+ name := * item .User
57
+ host := * item .Host
58
+ created , err := time .Parse (time .RFC3339 , * item .CreateTime )
59
+ if err != nil {
60
+ created = time.Time {}
61
+ }
62
+ if isResourcePersist (name , & created ) {
63
+ continue
64
+ }
65
+ if ! strings .Contains (name , "test" ) {
66
+ continue
67
+ }
68
+ log .Printf ("Will delete %s %s@%s" , * id , name , host )
69
+ _ , err = service .DeleteAccount (ctx , * id , name , host )
70
+ if err != nil {
71
+ continue
72
+ }
73
+ }
74
+
75
+ return nil
76
+ },
77
+ })
78
+ }
79
+
15
80
func TestAccTencentCloudMysqlAccountResource (t * testing.T ) {
16
81
t .Parallel ()
17
82
resource .Test (t , resource.TestCase {
0 commit comments