Some checks failed
CI Workflow / Code coverage report (pull_request) Has been skipped
Publish / Check GoReleaser config (push) Successful in 9s
Publish / Publish provider (push) Successful in 30m38s
CI Workflow / Check GoReleaser config (pull_request) Successful in 6s
CI Workflow / CI (pull_request) Failing after 22m26s
CI Workflow / Test readiness for publishing provider (pull_request) Successful in 37m30s
## Description
<!-- **Please link some issue here describing what you are trying to achieve.**
In case there is no issue present for your PR, please consider creating one.
At least please give us some description what you are trying to achieve and why your change is needed. -->
relates to #1234
## Checklist
- [ ] Issue was linked above
- [ ] Code format was applied: `make fmt`
- [ ] Examples were added / adjusted (see `examples/` directory)
- [x] Docs are up-to-date: `make generate-docs` (will be checked by CI)
- [ ] Unit tests got implemented or updated
- [ ] Acceptance tests got implemented or updated (see e.g. [here](f5f99d1709/stackit/internal/services/dns/dns_acc_test.go))
- [x] Unit tests are passing: `make test` (will be checked by CI)
- [x] No linter issues: `make lint` (will be checked by CI)
Reviewed-on: #54
Co-authored-by: Marcel S. Henselin <marcel.henselin@stackit.cloud>
Co-committed-by: Marcel S. Henselin <marcel.henselin@stackit.cloud>
92 lines
2.7 KiB
Go
92 lines
2.7 KiB
Go
package postgresflexalpha
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
|
|
"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/stackit/internal/utils"
|
|
)
|
|
|
|
// mapFields maps fields from a ListDatabase API response to a resourceModel for the data source.
|
|
func mapFields(
|
|
source *postgresflexalpha.ListDatabase,
|
|
model *dataSourceModel,
|
|
region string,
|
|
) 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 given is nil")
|
|
}
|
|
|
|
var databaseId int64
|
|
if model.DatabaseId.ValueInt64() != 0 {
|
|
databaseId = model.DatabaseId.ValueInt64()
|
|
} else if source.Id != nil {
|
|
databaseId = *source.Id
|
|
} else {
|
|
return fmt.Errorf("database id not present")
|
|
}
|
|
|
|
model.Id = types.Int64Value(databaseId)
|
|
model.DatabaseId = types.Int64Value(databaseId)
|
|
model.Name = types.StringValue(source.GetName())
|
|
model.Owner = types.StringPointerValue(cleanString(source.Owner))
|
|
model.Region = types.StringValue(region)
|
|
model.ProjectId = types.StringValue(model.ProjectId.ValueString())
|
|
model.InstanceId = types.StringValue(model.InstanceId.ValueString())
|
|
model.TerraformID = utils.BuildInternalTerraformId(
|
|
model.ProjectId.ValueString(),
|
|
region,
|
|
model.InstanceId.ValueString(),
|
|
strconv.FormatInt(databaseId, 10),
|
|
)
|
|
|
|
return nil
|
|
}
|
|
|
|
// mapResourceFields maps fields from a GetDatabase API response to a resourceModel for the resource.
|
|
func mapResourceFields(source *postgresflexalpha.GetDatabaseResponse, 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.DatabaseId = types.Int64Value(databaseId)
|
|
model.Name = types.StringValue(source.GetName())
|
|
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
|
|
}
|