fix: include recent api changes Reviewed-on: #65 Co-authored-by: Marcel S. Henselin <marcel.henselin@stackit.cloud> Co-committed-by: Marcel S. Henselin <marcel.henselin@stackit.cloud>
193 lines
5.7 KiB
Go
193 lines
5.7 KiB
Go
package sqlserverflexalpha
|
|
|
|
import (
|
|
"fmt"
|
|
"slices"
|
|
"strconv"
|
|
|
|
"github.com/hashicorp/terraform-plugin-framework/attr"
|
|
"github.com/hashicorp/terraform-plugin-framework/types"
|
|
|
|
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/sqlserverflexalpha"
|
|
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/conversion"
|
|
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/core"
|
|
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/utils"
|
|
)
|
|
|
|
// mapDataSourceFields maps the API response to a dataSourceModel.
|
|
func mapDataSourceFields(userResp *sqlserverflexalpha.GetUserResponse, model *dataSourceModel, region string) error {
|
|
if userResp == nil {
|
|
return fmt.Errorf("response is nil")
|
|
}
|
|
if model == nil {
|
|
return fmt.Errorf("model input is nil")
|
|
}
|
|
user := userResp
|
|
|
|
// Handle user ID
|
|
var userId int64
|
|
if model.UserId.ValueInt64() != 0 {
|
|
userId = model.UserId.ValueInt64()
|
|
} else if user.Id != nil {
|
|
userId = *user.Id
|
|
} else {
|
|
return fmt.Errorf("user id not present")
|
|
}
|
|
|
|
// Set main attributes
|
|
model.Id = utils.BuildInternalTerraformId(
|
|
model.ProjectId.ValueString(), region, model.InstanceId.ValueString(), strconv.FormatInt(userId, 10),
|
|
)
|
|
model.UserId = types.Int64Value(userId)
|
|
model.Username = types.StringPointerValue(user.Username)
|
|
|
|
// Map roles
|
|
if user.Roles == nil {
|
|
model.Roles = types.List(types.SetNull(types.StringType))
|
|
} else {
|
|
resRoles := *user.Roles
|
|
slices.Sort(resRoles)
|
|
|
|
var roles []attr.Value
|
|
for _, role := range resRoles {
|
|
roles = append(roles, types.StringValue(string(role)))
|
|
}
|
|
rolesSet, diags := types.SetValue(types.StringType, roles)
|
|
if diags.HasError() {
|
|
return fmt.Errorf("failed to map roles: %w", core.DiagsToError(diags))
|
|
}
|
|
model.Roles = types.List(rolesSet)
|
|
}
|
|
|
|
// Set remaining attributes
|
|
model.Host = types.StringPointerValue(user.Host)
|
|
model.Port = types.Int64PointerValue(user.Port)
|
|
model.Region = types.StringValue(region)
|
|
model.Status = types.StringPointerValue(user.Status)
|
|
model.DefaultDatabase = types.StringPointerValue(user.DefaultDatabase)
|
|
|
|
return nil
|
|
}
|
|
|
|
// mapFields maps the API response to a resourceModel.
|
|
func mapFields(userResp *sqlserverflexalpha.GetUserResponse, model *resourceModel, region string) error {
|
|
if userResp == nil {
|
|
return fmt.Errorf("response is nil")
|
|
}
|
|
if model == nil {
|
|
return fmt.Errorf("model input is nil")
|
|
}
|
|
user := userResp
|
|
|
|
// Handle user ID
|
|
var userId int64
|
|
if model.UserId.ValueInt64() != 0 {
|
|
userId = model.UserId.ValueInt64()
|
|
} else if user.Id != nil {
|
|
userId = *user.Id
|
|
} else {
|
|
return fmt.Errorf("user id not present")
|
|
}
|
|
|
|
// Set main attributes
|
|
model.Id = types.Int64Value(userId)
|
|
model.UserId = types.Int64Value(userId)
|
|
model.Username = types.StringPointerValue(user.Username)
|
|
|
|
// Map roles
|
|
if user.Roles != nil {
|
|
resRoles := *user.Roles
|
|
slices.Sort(resRoles)
|
|
|
|
var roles []attr.Value
|
|
for _, role := range resRoles {
|
|
roles = append(roles, types.StringValue(string(role)))
|
|
}
|
|
rolesSet, diags := types.SetValue(types.StringType, roles)
|
|
if diags.HasError() {
|
|
return fmt.Errorf("failed to map roles: %w", core.DiagsToError(diags))
|
|
}
|
|
model.Roles = types.List(rolesSet)
|
|
}
|
|
|
|
// Ensure roles is not null
|
|
if model.Roles.IsNull() || model.Roles.IsUnknown() {
|
|
model.Roles = types.List(types.SetNull(types.StringType))
|
|
}
|
|
|
|
// Set connection details
|
|
model.Host = types.StringPointerValue(user.Host)
|
|
model.Port = types.Int64PointerValue(user.Port)
|
|
model.Region = types.StringValue(region)
|
|
return nil
|
|
}
|
|
|
|
// mapFieldsCreate maps the API response from creating a user to a resourceModel.
|
|
func mapFieldsCreate(userResp *sqlserverflexalpha.CreateUserResponse, model *resourceModel, region string) error {
|
|
if userResp == nil {
|
|
return fmt.Errorf("response is nil")
|
|
}
|
|
if model == nil {
|
|
return fmt.Errorf("model input is nil")
|
|
}
|
|
user := userResp
|
|
|
|
if user.Id == nil {
|
|
return fmt.Errorf("user id not present")
|
|
}
|
|
userId := *user.Id
|
|
model.Id = types.Int64Value(userId)
|
|
model.UserId = types.Int64Value(userId)
|
|
model.Username = types.StringPointerValue(user.Username)
|
|
|
|
if user.Password == nil {
|
|
return fmt.Errorf("user password not present")
|
|
}
|
|
model.Password = types.StringValue(*user.Password)
|
|
|
|
if user.Roles != nil {
|
|
resRoles := *user.Roles
|
|
slices.Sort(resRoles)
|
|
|
|
var roles []attr.Value
|
|
for _, role := range resRoles {
|
|
roles = append(roles, types.StringValue(string(role)))
|
|
}
|
|
rolesSet, diags := types.SetValue(types.StringType, roles)
|
|
if diags.HasError() {
|
|
return fmt.Errorf("failed to map roles: %w", core.DiagsToError(diags))
|
|
}
|
|
model.Roles = types.List(rolesSet)
|
|
}
|
|
|
|
if model.Roles.IsNull() || model.Roles.IsUnknown() {
|
|
model.Roles = types.List(types.SetNull(types.StringType))
|
|
}
|
|
|
|
model.Password = types.StringPointerValue(user.Password)
|
|
model.Uri = types.StringPointerValue(user.Uri)
|
|
|
|
model.Host = types.StringPointerValue(user.Host)
|
|
model.Port = types.Int64PointerValue(user.Port)
|
|
model.Region = types.StringValue(region)
|
|
model.Status = types.StringPointerValue(user.Status)
|
|
model.DefaultDatabase = types.StringPointerValue(user.DefaultDatabase)
|
|
|
|
return nil
|
|
}
|
|
|
|
// toCreatePayload converts a resourceModel to an API CreateUserRequestPayload.
|
|
func toCreatePayload(
|
|
model *resourceModel,
|
|
roles []string,
|
|
) (*sqlserverflexalpha.CreateUserRequestPayload, error) {
|
|
if model == nil {
|
|
return nil, fmt.Errorf("nil model")
|
|
}
|
|
|
|
return &sqlserverflexalpha.CreateUserRequestPayload{
|
|
Username: conversion.StringValueToPointer(model.Username),
|
|
DefaultDatabase: conversion.StringValueToPointer(model.DefaultDatabase),
|
|
Roles: &roles,
|
|
}, nil
|
|
}
|