Some checks failed
CI Workflow / Check GoReleaser config (pull_request) Successful in 4s
CI Workflow / Test readiness for publishing provider (pull_request) Failing after 3m57s
CI Workflow / CI run tests (pull_request) Failing after 5m5s
CI Workflow / CI run build and linting (pull_request) Failing after 4m50s
CI Workflow / Code coverage report (pull_request) Has been skipped
139 lines
4.1 KiB
Go
139 lines
4.1 KiB
Go
package postgresflexalpha
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/hashicorp/terraform-plugin-framework/attr"
|
|
"github.com/hashicorp/terraform-plugin-framework/types"
|
|
"github.com/stackitcloud/stackit-sdk-go/services/postgresflex/v3alpha1api"
|
|
|
|
"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 *v3alpha1api.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 int32
|
|
if model.UserId.ValueInt32() != 0 {
|
|
userId = model.UserId.ValueInt32()
|
|
} else {
|
|
return fmt.Errorf("user id not present")
|
|
}
|
|
|
|
model.TerraformID = utils.BuildInternalTerraformId(
|
|
model.ProjectId.ValueString(), region, model.InstanceId.ValueString(), string(userId),
|
|
)
|
|
|
|
model.UserId = types.Int32Value(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.Int32Value(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) []v3alpha1api.UserRole {
|
|
var userRoles = make([]v3alpha1api.UserRole, 0, len(roles))
|
|
for _, role := range roles {
|
|
userRoles = append(userRoles, v3alpha1api.UserRole(role))
|
|
}
|
|
return userRoles
|
|
}
|
|
|
|
// toUpdatePayload creates an API update payload from the resource model.
|
|
func toUpdatePayload(model *resourceModel, roles []string) (
|
|
*v3alpha1api.UpdateUserRequestPayload,
|
|
error,
|
|
) {
|
|
if model == nil {
|
|
return nil, fmt.Errorf("nil model")
|
|
}
|
|
if roles == nil {
|
|
return nil, fmt.Errorf("nil roles")
|
|
}
|
|
|
|
return &v3alpha1api.UpdateUserRequestPayload{
|
|
Name: model.Name.ValueStringPointer(),
|
|
Roles: toPayloadRoles(roles),
|
|
}, nil
|
|
}
|
|
|
|
// toCreatePayload creates an API create payload from the resource model.
|
|
func toCreatePayload(model *resourceModel, roles []string) (*v3alpha1api.CreateUserRequestPayload, error) {
|
|
if model == nil {
|
|
return nil, fmt.Errorf("nil model")
|
|
}
|
|
if roles == nil {
|
|
return nil, fmt.Errorf("nil roles")
|
|
}
|
|
|
|
return &v3alpha1api.CreateUserRequestPayload{
|
|
Roles: toPayloadRoles(roles),
|
|
Name: model.Name.ValueString(),
|
|
}, nil
|
|
}
|
|
|
|
// mapResourceFields maps API response to the resource model, preserving existing ID.
|
|
func mapResourceFields(userResp *v3alpha1api.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 int32
|
|
if !model.UserId.IsNull() && !model.UserId.IsUnknown() && model.UserId.ValueInt32() != 0 {
|
|
userId = model.UserId.ValueInt32()
|
|
} else if user.Id != 0 {
|
|
userId = user.Id
|
|
} else {
|
|
return fmt.Errorf("user id not present")
|
|
}
|
|
|
|
model.Id = types.Int32Value(userId)
|
|
model.UserId = types.Int32Value(userId)
|
|
model.Name = types.StringValue(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.StringValue(user.Status)
|
|
return nil
|
|
}
|