feat: refactor data source models and update mapping functions for improved consistency

This commit is contained in:
Andre_Harms 2026-02-09 21:28:49 +01:00
parent f0e7c19cdf
commit 184e133a2a
34 changed files with 980 additions and 1017 deletions

View file

@ -25,17 +25,17 @@ var (
_ datasource.DataSource = &userDataSource{}
)
// DataSourceModel maps the data source schema data.
type DataSourceModel struct {
postgresflexalpha.UserModel
TerraformID types.String `tfsdk:"id"`
}
// NewUserDataSource is a helper function to simplify the provider implementation.
func NewUserDataSource() datasource.DataSource {
return &userDataSource{}
}
// dataSourceModel maps the data source schema data.
type dataSourceModel struct {
postgresflexalpha.UserModel
TerraformID types.String `tfsdk:"id"`
}
// userDataSource is the data source implementation.
type userDataSource struct {
client *postgresflex.APIClient
@ -90,7 +90,7 @@ func (r *userDataSource) Read(
req datasource.ReadRequest,
resp *datasource.ReadResponse,
) { // nolint:gocritic // function signature required by Terraform
var model DataSourceModel
var model dataSourceModel
diags := req.Config.Get(ctx, &model)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {

View file

@ -13,7 +13,7 @@ import (
)
// mapDataSourceFields maps API response to data source model, preserving existing ID.
func mapDataSourceFields(userResp *postgresflex.GetUserResponse, model *DataSourceModel, region string) error {
func mapDataSourceFields(userResp *postgresflex.GetUserResponse, model *dataSourceModel, region string) error {
if userResp == nil {
return fmt.Errorf("response is nil")
}
@ -68,7 +68,7 @@ func toPayloadRoles(roles *[]string) *[]postgresflex.UserRole {
}
// toUpdatePayload creates an API update payload from the resource model.
func toUpdatePayload(model *ResourceModel, roles *[]string) (
func toUpdatePayload(model *resourceModel, roles *[]string) (
*postgresflex.UpdateUserRequestPayload,
error,
) {
@ -86,7 +86,7 @@ func toUpdatePayload(model *ResourceModel, roles *[]string) (
}
// toCreatePayload creates an API create payload from the resource model.
func toCreatePayload(model *ResourceModel, roles *[]string) (*postgresflex.CreateUserRequestPayload, error) {
func toCreatePayload(model *resourceModel, roles *[]string) (*postgresflex.CreateUserRequestPayload, error) {
if model == nil {
return nil, fmt.Errorf("nil model")
}
@ -101,7 +101,7 @@ func toCreatePayload(model *ResourceModel, roles *[]string) (*postgresflex.Creat
}
// mapResourceFields maps API response to the resource model, preserving existing ID.
func mapResourceFields(userResp *postgresflex.GetUserResponse, model *ResourceModel, region string) error {
func mapResourceFields(userResp *postgresflex.GetUserResponse, model *resourceModel, region string) error {
if userResp == nil {
return fmt.Errorf("response is nil")
}
@ -118,9 +118,7 @@ func mapResourceFields(userResp *postgresflex.GetUserResponse, model *ResourceMo
} else {
return fmt.Errorf("user id not present")
}
model.TerraformID = utils.BuildInternalTerraformId(
model.ProjectId.ValueString(), region, model.InstanceId.ValueString(), strconv.FormatInt(userId, 10),
)
model.Id = types.Int64Value(userId)
model.UserId = types.Int64Value(userId)
model.Name = types.StringPointerValue(user.Name)

View file

@ -9,7 +9,6 @@ import (
"github.com/stackitcloud/stackit-sdk-go/core/utils"
postgresflex "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/postgresflexalpha"
data "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/postgresflexalpha/user/datasources_gen"
resource "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/postgresflexalpha/user/resources_gen"
)
func TestMapDataSourceFields(t *testing.T) {
@ -18,14 +17,14 @@ func TestMapDataSourceFields(t *testing.T) {
description string
input *postgresflex.GetUserResponse
region string
expected DataSourceModel
expected dataSourceModel
isValid bool
}{
{
"default_values",
&postgresflex.GetUserResponse{},
testRegion,
DataSourceModel{
dataSourceModel{
UserModel: data.UserModel{
Id: types.Int64Value(1),
UserId: types.Int64Value(1),
@ -51,7 +50,7 @@ func TestMapDataSourceFields(t *testing.T) {
Name: utils.Ptr("username"),
},
testRegion,
DataSourceModel{
dataSourceModel{
UserModel: data.UserModel{
Id: types.Int64Value(1),
@ -84,7 +83,7 @@ func TestMapDataSourceFields(t *testing.T) {
Status: utils.Ptr("status"),
},
testRegion,
DataSourceModel{
dataSourceModel{
UserModel: data.UserModel{
Id: types.Int64Value(1),
UserId: types.Int64Value(1),
@ -103,28 +102,28 @@ func TestMapDataSourceFields(t *testing.T) {
"nil_response",
nil,
testRegion,
DataSourceModel{},
dataSourceModel{},
false,
},
{
"nil_response_2",
&postgresflex.GetUserResponse{},
testRegion,
DataSourceModel{},
dataSourceModel{},
false,
},
{
"no_resource_id",
&postgresflex.GetUserResponse{},
testRegion,
DataSourceModel{},
dataSourceModel{},
false,
},
}
for _, tt := range tests {
t.Run(
tt.description, func(t *testing.T) {
state := &DataSourceModel{
state := &dataSourceModel{
UserModel: data.UserModel{
ProjectId: tt.expected.ProjectId,
InstanceId: tt.expected.InstanceId,
@ -155,7 +154,7 @@ func TestMapFieldsCreate(t *testing.T) {
description string
input *postgresflex.GetUserResponse
region string
expected ResourceModel
expected resourceModel
isValid bool
}{
{
@ -164,19 +163,16 @@ func TestMapFieldsCreate(t *testing.T) {
Id: utils.Ptr(int64(1)),
},
testRegion,
ResourceModel{
UserModel: resource.UserModel{
UserId: types.Int64Value(1),
InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"),
Name: types.StringNull(),
Roles: types.List(types.SetNull(types.StringType)),
Password: types.StringNull(),
Region: types.StringValue(testRegion),
Status: types.StringNull(),
ConnectionString: types.StringNull(),
},
TerraformID: types.StringValue("pid,region,iid,1"),
resourceModel{
UserId: types.Int64Value(1),
InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"),
Name: types.StringNull(),
Roles: types.List(types.SetNull(types.StringType)),
Password: types.StringNull(),
Region: types.StringValue(testRegion),
Status: types.StringNull(),
ConnectionString: types.StringNull(),
},
true,
},
@ -188,19 +184,16 @@ func TestMapFieldsCreate(t *testing.T) {
Status: utils.Ptr("status"),
},
testRegion,
ResourceModel{
UserModel: resource.UserModel{
UserId: types.Int64Value(1),
InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"),
Name: types.StringValue("username"),
Roles: types.List(types.SetNull(types.StringType)),
Password: types.StringNull(),
Region: types.StringValue(testRegion),
Status: types.StringValue("status"),
ConnectionString: types.StringValue("connection_string"),
},
TerraformID: types.StringValue("pid,region,iid,1"),
resourceModel{
UserId: types.Int64Value(1),
InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"),
Name: types.StringValue("username"),
Roles: types.List(types.SetNull(types.StringType)),
Password: types.StringNull(),
Region: types.StringValue(testRegion),
Status: types.StringValue("status"),
ConnectionString: types.StringValue("connection_string"),
},
true,
},
@ -212,19 +205,16 @@ func TestMapFieldsCreate(t *testing.T) {
Status: nil,
},
testRegion,
ResourceModel{
UserModel: resource.UserModel{
UserId: types.Int64Value(1),
InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"),
Name: types.StringNull(),
Roles: types.List(types.SetNull(types.StringType)),
Password: types.StringNull(),
Region: types.StringValue(testRegion),
Status: types.StringNull(),
ConnectionString: types.StringNull(),
},
TerraformID: types.StringValue("pid,region,iid,1"),
resourceModel{
UserId: types.Int64Value(1),
InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"),
Name: types.StringNull(),
Roles: types.List(types.SetNull(types.StringType)),
Password: types.StringNull(),
Region: types.StringValue(testRegion),
Status: types.StringNull(),
ConnectionString: types.StringNull(),
},
true,
},
@ -232,32 +222,30 @@ func TestMapFieldsCreate(t *testing.T) {
"nil_response",
nil,
testRegion,
ResourceModel{},
resourceModel{},
false,
},
{
"nil_response_2",
&postgresflex.GetUserResponse{},
testRegion,
ResourceModel{},
resourceModel{},
false,
},
{
"no_resource_id",
&postgresflex.GetUserResponse{},
testRegion,
ResourceModel{},
resourceModel{},
false,
},
}
for _, tt := range tests {
t.Run(
tt.description, func(t *testing.T) {
state := &ResourceModel{
UserModel: resource.UserModel{
ProjectId: tt.expected.ProjectId,
InstanceId: tt.expected.InstanceId,
},
state := &resourceModel{
ProjectId: tt.expected.ProjectId,
InstanceId: tt.expected.InstanceId,
}
err := mapResourceFields(tt.input, state, tt.region)
@ -284,7 +272,7 @@ func TestMapFields(t *testing.T) {
description string
input *postgresflex.GetUserResponse
region string
expected ResourceModel
expected resourceModel
isValid bool
}{
{
@ -293,19 +281,16 @@ func TestMapFields(t *testing.T) {
Id: utils.Ptr(int64(1)),
},
testRegion,
ResourceModel{
UserModel: resource.UserModel{
Id: types.Int64Value(1),
UserId: types.Int64Value(int64(1)),
InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"),
Name: types.StringNull(),
Roles: types.List(types.SetNull(types.StringType)),
Region: types.StringValue(testRegion),
Status: types.StringNull(),
ConnectionString: types.StringNull(),
},
TerraformID: types.StringValue("pid,region,iid,1"),
resourceModel{
Id: types.Int64Value(1),
UserId: types.Int64Value(int64(1)),
InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"),
Name: types.StringNull(),
Roles: types.List(types.SetNull(types.StringType)),
Region: types.StringValue(testRegion),
Status: types.StringNull(),
ConnectionString: types.StringNull(),
},
true,
},
@ -321,27 +306,24 @@ func TestMapFields(t *testing.T) {
Name: utils.Ptr("username"),
},
testRegion,
ResourceModel{
UserModel: resource.UserModel{
Id: types.Int64Value(1),
UserId: types.Int64Value(1),
InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"),
Name: types.StringValue("username"),
Roles: types.List(
types.SetValueMust(
types.StringType, []attr.Value{
types.StringValue("role_1"),
types.StringValue("role_2"),
types.StringValue(""),
},
),
resourceModel{
Id: types.Int64Value(1),
UserId: types.Int64Value(1),
InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"),
Name: types.StringValue("username"),
Roles: types.List(
types.SetValueMust(
types.StringType, []attr.Value{
types.StringValue("role_1"),
types.StringValue("role_2"),
types.StringValue(""),
},
),
Region: types.StringValue(testRegion),
Status: types.StringNull(),
ConnectionString: types.StringNull(),
},
TerraformID: types.StringValue("pid,region,iid,1"),
),
Region: types.StringValue(testRegion),
Status: types.StringNull(),
ConnectionString: types.StringNull(),
},
true,
},
@ -352,19 +334,16 @@ func TestMapFields(t *testing.T) {
Name: nil,
},
testRegion,
ResourceModel{
UserModel: resource.UserModel{
Id: types.Int64Value(1),
UserId: types.Int64Value(1),
InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"),
Name: types.StringNull(),
Roles: types.List(types.SetNull(types.StringType)),
Region: types.StringValue(testRegion),
Status: types.StringNull(),
ConnectionString: types.StringNull(),
},
TerraformID: types.StringValue("pid,region,iid,1"),
resourceModel{
Id: types.Int64Value(1),
UserId: types.Int64Value(1),
InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"),
Name: types.StringNull(),
Roles: types.List(types.SetNull(types.StringType)),
Region: types.StringValue(testRegion),
Status: types.StringNull(),
ConnectionString: types.StringNull(),
},
true,
},
@ -372,32 +351,30 @@ func TestMapFields(t *testing.T) {
"nil_response",
nil,
testRegion,
ResourceModel{},
resourceModel{},
false,
},
{
"nil_response_2",
&postgresflex.GetUserResponse{},
testRegion,
ResourceModel{},
resourceModel{},
false,
},
{
"no_resource_id",
&postgresflex.GetUserResponse{},
testRegion,
ResourceModel{},
resourceModel{},
false,
},
}
for _, tt := range tests {
t.Run(
tt.description, func(t *testing.T) {
state := &ResourceModel{
UserModel: resource.UserModel{
ProjectId: tt.expected.ProjectId,
InstanceId: tt.expected.InstanceId,
},
state := &resourceModel{
ProjectId: tt.expected.ProjectId,
InstanceId: tt.expected.InstanceId,
}
err := mapResourceFields(tt.input, state, tt.region)
if !tt.isValid && err == nil {
@ -420,14 +397,14 @@ func TestMapFields(t *testing.T) {
func TestToCreatePayload(t *testing.T) {
tests := []struct {
description string
input *ResourceModel
input *resourceModel
inputRoles *[]string
expected *postgresflex.CreateUserRequestPayload
isValid bool
}{
{
"default_values",
&ResourceModel{},
&resourceModel{},
&[]string{},
&postgresflex.CreateUserRequestPayload{
Name: nil,
@ -437,10 +414,8 @@ func TestToCreatePayload(t *testing.T) {
},
{
"simple_values",
&ResourceModel{
UserModel: resource.UserModel{
Name: types.StringValue("username"),
},
&resourceModel{
Name: types.StringValue("username"),
},
&[]string{
"role_1",
@ -457,10 +432,8 @@ func TestToCreatePayload(t *testing.T) {
},
{
"null_fields_and_int_conversions",
&ResourceModel{
UserModel: resource.UserModel{
Name: types.StringNull(),
},
&resourceModel{
Name: types.StringNull(),
},
&[]string{
"",
@ -482,7 +455,7 @@ func TestToCreatePayload(t *testing.T) {
},
{
"nil_roles",
&ResourceModel{},
&resourceModel{},
nil,
nil,
false,
@ -512,14 +485,14 @@ func TestToCreatePayload(t *testing.T) {
func TestToUpdatePayload(t *testing.T) {
tests := []struct {
description string
input *ResourceModel
input *resourceModel
inputRoles *[]string
expected *postgresflex.UpdateUserRequestPayload
isValid bool
}{
{
"default_values",
&ResourceModel{},
&resourceModel{},
&[]string{},
&postgresflex.UpdateUserRequestPayload{
Roles: &[]postgresflex.UserRole{},
@ -528,10 +501,8 @@ func TestToUpdatePayload(t *testing.T) {
},
{
"default_values",
&ResourceModel{
UserModel: resource.UserModel{
Name: types.StringValue("username"),
},
&resourceModel{
Name: types.StringValue("username"),
},
&[]string{
"role_1",
@ -548,10 +519,8 @@ func TestToUpdatePayload(t *testing.T) {
},
{
"null_fields_and_int_conversions",
&ResourceModel{
UserModel: resource.UserModel{
Name: types.StringNull(),
},
&resourceModel{
Name: types.StringNull(),
},
&[]string{
"",
@ -572,7 +541,7 @@ func TestToUpdatePayload(t *testing.T) {
},
{
"nil_roles",
&ResourceModel{},
&resourceModel{},
nil,
nil,
false,

View file

@ -41,12 +41,14 @@ var (
extractErrorMessage = "Extracting identity data: %v"
)
// ResourceModel represents the Terraform resource state for a PostgreSQL Flex user.
type ResourceModel struct {
postgresflexalpha.UserModel
TerraformID types.String `tfsdk:"id"`
// NewUserResource is a helper function to simplify the provider implementation.
func NewUserResource() resource.Resource {
return &userResource{}
}
// resourceModel represents the Terraform resource state for a PostgreSQL Flex user.
type resourceModel = postgresflexalpha.UserModel
// UserResourceIdentityModel describes the resource's identity attributes.
type UserResourceIdentityModel struct {
ProjectID types.String `tfsdk:"project_id"`
@ -55,11 +57,6 @@ type UserResourceIdentityModel struct {
UserID types.Int64 `tfsdk:"database_id"`
}
// NewUserResource is a helper function to simplify the provider implementation.
func NewUserResource() resource.Resource {
return &userResource{}
}
// userResource implements the resource handling for a PostgreSQL Flex user.
type userResource struct {
client *postgresflex.APIClient
@ -73,7 +70,7 @@ func (r *userResource) ModifyPlan(
req resource.ModifyPlanRequest,
resp *resource.ModifyPlanResponse,
) { // nolint:gocritic // function signature required by Terraform
var configModel ResourceModel
var configModel resourceModel
// skip initial empty configuration to avoid follow-up errors
if req.Config.Raw.IsNull() {
return
@ -83,7 +80,7 @@ func (r *userResource) ModifyPlan(
return
}
var planModel ResourceModel
var planModel resourceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &planModel)...)
if resp.Diagnostics.HasError() {
return
@ -153,7 +150,7 @@ func (r *userResource) Create(
req resource.CreateRequest,
resp *resource.CreateResponse,
) { // nolint:gocritic // function signature required by Terraform
var model ResourceModel
var model resourceModel
diags := req.Plan.Get(ctx, &model)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
@ -252,7 +249,7 @@ func (r *userResource) Read(
req resource.ReadRequest,
resp *resource.ReadResponse,
) { // nolint:gocritic // function signature required by Terraform
var model ResourceModel
var model resourceModel
diags := req.State.Get(ctx, &model)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
@ -312,7 +309,7 @@ func (r *userResource) Update(
req resource.UpdateRequest,
resp *resource.UpdateResponse,
) { // nolint:gocritic // function signature required by Terraform
var model ResourceModel
var model resourceModel
diags := req.Plan.Get(ctx, &model)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
@ -342,7 +339,7 @@ func (r *userResource) Update(
ctx = core.InitProviderContext(ctx)
// Retrieve values from state
var stateModel ResourceModel
var stateModel resourceModel
diags = req.State.Get(ctx, &stateModel)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
@ -414,7 +411,7 @@ func (r *userResource) Delete(
req resource.DeleteRequest,
resp *resource.DeleteResponse,
) { // nolint:gocritic // function signature required by Terraform
var model ResourceModel
var model resourceModel
diags := req.State.Get(ctx, &model)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
@ -559,7 +556,7 @@ func (r *userResource) ImportState(
tflog.Info(ctx, "postgresflexalpha user state imported")
}
func mapFields(userResp *postgresflex.GetUserResponse, model *ResourceModel, region string) error {
func mapFields(userResp *postgresflex.GetUserResponse, model *resourceModel, region string) error {
if userResp == nil {
return fmt.Errorf("response is nil")
}
@ -576,9 +573,7 @@ func mapFields(userResp *postgresflex.GetUserResponse, model *ResourceModel, reg
} 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.StringPointerValue(user.Name)
@ -602,7 +597,7 @@ func mapFields(userResp *postgresflex.GetUserResponse, model *ResourceModel, reg
// 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 *ResourceModel, arg *clientArg) (bool, error) {
func (r *userResource) getUserResource(ctx context.Context, model *resourceModel, arg *clientArg) (bool, error) {
if arg.userId > math.MaxInt32 {
return false, errors.New("error in type conversion: int value too large (userId)")
@ -638,7 +633,7 @@ type clientArg struct {
// extractIdentityData extracts essential identifiers from the resource model, falling back to the identity model.
func (r *userResource) extractIdentityData(
model ResourceModel,
model resourceModel,
identity UserResourceIdentityModel,
) (*clientArg, error) {