Skip to content

Commit

Permalink
fix(mysql_user): Fixes update and import errors and some refactoring (#…
Browse files Browse the repository at this point in the history
…476)

* docs(mysql_users): Modify the list of arguments and attributes

* refactor: Change to a reusable function

* fix(mysql_users): Fix update and import errors

* fix(mysql_users): Add the is_system_table_access attribute to the datasource
  • Loading branch information
youngmn authored Nov 6, 2024
1 parent 13e9e9d commit 2de2445
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 49 deletions.
4 changes: 2 additions & 2 deletions docs/data-sources/mysql_users.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ This data source exports the following attributes in addition to the argument ab

* `mysql_user_list` - The list of users to add .
* `name` - MySQL User ID.
* `password` - MySQL User Password.
* `host_ip` - MySQL user host.
* `authority` - MySQL User Authority.
* `authority` - MySQL User Authority.
* `is_system_table_access` - MySQL User system table accessibility.
5 changes: 3 additions & 2 deletions docs/resources/mysql_users.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,12 @@ resource "ncloud_mysql_users" "mysql_users" {
The following arguments are supported:

* `mysql_instance_no` - (Required) The ID of the associated Mysql Instance.
* `mysql_user_list` - The list of users to add .
* `mysql_user_list` - The list of users to add.
* `name` - (Required) MySQL User ID. Only English alphabets, numbers and special characters ( \ _ , - ) are allowed and must start with an English alphabet. Min: 4, Max: 16
* `password` - (Required) MySQL User Password. At least one English alphabet, number and special character must be included. Certain special characters ( ` & + \ " ' / space ) cannot be used. Min: 8, Max: 20
* `host_ip` - (Required) MySQL user host. ex) Overall connection permitted: %, Connection by specific IPs permitted: 1.1.1.1, IP band connection permitted: 1.1.1.%
* `authority` - (Required) MySQL User Authority. You can select `READ|CRUD|DDL`.
* `is_system_table_access` - (Optional) Enable system table accessibility. Default: `true`. Options: `true`| `false`

## Attribute Reference
In addition to all arguments above, the following attributes are exported
Expand All @@ -86,4 +87,4 @@ import {
to = ncloud_mysql_users.rsc_name
id = "12345"
}
```
```
12 changes: 12 additions & 0 deletions internal/common/structures.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,15 @@ func expandSourceBuildEnvVarsParams(eVars []interface{}) ([]*sourcebuild.Project

return envVars, nil
}

func ReverseList[T any](list []T) []T {
if len(list) <= 1 {
return list
}

for i, j := 0, len(list)-1; i < j; i, j = i+1, j-1 {
list[i], list[j] = list[j], list[i]
}

return list
}
14 changes: 1 addition & 13 deletions internal/service/mongodb/mongodb_users_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func GetMongoDbUserAllList(ctx context.Context, config *conn.ProviderConfig, id

tflog.Info(ctx, "GetMongodbUserList response="+common.MarshalUncheckedString(resp))

return reverse(resp.CloudMongoDbUserList), nil
return common.ReverseList(resp.CloudMongoDbUserList), nil
}

type mongodbUsersDataSourceModel struct {
Expand Down Expand Up @@ -248,15 +248,3 @@ func (d *mongodbUser) refreshFromOutput(output *vmongodb.CloudMongoDbUser) {
d.DatabaseName = types.StringPointerValue(output.DatabaseName)
d.Authority = types.StringPointerValue(output.Authority)
}

func reverse(users []*vmongodb.CloudMongoDbUser) []*vmongodb.CloudMongoDbUser {
if len(users) <= 1 {
return users
}

for i, j := 0, len(users)-1; i < j; i, j = i+1, j-1 {
users[i], users[j] = users[j], users[i]
}

return users
}
8 changes: 4 additions & 4 deletions internal/service/mysql/mysql_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func (r *mysqlUsersResource) Read(ctx context.Context, req resource.ReadRequest,
return
}

output, err := GetMysqlUserList(ctx, r.config, state.MysqlInstanceNo.ValueString(), common.ConvertToStringList(state.MysqlUserList, "name"))
output, err := GetMysqlUserList(ctx, r.config, state.ID.ValueString(), common.ConvertToStringList(state.MysqlUserList, "name"))
if err != nil {
resp.Diagnostics.AddError("READING ERROR", err.Error())
return
Expand Down Expand Up @@ -273,7 +273,7 @@ func (r *mysqlUsersResource) Update(ctx context.Context, req resource.UpdateRequ
return
}

if diags := state.refreshFromOutput(ctx, output, state); diags.HasError() {
if diags := state.refreshFromOutput(ctx, output, plan); diags.HasError() {
resp.Diagnostics.AddError("READING ERROR", "refreshFromOutput error")
return
}
Expand All @@ -290,7 +290,7 @@ func (r *mysqlUsersResource) Delete(ctx context.Context, req resource.DeleteRequ
return
}

_, err := waitMysqlCreation(ctx, r.config, state.MysqlInstanceNo.ValueString())
_, err := waitMysqlCreation(ctx, r.config, state.ID.ValueString())
if err != nil {
resp.Diagnostics.AddError("WAITING FOR MYSQL CREATION ERROR", err.Error())
return
Expand Down Expand Up @@ -414,7 +414,7 @@ func (r *mysqlUsersResourceModel) refreshFromOutput(ctx context.Context, output

r.MysqlUserList = mysqlUsers

return nil
return diags
}

func convertToCloudMysqlUserParameter(values basetypes.ListValue) []*vmysql.CloudMysqlUserParameter {
Expand Down
48 changes: 20 additions & 28 deletions internal/service/mysql/mysql_users_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ func (d *mysqlUsersDataSource) Schema(ctx context.Context, req datasource.Schema
"authority": schema.StringAttribute{
Computed: true,
},
"is_system_table_access": schema.BoolAttribute{
Computed: true,
},
},
},
},
Expand Down Expand Up @@ -121,12 +124,10 @@ func (d *mysqlUsersDataSource) Read(ctx context.Context, req datasource.ReadRequ

if !data.ID.IsNull() && !data.ID.IsUnknown() {
mysqlId = data.ID.ValueString()
data.MysqlInstanceNo = data.ID
}

if !data.MysqlInstanceNo.IsNull() && !data.MysqlInstanceNo.IsUnknown() {
mysqlId = data.MysqlInstanceNo.ValueString()
data.ID = data.MysqlInstanceNo
}

output, err := GetMysqlUserAllList(ctx, d.config, mysqlId)
Expand All @@ -142,7 +143,7 @@ func (d *mysqlUsersDataSource) Read(ctx context.Context, req datasource.ReadRequ

mysqlUserList := flattenMysqlUsers(output)
fillteredList := common.FilterModels(ctx, data.Filters, mysqlUserList)
if diags := data.refreshFromOutput(ctx, fillteredList, data.MysqlInstanceNo.ValueString()); diags.HasError() {
if diags := data.refreshFromOutput(ctx, fillteredList, mysqlId); diags.HasError() {
resp.Diagnostics.AddError("READING ERROR", "refreshFromOutput error")
return
}
Expand Down Expand Up @@ -199,11 +200,10 @@ func GetMysqlUserAllList(ctx context.Context, config *conn.ProviderConfig, id st
return nil, nil
}

allUsers = reverseAndExcludeFirst(allUsers)

tflog.Info(ctx, "GetMysqlUserList response="+common.MarshalUncheckedString(allUsers))
reverseUsers := common.ReverseList(allUsers)
tflog.Info(ctx, "GetMysqlUserList response="+common.MarshalUncheckedString(reverseUsers))

return allUsers, nil
return reverseUsers, nil
}

type mysqlUsersDataSourceModel struct {
Expand All @@ -215,22 +215,25 @@ type mysqlUsersDataSourceModel struct {
}

type mysqlUser struct {
UserName types.String `tfsdk:"name"`
HostIp types.String `tfsdk:"host_ip"`
Authority types.String `tfsdk:"authority"`
UserName types.String `tfsdk:"name"`
HostIp types.String `tfsdk:"host_ip"`
Authority types.String `tfsdk:"authority"`
IsSystemTableAccess types.Bool `tfsdk:"is_system_table_access"`
}

type mysqlUserToJsonConvert struct {
UserName string `json:"name"`
HostIp string `json:"host_ip"`
Authority string `json:"authority"`
UserName string `json:"name"`
HostIp string `json:"host_ip"`
Authority string `json:"authority"`
IsSystemTableAccess bool `json:"is_system_table_access"`
}

func (d mysqlUser) attrTypes() map[string]attr.Type {
return map[string]attr.Type{
"name": types.StringType,
"host_ip": types.StringType,
"authority": types.StringType,
"name": types.StringType,
"host_ip": types.StringType,
"authority": types.StringType,
"is_system_table_access": types.BoolType,
}
}

Expand Down Expand Up @@ -275,16 +278,5 @@ func (d *mysqlUser) refreshFromOutput(output *vmysql.CloudMysqlUser) {
d.UserName = types.StringPointerValue(output.UserName)
d.HostIp = types.StringPointerValue(output.HostIp)
d.Authority = types.StringPointerValue(output.Authority)
}

func reverseAndExcludeFirst(users []*vmysql.CloudMysqlUser) []*vmysql.CloudMysqlUser {
if len(users) <= 1 {
return users
}

for i, j := 0, len(users)-1; i < j; i, j = i+1, j-1 {
users[i], users[j] = users[j], users[i]
}

return users[1:]
d.IsSystemTableAccess = types.BoolPointerValue(output.IsSystemTableAccess)
}

0 comments on commit 2de2445

Please sign in to comment.