Make roles optional (#432)

* make roles optional

* check if roles is unknown

* add descriptions

* update docs

* change role condition handling

* Update stackit/internal/services/sqlserverflex/user/resource.go

Co-authored-by: João Palet <joao.palet@outlook.com>

* Update stackit/internal/services/sqlserverflex/user/datasource.go

Co-authored-by: João Palet <joao.palet@outlook.com>

* Update stackit/internal/services/sqlserverflex/user/datasource.go

Co-authored-by: João Palet <joao.palet@outlook.com>

* fix escapes

* adapt unit tests

* update docs

* Update stackit/internal/services/sqlserverflex/user/datasource.go

Co-authored-by: João Palet <joao.palet@outlook.com>

* Update stackit/internal/services/sqlserverflex/user/resource.go

Co-authored-by: João Palet <joao.palet@outlook.com>

* adapt unit tests

* update docs

---------

Co-authored-by: João Palet <joao.palet@outlook.com>
This commit is contained in:
GokceGK 2024-06-26 10:30:12 +02:00 committed by GitHub
parent 9f82c3262b
commit 7fbb13c0b6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 32 additions and 16 deletions

View file

@ -34,5 +34,5 @@ data "stackit_sqlserverflex_user" "example" {
- `host` (String)
- `id` (String) Terraform's internal data source. ID. It is structured as "`project_id`,`instance_id`,`user_id`".
- `port` (Number)
- `roles` (Set of String)
- `username` (String)
- `roles` (Set of String) Database access levels for the user. Possible values: [`##STACKIT_LoginManager##`, `##STACKIT_DatabaseManager##`]
- `username` (String) Username of the SQLServer Flex instance.

View file

@ -28,16 +28,16 @@ resource "stackit_sqlserverflex_user" "example" {
- `instance_id` (String) ID of the SQLServer Flex instance.
- `project_id` (String) STACKIT project ID to which the instance is associated.
- `username` (String)
- `username` (String) Username of the SQLServer Flex instance.
### Optional
- `roles` (Set of String)
- `roles` (Set of String) Database access levels for the user. Possible values: [`##STACKIT_LoginManager##`, `##STACKIT_DatabaseManager##`]
### Read-Only
- `host` (String)
- `id` (String) Terraform's internal resource ID. It is structured as "`project_id`,`instance_id`,`user_id`".
- `password` (String, Sensitive)
- `password` (String, Sensitive) Password of the user account.
- `port` (Number)
- `user_id` (String) User ID.

View file

@ -95,6 +95,9 @@ func (r *userDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, r
"user_id": "User ID.",
"instance_id": "ID of the SQLServer Flex instance.",
"project_id": "STACKIT project ID to which the instance is associated.",
"username": "Username of the SQLServer Flex instance.",
"roles": "Database access levels for the user. Possible values: [`##STACKIT_LoginManager##`, `##STACKIT_DatabaseManager##`]",
"password": "Password of the user account.",
}
resp.Schema = schema.Schema{
@ -128,9 +131,11 @@ func (r *userDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, r
},
},
"username": schema.StringAttribute{
Computed: true,
Description: descriptions["username"],
Computed: true,
},
"roles": schema.SetAttribute{
Description: descriptions["roles"],
ElementType: types.StringType,
Computed: true,
},

View file

@ -105,6 +105,9 @@ func (r *userResource) Schema(_ context.Context, _ resource.SchemaRequest, resp
"user_id": "User ID.",
"instance_id": "ID of the SQLServer Flex instance.",
"project_id": "STACKIT project ID to which the instance is associated.",
"username": "Username of the SQLServer Flex instance.",
"roles": "Database access levels for the user. Possible values: [`##STACKIT_LoginManager##`, `##STACKIT_DatabaseManager##`]",
"password": "Password of the user account.",
}
resp.Schema = schema.Schema{
@ -152,16 +155,17 @@ func (r *userResource) Schema(_ context.Context, _ resource.SchemaRequest, resp
},
},
"username": schema.StringAttribute{
Required: true,
Description: descriptions["username"],
Required: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
stringplanmodifier.UseStateForUnknown(),
},
},
"roles": schema.SetAttribute{
Description: descriptions["roles"],
ElementType: types.StringType,
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.Set{
setplanmodifier.RequiresReplace(),
},
@ -172,8 +176,9 @@ func (r *userResource) Schema(_ context.Context, _ resource.SchemaRequest, resp
},
},
"password": schema.StringAttribute{
Computed: true,
Sensitive: true,
Description: descriptions["password"],
Computed: true,
Sensitive: true,
},
"host": schema.StringAttribute{
Computed: true,
@ -366,9 +371,7 @@ func mapFieldsCreate(userResp *sqlserverflex.CreateUserResponse, model *Model) e
}
model.Password = types.StringValue(*user.Password)
if user.Roles == nil {
model.Roles = types.SetNull(types.StringType)
} else {
if user.Roles != nil {
roles := []attr.Value{}
for _, role := range *user.Roles {
roles = append(roles, types.StringValue(role))
@ -379,6 +382,11 @@ func mapFieldsCreate(userResp *sqlserverflex.CreateUserResponse, model *Model) e
}
model.Roles = rolesSet
}
if model.Roles.IsNull() || model.Roles.IsUnknown() {
model.Roles = types.SetNull(types.StringType)
}
model.Host = types.StringPointerValue(user.Host)
model.Port = types.Int64PointerValue(user.Port)
return nil
@ -412,9 +420,7 @@ func mapFields(userResp *sqlserverflex.GetUserResponse, model *Model) error {
model.UserId = types.StringValue(userId)
model.Username = types.StringPointerValue(user.Username)
if user.Roles == nil {
model.Roles = types.SetNull(types.StringType)
} else {
if user.Roles != nil {
roles := []attr.Value{}
for _, role := range *user.Roles {
roles = append(roles, types.StringValue(role))
@ -425,6 +431,11 @@ func mapFields(userResp *sqlserverflex.GetUserResponse, model *Model) error {
}
model.Roles = rolesSet
}
if model.Roles.IsNull() || model.Roles.IsUnknown() {
model.Roles = types.SetNull(types.StringType)
}
model.Host = types.StringPointerValue(user.Host)
model.Port = types.Int64PointerValue(user.Port)
return nil