From 71dae54b8b04af403270fa6fb0def3098f71c64b Mon Sep 17 00:00:00 2001 From: Andre Harms Date: Thu, 5 Feb 2026 21:05:29 +0100 Subject: [PATCH] feat: rename Model to ResourceModel for clarity in user resource handling --- .../services/postgresflexalpha/user/mapper.go | 6 +-- .../postgresflexalpha/user/mapper_test.go | 52 +++++++++---------- .../postgresflexalpha/user/resource.go | 30 ++++++----- 3 files changed, 46 insertions(+), 42 deletions(-) diff --git a/stackit/internal/services/postgresflexalpha/user/mapper.go b/stackit/internal/services/postgresflexalpha/user/mapper.go index 655d7d80..519f46e1 100644 --- a/stackit/internal/services/postgresflexalpha/user/mapper.go +++ b/stackit/internal/services/postgresflexalpha/user/mapper.go @@ -71,7 +71,7 @@ func toPayloadRoles(roles *[]string) *[]postgresflex.UserRole { } // toUpdatePayload creates an API update payload from the resource model. -func toUpdatePayload(model *Model, roles *[]string) ( +func toUpdatePayload(model *ResourceModel, roles *[]string) ( *postgresflex.UpdateUserRequestPayload, error, ) { @@ -89,7 +89,7 @@ func toUpdatePayload(model *Model, roles *[]string) ( } // 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 { 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. -func mapResourceFields(userResp *postgresflex.GetUserResponse, model *Model, region string) error { +func mapResourceFields(userResp *postgresflex.GetUserResponse, model *ResourceModel, region string) error { if userResp == nil { return fmt.Errorf("response is nil") } diff --git a/stackit/internal/services/postgresflexalpha/user/mapper_test.go b/stackit/internal/services/postgresflexalpha/user/mapper_test.go index 863dbbcc..3e02d25b 100644 --- a/stackit/internal/services/postgresflexalpha/user/mapper_test.go +++ b/stackit/internal/services/postgresflexalpha/user/mapper_test.go @@ -169,7 +169,7 @@ func TestMapFieldsCreate(t *testing.T) { description string input *postgresflex.GetUserResponse region string - expected Model + expected ResourceModel isValid bool }{ { @@ -178,7 +178,7 @@ func TestMapFieldsCreate(t *testing.T) { Id: utils.Ptr(int64(1)), }, testRegion, - Model{ + ResourceModel{ UserModel: resource.UserModel{ UserId: types.Int64Value(1), InstanceId: types.StringValue("iid"), @@ -205,7 +205,7 @@ func TestMapFieldsCreate(t *testing.T) { Status: utils.Ptr("status"), }, testRegion, - Model{ + ResourceModel{ UserModel: resource.UserModel{ UserId: types.Int64Value(1), InstanceId: types.StringValue("iid"), @@ -232,7 +232,7 @@ func TestMapFieldsCreate(t *testing.T) { Status: nil, }, testRegion, - Model{ + ResourceModel{ UserModel: resource.UserModel{ UserId: types.Int64Value(1), InstanceId: types.StringValue("iid"), @@ -254,28 +254,28 @@ func TestMapFieldsCreate(t *testing.T) { "nil_response", nil, testRegion, - Model{}, + ResourceModel{}, false, }, { "nil_response_2", &postgresflex.GetUserResponse{}, testRegion, - Model{}, + ResourceModel{}, false, }, { "no_resource_id", &postgresflex.GetUserResponse{}, testRegion, - Model{}, + ResourceModel{}, false, }, } for _, tt := range tests { t.Run( tt.description, func(t *testing.T) { - state := &Model{ + state := &ResourceModel{ UserModel: resource.UserModel{ ProjectId: tt.expected.ProjectId, InstanceId: tt.expected.InstanceId, @@ -306,7 +306,7 @@ func TestMapFields(t *testing.T) { description string input *postgresflex.GetUserResponse region string - expected Model + expected ResourceModel isValid bool }{ { @@ -315,7 +315,7 @@ func TestMapFields(t *testing.T) { Id: utils.Ptr(int64(1)), }, testRegion, - Model{ + ResourceModel{ UserModel: resource.UserModel{ Id: types.Int64Value(1), UserId: types.Int64Value(int64(1)), @@ -347,7 +347,7 @@ func TestMapFields(t *testing.T) { Port: utils.Ptr(int64(1234)), }, testRegion, - Model{ + ResourceModel{ UserModel: resource.UserModel{ Id: types.Int64Value(1), UserId: types.Int64Value(1), @@ -382,7 +382,7 @@ func TestMapFields(t *testing.T) { Port: utils.Ptr(int64(2123456789)), }, testRegion, - Model{ + ResourceModel{ UserModel: resource.UserModel{ Id: types.Int64Value(1), UserId: types.Int64Value(1), @@ -404,28 +404,28 @@ func TestMapFields(t *testing.T) { "nil_response", nil, testRegion, - Model{}, + ResourceModel{}, false, }, { "nil_response_2", &postgresflex.GetUserResponse{}, testRegion, - Model{}, + ResourceModel{}, false, }, { "no_resource_id", &postgresflex.GetUserResponse{}, testRegion, - Model{}, + ResourceModel{}, false, }, } for _, tt := range tests { t.Run( tt.description, func(t *testing.T) { - state := &Model{ + state := &ResourceModel{ UserModel: resource.UserModel{ ProjectId: tt.expected.ProjectId, InstanceId: tt.expected.InstanceId, @@ -452,14 +452,14 @@ func TestMapFields(t *testing.T) { func TestToCreatePayload(t *testing.T) { tests := []struct { description string - input *Model + input *ResourceModel inputRoles *[]string expected *postgresflex.CreateUserRequestPayload isValid bool }{ { "default_values", - &Model{}, + &ResourceModel{}, &[]string{}, &postgresflex.CreateUserRequestPayload{ Name: nil, @@ -469,7 +469,7 @@ func TestToCreatePayload(t *testing.T) { }, { "simple_values", - &Model{ + &ResourceModel{ UserModel: resource.UserModel{ Name: types.StringValue("username"), }, @@ -489,7 +489,7 @@ func TestToCreatePayload(t *testing.T) { }, { "null_fields_and_int_conversions", - &Model{ + &ResourceModel{ UserModel: resource.UserModel{ Name: types.StringNull(), }, @@ -514,7 +514,7 @@ func TestToCreatePayload(t *testing.T) { }, { "nil_roles", - &Model{}, + &ResourceModel{}, nil, nil, false, @@ -544,14 +544,14 @@ func TestToCreatePayload(t *testing.T) { func TestToUpdatePayload(t *testing.T) { tests := []struct { description string - input *Model + input *ResourceModel inputRoles *[]string expected *postgresflex.UpdateUserRequestPayload isValid bool }{ { "default_values", - &Model{}, + &ResourceModel{}, &[]string{}, &postgresflex.UpdateUserRequestPayload{ Roles: &[]postgresflex.UserRole{}, @@ -560,7 +560,7 @@ func TestToUpdatePayload(t *testing.T) { }, { "default_values", - &Model{ + &ResourceModel{ UserModel: resource.UserModel{ Name: types.StringValue("username"), }, @@ -580,7 +580,7 @@ func TestToUpdatePayload(t *testing.T) { }, { "null_fields_and_int_conversions", - &Model{ + &ResourceModel{ UserModel: resource.UserModel{ Name: types.StringNull(), }, @@ -604,7 +604,7 @@ func TestToUpdatePayload(t *testing.T) { }, { "nil_roles", - &Model{}, + &ResourceModel{}, nil, nil, false, diff --git a/stackit/internal/services/postgresflexalpha/user/resource.go b/stackit/internal/services/postgresflexalpha/user/resource.go index 760a4fe1..06c2a1a5 100644 --- a/stackit/internal/services/postgresflexalpha/user/resource.go +++ b/stackit/internal/services/postgresflexalpha/user/resource.go @@ -33,8 +33,8 @@ var ( _ resource.ResourceWithModifyPlan = &userResource{} ) -// Model represents the Terraform resource state for a PostgreSQL Flex user. -type Model struct { +// ResourceModel represents the Terraform resource state for a PostgreSQL Flex user. +type ResourceModel struct { postgresflexalpha.UserModel TerraformID types.String `tfsdk:"id"` } @@ -44,7 +44,7 @@ func NewUserResource() resource.Resource { return &userResource{} } -// userResource is the resource implementation. +// userResource implements the resource handling for a PostgreSQL Flex user. type userResource struct { client *postgresflex.APIClient providerData core.ProviderData @@ -57,7 +57,7 @@ func (r *userResource) ModifyPlan( req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse, ) { // nolint:gocritic // function signature required by Terraform - var configModel Model + var configModel ResourceModel // skip initial empty configuration to avoid follow-up errors if req.Config.Raw.IsNull() { return @@ -67,7 +67,7 @@ func (r *userResource) ModifyPlan( return } - var planModel Model + var planModel ResourceModel resp.Diagnostics.Append(req.Plan.Get(ctx, &planModel)...) if resp.Diagnostics.HasError() { return @@ -137,7 +137,7 @@ func (r *userResource) Create( req resource.CreateRequest, resp *resource.CreateResponse, ) { // nolint:gocritic // function signature required by Terraform - var model Model + var model ResourceModel diags := req.Plan.Get(ctx, &model) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { @@ -189,6 +189,7 @@ func (r *userResource) Create( ctx = core.LogResponse(ctx) + // Verify creation exists, err := r.getUserResource(ctx, &model) if err != nil { @@ -218,7 +219,7 @@ func (r *userResource) Read( req resource.ReadRequest, resp *resource.ReadResponse, ) { // nolint:gocritic // function signature required by Terraform - var model Model + var model ResourceModel diags := req.State.Get(ctx, &model) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { @@ -227,6 +228,7 @@ func (r *userResource) Read( ctx = core.InitProviderContext(ctx) + // Read resource state exists, err := r.getUserResource(ctx, &model) if err != nil { @@ -256,7 +258,7 @@ func (r *userResource) Update( req resource.UpdateRequest, resp *resource.UpdateResponse, ) { // nolint:gocritic // function signature required by Terraform - var model Model + var model ResourceModel diags := req.Plan.Get(ctx, &model) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { @@ -268,7 +270,7 @@ func (r *userResource) Update( arg := r.getClientArg(&model) // Retrieve values from state - var stateModel Model + var stateModel ResourceModel diags = req.State.Get(ctx, &stateModel) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { @@ -309,6 +311,7 @@ func (r *userResource) Update( ctx = core.LogResponse(ctx) + // Verify update exists, err := r.getUserResource(ctx, &stateModel) if err != nil { @@ -339,7 +342,7 @@ func (r *userResource) Delete( req resource.DeleteRequest, resp *resource.DeleteResponse, ) { // nolint:gocritic // function signature required by Terraform - var model Model + var model ResourceModel diags := req.State.Get(ctx, &model) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { @@ -365,6 +368,7 @@ func (r *userResource) Delete( ctx = core.LogResponse(ctx) + // Verify deletion exists, err := r.getUserResource(ctx, &model) if err != nil { 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. // 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) arg := r.getClientArg(model) @@ -497,7 +501,7 @@ type clientArg struct { } // getClientArg constructs client arguments from the model. -func (r *userResource) getClientArg(model *Model) *clientArg { +func (r *userResource) getClientArg(model *ResourceModel) *clientArg { return &clientArg{ projectId: model.ProjectId.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. -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) ctx = tflog.SetField(ctx, "project_id", usrCtx.projectId)