## 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: #46
Reviewed-by: Marcel_Henselin <marcel.henselin@stackit.cloud>
Co-authored-by: Andre Harms <andre.harms@stackit.cloud>
Co-committed-by: Andre Harms <andre.harms@stackit.cloud>
97 lines
3.2 KiB
Go
97 lines
3.2 KiB
Go
package sqlserverflexalpha
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/hashicorp/terraform-plugin-framework/types"
|
|
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/sqlserverflexalpha"
|
|
"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 *sqlserverflexalpha.GetDatabaseResponse, 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.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.DatabaseName = types.StringValue(source.GetName())
|
|
model.Name = types.StringValue(source.GetName())
|
|
model.Owner = types.StringValue(strings.Trim(source.GetOwner(), "\""))
|
|
model.Region = types.StringValue(region)
|
|
model.ProjectId = types.StringValue(model.ProjectId.ValueString())
|
|
model.InstanceId = types.StringValue(model.InstanceId.ValueString())
|
|
model.CompatibilityLevel = types.Int64Value(source.GetCompatibilityLevel())
|
|
model.CollationName = types.StringValue(source.GetCollationName())
|
|
|
|
model.TerraformID = utils.BuildInternalTerraformId(
|
|
model.ProjectId.ValueString(),
|
|
region,
|
|
model.InstanceId.ValueString(),
|
|
model.DatabaseName.ValueString(),
|
|
)
|
|
|
|
return nil
|
|
}
|
|
|
|
// mapResourceFields maps fields from a ListDatabase API response to a resourceModel for the resource.
|
|
func mapResourceFields(source *sqlserverflexalpha.GetDatabaseResponse, model *resourceModel, 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 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.DatabaseName = types.StringValue(source.GetName())
|
|
model.Name = types.StringValue(source.GetName())
|
|
model.Owner = types.StringValue(strings.Trim(source.GetOwner(), "\""))
|
|
model.Region = types.StringValue(region)
|
|
model.ProjectId = types.StringValue(model.ProjectId.ValueString())
|
|
model.InstanceId = types.StringValue(model.InstanceId.ValueString())
|
|
|
|
return nil
|
|
}
|
|
|
|
// toCreatePayload converts the resource model to an API create payload.
|
|
func toCreatePayload(model *resourceModel) (*sqlserverflexalpha.CreateDatabaseRequestPayload, error) {
|
|
if model == nil {
|
|
return nil, fmt.Errorf("nil model")
|
|
}
|
|
|
|
return &sqlserverflexalpha.CreateDatabaseRequestPayload{
|
|
Name: model.Name.ValueStringPointer(),
|
|
Owner: model.Owner.ValueStringPointer(),
|
|
Collation: model.Collation.ValueStringPointer(),
|
|
Compatibility: model.Compatibility.ValueInt64Pointer(),
|
|
}, nil
|
|
}
|