Skip to content

Commit 7317811

Browse files
tongyimingmikatong
andauthored
Feat/cynosdb (#1508)
* append tencentcloud_cynosdb_audit_log_file * append tencentcloud_cynosdb_security_group * append tencentcloud_cynosdb_backup * append tencentcloud_cynosdb_accounts * append tencentcloud_cynosdb_cluster_instance_groups * append tencentcloud_cynosdb_cluster_params * append tencentcloud_cynosdb_param_templates * add unit test && doc * update vendor * update * update * add changelog Co-authored-by: mikatong <mikatong@tencent.com>
1 parent e962664 commit 7317811

30 files changed

+7131
-215
lines changed

.changelog/1508.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_cynosdb_accounts
3+
```
4+
5+
```release-note:new-data-source
6+
tencentcloud_cynosdb_cluster_instance_groups
7+
```
8+
9+
```release-note:new-data-source
10+
tencentcloud_cynosdb_cluster_params
11+
```
12+
13+
```release-note:new-data-source
14+
tencentcloud_cynosdb_param_templates
15+
```
16+
17+
```release-note:new-resource
18+
tencentcloud_cynosdb_audit_log_file
19+
```
20+
21+
```release-note:new-resource
22+
tencentcloud_cynosdb_security_group
23+
```

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ require (
3737
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cls v1.0.412
3838
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.573
3939
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.553
40-
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cynosdb v1.0.488
40+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cynosdb v1.0.572
4141
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dayu v1.0.335
4242
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dbbrain v1.0.542
4343
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dc v1.0.199

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,8 @@ github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.553 h1:Pl1kYgF
554554
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm v1.0.553/go.mod h1:dnnqPxXYK+kax3e1MKo/PI3iWJtytm6ogWKQHJS7SGE=
555555
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cynosdb v1.0.488 h1:A1seXWtMf2atBjSNYvcwxyDoFzCMgqyVnsxnWzhqJEA=
556556
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cynosdb v1.0.488/go.mod h1:T8mL4dQLkPTL6VtVMeBYol3asSUS53ycP9PJf5Qp1GE=
557+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cynosdb v1.0.572 h1:s6YaKNJla+zow8j+9UDFVWblPRv8kwS4dlAY1IWT15o=
558+
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cynosdb v1.0.572/go.mod h1:Wp8vjOiPCqWs8ERh6+XcMYdGul6L8bU/icFY6JzxGdU=
557559
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dayu v1.0.335 h1:D8qrelkK5udv8RzJJIABMzItGIyaZoYnxEVeIsYqiNw=
558560
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dayu v1.0.335/go.mod h1:pz4s3nOhoB9cY0+uWzifuwr7lfh/Gvi1rv0ADxpPzD4=
559561
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dbbrain v1.0.542 h1:96OU+NJZKgx6VC7KTu4syZ4I71LwUHLAl5BeKNHanio=

tencentcloud/basic_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -880,3 +880,25 @@ const (
880880
)
881881

882882
// End of CI
883+
884+
// Cynosdb
885+
const (
886+
defaultCynosdbClusterId = "cynosdbmysql-bws8h88b"
887+
defaultCynosdbClusterInstanceId = "cynosdbmysql-ins-afqx1hy0"
888+
defaultCynosdbSecurityGroup = "sg-baxfiao5"
889+
)
890+
891+
const CommonCynosdb = `
892+
893+
variable "cynosdb_cluster_id" {
894+
default = "` + defaultCynosdbClusterId + `"
895+
}
896+
variable "cynosdb_cluster_instance_id" {
897+
default = "` + defaultCynosdbClusterInstanceId + `"
898+
}
899+
variable "cynosdb_cluster_security_group_id" {
900+
default = "` + defaultCynosdbSecurityGroup + `"
901+
}
902+
`
903+
904+
// End of Cynosdb
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 cynosdb accounts
3+
4+
Example Usage
5+
6+
```hcl
7+
data "tencentcloud_cynosdb_accounts" "accounts" {
8+
cluster_id = "cynosdbmysql-bws8h88b"
9+
account_names = ["root"]
10+
}
11+
```
12+
*/
13+
package tencentcloud
14+
15+
import (
16+
"context"
17+
18+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
19+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
20+
cynosdb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cynosdb/v20190107"
21+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
22+
)
23+
24+
func dataSourceTencentCloudCynosdbAccounts() *schema.Resource {
25+
return &schema.Resource{
26+
Read: dataSourceTencentCloudCynosdbAccountsRead,
27+
Schema: map[string]*schema.Schema{
28+
"cluster_id": {
29+
Required: true,
30+
Type: schema.TypeString,
31+
Description: "The ID of cluster.",
32+
},
33+
34+
"account_names": {
35+
Optional: true,
36+
Type: schema.TypeSet,
37+
Elem: &schema.Schema{
38+
Type: schema.TypeString,
39+
},
40+
Description: "List of accounts to be filtered.",
41+
},
42+
43+
"hosts": {
44+
Optional: true,
45+
Type: schema.TypeSet,
46+
Elem: &schema.Schema{
47+
Type: schema.TypeString,
48+
},
49+
Description: "List of hosts to be filtered.",
50+
},
51+
52+
"account_set": {
53+
Type: schema.TypeList,
54+
Computed: true,
55+
Description: "Database account list.&amp;quot;&amp;quot;Note: This field may return null, indicating that no valid value can be obtained.",
56+
Elem: &schema.Resource{
57+
Schema: map[string]*schema.Schema{
58+
"account_name": {
59+
Type: schema.TypeString,
60+
Computed: true,
61+
Description: "Account name of database.",
62+
},
63+
"description": {
64+
Type: schema.TypeString,
65+
Computed: true,
66+
Description: "The account description of database.",
67+
},
68+
"create_time": {
69+
Type: schema.TypeString,
70+
Computed: true,
71+
Description: "Create time.",
72+
},
73+
"update_time": {
74+
Type: schema.TypeString,
75+
Computed: true,
76+
Description: "Update time.",
77+
},
78+
"host": {
79+
Type: schema.TypeString,
80+
Computed: true,
81+
Description: "Host.",
82+
},
83+
"max_user_connections": {
84+
Type: schema.TypeInt,
85+
Computed: true,
86+
Description: "Maximum number of user connections.",
87+
},
88+
},
89+
},
90+
},
91+
92+
"result_output_file": {
93+
Type: schema.TypeString,
94+
Optional: true,
95+
Description: "Used to save results.",
96+
},
97+
},
98+
}
99+
}
100+
101+
func dataSourceTencentCloudCynosdbAccountsRead(d *schema.ResourceData, meta interface{}) error {
102+
defer logElapsed("data_source.tencentcloud_cynosdb_accounts.read")()
103+
defer inconsistentCheck(d, meta)()
104+
105+
logId := getLogId(contextNil)
106+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
107+
108+
var clusterId string
109+
110+
paramMap := make(map[string]interface{})
111+
if v, ok := d.GetOk("cluster_id"); ok {
112+
clusterId = v.(string)
113+
}
114+
115+
if v, ok := d.GetOk("account_names"); ok {
116+
accountNamesSet := v.(*schema.Set).List()
117+
paramMap["account_names"] = accountNamesSet
118+
}
119+
120+
if v, ok := d.GetOk("hosts"); ok {
121+
hostsSet := v.(*schema.Set).List()
122+
paramMap["Hosts"] = helper.InterfacesStringsPoint(hostsSet)
123+
}
124+
125+
service := CynosdbService{client: meta.(*TencentCloudClient).apiV3Conn}
126+
127+
var accountSet []*cynosdb.Account
128+
129+
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
130+
response, e := service.DescribeCynosdbAccountsByFilter(ctx, clusterId, paramMap)
131+
if e != nil {
132+
return retryError(e)
133+
}
134+
accountSet = response.AccountSet
135+
return nil
136+
})
137+
if err != nil {
138+
return err
139+
}
140+
141+
ids := make([]string, 0, len(accountSet))
142+
tmpList := make([]map[string]interface{}, 0, len(accountSet))
143+
for _, item := range accountSet {
144+
ids = append(ids, *item.AccountName)
145+
account := make(map[string]interface{})
146+
account["account_name"] = item.AccountName
147+
account["description"] = item.Description
148+
account["create_time"] = item.CreateTime
149+
account["update_time"] = item.UpdateTime
150+
account["host"] = item.Host
151+
account["max_user_connections"] = item.MaxUserConnections
152+
153+
tmpList = append(tmpList, account)
154+
}
155+
d.SetId(helper.DataResourceIdsHash(ids))
156+
d.Set("account_set", tmpList)
157+
output, ok := d.GetOk("result_output_file")
158+
if ok && output.(string) != "" {
159+
if e := writeToFile(output.(string), tmpList); e != nil {
160+
return e
161+
}
162+
}
163+
return nil
164+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
7+
)
8+
9+
func TestAccTencentCloudCynosdbAccountsDataSource_basic(t *testing.T) {
10+
t.Parallel()
11+
resource.Test(t, resource.TestCase{
12+
PreCheck: func() {
13+
testAccPreCheck(t)
14+
},
15+
Providers: testAccProviders,
16+
Steps: []resource.TestStep{
17+
{
18+
Config: testAccCynosdbAccountsDataSource,
19+
Check: resource.ComposeTestCheckFunc(
20+
testAccCheckTencentCloudDataSourceID("data.tencentcloud_cynosdb_accounts.accounts"),
21+
resource.TestCheckResourceAttr("data.tencentcloud_cynosdb_accounts.accounts", "account_set.#", "1"),
22+
),
23+
},
24+
},
25+
})
26+
}
27+
28+
const testAccCynosdbAccountsDataSource = CommonCynosdb + `
29+
30+
data "tencentcloud_cynosdb_accounts" "accounts" {
31+
cluster_id = var.cynosdb_cluster_id
32+
account_names = ["root"]
33+
}
34+
35+
`

0 commit comments

Comments
 (0)