Feat/alpa postgres user (#5)

* chore: add stackit_postgresflexalpha_user resource

* chore: refactor postgresflex user resource to postgresflexalpha

* chore: refactor wait handlers and update API client interfaces for postgresflexalpha

* chore: add stackit_postgresflexalpha_user data source example

---------

Co-authored-by: Andre Harms <andre.harms@stackit.cloud>
This commit is contained in:
Marcel S. Henselin 2025-12-19 11:48:21 +01:00 committed by GitHub
parent 75e003ae9a
commit ce2f3fca00
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 765 additions and 581 deletions

View file

@ -1,6 +1,4 @@
// Copyright (c) STACKIT
package postgresflexa
package postgresflexalpha
import (
"testing"
@ -9,137 +7,138 @@ import (
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/stackitcloud/stackit-sdk-go/core/utils"
"github.com/stackitcloud/stackit-sdk-go/services/postgresflex"
"github.com/stackitcloud/terraform-provider-stackit/pkg/postgresflexalpha"
)
func TestMapFieldsCreate(t *testing.T) {
const testRegion = "region"
tests := []struct {
description string
input *postgresflex.CreateUserResponse
input *postgresflexalpha.CreateUserResponse
updateRoles *postgresflexalpha.UpdateUserRequestPayload
region string
expected Model
isValid bool
}{
{
"default_values",
&postgresflex.CreateUserResponse{
Item: &postgresflex.User{
Id: utils.Ptr("uid"),
Password: utils.Ptr(""),
},
&postgresflexalpha.CreateUserResponse{
Id: utils.Ptr(int64(1)),
Password: utils.Ptr(""),
},
&postgresflexalpha.UpdateUserRequestPayload{
Roles: &[]postgresflexalpha.UserRole{},
},
testRegion,
Model{
Id: types.StringValue("pid,region,iid,uid"),
UserId: types.StringValue("uid"),
InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"),
Username: types.StringNull(),
Roles: types.SetNull(types.StringType),
Password: types.StringValue(""),
Host: types.StringNull(),
Port: types.Int64Null(),
Uri: types.StringNull(),
Region: types.StringValue(testRegion),
Id: types.StringValue("pid,region,iid,1"),
UserId: types.Int64Value(1),
InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"),
Username: types.StringNull(),
Roles: types.SetValueMust(types.StringType, []attr.Value{}),
Password: types.StringValue(""),
Host: types.StringNull(),
Port: types.Int64Null(),
Uri: types.StringNull(),
Region: types.StringValue(testRegion),
Status: types.StringNull(),
ConnectionString: types.StringNull(),
},
true,
},
{
"simple_values",
&postgresflex.CreateUserResponse{
Item: &postgresflex.User{
Id: utils.Ptr("uid"),
Roles: &[]string{
"role_1",
"role_2",
"",
},
Username: utils.Ptr("username"),
Password: utils.Ptr("password"),
Host: utils.Ptr("host"),
Port: utils.Ptr(int64(1234)),
Uri: utils.Ptr("uri"),
},
&postgresflexalpha.CreateUserResponse{
Id: utils.Ptr(int64(1)),
Name: utils.Ptr("username"),
Password: utils.Ptr("password"),
ConnectionString: utils.Ptr("connection_string"),
Status: utils.Ptr("status"),
},
&postgresflexalpha.UpdateUserRequestPayload{
Roles: &[]postgresflexalpha.UserRole{},
},
testRegion,
Model{
Id: types.StringValue("pid,region,iid,uid"),
UserId: types.StringValue("uid"),
InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"),
Username: types.StringValue("username"),
Roles: types.SetValueMust(types.StringType, []attr.Value{
types.StringValue("role_1"),
types.StringValue("role_2"),
types.StringValue(""),
}),
Password: types.StringValue("password"),
Host: types.StringValue("host"),
Port: types.Int64Value(1234),
Uri: types.StringValue("uri"),
Region: types.StringValue(testRegion),
Id: types.StringValue("pid,region,iid,1"),
UserId: types.Int64Value(1),
InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"),
Username: types.StringValue("username"),
Roles: types.SetValueMust(types.StringType, []attr.Value{}),
Password: types.StringValue("password"),
Host: types.StringNull(),
Port: types.Int64Null(),
Uri: types.StringNull(),
Region: types.StringValue(testRegion),
Status: types.StringValue("status"),
ConnectionString: types.StringValue("connection_string"),
},
true,
},
{
"null_fields_and_int_conversions",
&postgresflex.CreateUserResponse{
Item: &postgresflex.User{
Id: utils.Ptr("uid"),
Roles: &[]string{},
Username: nil,
Password: utils.Ptr(""),
Host: nil,
Port: utils.Ptr(int64(2123456789)),
Uri: nil,
},
&postgresflexalpha.CreateUserResponse{
Id: utils.Ptr(int64(1)),
Name: nil,
Password: utils.Ptr(""),
ConnectionString: nil,
Status: nil,
},
&postgresflexalpha.UpdateUserRequestPayload{
Roles: &[]postgresflexalpha.UserRole{},
},
testRegion,
Model{
Id: types.StringValue("pid,region,iid,uid"),
UserId: types.StringValue("uid"),
InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"),
Username: types.StringNull(),
Roles: types.SetValueMust(types.StringType, []attr.Value{}),
Password: types.StringValue(""),
Host: types.StringNull(),
Port: types.Int64Value(2123456789),
Uri: types.StringNull(),
Region: types.StringValue(testRegion),
Id: types.StringValue("pid,region,iid,1"),
UserId: types.Int64Value(1),
InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"),
Username: types.StringNull(),
Roles: types.SetValueMust(types.StringType, []attr.Value{}),
Password: types.StringValue(""),
Host: types.StringNull(),
Port: types.Int64Null(),
Uri: types.StringNull(),
Region: types.StringValue(testRegion),
Status: types.StringNull(),
ConnectionString: types.StringNull(),
},
true,
},
{
"nil_response",
nil,
nil,
testRegion,
Model{},
false,
},
{
"nil_response_2",
&postgresflex.CreateUserResponse{},
&postgresflexalpha.CreateUserResponse{},
&postgresflexalpha.UpdateUserRequestPayload{},
testRegion,
Model{},
false,
},
{
"no_resource_id",
&postgresflex.CreateUserResponse{
Item: &postgresflex.User{},
},
&postgresflexalpha.CreateUserResponse{},
&postgresflexalpha.UpdateUserRequestPayload{},
testRegion,
Model{},
false,
},
{
"no_password",
&postgresflex.CreateUserResponse{
Item: &postgresflex.User{
Id: utils.Ptr("uid"),
},
&postgresflexalpha.CreateUserResponse{
Id: utils.Ptr(int64(1)),
},
&postgresflexalpha.UpdateUserRequestPayload{
Roles: &[]postgresflexalpha.UserRole{},
},
testRegion,
Model{},
@ -147,25 +146,31 @@ func TestMapFieldsCreate(t *testing.T) {
},
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
state := &Model{
ProjectId: tt.expected.ProjectId,
InstanceId: tt.expected.InstanceId,
}
err := mapFieldsCreate(tt.input, state, tt.region)
if !tt.isValid && err == nil {
t.Fatalf("Should have failed")
}
if tt.isValid && err != nil {
t.Fatalf("Should not have failed: %v", err)
}
if tt.isValid {
diff := cmp.Diff(state, &tt.expected)
if diff != "" {
t.Fatalf("Data does not match: %s", diff)
t.Run(
tt.description, func(t *testing.T) {
state := &Model{
ProjectId: tt.expected.ProjectId,
InstanceId: tt.expected.InstanceId,
}
}
})
var roles *[]postgresflexalpha.UserRole
if tt.updateRoles != nil {
roles = tt.updateRoles.Roles
}
err := mapFieldsCreate(tt.input, roles, state, tt.region)
if !tt.isValid && err == nil {
t.Fatalf("Should have failed")
}
if tt.isValid && err != nil {
t.Fatalf("Should not have failed: %v", err)
}
if tt.isValid {
diff := cmp.Diff(state, &tt.expected)
if diff != "" {
t.Fatalf("Data does not match: %s", diff)
}
}
},
)
}
}
@ -173,84 +178,85 @@ func TestMapFields(t *testing.T) {
const testRegion = "region"
tests := []struct {
description string
input *postgresflex.GetUserResponse
input *postgresflexalpha.GetUserResponse
region string
expected Model
isValid bool
}{
{
"default_values",
&postgresflex.GetUserResponse{
Item: &postgresflex.UserResponse{},
},
&postgresflexalpha.GetUserResponse{},
testRegion,
Model{
Id: types.StringValue("pid,region,iid,uid"),
UserId: types.StringValue("uid"),
InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"),
Username: types.StringNull(),
Roles: types.SetNull(types.StringType),
Host: types.StringNull(),
Port: types.Int64Null(),
Region: types.StringValue(testRegion),
Id: types.StringValue("pid,region,iid,1"),
UserId: types.Int64Value(int64(1)),
InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"),
Username: types.StringNull(),
Roles: types.SetNull(types.StringType),
Host: types.StringNull(),
Port: types.Int64Null(),
Region: types.StringValue(testRegion),
Status: types.StringNull(),
ConnectionString: types.StringNull(),
},
true,
},
{
"simple_values",
&postgresflex.GetUserResponse{
Item: &postgresflex.UserResponse{
Roles: &[]string{
"role_1",
"role_2",
"",
},
Username: utils.Ptr("username"),
Host: utils.Ptr("host"),
Port: utils.Ptr(int64(1234)),
&postgresflexalpha.GetUserResponse{
Roles: &[]postgresflexalpha.UserRole{
"role_1",
"role_2",
"",
},
Name: utils.Ptr("username"),
Host: utils.Ptr("host"),
Port: utils.Ptr(int64(1234)),
},
testRegion,
Model{
Id: types.StringValue("pid,region,iid,uid"),
UserId: types.StringValue("uid"),
Id: types.StringValue("pid,region,iid,1"),
UserId: types.Int64Value(1),
InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"),
Username: types.StringValue("username"),
Roles: types.SetValueMust(types.StringType, []attr.Value{
types.StringValue("role_1"),
types.StringValue("role_2"),
types.StringValue(""),
}),
Host: types.StringValue("host"),
Port: types.Int64Value(1234),
Region: types.StringValue(testRegion),
Roles: types.SetValueMust(
types.StringType, []attr.Value{
types.StringValue("role_1"),
types.StringValue("role_2"),
types.StringValue(""),
},
),
Host: types.StringValue("host"),
Port: types.Int64Value(1234),
Region: types.StringValue(testRegion),
Status: types.StringNull(),
ConnectionString: types.StringNull(),
},
true,
},
{
"null_fields_and_int_conversions",
&postgresflex.GetUserResponse{
Item: &postgresflex.UserResponse{
Id: utils.Ptr("uid"),
Roles: &[]string{},
Username: nil,
Host: nil,
Port: utils.Ptr(int64(2123456789)),
},
&postgresflexalpha.GetUserResponse{
Id: utils.Ptr(int64(1)),
Name: nil,
Host: nil,
Port: utils.Ptr(int64(2123456789)),
},
testRegion,
Model{
Id: types.StringValue("pid,region,iid,uid"),
UserId: types.StringValue("uid"),
InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"),
Username: types.StringNull(),
Roles: types.SetValueMust(types.StringType, []attr.Value{}),
Host: types.StringNull(),
Port: types.Int64Value(2123456789),
Region: types.StringValue(testRegion),
Id: types.StringValue("pid,region,iid,1"),
UserId: types.Int64Value(1),
InstanceId: types.StringValue("iid"),
ProjectId: types.StringValue("pid"),
Username: types.StringNull(),
Roles: types.SetValueMust(types.StringType, []attr.Value{}),
Host: types.StringNull(),
Port: types.Int64Value(2123456789),
Region: types.StringValue(testRegion),
Status: types.StringNull(),
ConnectionString: types.StringNull(),
},
true,
},
@ -263,42 +269,42 @@ func TestMapFields(t *testing.T) {
},
{
"nil_response_2",
&postgresflex.GetUserResponse{},
&postgresflexalpha.GetUserResponse{},
testRegion,
Model{},
false,
},
{
"no_resource_id",
&postgresflex.GetUserResponse{
Item: &postgresflex.UserResponse{},
},
&postgresflexalpha.GetUserResponse{},
testRegion,
Model{},
false,
},
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
state := &Model{
ProjectId: tt.expected.ProjectId,
InstanceId: tt.expected.InstanceId,
UserId: tt.expected.UserId,
}
err := mapFields(tt.input, state, tt.region)
if !tt.isValid && err == nil {
t.Fatalf("Should have failed")
}
if tt.isValid && err != nil {
t.Fatalf("Should not have failed: %v", err)
}
if tt.isValid {
diff := cmp.Diff(state, &tt.expected)
if diff != "" {
t.Fatalf("Data does not match: %s", diff)
t.Run(
tt.description, func(t *testing.T) {
state := &Model{
ProjectId: tt.expected.ProjectId,
InstanceId: tt.expected.InstanceId,
UserId: tt.expected.UserId,
}
}
})
err := mapFields(tt.input, state, tt.region)
if !tt.isValid && err == nil {
t.Fatalf("Should have failed")
}
if tt.isValid && err != nil {
t.Fatalf("Should not have failed: %v", err)
}
if tt.isValid {
diff := cmp.Diff(state, &tt.expected)
if diff != "" {
t.Fatalf("Data does not match: %s", diff)
}
}
},
)
}
}
@ -306,35 +312,35 @@ func TestToCreatePayload(t *testing.T) {
tests := []struct {
description string
input *Model
inputRoles []string
expected *postgresflex.CreateUserPayload
inputRoles *[]string
expected *postgresflexalpha.CreateUserRequestPayload
isValid bool
}{
{
"default_values",
&Model{},
[]string{},
&postgresflex.CreateUserPayload{
Roles: &[]string{},
Username: nil,
&[]string{},
&postgresflexalpha.CreateUserRequestPayload{
Name: nil,
Roles: &[]postgresflexalpha.UserRole{},
},
true,
},
{
"default_values",
"simple_values",
&Model{
Username: types.StringValue("username"),
},
[]string{
&[]string{
"role_1",
"role_2",
},
&postgresflex.CreateUserPayload{
Roles: &[]string{
&postgresflexalpha.CreateUserRequestPayload{
Name: utils.Ptr("username"),
Roles: &[]postgresflexalpha.UserRole{
"role_1",
"role_2",
},
Username: utils.Ptr("username"),
},
true,
},
@ -343,21 +349,21 @@ func TestToCreatePayload(t *testing.T) {
&Model{
Username: types.StringNull(),
},
[]string{
&[]string{
"",
},
&postgresflex.CreateUserPayload{
Roles: &[]string{
&postgresflexalpha.CreateUserRequestPayload{
Roles: &[]postgresflexalpha.UserRole{
"",
},
Username: nil,
Name: nil,
},
true,
},
{
"nil_model",
nil,
[]string{},
&[]string{},
nil,
false,
},
@ -370,21 +376,23 @@ func TestToCreatePayload(t *testing.T) {
},
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
output, err := toCreatePayload(tt.input, tt.inputRoles)
if !tt.isValid && err == nil {
t.Fatalf("Should have failed")
}
if tt.isValid && err != nil {
t.Fatalf("Should not have failed: %v", err)
}
if tt.isValid {
diff := cmp.Diff(output, tt.expected)
if diff != "" {
t.Fatalf("Data does not match: %s", diff)
t.Run(
tt.description, func(t *testing.T) {
output, err := toCreatePayload(tt.input, tt.inputRoles)
if !tt.isValid && err == nil {
t.Fatalf("Should have failed")
}
}
})
if tt.isValid && err != nil {
t.Fatalf("Should not have failed: %v", err)
}
if tt.isValid {
diff := cmp.Diff(output, tt.expected)
if diff != "" {
t.Fatalf("Data does not match: %s", diff)
}
}
},
)
}
}
@ -392,16 +400,16 @@ func TestToUpdatePayload(t *testing.T) {
tests := []struct {
description string
input *Model
inputRoles []string
expected *postgresflex.UpdateUserPayload
inputRoles *[]string
expected *postgresflexalpha.UpdateUserRequestPayload
isValid bool
}{
{
"default_values",
&Model{},
[]string{},
&postgresflex.UpdateUserPayload{
Roles: &[]string{},
&[]string{},
&postgresflexalpha.UpdateUserRequestPayload{
Roles: &[]postgresflexalpha.UserRole{},
},
true,
},
@ -410,12 +418,12 @@ func TestToUpdatePayload(t *testing.T) {
&Model{
Username: types.StringValue("username"),
},
[]string{
&[]string{
"role_1",
"role_2",
},
&postgresflex.UpdateUserPayload{
Roles: &[]string{
&postgresflexalpha.UpdateUserRequestPayload{
Roles: &[]postgresflexalpha.UserRole{
"role_1",
"role_2",
},
@ -427,11 +435,11 @@ func TestToUpdatePayload(t *testing.T) {
&Model{
Username: types.StringNull(),
},
[]string{
&[]string{
"",
},
&postgresflex.UpdateUserPayload{
Roles: &[]string{
&postgresflexalpha.UpdateUserRequestPayload{
Roles: &[]postgresflexalpha.UserRole{
"",
},
},
@ -440,7 +448,7 @@ func TestToUpdatePayload(t *testing.T) {
{
"nil_model",
nil,
[]string{},
&[]string{},
nil,
false,
},
@ -453,20 +461,22 @@ func TestToUpdatePayload(t *testing.T) {
},
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
output, err := toUpdatePayload(tt.input, tt.inputRoles)
if !tt.isValid && err == nil {
t.Fatalf("Should have failed")
}
if tt.isValid && err != nil {
t.Fatalf("Should not have failed: %v", err)
}
if tt.isValid {
diff := cmp.Diff(output, tt.expected)
if diff != "" {
t.Fatalf("Data does not match: %s", diff)
t.Run(
tt.description, func(t *testing.T) {
output, err := toUpdatePayload(tt.input, tt.inputRoles)
if !tt.isValid && err == nil {
t.Fatalf("Should have failed")
}
}
})
if tt.isValid && err != nil {
t.Fatalf("Should not have failed: %v", err)
}
if tt.isValid {
diff := cmp.Diff(output, tt.expected)
if diff != "" {
t.Fatalf("Data does not match: %s", diff)
}
}
},
)
}
}