chore: changed and refactored providers #36

Merged
marcel.henselin merged 31 commits from feat/alpha_user_database into alpha 2026-02-10 08:10:03 +00:00
3 changed files with 46 additions and 42 deletions
Showing only changes of commit 71dae54b8b - Show all commits

View file

@ -71,7 +71,7 @@ func toPayloadRoles(roles *[]string) *[]postgresflex.UserRole {
} }
// toUpdatePayload creates an API update payload from the resource model. // toUpdatePayload creates an API update payload from the resource model.
func toUpdatePayload(model *Model, roles *[]string) ( func toUpdatePayload(model *ResourceModel, roles *[]string) (
*postgresflex.UpdateUserRequestPayload, *postgresflex.UpdateUserRequestPayload,
error, error,
) { ) {
@ -89,7 +89,7 @@ func toUpdatePayload(model *Model, roles *[]string) (
} }
// toCreatePayload creates an API create payload from the resource model. // toCreatePayload creates an API create payload from the resource model.
func toCreatePayload(model *Model, roles *[]string) (*postgresflex.CreateUserRequestPayload, error) { func toCreatePayload(model *ResourceModel, roles *[]string) (*postgresflex.CreateUserRequestPayload, error) {
if model == nil { if model == nil {
return nil, fmt.Errorf("nil model") return nil, fmt.Errorf("nil model")
} }
@ -104,7 +104,7 @@ func toCreatePayload(model *Model, roles *[]string) (*postgresflex.CreateUserReq
} }
// mapResourceFields maps API response to the resource model, preserving existing ID. // mapResourceFields maps API response to the resource model, preserving existing ID.
func mapResourceFields(userResp *postgresflex.GetUserResponse, model *Model, region string) error { func mapResourceFields(userResp *postgresflex.GetUserResponse, model *ResourceModel, region string) error {
if userResp == nil { if userResp == nil {
return fmt.Errorf("response is nil") return fmt.Errorf("response is nil")
} }

View file

@ -169,7 +169,7 @@ func TestMapFieldsCreate(t *testing.T) {
description string description string
input *postgresflex.GetUserResponse input *postgresflex.GetUserResponse
region string region string
expected Model expected ResourceModel
isValid bool isValid bool
}{ }{
{ {
@ -178,7 +178,7 @@ func TestMapFieldsCreate(t *testing.T) {
Id: utils.Ptr(int64(1)), Id: utils.Ptr(int64(1)),
}, },
testRegion, testRegion,
Model{ ResourceModel{
UserModel: resource.UserModel{ UserModel: resource.UserModel{
UserId: types.Int64Value(1), UserId: types.Int64Value(1),
InstanceId: types.StringValue("iid"), InstanceId: types.StringValue("iid"),
@ -205,7 +205,7 @@ func TestMapFieldsCreate(t *testing.T) {
Status: utils.Ptr("status"), Status: utils.Ptr("status"),
}, },
testRegion, testRegion,
Model{ ResourceModel{
UserModel: resource.UserModel{ UserModel: resource.UserModel{
UserId: types.Int64Value(1), UserId: types.Int64Value(1),
InstanceId: types.StringValue("iid"), InstanceId: types.StringValue("iid"),
@ -232,7 +232,7 @@ func TestMapFieldsCreate(t *testing.T) {
Status: nil, Status: nil,
}, },
testRegion, testRegion,
Model{ ResourceModel{
UserModel: resource.UserModel{ UserModel: resource.UserModel{
UserId: types.Int64Value(1), UserId: types.Int64Value(1),
InstanceId: types.StringValue("iid"), InstanceId: types.StringValue("iid"),
@ -254,28 +254,28 @@ func TestMapFieldsCreate(t *testing.T) {
"nil_response", "nil_response",
nil, nil,
testRegion, testRegion,
Model{}, ResourceModel{},
false, false,
}, },
{ {
"nil_response_2", "nil_response_2",
&postgresflex.GetUserResponse{}, &postgresflex.GetUserResponse{},
testRegion, testRegion,
Model{}, ResourceModel{},
false, false,
}, },
{ {
"no_resource_id", "no_resource_id",
&postgresflex.GetUserResponse{}, &postgresflex.GetUserResponse{},
testRegion, testRegion,
Model{}, ResourceModel{},
false, false,
}, },
} }
for _, tt := range tests { for _, tt := range tests {
t.Run( t.Run(
tt.description, func(t *testing.T) { tt.description, func(t *testing.T) {
state := &Model{ state := &ResourceModel{
UserModel: resource.UserModel{ UserModel: resource.UserModel{
ProjectId: tt.expected.ProjectId, ProjectId: tt.expected.ProjectId,
InstanceId: tt.expected.InstanceId, InstanceId: tt.expected.InstanceId,
@ -306,7 +306,7 @@ func TestMapFields(t *testing.T) {
description string description string
input *postgresflex.GetUserResponse input *postgresflex.GetUserResponse
region string region string
expected Model expected ResourceModel
isValid bool isValid bool
}{ }{
{ {
@ -315,7 +315,7 @@ func TestMapFields(t *testing.T) {
Id: utils.Ptr(int64(1)), Id: utils.Ptr(int64(1)),
}, },
testRegion, testRegion,
Model{ ResourceModel{
UserModel: resource.UserModel{ UserModel: resource.UserModel{
Id: types.Int64Value(1), Id: types.Int64Value(1),
UserId: types.Int64Value(int64(1)), UserId: types.Int64Value(int64(1)),
@ -347,7 +347,7 @@ func TestMapFields(t *testing.T) {
Port: utils.Ptr(int64(1234)), Port: utils.Ptr(int64(1234)),
}, },
testRegion, testRegion,
Model{ ResourceModel{
UserModel: resource.UserModel{ UserModel: resource.UserModel{
Id: types.Int64Value(1), Id: types.Int64Value(1),
UserId: types.Int64Value(1), UserId: types.Int64Value(1),
@ -382,7 +382,7 @@ func TestMapFields(t *testing.T) {
Port: utils.Ptr(int64(2123456789)), Port: utils.Ptr(int64(2123456789)),
}, },
testRegion, testRegion,
Model{ ResourceModel{
UserModel: resource.UserModel{ UserModel: resource.UserModel{
Id: types.Int64Value(1), Id: types.Int64Value(1),
UserId: types.Int64Value(1), UserId: types.Int64Value(1),
@ -404,28 +404,28 @@ func TestMapFields(t *testing.T) {
"nil_response", "nil_response",
nil, nil,
testRegion, testRegion,
Model{}, ResourceModel{},
false, false,
}, },
{ {
"nil_response_2", "nil_response_2",
&postgresflex.GetUserResponse{}, &postgresflex.GetUserResponse{},
testRegion, testRegion,
Model{}, ResourceModel{},
false, false,
}, },
{ {
"no_resource_id", "no_resource_id",
&postgresflex.GetUserResponse{}, &postgresflex.GetUserResponse{},
testRegion, testRegion,
Model{}, ResourceModel{},
false, false,
}, },
} }
for _, tt := range tests { for _, tt := range tests {
t.Run( t.Run(
tt.description, func(t *testing.T) { tt.description, func(t *testing.T) {
state := &Model{ state := &ResourceModel{
UserModel: resource.UserModel{ UserModel: resource.UserModel{
ProjectId: tt.expected.ProjectId, ProjectId: tt.expected.ProjectId,
InstanceId: tt.expected.InstanceId, InstanceId: tt.expected.InstanceId,
@ -452,14 +452,14 @@ func TestMapFields(t *testing.T) {
func TestToCreatePayload(t *testing.T) { func TestToCreatePayload(t *testing.T) {
tests := []struct { tests := []struct {
description string description string
input *Model input *ResourceModel
inputRoles *[]string inputRoles *[]string
expected *postgresflex.CreateUserRequestPayload expected *postgresflex.CreateUserRequestPayload
isValid bool isValid bool
}{ }{
{ {
"default_values", "default_values",
&Model{}, &ResourceModel{},
&[]string{}, &[]string{},
&postgresflex.CreateUserRequestPayload{ &postgresflex.CreateUserRequestPayload{
Name: nil, Name: nil,
@ -469,7 +469,7 @@ func TestToCreatePayload(t *testing.T) {
}, },
{ {
"simple_values", "simple_values",
&Model{ &ResourceModel{
UserModel: resource.UserModel{ UserModel: resource.UserModel{
Name: types.StringValue("username"), Name: types.StringValue("username"),
}, },
@ -489,7 +489,7 @@ func TestToCreatePayload(t *testing.T) {
}, },
{ {
"null_fields_and_int_conversions", "null_fields_and_int_conversions",
&Model{ &ResourceModel{
UserModel: resource.UserModel{ UserModel: resource.UserModel{
Name: types.StringNull(), Name: types.StringNull(),
}, },
@ -514,7 +514,7 @@ func TestToCreatePayload(t *testing.T) {
}, },
{ {
"nil_roles", "nil_roles",
&Model{}, &ResourceModel{},
nil, nil,
nil, nil,
false, false,
@ -544,14 +544,14 @@ func TestToCreatePayload(t *testing.T) {
func TestToUpdatePayload(t *testing.T) { func TestToUpdatePayload(t *testing.T) {
tests := []struct { tests := []struct {
description string description string
input *Model input *ResourceModel
inputRoles *[]string inputRoles *[]string
expected *postgresflex.UpdateUserRequestPayload expected *postgresflex.UpdateUserRequestPayload
isValid bool isValid bool
}{ }{
{ {
"default_values", "default_values",
&Model{}, &ResourceModel{},
&[]string{}, &[]string{},
&postgresflex.UpdateUserRequestPayload{ &postgresflex.UpdateUserRequestPayload{
Roles: &[]postgresflex.UserRole{}, Roles: &[]postgresflex.UserRole{},
@ -560,7 +560,7 @@ func TestToUpdatePayload(t *testing.T) {
}, },
{ {
"default_values", "default_values",
&Model{ &ResourceModel{
UserModel: resource.UserModel{ UserModel: resource.UserModel{
Name: types.StringValue("username"), Name: types.StringValue("username"),
}, },
@ -580,7 +580,7 @@ func TestToUpdatePayload(t *testing.T) {
}, },
{ {
"null_fields_and_int_conversions", "null_fields_and_int_conversions",
&Model{ &ResourceModel{
UserModel: resource.UserModel{ UserModel: resource.UserModel{
Name: types.StringNull(), Name: types.StringNull(),
}, },
@ -604,7 +604,7 @@ func TestToUpdatePayload(t *testing.T) {
}, },
{ {
"nil_roles", "nil_roles",
&Model{}, &ResourceModel{},
nil, nil,
nil, nil,
false, false,

View file

@ -33,8 +33,8 @@ var (
_ resource.ResourceWithModifyPlan = &userResource{} _ resource.ResourceWithModifyPlan = &userResource{}
) )
// Model represents the Terraform resource state for a PostgreSQL Flex user. // ResourceModel represents the Terraform resource state for a PostgreSQL Flex user.
type Model struct { type ResourceModel struct {
postgresflexalpha.UserModel postgresflexalpha.UserModel
TerraformID types.String `tfsdk:"id"` TerraformID types.String `tfsdk:"id"`
} }
@ -44,7 +44,7 @@ func NewUserResource() resource.Resource {
return &userResource{} return &userResource{}
} }
// userResource is the resource implementation. // userResource implements the resource handling for a PostgreSQL Flex user.
type userResource struct { type userResource struct {
client *postgresflex.APIClient client *postgresflex.APIClient
providerData core.ProviderData providerData core.ProviderData
@ -57,7 +57,7 @@ func (r *userResource) ModifyPlan(
req resource.ModifyPlanRequest, req resource.ModifyPlanRequest,
resp *resource.ModifyPlanResponse, resp *resource.ModifyPlanResponse,
) { // nolint:gocritic // function signature required by Terraform ) { // nolint:gocritic // function signature required by Terraform
var configModel Model var configModel ResourceModel
// skip initial empty configuration to avoid follow-up errors // skip initial empty configuration to avoid follow-up errors
if req.Config.Raw.IsNull() { if req.Config.Raw.IsNull() {
return return
@ -67,7 +67,7 @@ func (r *userResource) ModifyPlan(
return return
} }
var planModel Model var planModel ResourceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &planModel)...) resp.Diagnostics.Append(req.Plan.Get(ctx, &planModel)...)
if resp.Diagnostics.HasError() { if resp.Diagnostics.HasError() {
return return
@ -137,7 +137,7 @@ func (r *userResource) Create(
req resource.CreateRequest, req resource.CreateRequest,
resp *resource.CreateResponse, resp *resource.CreateResponse,
) { // nolint:gocritic // function signature required by Terraform ) { // nolint:gocritic // function signature required by Terraform
var model Model var model ResourceModel
diags := req.Plan.Get(ctx, &model) diags := req.Plan.Get(ctx, &model)
resp.Diagnostics.Append(diags...) resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() { if resp.Diagnostics.HasError() {
@ -189,6 +189,7 @@ func (r *userResource) Create(
ctx = core.LogResponse(ctx) ctx = core.LogResponse(ctx)
// Verify creation
exists, err := r.getUserResource(ctx, &model) exists, err := r.getUserResource(ctx, &model)
if err != nil { if err != nil {
@ -218,7 +219,7 @@ func (r *userResource) Read(
req resource.ReadRequest, req resource.ReadRequest,
resp *resource.ReadResponse, resp *resource.ReadResponse,
) { // nolint:gocritic // function signature required by Terraform ) { // nolint:gocritic // function signature required by Terraform
var model Model var model ResourceModel
diags := req.State.Get(ctx, &model) diags := req.State.Get(ctx, &model)
resp.Diagnostics.Append(diags...) resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() { if resp.Diagnostics.HasError() {
@ -227,6 +228,7 @@ func (r *userResource) Read(
ctx = core.InitProviderContext(ctx) ctx = core.InitProviderContext(ctx)
// Read resource state
exists, err := r.getUserResource(ctx, &model) exists, err := r.getUserResource(ctx, &model)
if err != nil { if err != nil {
@ -256,7 +258,7 @@ func (r *userResource) Update(
req resource.UpdateRequest, req resource.UpdateRequest,
resp *resource.UpdateResponse, resp *resource.UpdateResponse,
) { // nolint:gocritic // function signature required by Terraform ) { // nolint:gocritic // function signature required by Terraform
var model Model var model ResourceModel
diags := req.Plan.Get(ctx, &model) diags := req.Plan.Get(ctx, &model)
resp.Diagnostics.Append(diags...) resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() { if resp.Diagnostics.HasError() {
@ -268,7 +270,7 @@ func (r *userResource) Update(
arg := r.getClientArg(&model) arg := r.getClientArg(&model)
// Retrieve values from state // Retrieve values from state
var stateModel Model var stateModel ResourceModel
diags = req.State.Get(ctx, &stateModel) diags = req.State.Get(ctx, &stateModel)
resp.Diagnostics.Append(diags...) resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() { if resp.Diagnostics.HasError() {
@ -309,6 +311,7 @@ func (r *userResource) Update(
ctx = core.LogResponse(ctx) ctx = core.LogResponse(ctx)
// Verify update
exists, err := r.getUserResource(ctx, &stateModel) exists, err := r.getUserResource(ctx, &stateModel)
if err != nil { if err != nil {
@ -339,7 +342,7 @@ func (r *userResource) Delete(
req resource.DeleteRequest, req resource.DeleteRequest,
resp *resource.DeleteResponse, resp *resource.DeleteResponse,
) { // nolint:gocritic // function signature required by Terraform ) { // nolint:gocritic // function signature required by Terraform
var model Model var model ResourceModel
diags := req.State.Get(ctx, &model) diags := req.State.Get(ctx, &model)
resp.Diagnostics.Append(diags...) resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() { if resp.Diagnostics.HasError() {
@ -365,6 +368,7 @@ func (r *userResource) Delete(
ctx = core.LogResponse(ctx) ctx = core.LogResponse(ctx)
// Verify deletion
exists, err := r.getUserResource(ctx, &model) exists, err := r.getUserResource(ctx, &model)
if err != nil { if err != nil {
core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting user", fmt.Sprintf("Calling API: %v", err)) core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting user", fmt.Sprintf("Calling API: %v", err))
@ -459,7 +463,7 @@ func mapFields(userResp *postgresflex.GetUserResponse, model *Model, region stri
// getUserResource refreshes the resource state by calling the API and mapping the response to the model. // getUserResource refreshes the resource state by calling the API and mapping the response to the model.
// Returns true if the resource state was successfully refreshed, false if the resource does not exist. // Returns true if the resource state was successfully refreshed, false if the resource does not exist.
func (r *userResource) getUserResource(ctx context.Context, model *Model) (bool, error) { func (r *userResource) getUserResource(ctx context.Context, model *ResourceModel) (bool, error) {
ctx = r.setTFLogFields(ctx, model) ctx = r.setTFLogFields(ctx, model)
arg := r.getClientArg(model) arg := r.getClientArg(model)
@ -497,7 +501,7 @@ type clientArg struct {
} }
// getClientArg constructs client arguments from the model. // getClientArg constructs client arguments from the model.
func (r *userResource) getClientArg(model *Model) *clientArg { func (r *userResource) getClientArg(model *ResourceModel) *clientArg {
return &clientArg{ return &clientArg{
projectId: model.ProjectId.ValueString(), projectId: model.ProjectId.ValueString(),
instanceId: model.InstanceId.ValueString(), instanceId: model.InstanceId.ValueString(),
@ -507,7 +511,7 @@ func (r *userResource) getClientArg(model *Model) *clientArg {
} }
// setTFLogFields adds relevant fields to the context for terraform logging purposes. // setTFLogFields adds relevant fields to the context for terraform logging purposes.
func (r *userResource) setTFLogFields(ctx context.Context, model *Model) context.Context { func (r *userResource) setTFLogFields(ctx context.Context, model *ResourceModel) context.Context {
usrCtx := r.getClientArg(model) usrCtx := r.getClientArg(model)
ctx = tflog.SetField(ctx, "project_id", usrCtx.projectId) ctx = tflog.SetField(ctx, "project_id", usrCtx.projectId)