From 7fbb13c0b6f3603142618d449e91325da04b9394 Mon Sep 17 00:00:00 2001 From: GokceGK <161626272+GokceGK@users.noreply.github.com> Date: Wed, 26 Jun 2024 10:30:12 +0200 Subject: [PATCH] Make roles optional (#432) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 * Update stackit/internal/services/sqlserverflex/user/datasource.go Co-authored-by: João Palet * Update stackit/internal/services/sqlserverflex/user/datasource.go Co-authored-by: João Palet * fix escapes * adapt unit tests * update docs * Update stackit/internal/services/sqlserverflex/user/datasource.go Co-authored-by: João Palet * Update stackit/internal/services/sqlserverflex/user/resource.go Co-authored-by: João Palet * adapt unit tests * update docs --------- Co-authored-by: João Palet --- docs/data-sources/sqlserverflex_user.md | 4 +-- docs/resources/sqlserverflex_user.md | 6 ++-- .../services/sqlserverflex/user/datasource.go | 7 ++++- .../services/sqlserverflex/user/resource.go | 31 +++++++++++++------ 4 files changed, 32 insertions(+), 16 deletions(-) diff --git a/docs/data-sources/sqlserverflex_user.md b/docs/data-sources/sqlserverflex_user.md index edb5c47f..2b9d32a0 100644 --- a/docs/data-sources/sqlserverflex_user.md +++ b/docs/data-sources/sqlserverflex_user.md @@ -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. diff --git a/docs/resources/sqlserverflex_user.md b/docs/resources/sqlserverflex_user.md index c9da87ce..79b28166 100644 --- a/docs/resources/sqlserverflex_user.md +++ b/docs/resources/sqlserverflex_user.md @@ -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. diff --git a/stackit/internal/services/sqlserverflex/user/datasource.go b/stackit/internal/services/sqlserverflex/user/datasource.go index 0ef1f451..c23932ef 100644 --- a/stackit/internal/services/sqlserverflex/user/datasource.go +++ b/stackit/internal/services/sqlserverflex/user/datasource.go @@ -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, }, diff --git a/stackit/internal/services/sqlserverflex/user/resource.go b/stackit/internal/services/sqlserverflex/user/resource.go index 36a189da..c24e6d35 100644 --- a/stackit/internal/services/sqlserverflex/user/resource.go +++ b/stackit/internal/services/sqlserverflex/user/resource.go @@ -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