Skip to content

Commit e2f8a48

Browse files
authored
Feat/support dcdb data source (#1365)
* support dcdb account resource * add changelog * fix lint issue * fix e2e issue * 1.support dcdb hourdb. 2.support dcdb sg attachment * support dcdb data resource * fix tfprovider-lint issues * optimize test case * add changelog
1 parent fc1a4bb commit e2f8a48

26 files changed

+2696
-39
lines changed

.changelog/1365.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
```release-note:new-data-source
2+
tencentcloud_dcdb_instances
3+
tencentcloud_dcdb_accounts
4+
tencentcloud_dcdb_databases
5+
tencentcloud_dcdb_parameters
6+
tencentcloud_dcdb_shards
7+
tencentcloud_dcdb_security_groups
8+
```

go.sum

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -494,14 +494,12 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.486/go.mod
494494
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.488/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
495495
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.490/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
496496
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.493/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
497-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.500/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
498497
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.503/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
499498
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.514/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
500499
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.515/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
501500
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.518/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
502501
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.519/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
503502
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.524/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
504-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.527 h1:hpZMjoYnR+ma5HMWWNaNc5fshpFXXaUPrZMo4OXPxYQ=
505503
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.527/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
506504
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.529 h1:B26Pn9N5O3Mmbh8icZSpZ8kkhcfnapUdU/TBxPDWggA=
507505
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.529/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=
@@ -542,8 +540,6 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/redis v1.0.490 h1:JEW8+
542540
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/redis v1.0.490/go.mod h1:1yBtZUeKla8GNnVnJ1JOgMVBi/YgjEuQgzO4uNmisdI=
543541
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/scf v1.0.275 h1:hn5RrN/qkcObnyKfJ+raey/riVeRqHJFvY34l2YgELs=
544542
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/scf v1.0.275/go.mod h1:Pew6DV5oBGrzHYWZ8ssiHeJS/Z39ggVv1y5ADGWdO4s=
545-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ses v1.0.526 h1:n8RVsww+jKkYxEVebB9VDNi/P1lOoOcpxa8qkg0BfMM=
546-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ses v1.0.526/go.mod h1:sQwqZ7D79UayMx/K6t9WDJBkEnwrJWAMtWp0wFnL4yY=
547543
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ses v1.0.529 h1:NezuBQEG7ihXbwueREo4qavBAo6ZcHuWGZt1dR+VdKc=
548544
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/ses v1.0.529/go.mod h1:Xfmf4Rq4J2ukz6NoEwpADQS5ws3By3KDPemCQeeMF+M=
549545
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sms v1.0.486 h1:eHLaL+hl5X5f8Apuf2SGVclO3MRev/E3AfA/0aZQGUA=
@@ -568,8 +564,6 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tdmq v1.0.268 h1:ez5lvK
568564
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tdmq v1.0.268/go.mod h1:fchXZhmqaYaG2c4wTCBTdnW6TFAtxl3D/P/yuuuLMfA=
569565
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tem v1.0.527 h1:nRFYv2eZ+qMsOYID+ZmI4nSJuuXUgm2SCLFVu3bQmnw=
570566
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tem v1.0.527/go.mod h1:mWXRd5WNzX7ypg1MNliZvz5mlfIJ9h0KvyOGnH86KFc=
571-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/teo v1.0.500 h1:8pOrQK3GLskFEHGYR3s6is0YLAgSe7vYs7wdC+brA30=
572-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/teo v1.0.500/go.mod h1:eQfp4Z6W1vg5vBVkR6i/ZdgKeXWoMECCvQKGTvU5dP0=
573567
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/teo v1.0.529 h1:vWUgseUvHs1fW/Ok+x3ld9UIhrYRNO9Yr8ccX8wmkkY=
574568
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/teo v1.0.529/go.mod h1:vOd23iOVeQqm5LSEXUmE8773kiUCwGuoJnTO0po5D+Q=
575569
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke v1.0.519 h1:o8NsQPLV6T8TD4sHxufCwtCsqYM4CUM1132zut6toww=

tencentcloud/basic_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,17 @@ const (
761761

762762
// End of TCM
763763

764+
// DCDB
765+
const (
766+
defaultDcdbInstanceId = "tdsqlshard-lgz66iqr"
767+
defaultDcdbInstanceName = "keep-dcdb-test"
768+
defaultDcdbInsVpcId = "vpc-4owdpnwr"
769+
defaultDcdbInsIdSubnetId = "subnet-qylstu34"
770+
defaultDcdbSGId = "sg-ijato2x1"
771+
defaultDcdbSGName = "default"
772+
)
773+
774+
// End of DCDB
764775
// SES
765776
const (
766777
defaultRegionSes = "ap-hongkong"
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
/*
2+
Use this data source to query detailed information of dcdb accounts.
3+
4+
Example Usage
5+
6+
```hcl
7+
data "tencentcloud_dcdb_accounts" "foo" {
8+
instance_id = tencentcloud_dcdb_account.foo.instance_id
9+
}
10+
```
11+
*/
12+
package tencentcloud
13+
14+
import (
15+
"context"
16+
"log"
17+
18+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
19+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
20+
dcdb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dcdb/v20180411"
21+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
22+
)
23+
24+
func dataSourceTencentCloudDcdbAccounts() *schema.Resource {
25+
return &schema.Resource{
26+
Read: dataSourceTencentCloudDcdbAccountsRead,
27+
Schema: map[string]*schema.Schema{
28+
"instance_id": {
29+
Type: schema.TypeString,
30+
Required: true,
31+
Description: "instance id.",
32+
},
33+
"list": {
34+
Type: schema.TypeList,
35+
Computed: true,
36+
Description: "Cloud database account information.",
37+
Elem: &schema.Resource{
38+
Schema: map[string]*schema.Schema{
39+
"user_name": {
40+
Type: schema.TypeString,
41+
Computed: true,
42+
Description: "User Name.",
43+
},
44+
"host": {
45+
Type: schema.TypeString,
46+
Computed: true,
47+
Description: "From which host the user can log in (corresponding to the host field of MySQL users, UserName + Host uniquely identifies a user, in the form of IP, the IP segment ends with %; supports filling in %; if it is empty, it defaults to %).",
48+
},
49+
"description": {
50+
Type: schema.TypeString,
51+
Computed: true,
52+
Description: "User remarks info.",
53+
},
54+
"create_time": {
55+
Type: schema.TypeString,
56+
Computed: true,
57+
Description: "Creation time.",
58+
},
59+
"update_time": {
60+
Type: schema.TypeString,
61+
Computed: true,
62+
Description: "Last update time.",
63+
},
64+
"read_only": {
65+
Type: schema.TypeInt,
66+
Computed: true,
67+
Description: "Read-only flag, 0: No, 1: The SQL request of this account is preferentially executed on the standby machine, and the host is selected for execution when the standby machine is unavailable. 2: The standby machine is preferentially selected for execution, and the operation fails when the standby machine is unavailable.",
68+
},
69+
"delay_thresh": {
70+
Type: schema.TypeInt,
71+
Computed: true,
72+
Description: "If the standby machine delay exceeds the setting value of this parameter, the system will consider that the standby machine is faulty and recommend that the parameter value be greater than 10. This parameter takes effect when ReadOnly selects 1 and 2.",
73+
},
74+
"slave_const": {
75+
Type: schema.TypeInt,
76+
Computed: true,
77+
Description: "For read-only accounts, set the policy whether to fix the standby machine, 0: not fix the standby machine, that is, the standby machine will not disconnect from the client if it does not meet the conditions, the Proxy selects other available standby machines, 1: the standby machine will be disconnected if the conditions are not met, Make sure a connection is secured to the standby machine.",
78+
},
79+
},
80+
},
81+
},
82+
"result_output_file": {
83+
Type: schema.TypeString,
84+
Optional: true,
85+
Description: "Used to save results.",
86+
},
87+
},
88+
}
89+
}
90+
91+
func dataSourceTencentCloudDcdbAccountsRead(d *schema.ResourceData, meta interface{}) error {
92+
defer logElapsed("data_source.tencentcloud_dcdb_accounts.read")()
93+
defer inconsistentCheck(d, meta)()
94+
95+
logId := getLogId(contextNil)
96+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
97+
98+
paramMap := make(map[string]interface{})
99+
if v, ok := d.GetOk("instance_id"); ok {
100+
paramMap["instance_id"] = helper.String(v.(string))
101+
}
102+
103+
dcdbService := DcdbService{client: meta.(*TencentCloudClient).apiV3Conn}
104+
105+
var dbAccountList []*dcdb.DBAccount
106+
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
107+
results, e := dcdbService.DescribeDcdbAccountsByFilter(ctx, paramMap)
108+
if e != nil {
109+
return retryError(e)
110+
}
111+
dbAccountList = results
112+
return nil
113+
})
114+
if err != nil {
115+
log.Printf("[CRITAL]%s read Dcdb list failed, reason:%+v", logId, err)
116+
return err
117+
}
118+
119+
retList := []interface{}{}
120+
if dbAccountList != nil {
121+
ids := make([]string, 0, len(dbAccountList))
122+
for _, dbA := range dbAccountList {
123+
listMap := map[string]interface{}{}
124+
if dbA.UserName != nil {
125+
listMap["user_name"] = dbA.UserName
126+
}
127+
if dbA.Host != nil {
128+
listMap["host"] = dbA.Host
129+
}
130+
if dbA.Description != nil {
131+
listMap["description"] = dbA.Description
132+
}
133+
if dbA.CreateTime != nil {
134+
listMap["create_time"] = dbA.CreateTime
135+
}
136+
if dbA.UpdateTime != nil {
137+
listMap["update_time"] = dbA.UpdateTime
138+
}
139+
if dbA.ReadOnly != nil {
140+
listMap["read_only"] = dbA.ReadOnly
141+
}
142+
if dbA.DelayThresh != nil {
143+
listMap["delay_thresh"] = dbA.DelayThresh
144+
}
145+
if dbA.SlaveConst != nil {
146+
listMap["slave_const"] = dbA.SlaveConst
147+
}
148+
ids = append(ids, *dbA.UserName+FILED_SP+*dbA.Host)
149+
retList = append(retList, listMap)
150+
}
151+
152+
d.SetId(helper.DataResourceIdsHash(ids))
153+
_ = d.Set("list", retList)
154+
}
155+
156+
output, ok := d.GetOk("result_output_file")
157+
if ok && output.(string) != "" {
158+
if e := writeToFile(output.(string), retList); e != nil {
159+
return e
160+
}
161+
}
162+
163+
return nil
164+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package tencentcloud
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
8+
)
9+
10+
func TestAccTencentCloudDcdbAccountsDataSource(t *testing.T) {
11+
t.Parallel()
12+
13+
resource.Test(t, resource.TestCase{
14+
PreCheck: func() { testAccPreCheck(t) },
15+
Providers: testAccProviders,
16+
CheckDestroy: testAccCheckDcdbAccountDestroy,
17+
Steps: []resource.TestStep{
18+
{
19+
Config: fmt.Sprintf(testAccDataSourceDcdbAccounts, defaultDcdbInstanceId),
20+
Check: resource.ComposeTestCheckFunc(
21+
testAccCheckTencentCloudDataSourceID("data.tencentcloud_dcdb_accounts.basic"),
22+
resource.TestCheckResourceAttr("data.tencentcloud_dcdb_accounts.basic", "list.#", "1"),
23+
resource.TestCheckResourceAttrSet("data.tencentcloud_dcdb_accounts.basic", "list.0.user_name"),
24+
resource.TestCheckResourceAttr("data.tencentcloud_dcdb_accounts.basic", "list.0.user_name", "mysql_ds"),
25+
resource.TestCheckResourceAttr("data.tencentcloud_dcdb_accounts.basic", "list.0.host", "127.0.0.1"),
26+
resource.TestCheckResourceAttr("data.tencentcloud_dcdb_accounts.basic", "list.0.description", "this is a test account"),
27+
resource.TestCheckResourceAttrSet("data.tencentcloud_dcdb_accounts.basic", "list.0.create_time"),
28+
resource.TestCheckResourceAttrSet("data.tencentcloud_dcdb_accounts.basic", "list.0.update_time"),
29+
resource.TestCheckResourceAttr("data.tencentcloud_dcdb_accounts.basic", "list.0.read_only", "0"),
30+
resource.TestCheckResourceAttrSet("data.tencentcloud_dcdb_accounts.basic", "list.0.delay_thresh"),
31+
resource.TestCheckResourceAttrSet("data.tencentcloud_dcdb_accounts.basic", "list.0.slave_const"),
32+
),
33+
},
34+
},
35+
})
36+
}
37+
38+
const testAccDataSourceDcdbAccounts = `
39+
resource "tencentcloud_dcdb_account" "basic" {
40+
instance_id = "%s" # use the hard code before the dcdb_instance resource is ready.
41+
user_name = "mysql_ds"
42+
host = "127.0.0.1"
43+
password = "===password==="
44+
read_only = 0
45+
description = "this is a test account"
46+
max_user_connections = 10
47+
}
48+
49+
data "tencentcloud_dcdb_accounts" "basic" {
50+
instance_id = tencentcloud_dcdb_account.basic.instance_id
51+
}
52+
53+
`
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
Use this data source to query detailed information of dcdb databases
3+
4+
Example Usage
5+
6+
```hcl
7+
data "tencentcloud_dcdb_databases" "databases" {
8+
instance_id = "your_dcdb_instance_id"
9+
}
10+
```
11+
*/
12+
package tencentcloud
13+
14+
import (
15+
"context"
16+
"log"
17+
18+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
19+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
20+
dcdb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dcdb/v20180411"
21+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
22+
)
23+
24+
func dataSourceTencentCloudDcdbDatabases() *schema.Resource {
25+
return &schema.Resource{
26+
Read: dataSourceTencentCloudDcdbDatabasesRead,
27+
Schema: map[string]*schema.Schema{
28+
"instance_id": {
29+
Type: schema.TypeString,
30+
Required: true,
31+
Description: "instance id.",
32+
},
33+
34+
"list": {
35+
Type: schema.TypeList,
36+
Computed: true,
37+
Description: "Database information.",
38+
Elem: &schema.Resource{
39+
Schema: map[string]*schema.Schema{
40+
"db_name": {
41+
Type: schema.TypeString,
42+
Computed: true,
43+
Description: "Database Name.",
44+
},
45+
},
46+
},
47+
},
48+
49+
"result_output_file": {
50+
Type: schema.TypeString,
51+
Optional: true,
52+
Description: "Used to save results.",
53+
},
54+
},
55+
}
56+
}
57+
58+
func dataSourceTencentCloudDcdbDatabasesRead(d *schema.ResourceData, meta interface{}) error {
59+
defer logElapsed("data_source.tencentcloud_dcdb_databases.read")()
60+
defer inconsistentCheck(d, meta)()
61+
62+
logId := getLogId(contextNil)
63+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
64+
65+
paramMap := make(map[string]interface{})
66+
if v, ok := d.GetOk("instance_id"); ok {
67+
paramMap["instance_id"] = helper.String(v.(string))
68+
}
69+
70+
dcdbService := DcdbService{client: meta.(*TencentCloudClient).apiV3Conn}
71+
72+
var dbs []*dcdb.Database
73+
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
74+
results, e := dcdbService.DescribeDcdbDatabasesByFilter(ctx, paramMap)
75+
if e != nil {
76+
return retryError(e)
77+
}
78+
dbs = results
79+
return nil
80+
})
81+
if err != nil {
82+
log.Printf("[CRITAL]%s read Dcdb list failed, reason:%+v", logId, err)
83+
return err
84+
}
85+
86+
ids := make([]string, 0, len(dbs))
87+
list := make([]map[string]interface{}, 0, len(dbs))
88+
if dbs != nil {
89+
for _, db := range dbs {
90+
dbMap := map[string]interface{}{}
91+
if db.DbName != nil {
92+
dbMap["db_name"] = db.DbName
93+
}
94+
ids = append(ids, *db.DbName)
95+
list = append(list, dbMap)
96+
}
97+
d.SetId(helper.DataResourceIdsHash(ids))
98+
_ = d.Set("list", list)
99+
}
100+
101+
output, ok := d.GetOk("result_output_file")
102+
if ok && output.(string) != "" {
103+
if e := writeToFile(output.(string), list); e != nil {
104+
return e
105+
}
106+
}
107+
108+
return nil
109+
}

0 commit comments

Comments
 (0)