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) - `host` (String)
- `id` (String) Terraform's internal data source. ID. It is structured as "`project_id`,`instance_id`,`user_id`". - `id` (String) Terraform's internal data source. ID. It is structured as "`project_id`,`instance_id`,`user_id`".
- `port` (Number) - `port` (Number)
- `roles` (Set of String) - `roles` (Set of String) Database access levels for the user. Possible values: [`##STACKIT_LoginManager##`, `##STACKIT_DatabaseManager##`]
- `username` (String) - `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. - `instance_id` (String) ID of the SQLServer Flex instance.
- `project_id` (String) STACKIT project ID to which the instance is associated. - `project_id` (String) STACKIT project ID to which the instance is associated.
- `username` (String) - `username` (String) Username of the SQLServer Flex instance.
### Optional ### Optional
- `roles` (Set of String) - `roles` (Set of String) Database access levels for the user. Possible values: [`##STACKIT_LoginManager##`, `##STACKIT_DatabaseManager##`]
### Read-Only ### Read-Only
- `host` (String) - `host` (String)
- `id` (String) Terraform's internal resource ID. It is structured as "`project_id`,`instance_id`,`user_id`". - `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) - `port` (Number)
- `user_id` (String) User ID. - `user_id` (String) User ID.

View file

@ -95,6 +95,9 @@ func (r *userDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, r
"user_id": "User ID.", "user_id": "User ID.",
"instance_id": "ID of the SQLServer Flex instance.", "instance_id": "ID of the SQLServer Flex instance.",
"project_id": "STACKIT project ID to which the instance is associated.", "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{ resp.Schema = schema.Schema{
@ -128,9 +131,11 @@ func (r *userDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, r
}, },
}, },
"username": schema.StringAttribute{ "username": schema.StringAttribute{
Computed: true, Description: descriptions["username"],
Computed: true,
}, },
"roles": schema.SetAttribute{ "roles": schema.SetAttribute{
Description: descriptions["roles"],
ElementType: types.StringType, ElementType: types.StringType,
Computed: true, Computed: true,
}, },

View file

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