feat: enhance mapping functions for ListDatabase API response and improve error handling
This commit is contained in:
parent
4830d6196d
commit
936ad54f95
1 changed files with 53 additions and 11 deletions
|
|
@ -6,15 +6,18 @@ import (
|
||||||
|
|
||||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||||
"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"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// mapFields maps fields from a ListDatabase API response to a DatabaseModel for the data source.
|
// mapFields maps fields from a ListDatabase API response to a ResourceModel for the data source.
|
||||||
func mapFields(resp *postgresflexalpha.ListDatabase, model *postgresflexalpha2.DatabaseModel, region string) error {
|
func mapFields(
|
||||||
if resp == nil {
|
source *postgresflexalpha.ListDatabase,
|
||||||
|
model *DataSourceModel,
|
||||||
|
region string,
|
||||||
|
) error {
|
||||||
|
if source == nil {
|
||||||
return fmt.Errorf("response is nil")
|
return fmt.Errorf("response is nil")
|
||||||
}
|
}
|
||||||
if resp.Id == nil || *resp.Id == 0 {
|
if source.Id == nil || *source.Id == 0 {
|
||||||
return fmt.Errorf("id not present")
|
return fmt.Errorf("id not present")
|
||||||
}
|
}
|
||||||
if model == nil {
|
if model == nil {
|
||||||
|
|
@ -24,20 +27,59 @@ func mapFields(resp *postgresflexalpha.ListDatabase, model *postgresflexalpha2.D
|
||||||
var databaseId int64
|
var databaseId int64
|
||||||
if model.Id.ValueInt64() != 0 {
|
if model.Id.ValueInt64() != 0 {
|
||||||
databaseId = model.Id.ValueInt64()
|
databaseId = model.Id.ValueInt64()
|
||||||
} else if resp.Id != nil {
|
} else if source.Id != nil {
|
||||||
databaseId = *resp.Id
|
databaseId = *source.Id
|
||||||
} else {
|
} else {
|
||||||
return fmt.Errorf("database id not present")
|
return fmt.Errorf("database id not present")
|
||||||
}
|
}
|
||||||
|
|
||||||
model.Id = types.Int64Value(databaseId)
|
model.Id = types.Int64Value(databaseId)
|
||||||
model.Name = types.StringPointerValue(resp.Name)
|
model.Name = types.StringPointerValue(source.Name)
|
||||||
model.Region = types.StringValue(region)
|
model.Owner = types.StringPointerValue(cleanString(source.Owner))
|
||||||
model.Owner = types.StringPointerValue(cleanString(resp.Owner))
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// cleanString removes leading and trailing quotes from a string pointer.
|
// mapResourceFields maps fields from a ListDatabase API response to a ResourceModel for the resource.
|
||||||
|
func mapResourceFields(source *postgresflexalpha.ListDatabase, model *ResourceModel) error {
|
||||||
|
if source == nil {
|
||||||
|
return fmt.Errorf("response is nil")
|
||||||
|
}
|
||||||
|
if source.Id == nil || *source.Id == 0 {
|
||||||
|
return fmt.Errorf("id not present")
|
||||||
|
}
|
||||||
|
if model == nil {
|
||||||
|
return fmt.Errorf("model input is nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
var databaseId int64
|
||||||
|
if model.Id.ValueInt64() != 0 {
|
||||||
|
databaseId = model.Id.ValueInt64()
|
||||||
|
} else if source.Id != nil {
|
||||||
|
databaseId = *source.Id
|
||||||
|
} else {
|
||||||
|
return fmt.Errorf("database id not present")
|
||||||
|
}
|
||||||
|
|
||||||
|
model.Id = types.Int64Value(databaseId)
|
||||||
|
model.Name = types.StringPointerValue(source.Name)
|
||||||
|
model.Owner = types.StringPointerValue(cleanString(source.Owner))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// toCreatePayload converts the resource model to an API create payload.
|
||||||
|
func toCreatePayload(model *ResourceModel) (*postgresflexalpha.CreateDatabaseRequestPayload, error) {
|
||||||
|
if model == nil {
|
||||||
|
return nil, fmt.Errorf("nil model")
|
||||||
|
}
|
||||||
|
|
||||||
|
return &postgresflexalpha.CreateDatabaseRequestPayload{
|
||||||
|
Name: model.Name.ValueStringPointer(),
|
||||||
|
Owner: model.Owner.ValueStringPointer(),
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// cleanString removes leading and trailing quotes which are sometimes returned by the API.
|
||||||
func cleanString(s *string) *string {
|
func cleanString(s *string) *string {
|
||||||
if s == nil {
|
if s == nil {
|
||||||
return nil
|
return nil
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue