Skip to content

Commit 2475ad1

Browse files
authored
fix(cdb): [117252423]Fix the problem of multiple values ​​in fields d… (#2619)
* fix(cdb): [117252423]Fix the problem of multiple values ​​in fields database, table, column * fix: modify test * fix: modify test * fix: modify test * fix: modify test * feat: add changelog
1 parent 06e4c7b commit 2475ad1

File tree

4 files changed

+110
-128
lines changed

4 files changed

+110
-128
lines changed

.changelog/2619.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
resource/tencentcloud_mysql_privilege: Fix the problem of multiple values ​​in fields database, table, column
3+
```

tencentcloud/services/cdb/resource_tc_mysql_instance_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,3 +655,40 @@ resource "tencentcloud_mysql_instance" "mysql8" {
655655
}
656656
}`, value)
657657
}
658+
659+
const testAccMysql = `
660+
resource "tencentcloud_mysql_instance" "mysql" {
661+
auto_renew_flag = 0
662+
availability_zone = "ap-guangzhou-6"
663+
charge_type = "POSTPAID"
664+
cpu = 4
665+
device_type = "UNIVERSAL"
666+
engine_version = "8.0"
667+
first_slave_zone = "ap-guangzhou-7"
668+
force_delete = false
669+
instance_name = "tf-test"
670+
internet_service = 0
671+
mem_size = 8000
672+
root_password = "password123"
673+
prepaid_period = 1
674+
project_id = 0
675+
security_groups = [
676+
"sg-05f7wnhn",
677+
]
678+
slave_deploy_mode = 1
679+
slave_sync_mode = 0
680+
subnet_id = "subnet-j10lsueq"
681+
tags = {}
682+
volume_size = 100
683+
vpc_id = "vpc-m0d2dbnn"
684+
# wait_switch = 1
685+
686+
parameters = {
687+
character_set_server = "utf8"
688+
lower_case_table_names = "0"
689+
max_connections = "1000"
690+
max_user_connections = 2
691+
long_query_time = "0.200000"
692+
}
693+
}
694+
`

tencentcloud/services/cdb/resource_tc_mysql_privilege.go

Lines changed: 52 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,6 @@ type ResourceTencentCloudMysqlPrivilegeId struct {
2525
AccountHost string `json:"AccountHost,omitempty"`
2626
}
2727

28-
func resourceTencentCloudMysqlPrivilegeHash(v interface{}) int {
29-
vmap := v.(map[string]interface{})
30-
hashMap := map[string]interface{}{}
31-
hashMap["database_name"] = vmap["database_name"]
32-
33-
if vmap["table_name"] != nil {
34-
hashMap["table_name"] = vmap["table_name"]
35-
}
36-
if hashMap["column_name"] != nil {
37-
hashMap["column_name"] = vmap["column_name"]
38-
}
39-
slice := []string{}
40-
for _, v := range vmap["privileges"].(*schema.Set).List() {
41-
slice = append(slice, v.(string))
42-
}
43-
hashMap["privileges"] = slice
44-
b, _ := json.Marshal(hashMap)
45-
return helper.HashString(string(b))
46-
}
47-
4828
func ResourceTencentCloudMysqlPrivilege() *schema.Resource {
4929
return &schema.Resource{
5030
Create: resourceTencentCloudMysqlPrivilegeCreate,
@@ -94,7 +74,6 @@ func ResourceTencentCloudMysqlPrivilege() *schema.Resource {
9474
Type: schema.TypeSet,
9575
Optional: true,
9676
Description: "Database privileges list.",
97-
Set: resourceTencentCloudMysqlPrivilegeHash,
9877
Elem: &schema.Resource{
9978
Schema: map[string]*schema.Schema{
10079
"database_name": {
@@ -103,12 +82,9 @@ func ResourceTencentCloudMysqlPrivilege() *schema.Resource {
10382
Description: "Database name.",
10483
},
10584
"privileges": {
106-
Type: schema.TypeSet,
107-
Required: true,
108-
Elem: &schema.Schema{Type: schema.TypeString},
109-
Set: func(v interface{}) int {
110-
return helper.HashString(v.(string))
111-
},
85+
Type: schema.TypeSet,
86+
Required: true,
87+
Elem: &schema.Schema{Type: schema.TypeString},
11288
Description: `Database privilege.available values for Privileges:` + strings.Join(MYSQL_DATABASE_PRIVILEGE, ",") + ".",
11389
},
11490
},
@@ -118,7 +94,6 @@ func ResourceTencentCloudMysqlPrivilege() *schema.Resource {
11894
Type: schema.TypeSet,
11995
Optional: true,
12096
Description: "Table privileges list.",
121-
Set: resourceTencentCloudMysqlPrivilegeHash,
12297
Elem: &schema.Resource{
12398
Schema: map[string]*schema.Schema{
12499
"database_name": {
@@ -132,12 +107,9 @@ func ResourceTencentCloudMysqlPrivilege() *schema.Resource {
132107
Description: "Table name.",
133108
},
134109
"privileges": {
135-
Type: schema.TypeSet,
136-
Required: true,
137-
Elem: &schema.Schema{Type: schema.TypeString},
138-
Set: func(v interface{}) int {
139-
return helper.HashString(v.(string))
140-
},
110+
Type: schema.TypeSet,
111+
Required: true,
112+
Elem: &schema.Schema{Type: schema.TypeString},
141113
Description: `Table privilege.available values for Privileges:` + strings.Join(MYSQL_TABLE_PRIVILEGE, ",") + ".",
142114
},
143115
},
@@ -147,7 +119,6 @@ func ResourceTencentCloudMysqlPrivilege() *schema.Resource {
147119
Type: schema.TypeSet,
148120
Optional: true,
149121
Description: "Column privileges list.",
150-
Set: resourceTencentCloudMysqlPrivilegeHash,
151122
Elem: &schema.Resource{
152123
Schema: map[string]*schema.Schema{
153124
"database_name": {
@@ -166,12 +137,9 @@ func ResourceTencentCloudMysqlPrivilege() *schema.Resource {
166137
Description: "Column name.",
167138
},
168139
"privileges": {
169-
Type: schema.TypeSet,
170-
Required: true,
171-
Elem: &schema.Schema{Type: schema.TypeString},
172-
Set: func(v interface{}) int {
173-
return helper.HashString(v.(string))
174-
},
140+
Type: schema.TypeSet,
141+
Required: true,
142+
Elem: &schema.Schema{Type: schema.TypeString},
175143
Description: `Column privilege.available values for Privileges:` + strings.Join(MYSQL_COLUMN_PRIVILEGE, ",") + ".",
176144
},
177145
},
@@ -209,93 +177,66 @@ func (me *ResourceTencentCloudMysqlPrivilegeId) update(ctx context.Context, d *s
209177
}
210178
}
211179

212-
same := map[string]bool{}
213-
214-
sliceInterface = d.Get("database").(*schema.Set).List()
215-
if len(sliceInterface) > 0 {
216-
request.DatabasePrivileges = make([]*cdb.DatabasePrivilege, 0, len(sliceInterface))
217-
for _, v := range sliceInterface {
218-
vmap := v.(map[string]interface{})
219-
220-
trace := *sp(vmap["database_name"])
221-
if same[trace] {
222-
return errors.New("can not assign two permissions to a database and an account," + trace)
223-
} else {
224-
same[trace] = true
180+
if v, ok := d.GetOk("database"); ok {
181+
for _, item := range v.(*schema.Set).List() {
182+
dMap := item.(map[string]interface{})
183+
privilege := cdb.DatabasePrivilege{}
184+
if v, ok := dMap["database_name"]; ok {
185+
privilege.Database = helper.String(v.(string))
225186
}
226-
227-
p := &cdb.DatabasePrivilege{
228-
Database: sp(vmap["database_name"]),
229-
Privileges: []*string{},
230-
}
231-
232-
for _, privilege := range vmap["privileges"].(*schema.Set).List() {
233-
ptr := sp(privilege)
234-
if !tccommon.IsContains(MYSQL_DATABASE_PRIVILEGE, *ptr) {
235-
return errors.New("database privileges not support:" + *ptr)
187+
if v, ok := dMap["privileges"]; ok {
188+
privilegeList := []*string{}
189+
for _, v := range v.(*schema.Set).List() {
190+
privilegeList = append(privilegeList, helper.String(v.(string)))
236191
}
237-
p.Privileges = append(p.Privileges, ptr)
192+
privilege.Privileges = privilegeList
238193
}
239-
request.DatabasePrivileges = append(request.DatabasePrivileges, p)
194+
request.DatabasePrivileges = append(request.DatabasePrivileges, &privilege)
240195
}
241196
}
242197

243-
sliceInterface = d.Get("table").(*schema.Set).List()
244-
if len(sliceInterface) > 0 {
245-
request.TablePrivileges = make([]*cdb.TablePrivilege, 0, len(sliceInterface))
246-
for _, v := range sliceInterface {
247-
vmap := v.(map[string]interface{})
248-
249-
trace := *sp(vmap["database_name"]) + "." + *sp(vmap["table_name"])
250-
if same[trace] {
251-
return errors.New("can not assign two permissions to a table and an account," + trace)
252-
} else {
253-
same[trace] = true
198+
if v, ok := d.GetOk("table"); ok {
199+
for _, item := range v.(*schema.Set).List() {
200+
dMap := item.(map[string]interface{})
201+
privilege := cdb.TablePrivilege{}
202+
if v, ok := dMap["database_name"]; ok {
203+
privilege.Database = helper.String(v.(string))
254204
}
255-
256-
p := &cdb.TablePrivilege{
257-
Database: sp(vmap["database_name"]),
258-
Table: sp(vmap["table_name"]),
259-
Privileges: []*string{},
205+
if v, ok := dMap["table_name"]; ok {
206+
privilege.Table = helper.String(v.(string))
260207
}
261-
for _, privilege := range vmap["privileges"].(*schema.Set).List() {
262-
ptr := sp(privilege)
263-
if !tccommon.IsContains(MYSQL_TABLE_PRIVILEGE, *ptr) {
264-
return errors.New("table privileges not support:" + *ptr)
208+
if v, ok := dMap["privileges"]; ok {
209+
privilegeList := []*string{}
210+
for _, v := range v.(*schema.Set).List() {
211+
privilegeList = append(privilegeList, helper.String(v.(string)))
265212
}
266-
p.Privileges = append(p.Privileges, ptr)
213+
privilege.Privileges = privilegeList
267214
}
268-
request.TablePrivileges = append(request.TablePrivileges, p)
215+
request.TablePrivileges = append(request.TablePrivileges, &privilege)
269216
}
270217
}
271218

272-
sliceInterface = d.Get("column").(*schema.Set).List()
273-
if len(sliceInterface) > 0 {
274-
request.ColumnPrivileges = make([]*cdb.ColumnPrivilege, 0, len(sliceInterface))
275-
for _, v := range sliceInterface {
276-
vmap := v.(map[string]interface{})
277-
278-
trace := *sp(vmap["database_name"]) + "." + *sp(vmap["table_name"]) + "." + *sp(vmap["column_name"])
279-
if same[trace] {
280-
return errors.New("can not assign two permissions to a column and an account," + trace)
281-
} else {
282-
same[trace] = true
219+
if v, ok := d.GetOk("column"); ok {
220+
for _, item := range v.(*schema.Set).List() {
221+
dMap := item.(map[string]interface{})
222+
privilege := cdb.ColumnPrivilege{}
223+
if v, ok := dMap["database_name"]; ok {
224+
privilege.Database = helper.String(v.(string))
283225
}
284-
285-
p := &cdb.ColumnPrivilege{
286-
Database: sp(vmap["database_name"]),
287-
Table: sp(vmap["table_name"]),
288-
Column: sp(vmap["column_name"]),
289-
Privileges: []*string{},
226+
if v, ok := dMap["table_name"]; ok {
227+
privilege.Table = helper.String(v.(string))
228+
}
229+
if v, ok := dMap["column_name"]; ok {
230+
privilege.Column = helper.String(v.(string))
290231
}
291-
for _, privilege := range vmap["privileges"].(*schema.Set).List() {
292-
ptr := sp(privilege)
293-
if !tccommon.IsContains(MYSQL_COLUMN_PRIVILEGE, *ptr) {
294-
return errors.New("column privileges not support:" + *ptr)
232+
if v, ok := dMap["privileges"]; ok {
233+
privilegeList := []*string{}
234+
for _, v := range v.(*schema.Set).List() {
235+
privilegeList = append(privilegeList, helper.String(v.(string)))
295236
}
296-
p.Privileges = append(p.Privileges, ptr)
237+
privilege.Privileges = privilegeList
297238
}
298-
request.ColumnPrivileges = append(request.ColumnPrivileges, p)
239+
request.ColumnPrivileges = append(request.ColumnPrivileges, &privilege)
299240
}
300241
}
301242
}

tencentcloud/services/cdb/resource_tc_mysql_privilege_test.go

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,19 @@ func TestAccTencentCloudMysqlPrivilegeResource(t *testing.T) {
2828
CheckDestroy: testAccMysqlPrivilegeDestroy,
2929
Steps: []resource.TestStep{
3030
{
31-
Config: testAccMysqlPrivilege(),
31+
Config: testAccMysqlPrivilege,
3232
Check: resource.ComposeAggregateTestCheckFunc(
3333
testAccMysqlPrivilegeExists,
3434
resource.TestCheckResourceAttrSet(testAccTencentCloudMysqlPrivilegeName, "mysql_id"),
3535
resource.TestCheckResourceAttrSet(testAccTencentCloudMysqlPrivilegeName, "account_name"),
3636
resource.TestCheckResourceAttr(testAccTencentCloudMysqlPrivilegeName, "global.#", "1"),
3737
resource.TestCheckResourceAttr(testAccTencentCloudMysqlPrivilegeName, "table.#", "1"),
38-
resource.TestCheckResourceAttr(testAccTencentCloudMysqlPrivilegeName, "column.#", "1"),
38+
resource.TestCheckResourceAttr(testAccTencentCloudMysqlPrivilegeName, "column.#", "2"),
3939
resource.TestCheckTypeSetElemAttr(testAccTencentCloudMysqlPrivilegeName, "global.*", "TRIGGER"),
4040
),
4141
},
4242
{
43-
Config: testAccMysqlPrivilegeUpdate(),
43+
Config: testAccMysqlPrivilegeUpdate,
4444
Check: resource.ComposeAggregateTestCheckFunc(
4545
testAccMysqlPrivilegeExists,
4646
resource.TestCheckResourceAttrSet(testAccTencentCloudMysqlPrivilegeName, "mysql_id"),
@@ -183,19 +183,17 @@ func testAccMysqlPrivilegeDestroy(s *terraform.State) error {
183183
return nil
184184
}
185185

186-
func testAccMysqlPrivilege() string {
187-
return fmt.Sprintf(`
188-
%s
186+
const testAccMysqlPrivilege = testAccMysql + `
189187
resource "tencentcloud_mysql_account" "mysql_account" {
190-
mysql_id = local.mysql_id
188+
mysql_id = tencentcloud_mysql_instance.mysql.id
191189
name = "test11priv"
192190
host = "119.168.110.%%"
193191
password = "test1234"
194192
description = "test from terraform"
195193
}
196194
197195
resource "tencentcloud_mysql_privilege" "privilege" {
198-
mysql_id = local.mysql_id
196+
mysql_id = tencentcloud_mysql_instance.mysql.id
199197
account_name = tencentcloud_mysql_account.mysql_account.name
200198
account_host = tencentcloud_mysql_account.mysql_account.host
201199
global = ["TRIGGER"]
@@ -214,22 +212,26 @@ resource "tencentcloud_mysql_privilege" "privilege" {
214212
table_name = "user"
215213
column_name = "host"
216214
}
217-
}`, tcacctest.CommonPresetMysql)
218-
}
219215
220-
func testAccMysqlPrivilegeUpdate() string {
221-
return fmt.Sprintf(`
222-
%s
216+
column {
217+
privileges = ["SELECT"]
218+
database_name = "mysql"
219+
table_name = "user"
220+
column_name = "user"
221+
}
222+
}`
223+
224+
const testAccMysqlPrivilegeUpdate = testAccMysql + `
223225
resource "tencentcloud_mysql_account" "mysql_account" {
224-
mysql_id = local.mysql_id
226+
mysql_id = tencentcloud_mysql_instance.mysql.id
225227
name = "test11priv"
226228
host = "119.168.110.%%"
227229
password = "test1234"
228230
description = "test from terraform"
229231
}
230232
231233
resource "tencentcloud_mysql_privilege" "privilege" {
232-
mysql_id = local.mysql_id
234+
mysql_id = tencentcloud_mysql_instance.mysql.id
233235
account_name = tencentcloud_mysql_account.mysql_account.name
234236
account_host = tencentcloud_mysql_account.mysql_account.host
235237
global = ["TRIGGER","SELECT"]
@@ -243,5 +245,4 @@ resource "tencentcloud_mysql_privilege" "privilege" {
243245
database_name = "mysql"
244246
table_name = "db"
245247
}
246-
}`, tcacctest.CommonPresetMysql)
247-
}
248+
}`

0 commit comments

Comments
 (0)