feat: add project_id and region attributes to database data source schema and refactor user model mapping

This commit is contained in:
Andre_Harms 2026-02-09 15:38:54 +01:00
parent 1284bcc8a3
commit f0e7c19cdf
8 changed files with 51 additions and 630 deletions

View file

@ -178,23 +178,4 @@ func handleReadError(
http.StatusForbidden: fmt.Sprintf("Forbidden access to project %q.", projectId),
},
)
model.UserId = types.Int64Value(userId)
model.Username = types.StringPointerValue(user.Name)
if user.Roles == nil {
model.Roles = 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 = rolesSet
}
model.Region = types.StringValue(region)
model.Status = types.StringPointerValue(user.Status)
return nil
}

View file

@ -53,11 +53,8 @@ func mapDataSourceFields(userResp *postgresflex.GetUserResponse, model *DataSour
}
model.Id = types.Int64Value(userId)
model.Host = types.StringValue(user.GetHost())
model.Port = types.Int64Value(user.GetPort())
model.Region = types.StringValue(region)
model.Status = types.StringValue(user.GetStatus())
model.ConnectionString = types.StringValue(user.GetConnectionString())
return nil
}
@ -141,10 +138,7 @@ func mapResourceFields(userResp *postgresflex.GetUserResponse, model *ResourceMo
}
model.Roles = types.List(rolesSet)
}
model.Host = types.StringPointerValue(user.Host)
model.Port = types.Int64PointerValue(user.Port)
model.Region = types.StringValue(region)
model.Status = types.StringPointerValue(user.Status)
model.ConnectionString = types.StringPointerValue(user.ConnectionString)
return nil
}

View file

@ -27,17 +27,14 @@ func TestMapDataSourceFields(t *testing.T) {
testRegion,
DataSourceModel{
UserModel: data.UserModel{
Id: types.Int64Value(1),
UserId: types.Int64Value(1),
InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"),
Name: types.StringValue(""),
Roles: types.List(types.SetNull(types.StringType)),
Host: types.StringValue(""),
Port: types.Int64Value(0),
Status: types.StringValue(""),
Region: types.StringValue(testRegion),
ConnectionString: types.StringValue(""),
Id: types.Int64Value(1),
UserId: types.Int64Value(1),
InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"),
Name: types.StringValue(""),
Roles: types.List(types.SetNull(types.StringType)),
Status: types.StringValue(""),
Region: types.StringValue(testRegion),
},
TerraformID: types.StringValue("pid,region,iid,1"),
},
@ -52,8 +49,6 @@ func TestMapDataSourceFields(t *testing.T) {
"",
},
Name: utils.Ptr("username"),
Host: utils.Ptr("host"),
Port: utils.Ptr(int64(1234)),
},
testRegion,
DataSourceModel{
@ -73,11 +68,8 @@ func TestMapDataSourceFields(t *testing.T) {
},
),
),
Host: types.StringValue("host"),
Port: types.Int64Value(1234),
Region: types.StringValue(testRegion),
Status: types.StringValue(""),
ConnectionString: types.StringValue(""),
Region: types.StringValue(testRegion),
Status: types.StringValue(""),
},
TerraformID: types.StringValue("pid,region,iid,1"),
},
@ -86,28 +78,22 @@ func TestMapDataSourceFields(t *testing.T) {
{
"null_fields_and_int_conversions",
&postgresflex.GetUserResponse{
Id: utils.Ptr(int64(1)),
Roles: &[]postgresflex.UserRole{},
Name: nil,
Host: nil,
Port: utils.Ptr(int64(2123456789)),
Status: utils.Ptr("status"),
ConnectionString: utils.Ptr("connection_string"),
Id: utils.Ptr(int64(1)),
Roles: &[]postgresflex.UserRole{},
Name: nil,
Status: utils.Ptr("status"),
},
testRegion,
DataSourceModel{
UserModel: data.UserModel{
Id: types.Int64Value(1),
UserId: types.Int64Value(1),
InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"),
Name: types.StringValue(""),
Roles: types.List(types.SetValueMust(types.StringType, []attr.Value{})),
Host: types.StringValue(""),
Port: types.Int64Value(2123456789),
Region: types.StringValue(testRegion),
Status: types.StringValue("status"),
ConnectionString: types.StringValue("connection_string"),
Id: types.Int64Value(1),
UserId: types.Int64Value(1),
InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"),
Name: types.StringValue(""),
Roles: types.List(types.SetValueMust(types.StringType, []attr.Value{})),
Region: types.StringValue(testRegion),
Status: types.StringValue("status"),
},
TerraformID: types.StringValue("pid,region,iid,1"),
},
@ -186,8 +172,6 @@ func TestMapFieldsCreate(t *testing.T) {
Name: types.StringNull(),
Roles: types.List(types.SetNull(types.StringType)),
Password: types.StringNull(),
Host: types.StringNull(),
Port: types.Int64Null(),
Region: types.StringValue(testRegion),
Status: types.StringNull(),
ConnectionString: types.StringNull(),
@ -199,10 +183,9 @@ func TestMapFieldsCreate(t *testing.T) {
{
"simple_values",
&postgresflex.GetUserResponse{
Id: utils.Ptr(int64(1)),
Name: utils.Ptr("username"),
ConnectionString: utils.Ptr("connection_string"),
Status: utils.Ptr("status"),
Id: utils.Ptr(int64(1)),
Name: utils.Ptr("username"),
Status: utils.Ptr("status"),
},
testRegion,
ResourceModel{
@ -213,8 +196,6 @@ func TestMapFieldsCreate(t *testing.T) {
Name: types.StringValue("username"),
Roles: types.List(types.SetNull(types.StringType)),
Password: types.StringNull(),
Host: types.StringNull(),
Port: types.Int64Null(),
Region: types.StringValue(testRegion),
Status: types.StringValue("status"),
ConnectionString: types.StringValue("connection_string"),
@ -226,10 +207,9 @@ func TestMapFieldsCreate(t *testing.T) {
{
"null_fields_and_int_conversions",
&postgresflex.GetUserResponse{
Id: utils.Ptr(int64(1)),
Name: nil,
ConnectionString: nil,
Status: nil,
Id: utils.Ptr(int64(1)),
Name: nil,
Status: nil,
},
testRegion,
ResourceModel{
@ -240,8 +220,6 @@ func TestMapFieldsCreate(t *testing.T) {
Name: types.StringNull(),
Roles: types.List(types.SetNull(types.StringType)),
Password: types.StringNull(),
Host: types.StringNull(),
Port: types.Int64Null(),
Region: types.StringValue(testRegion),
Status: types.StringNull(),
ConnectionString: types.StringNull(),
@ -323,8 +301,6 @@ func TestMapFields(t *testing.T) {
ProjectId: types.StringValue("pid"),
Name: types.StringNull(),
Roles: types.List(types.SetNull(types.StringType)),
Host: types.StringNull(),
Port: types.Int64Null(),
Region: types.StringValue(testRegion),
Status: types.StringNull(),
ConnectionString: types.StringNull(),
@ -343,8 +319,6 @@ func TestMapFields(t *testing.T) {
"",
},
Name: utils.Ptr("username"),
Host: utils.Ptr("host"),
Port: utils.Ptr(int64(1234)),
},
testRegion,
ResourceModel{
@ -363,8 +337,6 @@ func TestMapFields(t *testing.T) {
},
),
),
Host: types.StringValue("host"),
Port: types.Int64Value(1234),
Region: types.StringValue(testRegion),
Status: types.StringNull(),
ConnectionString: types.StringNull(),
@ -378,8 +350,6 @@ func TestMapFields(t *testing.T) {
&postgresflex.GetUserResponse{
Id: utils.Ptr(int64(1)),
Name: nil,
Host: nil,
Port: utils.Ptr(int64(2123456789)),
},
testRegion,
ResourceModel{
@ -390,8 +360,6 @@ func TestMapFields(t *testing.T) {
ProjectId: types.StringValue("pid"),
Name: types.StringNull(),
Roles: types.List(types.SetNull(types.StringType)),
Host: types.StringNull(),
Port: types.Int64Value(2123456789),
Region: types.StringValue(testRegion),
Status: types.StringNull(),
ConnectionString: types.StringNull(),

View file

@ -10,6 +10,7 @@ import (
"strconv"
"strings"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/resource/identityschema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
@ -558,7 +559,7 @@ func (r *userResource) ImportState(
tflog.Info(ctx, "postgresflexalpha user state imported")
}
func mapFields(userResp *postgresflex.GetUserResponse, model *Model, region string) error {
func mapFields(userResp *postgresflex.GetUserResponse, model *ResourceModel, region string) error {
if userResp == nil {
return fmt.Errorf("response is nil")
}
@ -575,14 +576,14 @@ func mapFields(userResp *postgresflex.GetUserResponse, model *Model, region stri
} else {
return fmt.Errorf("user id not present")
}
model.Id = utils.BuildInternalTerraformId(
model.TerraformID = utils.BuildInternalTerraformId(
model.ProjectId.ValueString(), region, model.InstanceId.ValueString(), strconv.FormatInt(userId, 10),
)
model.UserId = types.Int64Value(userId)
model.Username = types.StringPointerValue(user.Name)
model.Name = types.StringPointerValue(user.Name)
if user.Roles == nil {
model.Roles = types.SetNull(types.StringType)
model.Roles = types.List(types.SetNull(types.StringType))
} else {
var roles []attr.Value
for _, role := range *user.Roles {
@ -592,7 +593,7 @@ func mapFields(userResp *postgresflex.GetUserResponse, model *Model, region stri
if diags.HasError() {
return fmt.Errorf("failed to map roles: %w", core.DiagsToError(diags))
}
model.Roles = rolesSet
model.Roles = types.List(rolesSet)
}
model.Region = types.StringValue(region)
model.Status = types.StringPointerValue(user.Status)