150 lines
4.8 KiB
Go
150 lines
4.8 KiB
Go
package postgresflexalpha
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
|
|
"github.com/hashicorp/terraform-plugin-framework/attr"
|
|
"github.com/hashicorp/terraform-plugin-framework/types"
|
|
postgresflex "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/postgresflexalpha"
|
|
"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 API response to data source model, preserving existing ID.
|
|
func mapDataSourceFields(userResp *postgresflex.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
|
|
|
|
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")
|
|
}
|
|
|
|
model.TerraformID = utils.BuildInternalTerraformId(
|
|
model.ProjectId.ValueString(), region, model.InstanceId.ValueString(), strconv.FormatInt(userId, 10),
|
|
)
|
|
|
|
model.UserId = types.Int64Value(userId)
|
|
model.Name = types.StringValue(user.GetName())
|
|
|
|
if user.Roles == nil {
|
|
model.Roles = types.List(types.SetNull(types.StringType))
|
|
} else {
|
|
var roles []attr.Value
|
|
for _, role := range *user.Roles {
|
|
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)
|
|
}
|
|
|
|
model.Id = types.Int64Value(userId)
|
|
model.Host = types.StringValue(user.GetHost())
|
|
model.Port = types.Int64Value(user.GetPort())
|
|
model.Region = types.StringValue(region)
|
|
model.Status = types.StringValue(user.GetStatus())
|
|
model.ConnectionString = types.StringValue(user.GetConnectionString())
|
|
return nil
|
|
}
|
|
|
|
// toPayloadRoles converts a string slice to the API's role type.
|
|
func toPayloadRoles(roles *[]string) *[]postgresflex.UserRole {
|
|
var userRoles = make([]postgresflex.UserRole, 0, len(*roles))
|
|
for _, role := range *roles {
|
|
userRoles = append(userRoles, postgresflex.UserRole(role))
|
|
}
|
|
return &userRoles
|
|
}
|
|
|
|
// toUpdatePayload creates an API update payload from the resource model.
|
|
func toUpdatePayload(model *Model, roles *[]string) (
|
|
*postgresflex.UpdateUserRequestPayload,
|
|
error,
|
|
) {
|
|
if model == nil {
|
|
return nil, fmt.Errorf("nil model")
|
|
}
|
|
if roles == nil {
|
|
return nil, fmt.Errorf("nil roles")
|
|
}
|
|
|
|
return &postgresflex.UpdateUserRequestPayload{
|
|
Name: conversion.StringValueToPointer(model.Name),
|
|
Roles: toPayloadRoles(roles),
|
|
}, nil
|
|
}
|
|
|
|
// toCreatePayload creates an API create payload from the resource model.
|
|
func toCreatePayload(model *Model, roles *[]string) (*postgresflex.CreateUserRequestPayload, error) {
|
|
if model == nil {
|
|
return nil, fmt.Errorf("nil model")
|
|
}
|
|
if roles == nil {
|
|
return nil, fmt.Errorf("nil roles")
|
|
}
|
|
|
|
return &postgresflex.CreateUserRequestPayload{
|
|
Roles: toPayloadRoles(roles),
|
|
Name: conversion.StringValueToPointer(model.Name),
|
|
}, nil
|
|
}
|
|
|
|
// mapResourceFields maps API response to the resource model, preserving existing ID.
|
|
func mapResourceFields(userResp *postgresflex.GetUserResponse, model *Model, region string) error {
|
|
if userResp == nil {
|
|
return fmt.Errorf("response is nil")
|
|
}
|
|
if model == nil {
|
|
return fmt.Errorf("model input is nil")
|
|
}
|
|
user := userResp
|
|
|
|
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")
|
|
}
|
|
model.TerraformID = utils.BuildInternalTerraformId(
|
|
model.ProjectId.ValueString(), region, model.InstanceId.ValueString(), strconv.FormatInt(userId, 10),
|
|
)
|
|
model.Id = types.Int64Value(userId)
|
|
model.UserId = types.Int64Value(userId)
|
|
model.Name = types.StringPointerValue(user.Name)
|
|
|
|
if user.Roles == nil {
|
|
model.Roles = types.List(types.SetNull(types.StringType))
|
|
} else {
|
|
var roles []attr.Value
|
|
for _, role := range *user.Roles {
|
|
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)
|
|
}
|
|
model.Host = types.StringPointerValue(user.Host)
|
|
model.Port = types.Int64PointerValue(user.Port)
|
|
model.Region = types.StringValue(region)
|
|
model.Status = types.StringPointerValue(user.Status)
|
|
model.ConnectionString = types.StringPointerValue(user.ConnectionString)
|
|
return nil
|
|
}
|