Skip to content

Commit dd688e3

Browse files
gitmknanonymous
andauthored
Feat/mariadb support 2 (#1375)
* feat: support db account * feat: support parameters * feat: support logFileRetentionPeriod * feat: support security_groups * feat: support datasource account * feat: support datasource securityGroups * fix: modify doc * feat: add changelog Co-authored-by: anonymous <anonymous@mail.org>
1 parent a0a6f5a commit dd688e3

26 files changed

+2972
-49
lines changed

.changelog/1375.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
```release-note:new-data-source
2+
tencentcloud_mariadb_accounts
3+
```
4+
5+
```release-note:new-data-source
6+
tencentcloud_mariadb_security_groups
7+
```
8+
9+
```release-note:new-resource
10+
tencentcloud_mariadb_account
11+
```
12+
13+
```release-note:new-resource
14+
tencentcloud_mariadb_parameters
15+
```
16+
17+
```release-note:new-resource
18+
tencentcloud_mariadb_log_file_retention_period
19+
```
20+
21+
```release-note:new-resource
22+
tencentcloud_mariadb_security_groups
23+
```

tencentcloud/basic_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -780,8 +780,9 @@ const (
780780
// End of SES
781781
// MARIADB
782782
const (
783-
defaultMariadbSubnetId = "subnet-ob6clqwk"
784-
defaultMariadbVpcId = "vpc-68vi2d3h"
783+
defaultMariadbSubnetId = "subnet-jdi5xn22"
784+
defaultMariadbVpcId = "vpc-k1t8ickr"
785+
defaultMariadbSecurityGroupId = "sg-7kpsbxdb"
785786
)
786787

787788
// End of MARIADB
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
/*
2+
Use this data source to query detailed information of mariadb accounts
3+
4+
Example Usage
5+
6+
```hcl
7+
data "tencentcloud_mariadb_accounts" "accounts" {
8+
instance_id = "tdsql-4pzs5b67"
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+
mariadb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mariadb/v20170312"
21+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
22+
)
23+
24+
func dataSourceTencentCloudMariadbAccounts() *schema.Resource {
25+
return &schema.Resource{
26+
Read: dataSourceTencentCloudMariadbAccountsRead,
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: "account list.",
38+
Elem: &schema.Resource{
39+
Schema: map[string]*schema.Schema{
40+
"user_name": {
41+
Type: schema.TypeString,
42+
Computed: true,
43+
Description: "username.",
44+
},
45+
"host": {
46+
Type: schema.TypeString,
47+
Computed: true,
48+
Description: "The host from which the user can log in (corresponding to the host field of MySQL users, UserName + Host uniquely identifies a user, in the form of IP, and the IP segment ends with %; supports filling in %; if it is empty, it defaults to %).",
49+
},
50+
"description": {
51+
Type: schema.TypeString,
52+
Computed: true,
53+
Description: "User remarks.",
54+
},
55+
"create_time": {
56+
Type: schema.TypeString,
57+
Computed: true,
58+
Description: "creation time.",
59+
},
60+
"update_time": {
61+
Type: schema.TypeString,
62+
Computed: true,
63+
Description: "Update time.",
64+
},
65+
"read_only": {
66+
Type: schema.TypeInt,
67+
Computed: true,
68+
Description: "Read-only flag, `0`: No, `1`: The SQL request of this account is preferentially executed on the standby machine, and the host machine 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.",
69+
},
70+
"delay_thresh": {
71+
Type: schema.TypeInt,
72+
Computed: true,
73+
Description: "This field is meaningful for read-only accounts, indicating that the standby machine with the active-standby delay less than this value is selected.",
74+
},
75+
"slave_const": {
76+
Type: schema.TypeInt,
77+
Computed: true,
78+
Description: "For read-only accounts, set whether the policy is to fix the standby machine, `0`: The standby machine is not fixed, that is, the standby machine does not meet the conditions and will not disconnect from the client, and the Proxy selects other available standby machines, `1`: The standby machine does not meet the conditions Disconnect, make sure one connection secures the standby.",
79+
},
80+
},
81+
},
82+
},
83+
84+
"result_output_file": {
85+
Type: schema.TypeString,
86+
Optional: true,
87+
Description: "Used to save results.",
88+
},
89+
},
90+
}
91+
}
92+
93+
func dataSourceTencentCloudMariadbAccountsRead(d *schema.ResourceData, meta interface{}) error {
94+
defer logElapsed("data_source.tencentcloud_mariadb_accounts.read")()
95+
defer inconsistentCheck(d, meta)()
96+
97+
logId := getLogId(contextNil)
98+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
99+
var instanceId string
100+
101+
paramMap := make(map[string]interface{})
102+
if v, ok := d.GetOk("instance_id"); ok {
103+
instanceId = v.(string)
104+
paramMap["instance_id"] = helper.String(v.(string))
105+
}
106+
107+
mariadbService := MariadbService{client: meta.(*TencentCloudClient).apiV3Conn}
108+
109+
var users []*mariadb.DBAccount
110+
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
111+
results, e := mariadbService.DescribeMariadbAccountsByFilter(ctx, paramMap)
112+
if e != nil {
113+
return retryError(e)
114+
}
115+
users = results
116+
return nil
117+
})
118+
if err != nil {
119+
log.Printf("[CRITAL]%s read Mariadb users failed, reason:%+v", logId, err)
120+
return err
121+
}
122+
123+
userList := []interface{}{}
124+
if users != nil {
125+
for _, user := range users {
126+
userMap := map[string]interface{}{}
127+
if user.UserName != nil {
128+
userMap["user_name"] = user.UserName
129+
}
130+
if user.Host != nil {
131+
userMap["host"] = user.Host
132+
}
133+
if user.Description != nil {
134+
userMap["description"] = user.Description
135+
}
136+
if user.CreateTime != nil {
137+
userMap["create_time"] = user.CreateTime
138+
}
139+
if user.UpdateTime != nil {
140+
userMap["update_time"] = user.UpdateTime
141+
}
142+
if user.ReadOnly != nil {
143+
userMap["read_only"] = user.ReadOnly
144+
}
145+
if user.DelayThresh != nil {
146+
userMap["delay_thresh"] = user.DelayThresh
147+
}
148+
if user.SlaveConst != nil {
149+
userMap["slave_const"] = user.SlaveConst
150+
}
151+
152+
userList = append(userList, userMap)
153+
}
154+
err = d.Set("list", userList)
155+
if err != nil {
156+
log.Printf("[CRITAL]%s provider set instances list fail, reason:%s\n ", logId, err.Error())
157+
return err
158+
}
159+
}
160+
d.SetId(instanceId)
161+
162+
output, ok := d.GetOk("result_output_file")
163+
if ok && output.(string) != "" {
164+
if e := writeToFile(output.(string), userList); e != nil {
165+
return e
166+
}
167+
}
168+
169+
return nil
170+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
7+
)
8+
9+
// go test -i; go test -test.run TestAccTencentCloudMariadbAccountsDataSource -v
10+
func TestAccTencentCloudMariadbAccountsDataSource(t *testing.T) {
11+
t.Parallel()
12+
13+
resource.Test(t, resource.TestCase{
14+
PreCheck: func() { testAccPreCheck(t) },
15+
Providers: testAccProviders,
16+
Steps: []resource.TestStep{
17+
{
18+
Config: testAccDataSourceMariadbAccounts,
19+
Check: resource.ComposeTestCheckFunc(
20+
testAccCheckTencentCloudDataSourceID("data.tencentcloud_mariadb_accounts.accounts"),
21+
),
22+
},
23+
},
24+
})
25+
}
26+
27+
const testAccDataSourceMariadbAccounts = testAccMariadbAccount + `
28+
29+
data "tencentcloud_mariadb_accounts" "accounts" {
30+
instance_id = tencentcloud_mariadb_hour_db_instance.basic.id
31+
}
32+
33+
`

0 commit comments

Comments
 (0)