feat: refactor data source models and update mapping functions for improved consistency
This commit is contained in:
parent
f0e7c19cdf
commit
184e133a2a
34 changed files with 980 additions and 1017 deletions
|
|
@ -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,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue