Skip to content

fix(cdb): [117252423]Fix the problem of multiple values ​​in fields d… #2619

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/2619.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/tencentcloud_mysql_privilege: Fix the problem of multiple values ​​in fields database, table, column
```
37 changes: 37 additions & 0 deletions tencentcloud/services/cdb/resource_tc_mysql_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -655,3 +655,40 @@ resource "tencentcloud_mysql_instance" "mysql8" {
}
}`, value)
}

const testAccMysql = `
resource "tencentcloud_mysql_instance" "mysql" {
auto_renew_flag = 0
availability_zone = "ap-guangzhou-6"
charge_type = "POSTPAID"
cpu = 4
device_type = "UNIVERSAL"
engine_version = "8.0"
first_slave_zone = "ap-guangzhou-7"
force_delete = false
instance_name = "tf-test"
internet_service = 0
mem_size = 8000
root_password = "password123"
prepaid_period = 1
project_id = 0
security_groups = [
"sg-05f7wnhn",
]
slave_deploy_mode = 1
slave_sync_mode = 0
subnet_id = "subnet-j10lsueq"
tags = {}
volume_size = 100
vpc_id = "vpc-m0d2dbnn"
# wait_switch = 1

parameters = {
character_set_server = "utf8"
lower_case_table_names = "0"
max_connections = "1000"
max_user_connections = 2
long_query_time = "0.200000"
}
}
`
163 changes: 52 additions & 111 deletions tencentcloud/services/cdb/resource_tc_mysql_privilege.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,6 @@ type ResourceTencentCloudMysqlPrivilegeId struct {
AccountHost string `json:"AccountHost,omitempty"`
}

func resourceTencentCloudMysqlPrivilegeHash(v interface{}) int {
vmap := v.(map[string]interface{})
hashMap := map[string]interface{}{}
hashMap["database_name"] = vmap["database_name"]

if vmap["table_name"] != nil {
hashMap["table_name"] = vmap["table_name"]
}
if hashMap["column_name"] != nil {
hashMap["column_name"] = vmap["column_name"]
}
slice := []string{}
for _, v := range vmap["privileges"].(*schema.Set).List() {
slice = append(slice, v.(string))
}
hashMap["privileges"] = slice
b, _ := json.Marshal(hashMap)
return helper.HashString(string(b))
}

func ResourceTencentCloudMysqlPrivilege() *schema.Resource {
return &schema.Resource{
Create: resourceTencentCloudMysqlPrivilegeCreate,
Expand Down Expand Up @@ -94,7 +74,6 @@ func ResourceTencentCloudMysqlPrivilege() *schema.Resource {
Type: schema.TypeSet,
Optional: true,
Description: "Database privileges list.",
Set: resourceTencentCloudMysqlPrivilegeHash,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"database_name": {
Expand All @@ -103,12 +82,9 @@ func ResourceTencentCloudMysqlPrivilege() *schema.Resource {
Description: "Database name.",
},
"privileges": {
Type: schema.TypeSet,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: func(v interface{}) int {
return helper.HashString(v.(string))
},
Type: schema.TypeSet,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: `Database privilege.available values for Privileges:` + strings.Join(MYSQL_DATABASE_PRIVILEGE, ",") + ".",
},
},
Expand All @@ -118,7 +94,6 @@ func ResourceTencentCloudMysqlPrivilege() *schema.Resource {
Type: schema.TypeSet,
Optional: true,
Description: "Table privileges list.",
Set: resourceTencentCloudMysqlPrivilegeHash,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"database_name": {
Expand All @@ -132,12 +107,9 @@ func ResourceTencentCloudMysqlPrivilege() *schema.Resource {
Description: "Table name.",
},
"privileges": {
Type: schema.TypeSet,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: func(v interface{}) int {
return helper.HashString(v.(string))
},
Type: schema.TypeSet,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: `Table privilege.available values for Privileges:` + strings.Join(MYSQL_TABLE_PRIVILEGE, ",") + ".",
},
},
Expand All @@ -147,7 +119,6 @@ func ResourceTencentCloudMysqlPrivilege() *schema.Resource {
Type: schema.TypeSet,
Optional: true,
Description: "Column privileges list.",
Set: resourceTencentCloudMysqlPrivilegeHash,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"database_name": {
Expand All @@ -166,12 +137,9 @@ func ResourceTencentCloudMysqlPrivilege() *schema.Resource {
Description: "Column name.",
},
"privileges": {
Type: schema.TypeSet,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: func(v interface{}) int {
return helper.HashString(v.(string))
},
Type: schema.TypeSet,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: `Column privilege.available values for Privileges:` + strings.Join(MYSQL_COLUMN_PRIVILEGE, ",") + ".",
},
},
Expand Down Expand Up @@ -209,93 +177,66 @@ func (me *ResourceTencentCloudMysqlPrivilegeId) update(ctx context.Context, d *s
}
}

same := map[string]bool{}

sliceInterface = d.Get("database").(*schema.Set).List()
if len(sliceInterface) > 0 {
request.DatabasePrivileges = make([]*cdb.DatabasePrivilege, 0, len(sliceInterface))
for _, v := range sliceInterface {
vmap := v.(map[string]interface{})

trace := *sp(vmap["database_name"])
if same[trace] {
return errors.New("can not assign two permissions to a database and an account," + trace)
} else {
same[trace] = true
if v, ok := d.GetOk("database"); ok {
for _, item := range v.(*schema.Set).List() {
dMap := item.(map[string]interface{})
privilege := cdb.DatabasePrivilege{}
if v, ok := dMap["database_name"]; ok {
privilege.Database = helper.String(v.(string))
}

p := &cdb.DatabasePrivilege{
Database: sp(vmap["database_name"]),
Privileges: []*string{},
}

for _, privilege := range vmap["privileges"].(*schema.Set).List() {
ptr := sp(privilege)
if !tccommon.IsContains(MYSQL_DATABASE_PRIVILEGE, *ptr) {
return errors.New("database privileges not support:" + *ptr)
if v, ok := dMap["privileges"]; ok {
privilegeList := []*string{}
for _, v := range v.(*schema.Set).List() {
privilegeList = append(privilegeList, helper.String(v.(string)))
}
p.Privileges = append(p.Privileges, ptr)
privilege.Privileges = privilegeList
}
request.DatabasePrivileges = append(request.DatabasePrivileges, p)
request.DatabasePrivileges = append(request.DatabasePrivileges, &privilege)
}
}

sliceInterface = d.Get("table").(*schema.Set).List()
if len(sliceInterface) > 0 {
request.TablePrivileges = make([]*cdb.TablePrivilege, 0, len(sliceInterface))
for _, v := range sliceInterface {
vmap := v.(map[string]interface{})

trace := *sp(vmap["database_name"]) + "." + *sp(vmap["table_name"])
if same[trace] {
return errors.New("can not assign two permissions to a table and an account," + trace)
} else {
same[trace] = true
if v, ok := d.GetOk("table"); ok {
for _, item := range v.(*schema.Set).List() {
dMap := item.(map[string]interface{})
privilege := cdb.TablePrivilege{}
if v, ok := dMap["database_name"]; ok {
privilege.Database = helper.String(v.(string))
}

p := &cdb.TablePrivilege{
Database: sp(vmap["database_name"]),
Table: sp(vmap["table_name"]),
Privileges: []*string{},
if v, ok := dMap["table_name"]; ok {
privilege.Table = helper.String(v.(string))
}
for _, privilege := range vmap["privileges"].(*schema.Set).List() {
ptr := sp(privilege)
if !tccommon.IsContains(MYSQL_TABLE_PRIVILEGE, *ptr) {
return errors.New("table privileges not support:" + *ptr)
if v, ok := dMap["privileges"]; ok {
privilegeList := []*string{}
for _, v := range v.(*schema.Set).List() {
privilegeList = append(privilegeList, helper.String(v.(string)))
}
p.Privileges = append(p.Privileges, ptr)
privilege.Privileges = privilegeList
}
request.TablePrivileges = append(request.TablePrivileges, p)
request.TablePrivileges = append(request.TablePrivileges, &privilege)
}
}

sliceInterface = d.Get("column").(*schema.Set).List()
if len(sliceInterface) > 0 {
request.ColumnPrivileges = make([]*cdb.ColumnPrivilege, 0, len(sliceInterface))
for _, v := range sliceInterface {
vmap := v.(map[string]interface{})

trace := *sp(vmap["database_name"]) + "." + *sp(vmap["table_name"]) + "." + *sp(vmap["column_name"])
if same[trace] {
return errors.New("can not assign two permissions to a column and an account," + trace)
} else {
same[trace] = true
if v, ok := d.GetOk("column"); ok {
for _, item := range v.(*schema.Set).List() {
dMap := item.(map[string]interface{})
privilege := cdb.ColumnPrivilege{}
if v, ok := dMap["database_name"]; ok {
privilege.Database = helper.String(v.(string))
}

p := &cdb.ColumnPrivilege{
Database: sp(vmap["database_name"]),
Table: sp(vmap["table_name"]),
Column: sp(vmap["column_name"]),
Privileges: []*string{},
if v, ok := dMap["table_name"]; ok {
privilege.Table = helper.String(v.(string))
}
if v, ok := dMap["column_name"]; ok {
privilege.Column = helper.String(v.(string))
}
for _, privilege := range vmap["privileges"].(*schema.Set).List() {
ptr := sp(privilege)
if !tccommon.IsContains(MYSQL_COLUMN_PRIVILEGE, *ptr) {
return errors.New("column privileges not support:" + *ptr)
if v, ok := dMap["privileges"]; ok {
privilegeList := []*string{}
for _, v := range v.(*schema.Set).List() {
privilegeList = append(privilegeList, helper.String(v.(string)))
}
p.Privileges = append(p.Privileges, ptr)
privilege.Privileges = privilegeList
}
request.ColumnPrivileges = append(request.ColumnPrivileges, p)
request.ColumnPrivileges = append(request.ColumnPrivileges, &privilege)
}
}
}
Expand Down
35 changes: 18 additions & 17 deletions tencentcloud/services/cdb/resource_tc_mysql_privilege_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@ func TestAccTencentCloudMysqlPrivilegeResource(t *testing.T) {
CheckDestroy: testAccMysqlPrivilegeDestroy,
Steps: []resource.TestStep{
{
Config: testAccMysqlPrivilege(),
Config: testAccMysqlPrivilege,
Check: resource.ComposeAggregateTestCheckFunc(
testAccMysqlPrivilegeExists,
resource.TestCheckResourceAttrSet(testAccTencentCloudMysqlPrivilegeName, "mysql_id"),
resource.TestCheckResourceAttrSet(testAccTencentCloudMysqlPrivilegeName, "account_name"),
resource.TestCheckResourceAttr(testAccTencentCloudMysqlPrivilegeName, "global.#", "1"),
resource.TestCheckResourceAttr(testAccTencentCloudMysqlPrivilegeName, "table.#", "1"),
resource.TestCheckResourceAttr(testAccTencentCloudMysqlPrivilegeName, "column.#", "1"),
resource.TestCheckResourceAttr(testAccTencentCloudMysqlPrivilegeName, "column.#", "2"),
resource.TestCheckTypeSetElemAttr(testAccTencentCloudMysqlPrivilegeName, "global.*", "TRIGGER"),
),
},
{
Config: testAccMysqlPrivilegeUpdate(),
Config: testAccMysqlPrivilegeUpdate,
Check: resource.ComposeAggregateTestCheckFunc(
testAccMysqlPrivilegeExists,
resource.TestCheckResourceAttrSet(testAccTencentCloudMysqlPrivilegeName, "mysql_id"),
Expand Down Expand Up @@ -183,19 +183,17 @@ func testAccMysqlPrivilegeDestroy(s *terraform.State) error {
return nil
}

func testAccMysqlPrivilege() string {
return fmt.Sprintf(`
%s
const testAccMysqlPrivilege = testAccMysql + `
resource "tencentcloud_mysql_account" "mysql_account" {
mysql_id = local.mysql_id
mysql_id = tencentcloud_mysql_instance.mysql.id
name = "test11priv"
host = "119.168.110.%%"
password = "test1234"
description = "test from terraform"
}

resource "tencentcloud_mysql_privilege" "privilege" {
mysql_id = local.mysql_id
mysql_id = tencentcloud_mysql_instance.mysql.id
account_name = tencentcloud_mysql_account.mysql_account.name
account_host = tencentcloud_mysql_account.mysql_account.host
global = ["TRIGGER"]
Expand All @@ -214,22 +212,26 @@ resource "tencentcloud_mysql_privilege" "privilege" {
table_name = "user"
column_name = "host"
}
}`, tcacctest.CommonPresetMysql)
}

func testAccMysqlPrivilegeUpdate() string {
return fmt.Sprintf(`
%s
column {
privileges = ["SELECT"]
database_name = "mysql"
table_name = "user"
column_name = "user"
}
}`

const testAccMysqlPrivilegeUpdate = testAccMysql + `
resource "tencentcloud_mysql_account" "mysql_account" {
mysql_id = local.mysql_id
mysql_id = tencentcloud_mysql_instance.mysql.id
name = "test11priv"
host = "119.168.110.%%"
password = "test1234"
description = "test from terraform"
}

resource "tencentcloud_mysql_privilege" "privilege" {
mysql_id = local.mysql_id
mysql_id = tencentcloud_mysql_instance.mysql.id
account_name = tencentcloud_mysql_account.mysql_account.name
account_host = tencentcloud_mysql_account.mysql_account.host
global = ["TRIGGER","SELECT"]
Expand All @@ -243,5 +245,4 @@ resource "tencentcloud_mysql_privilege" "privilege" {
database_name = "mysql"
table_name = "db"
}
}`, tcacctest.CommonPresetMysql)
}
}`
Loading