47 lines
1.4 KiB
Go
47 lines
1.4 KiB
Go
package postgresflexalpha
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/hashicorp/terraform-plugin-framework/types"
|
|
"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.
|
|
func mapFields(resp *postgresflexalpha.ListDatabase, model *postgresflexalpha2.DatabaseModel, region string) error {
|
|
if resp == nil {
|
|
return fmt.Errorf("response is nil")
|
|
}
|
|
if resp.Id == nil || *resp.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 resp.Id != nil {
|
|
databaseId = *resp.Id
|
|
} else {
|
|
return fmt.Errorf("database id not present")
|
|
}
|
|
|
|
model.Id = types.Int64Value(databaseId)
|
|
model.Name = types.StringPointerValue(resp.Name)
|
|
model.Region = types.StringValue(region)
|
|
model.Owner = types.StringPointerValue(cleanString(resp.Owner))
|
|
return nil
|
|
}
|
|
|
|
// cleanString removes leading and trailing quotes from a string pointer.
|
|
func cleanString(s *string) *string {
|
|
if s == nil {
|
|
return nil
|
|
}
|
|
res := strings.Trim(*s, "\"")
|
|
return &res
|
|
}
|