Some checks failed
Publish / Check GoReleaser config (push) Successful in 7s
Publish / Publish provider (push) Successful in 7m41s
CI Workflow / Check GoReleaser config (pull_request) Successful in 6s
CI Workflow / Prepare GO cache (pull_request) Successful in 10m18s
CI Workflow / Test readiness for publishing provider (pull_request) Has been cancelled
CI Workflow / Code coverage report (pull_request) Has been cancelled
CI Workflow / CI run build and linting (pull_request) Has been cancelled
CI Workflow / CI run tests (pull_request) Has been cancelled
TF Acceptance Tests Workflow / Acceptance Tests (pull_request) Failing after 26m13s
## 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: #85
144 lines
4.2 KiB
Go
144 lines
4.2 KiB
Go
package postgresflexalpha
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
|
|
"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 int64
|
|
if model.UserId.ValueInt64() == 0 {
|
|
return fmt.Errorf("user id not present")
|
|
}
|
|
userID = model.UserId.ValueInt64()
|
|
|
|
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) []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 int64
|
|
if !model.UserId.IsNull() && !model.UserId.IsUnknown() && model.UserId.ValueInt64() != 0 {
|
|
userID = model.UserId.ValueInt64()
|
|
} else if user.Id != 0 {
|
|
userID = int64(user.Id)
|
|
} else {
|
|
return fmt.Errorf("user id not present")
|
|
}
|
|
|
|
model.Id = utils.BuildInternalTerraformId(
|
|
model.ProjectId.ValueString(),
|
|
model.Region.ValueString(),
|
|
model.InstanceId.ValueString(),
|
|
strconv.FormatInt(userID, 10),
|
|
)
|
|
model.UserId = types.Int64Value(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
|
|
}
|