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,27 +7,25 @@ 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 TestMapDataSourceFields(t *testing.T) {
const testRegion = "region"
tests := []struct {
description string
input *postgresflex.GetUserResponse
input *postgresflexalpha.GetUserResponse
region string
expected DataSourceModel
isValid bool
}{
{
"default_values",
&postgresflex.GetUserResponse{
Item: &postgresflex.UserResponse{},
},
&postgresflexalpha.GetUserResponse{},
testRegion,
DataSourceModel{
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.StringNull(),
@ -42,58 +38,62 @@ func TestMapDataSourceFields(t *testing.T) {
},
{
"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,
DataSourceModel{
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)),
Roles: &[]postgresflexalpha.UserRole{},
Name: nil,
Host: nil,
Port: utils.Ptr(int64(2123456789)),
Status: utils.Ptr("status"),
ConnectionString: utils.Ptr("connection_string"),
},
testRegion,
DataSourceModel{
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.StringValue("status"),
ConnectionString: types.StringValue("connection_string"),
},
true,
},
@ -106,41 +106,41 @@ func TestMapDataSourceFields(t *testing.T) {
},
{
"nil_response_2",
&postgresflex.GetUserResponse{},
&postgresflexalpha.GetUserResponse{},
testRegion,
DataSourceModel{},
false,
},
{
"no_resource_id",
&postgresflex.GetUserResponse{
Item: &postgresflex.UserResponse{},
},
&postgresflexalpha.GetUserResponse{},
testRegion,
DataSourceModel{},
false,
},
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
state := &DataSourceModel{
ProjectId: tt.expected.ProjectId,
InstanceId: tt.expected.InstanceId,
UserId: tt.expected.UserId,
}
err := mapDataSourceFields(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 := &DataSourceModel{
ProjectId: tt.expected.ProjectId,
InstanceId: tt.expected.InstanceId,
UserId: tt.expected.UserId,
}
}
})
err := mapDataSourceFields(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)
}
}
},
)
}
}