chore: changed and refactored providers #36

Merged
marcel.henselin merged 31 commits from feat/alpha_user_database into alpha 2026-02-10 08:10:03 +00:00
2 changed files with 206 additions and 92 deletions
Showing only changes of commit c96328060d - Show all commits

View file

@ -21,7 +21,7 @@ func mapFields(
return fmt.Errorf("id not present") return fmt.Errorf("id not present")
} }
if model == nil { if model == nil {
return fmt.Errorf("model input is nil") return fmt.Errorf("model given is nil")
} }
var databaseId int64 var databaseId int64
@ -36,6 +36,7 @@ func mapFields(
model.Id = types.Int64Value(databaseId) model.Id = types.Int64Value(databaseId)
model.Name = types.StringPointerValue(source.Name) model.Name = types.StringPointerValue(source.Name)
model.Owner = types.StringPointerValue(cleanString(source.Owner)) model.Owner = types.StringPointerValue(cleanString(source.Owner))
model.Region = types.StringValue(region)
return nil return nil
} }

View file

@ -5,153 +5,266 @@ import (
"github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-framework/types" "github.com/hashicorp/terraform-plugin-framework/types"
"github.com/stackitcloud/stackit-sdk-go/core/utils"
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/postgresflexalpha" "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/postgresflexalpha"
postgresflexalpha2 "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/postgresflexalpha/database/datasources_gen" datasource "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/postgresflexalpha/database/datasources_gen"
resource "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/postgresflexalpha/database/resources_gen"
) )
func TestMapFields(t *testing.T) { func TestMapFields(t *testing.T) {
type input struct { type given struct {
resp *postgresflexalpha.ListDatabase source *postgresflexalpha.ListDatabase
model *postgresflexalpha2.DatabaseModel model *DataSourceModel
region string region string
} }
type expected struct { type expected struct {
model *postgresflexalpha2.DatabaseModel model *DataSourceModel
err bool err bool
} }
testcases := []struct { testcases := []struct {
name string name string
given input given given
want expected expected expected
}{ }{
{ {
name: "should map fields correctly when given a valid response", name: "should map fields correctly",
given: input{ given: given{
resp: &postgresflexalpha.ListDatabase{ source: &postgresflexalpha.ListDatabase{
Id: ptr(int64(123)), Id: utils.Ptr(int64(1)),
Name: ptr("my-db"), Name: utils.Ptr("my-db"),
Owner: ptr("\"my-owner\""), Owner: utils.Ptr("\"my-owner\""),
}, },
model: &postgresflexalpha2.DatabaseModel{}, model: &DataSourceModel{},
region: "eu01", region: "eu01",
}, },
want: expected{ expected: expected{
model: &postgresflexalpha2.DatabaseModel{ model: &DataSourceModel{
Id: types.Int64Value(123), DatabaseModel: datasource.DatabaseModel{
Name: types.StringValue("my-db"), Id: types.Int64Value(1),
Name: types.StringValue("my-db"),
Owner: types.StringValue("my-owner"),
},
Region: types.StringValue("eu01"), Region: types.StringValue("eu01"),
Owner: types.StringValue("my-owner"),
}, },
err: false,
}, },
}, },
{ {
name: "should use existing model ID if present", name: "should preserve existing model ID",
given: input{ given: given{
resp: &postgresflexalpha.ListDatabase{ source: &postgresflexalpha.ListDatabase{
Id: ptr(int64(456)), Id: utils.Ptr(int64(1)),
Name: ptr("my-db"), Name: utils.Ptr("my-db"),
}, },
model: &postgresflexalpha2.DatabaseModel{ model: &DataSourceModel{
Id: types.Int64Value(789), DatabaseModel: datasource.DatabaseModel{
Id: types.Int64Value(1),
},
}, },
region: "eu01", region: "eu01",
}, },
want: expected{ expected: expected{
model: &postgresflexalpha2.DatabaseModel{ model: &DataSourceModel{
Id: types.Int64Value(789), DatabaseModel: datasource.DatabaseModel{
Name: types.StringValue("my-db"), Id: types.Int64Value(1),
Name: types.StringValue("my-db"),
Owner: types.StringNull(),
},
Region: types.StringValue("eu01"), Region: types.StringValue("eu01"),
Owner: types.StringNull(),
}, },
err: false,
}, },
}, },
{ {
name: "should return an error when response is nil", name: "should fail on nil source",
given: input{ given: given{
resp: nil, source: nil,
model: &postgresflexalpha2.DatabaseModel{}, model: &DataSourceModel{},
},
want: expected{
err: true,
}, },
expected: expected{err: true},
}, },
{ {
name: "should return an error when response ID is nil", name: "should fail on nil source ID",
given: input{ given: given{
resp: &postgresflexalpha.ListDatabase{ source: &postgresflexalpha.ListDatabase{Id: nil},
Id: nil, model: &DataSourceModel{},
},
model: &postgresflexalpha2.DatabaseModel{},
},
want: expected{
err: true,
}, },
expected: expected{err: true},
}, },
{ {
name: "should return an error when model is nil", name: "should fail on nil model",
given: input{ given: given{
resp: &postgresflexalpha.ListDatabase{ source: &postgresflexalpha.ListDatabase{Id: utils.Ptr(int64(1))},
Id: ptr(int64(123)), model: nil,
},
model: nil,
},
want: expected{
err: true,
}, },
expected: expected{err: true},
}, },
} }
for _, tc := range testcases { for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) { t.Run(
err := mapFields(tc.given.resp, tc.given.model, tc.given.region) tc.name, func(t *testing.T) {
if (err != nil) != tc.want.err { err := mapFields(tc.given.source, tc.given.model, tc.given.region)
t.Fatalf("expected error: %v, got: %v", tc.want.err, err) if (err != nil) != tc.expected.err {
} t.Fatalf("expected error: %v, got: %v", tc.expected.err, err)
if err == nil {
if diff := cmp.Diff(tc.want.model, tc.given.model); diff != "" {
t.Errorf("model mismatch (-want +got):\n%s", diff)
} }
} if err == nil {
}) if diff := cmp.Diff(tc.expected.model, tc.given.model); diff != "" {
t.Errorf("model mismatch (-want +got):\n%s", diff)
}
}
},
)
}
}
func TestMapResourceFields(t *testing.T) {
type given struct {
source *postgresflexalpha.ListDatabase
model *ResourceModel
}
type expected struct {
model *ResourceModel
err bool
}
testcases := []struct {
name string
given given
expected expected
}{
{
name: "should map fields correctly",
given: given{
source: &postgresflexalpha.ListDatabase{
Id: utils.Ptr(int64(1)),
Name: utils.Ptr("my-db"),
Owner: utils.Ptr("\"my-owner\""),
},
model: &ResourceModel{},
},
expected: expected{
model: &ResourceModel{
DatabaseModel: resource.DatabaseModel{
Id: types.Int64Value(1),
Name: types.StringValue("my-db"),
Owner: types.StringValue("my-owner"),
},
},
},
},
{
name: "should fail on nil source",
given: given{
source: nil,
model: &ResourceModel{},
},
expected: expected{err: true},
},
}
for _, tc := range testcases {
t.Run(
tc.name, func(t *testing.T) {
err := mapResourceFields(tc.given.source, tc.given.model)
if (err != nil) != tc.expected.err {
t.Fatalf("expected error: %v, got: %v", tc.expected.err, err)
}
if err == nil {
if diff := cmp.Diff(tc.expected.model, tc.given.model); diff != "" {
t.Errorf("model mismatch (-want +got):\n%s", diff)
}
}
},
)
}
}
func TestToCreatePayload(t *testing.T) {
type given struct {
model *ResourceModel
}
type expected struct {
payload *postgresflexalpha.CreateDatabaseRequestPayload
err bool
}
testcases := []struct {
name string
given given
expected expected
}{
{
name: "should convert model to payload",
given: given{
model: &ResourceModel{
DatabaseModel: resource.DatabaseModel{
Name: types.StringValue("my-db"),
Owner: types.StringValue("my-owner"),
},
},
},
expected: expected{
payload: &postgresflexalpha.CreateDatabaseRequestPayload{
Name: utils.Ptr("my-db"),
Owner: utils.Ptr("my-owner"),
},
},
},
{
name: "should fail on nil model",
given: given{model: nil},
expected: expected{err: true},
},
}
for _, tc := range testcases {
t.Run(
tc.name, func(t *testing.T) {
actual, err := toCreatePayload(tc.given.model)
if (err != nil) != tc.expected.err {
t.Fatalf("expected error: %v, got: %v", tc.expected.err, err)
}
if err == nil {
if diff := cmp.Diff(tc.expected.payload, actual); diff != "" {
t.Errorf("payload mismatch (-want +got):\n%s", diff)
}
}
},
)
} }
} }
func TestCleanString(t *testing.T) { func TestCleanString(t *testing.T) {
testcases := []struct { testcases := []struct {
name string name string
given *string given *string
want *string expected *string
}{ }{
{ {
name: "should remove quotes from a string", name: "should remove quotes",
given: ptr("\"quoted\""), given: utils.Ptr("\"quoted\""),
want: ptr("quoted"), expected: utils.Ptr("quoted"),
}, },
{ {
name: "should return nil for a nil input", name: "should handle nil",
given: nil, given: nil,
want: nil, expected: nil,
}, },
{ {
name: "should not change a string without quotes", name: "should not change unquoted string",
given: ptr("unquoted"), given: utils.Ptr("unquoted"),
want: ptr("unquoted"), expected: utils.Ptr("unquoted"),
}, },
} }
for _, tc := range testcases { for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) { t.Run(
actual := cleanString(tc.given) tc.name, func(t *testing.T) {
if (actual == nil && tc.want != nil) || (actual != nil && tc.want == nil) || (actual != nil && *actual != *tc.want) { actual := cleanString(tc.given)
t.Errorf("expected %v, got %v", tc.want, actual) if diff := cmp.Diff(tc.expected, actual); diff != "" {
} t.Errorf("string mismatch (-want +got):\n%s", diff)
}) }
},
)
} }
} }
func ptr[T any](v T) *T {
return &v
}