## Description
<!-- **Please link some issue here describing what you are trying to achieve.**
In case there is no issue present for your PR, please consider creating one.
At least please give us some description what you are trying to achieve and why your change is needed. -->
relates to #1234
## Checklist
- [ ] Issue was linked above
- [ ] Code format was applied: `make fmt`
- [ ] Examples were added / adjusted (see `examples/` directory)
- [x] Docs are up-to-date: `make generate-docs` (will be checked by CI)
- [ ] Unit tests got implemented or updated
- [ ] Acceptance tests got implemented or updated (see e.g. [here](f5f99d1709/stackit/internal/services/dns/dns_acc_test.go))
- [x] Unit tests are passing: `make test` (will be checked by CI)
- [x] No linter issues: `make lint` (will be checked by CI)
Co-authored-by: Marcel S. Henselin <marcel.henselin@stackit.cloud>
Reviewed-on: #36
Reviewed-by: Marcel_Henselin <marcel.henselin@stackit.cloud>
Co-authored-by: Andre Harms <andre.harms@stackit.cloud>
Co-committed-by: Andre Harms <andre.harms@stackit.cloud>
142 lines
4.3 KiB
Go
142 lines
4.3 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.Region = types.StringValue(region)
|
|
model.Status = types.StringValue(user.GetStatus())
|
|
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 *resourceModel, 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 *resourceModel, 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 *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
|
|
|
|
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.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.Region = types.StringValue(region)
|
|
model.Status = types.StringPointerValue(user.Status)
|
|
return nil
|
|
}
|