Skip to content

Commit f40ad8b

Browse files
authored
Feat/cynosdb data source (#1904)
* add * add * feat/cynosdb-data-source * feat/cynosdb-data-source * feat/cynosdb-data-source * feat/cynosdb-data-source * feat/cynosdb-data-source * feat/cynosdb-data-source * feat/cynosdb-data-source
1 parent 19b5aa5 commit f40ad8b

21 files changed

+2064
-67
lines changed

.changelog/1904.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
```release-note:new-data-source
2+
tencentcloud_cynosdb_account_all_grant_privileges
3+
```
4+
5+
```release-note:new-data-source
6+
tencentcloud_cynosdb_resource_package_list
7+
```
8+
9+
```release-note:new-data-source
10+
tencentcloud_cynosdb_project_security_groups
11+
```
12+
13+
```release-note:new-data-source
14+
tencentcloud_cynosdb_resource_package_sale_specs
15+
```
16+
17+
```release-note:new-data-source
18+
tencentcloud_cynosdb_rollback_time_range
19+
```
Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
/*
2+
Use this data source to query detailed information of cynosdb account_all_grant_privileges
3+
4+
Example Usage
5+
6+
```hcl
7+
data "tencentcloud_cynosdb_account_all_grant_privileges" "account_all_grant_privileges" {
8+
cluster_id = "cynosdbmysql-bws8h88b"
9+
account {
10+
account_name = "keep_dts"
11+
host = "%"
12+
}
13+
}
14+
```
15+
*/
16+
package tencentcloud
17+
18+
import (
19+
"context"
20+
21+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
22+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
23+
cynosdb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cynosdb/v20190107"
24+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
25+
)
26+
27+
func dataSourceTencentCloudCynosdbAccountAllGrantPrivileges() *schema.Resource {
28+
return &schema.Resource{
29+
Read: dataSourceTencentCloudCynosdbAccountAllGrantPrivilegesRead,
30+
Schema: map[string]*schema.Schema{
31+
"cluster_id": {
32+
Required: true,
33+
Type: schema.TypeString,
34+
Description: "Cluster ID.",
35+
},
36+
"account": {
37+
Required: true,
38+
Type: schema.TypeList,
39+
MaxItems: 1,
40+
Description: "account information.",
41+
Elem: &schema.Resource{
42+
Schema: map[string]*schema.Schema{
43+
"account_name": {
44+
Type: schema.TypeString,
45+
Required: true,
46+
Description: "Account.",
47+
},
48+
"host": {
49+
Type: schema.TypeString,
50+
Optional: true,
51+
Description: "Host, default `%`.",
52+
},
53+
},
54+
},
55+
},
56+
"privilege_statements": {
57+
Computed: true,
58+
Type: schema.TypeSet,
59+
Elem: &schema.Schema{Type: schema.TypeString},
60+
Description: "Permission statement note: This field may return null, indicating that a valid value cannot be obtained.",
61+
},
62+
"global_privileges": {
63+
Computed: true,
64+
Type: schema.TypeSet,
65+
Elem: &schema.Schema{Type: schema.TypeString},
66+
Description: "Global permission note: This field may return null, indicating that a valid value cannot be obtained.",
67+
},
68+
"database_privileges": {
69+
Computed: true,
70+
Type: schema.TypeList,
71+
Description: "Database permissions note: This field may return null, indicating that a valid value cannot be obtained.",
72+
Elem: &schema.Resource{
73+
Schema: map[string]*schema.Schema{
74+
"db": {
75+
Type: schema.TypeString,
76+
Computed: true,
77+
Description: "database.",
78+
},
79+
"privileges": {
80+
Type: schema.TypeSet,
81+
Elem: &schema.Schema{Type: schema.TypeString},
82+
Computed: true,
83+
Description: "Permission List.",
84+
},
85+
},
86+
},
87+
},
88+
"table_privileges": {
89+
Computed: true,
90+
Type: schema.TypeList,
91+
Description: "Database table permissions note: This field may return null, indicating that a valid value cannot be obtained.",
92+
Elem: &schema.Resource{
93+
Schema: map[string]*schema.Schema{
94+
"db": {
95+
Type: schema.TypeString,
96+
Computed: true,
97+
Description: "Database name.",
98+
},
99+
"table_name": {
100+
Type: schema.TypeString,
101+
Computed: true,
102+
Description: "Table Name.",
103+
},
104+
"privileges": {
105+
Type: schema.TypeSet,
106+
Elem: &schema.Schema{
107+
Type: schema.TypeString,
108+
},
109+
Computed: true,
110+
Description: "Permission List.",
111+
},
112+
},
113+
},
114+
},
115+
"result_output_file": {
116+
Type: schema.TypeString,
117+
Optional: true,
118+
Description: "Used to save results.",
119+
},
120+
},
121+
}
122+
}
123+
124+
func dataSourceTencentCloudCynosdbAccountAllGrantPrivilegesRead(d *schema.ResourceData, meta interface{}) error {
125+
defer logElapsed("data_source.tencentcloud_cynosdb_account_all_grant_privileges.read")()
126+
defer inconsistentCheck(d, meta)()
127+
128+
var (
129+
logId = getLogId(contextNil)
130+
ctx = context.WithValue(context.TODO(), logIdKey, logId)
131+
service = CynosdbService{client: meta.(*TencentCloudClient).apiV3Conn}
132+
accountAllGrantPrivileges *cynosdb.DescribeAccountAllGrantPrivilegesResponseParams
133+
clusterId string
134+
)
135+
136+
paramMap := make(map[string]interface{})
137+
if v, ok := d.GetOk("cluster_id"); ok {
138+
paramMap["ClusterId"] = helper.String(v.(string))
139+
clusterId = v.(string)
140+
}
141+
142+
if dMap, ok := helper.InterfacesHeadMap(d, "account"); ok {
143+
inputAccount := cynosdb.InputAccount{}
144+
if v, ok := dMap["account_name"]; ok {
145+
inputAccount.AccountName = helper.String(v.(string))
146+
}
147+
if v, ok := dMap["host"]; ok {
148+
inputAccount.Host = helper.String(v.(string))
149+
}
150+
paramMap["Account"] = &inputAccount
151+
}
152+
153+
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
154+
result, e := service.DescribeCynosdbAccountAllGrantPrivilegesByFilter(ctx, paramMap)
155+
if e != nil {
156+
return retryError(e)
157+
}
158+
159+
accountAllGrantPrivileges = result
160+
return nil
161+
})
162+
163+
if err != nil {
164+
return err
165+
}
166+
167+
if accountAllGrantPrivileges.PrivilegeStatements != nil {
168+
_ = d.Set("privilege_statements", accountAllGrantPrivileges.PrivilegeStatements)
169+
}
170+
171+
if accountAllGrantPrivileges.GlobalPrivileges != nil {
172+
_ = d.Set("global_privileges", accountAllGrantPrivileges.GlobalPrivileges)
173+
}
174+
175+
if accountAllGrantPrivileges.DatabasePrivileges != nil {
176+
tmpList := []interface{}{}
177+
for _, databasePrivileges := range accountAllGrantPrivileges.DatabasePrivileges {
178+
databasePrivilegesMap := map[string]interface{}{}
179+
180+
if databasePrivileges.Db != nil {
181+
databasePrivilegesMap["db"] = databasePrivileges.Db
182+
}
183+
184+
if databasePrivileges.Privileges != nil {
185+
databasePrivilegesMap["privileges"] = databasePrivileges.Privileges
186+
}
187+
188+
tmpList = append(tmpList, databasePrivilegesMap)
189+
}
190+
191+
_ = d.Set("database_privileges", tmpList)
192+
}
193+
194+
if accountAllGrantPrivileges.TablePrivileges != nil {
195+
tmpList := []interface{}{}
196+
for _, tablePrivileges := range accountAllGrantPrivileges.TablePrivileges {
197+
tablePrivilegesMap := map[string]interface{}{}
198+
199+
if tablePrivileges.Db != nil {
200+
tablePrivilegesMap["db"] = tablePrivileges.Db
201+
}
202+
203+
if tablePrivileges.TableName != nil {
204+
tablePrivilegesMap["table_name"] = tablePrivileges.TableName
205+
}
206+
207+
if tablePrivileges.Privileges != nil {
208+
tablePrivilegesMap["privileges"] = tablePrivileges.Privileges
209+
}
210+
211+
tmpList = append(tmpList, tablePrivilegesMap)
212+
}
213+
214+
_ = d.Set("table_privileges", tmpList)
215+
}
216+
217+
d.SetId(clusterId)
218+
output, ok := d.GetOk("result_output_file")
219+
if ok && output.(string) != "" {
220+
if e := writeToFile(output.(string), d); e != nil {
221+
return e
222+
}
223+
}
224+
225+
return nil
226+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
)
8+
9+
// go test -i; go test -test.run TestAccTencentCloudCynosdbAccountAllGrantPrivilegesDataSource_basic -v
10+
func TestAccTencentCloudCynosdbAccountAllGrantPrivilegesDataSource_basic(t *testing.T) {
11+
t.Parallel()
12+
resource.Test(t, resource.TestCase{
13+
PreCheck: func() {
14+
testAccPreCheck(t)
15+
},
16+
Providers: testAccProviders,
17+
Steps: []resource.TestStep{
18+
{
19+
Config: testAccCynosdbAccountAllGrantPrivilegesDataSource,
20+
Check: resource.ComposeTestCheckFunc(
21+
testAccCheckTencentCloudDataSourceID("data.tencentcloud_cynosdb_account_all_grant_privileges.account_all_grant_privileges"),
22+
resource.TestCheckResourceAttrSet("data.tencentcloud_cynosdb_account_all_grant_privileges.account_all_grant_privileges", "database_privileges.#"),
23+
resource.TestCheckResourceAttrSet("data.tencentcloud_cynosdb_account_all_grant_privileges.account_all_grant_privileges", "global_privileges.#"),
24+
resource.TestCheckResourceAttrSet("data.tencentcloud_cynosdb_account_all_grant_privileges.account_all_grant_privileges", "privilege_statements.#"),
25+
resource.TestCheckResourceAttrSet("data.tencentcloud_cynosdb_account_all_grant_privileges.account_all_grant_privileges", "table_privileges.#"),
26+
),
27+
},
28+
},
29+
})
30+
}
31+
32+
const testAccCynosdbAccountAllGrantPrivilegesDataSource = `
33+
data "tencentcloud_cynosdb_account_all_grant_privileges" "account_all_grant_privileges" {
34+
cluster_id = "cynosdbmysql-bws8h88b"
35+
account {
36+
account_name = "keep_dts"
37+
host = "%"
38+
}
39+
}
40+
`

0 commit comments

Comments
 (0)