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
|
|
@ -19,12 +19,6 @@ import (
|
|||
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/postgresflexalpha"
|
||||
)
|
||||
|
||||
// DataSourceModel maps the data source schema data.
|
||||
type DataSourceModel struct {
|
||||
postgresflexalpha2.DatabaseModel
|
||||
TerraformID types.String `tfsdk:"id"`
|
||||
}
|
||||
|
||||
// Ensure the implementation satisfies the expected interfaces.
|
||||
var (
|
||||
_ datasource.DataSource = &databaseDataSource{}
|
||||
|
|
@ -35,6 +29,12 @@ func NewDatabaseDataSource() datasource.DataSource {
|
|||
return &databaseDataSource{}
|
||||
}
|
||||
|
||||
// dataSourceModel maps the data source schema data.
|
||||
type dataSourceModel struct {
|
||||
postgresflexalpha2.DatabaseModel
|
||||
TerraformID types.String `tfsdk:"id"`
|
||||
}
|
||||
|
||||
// databaseDataSource is the data source implementation.
|
||||
type databaseDataSource struct {
|
||||
client *postgresflexalpha.APIClient
|
||||
|
|
@ -89,7 +89,7 @@ func (r *databaseDataSource) Read(
|
|||
req datasource.ReadRequest,
|
||||
resp *datasource.ReadResponse,
|
||||
) { // nolint:gocritic // function signature required by Terraform
|
||||
var model DataSourceModel
|
||||
var model dataSourceModel
|
||||
diags := req.Config.Get(ctx, &model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
|
|
@ -141,7 +141,7 @@ func (r *databaseDataSource) Read(
|
|||
// getDatabaseByNameOrID retrieves a single database by ensuring either a unique ID or name is provided.
|
||||
func (r *databaseDataSource) getDatabaseByNameOrID(
|
||||
ctx context.Context,
|
||||
model *DataSourceModel,
|
||||
model *dataSourceModel,
|
||||
projectId, region, instanceId string,
|
||||
diags *diag.Diagnostics,
|
||||
) (*postgresflexalpha.ListDatabase, error) {
|
||||
|
|
|
|||
|
|
@ -9,10 +9,10 @@ import (
|
|||
"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.
|
||||
// mapFields maps fields from a ListDatabase API response to a resourceModel for the data source.
|
||||
func mapFields(
|
||||
source *postgresflexalpha.ListDatabase,
|
||||
model *DataSourceModel,
|
||||
model *dataSourceModel,
|
||||
region string,
|
||||
) error {
|
||||
if source == nil {
|
||||
|
|
@ -51,8 +51,8 @@ func mapFields(
|
|||
return nil
|
||||
}
|
||||
|
||||
// mapResourceFields maps fields from a ListDatabase API response to a ResourceModel for the resource.
|
||||
func mapResourceFields(source *postgresflexalpha.ListDatabase, model *ResourceModel) error {
|
||||
// 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")
|
||||
}
|
||||
|
|
@ -80,7 +80,7 @@ func mapResourceFields(source *postgresflexalpha.ListDatabase, model *ResourceMo
|
|||
}
|
||||
|
||||
// toCreatePayload converts the resource model to an API create payload.
|
||||
func toCreatePayload(model *ResourceModel) (*postgresflexalpha.CreateDatabaseRequestPayload, error) {
|
||||
func toCreatePayload(model *resourceModel) (*postgresflexalpha.CreateDatabaseRequestPayload, error) {
|
||||
if model == nil {
|
||||
return nil, fmt.Errorf("nil model")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,11 +13,11 @@ import (
|
|||
func TestMapFields(t *testing.T) {
|
||||
type given struct {
|
||||
source *postgresflexalpha.ListDatabase
|
||||
model *DataSourceModel
|
||||
model *dataSourceModel
|
||||
region string
|
||||
}
|
||||
type expected struct {
|
||||
model *DataSourceModel
|
||||
model *dataSourceModel
|
||||
err bool
|
||||
}
|
||||
|
||||
|
|
@ -34,11 +34,11 @@ func TestMapFields(t *testing.T) {
|
|||
Name: utils.Ptr("my-db"),
|
||||
Owner: utils.Ptr("\"my-owner\""),
|
||||
},
|
||||
model: &DataSourceModel{},
|
||||
model: &dataSourceModel{},
|
||||
region: "eu01",
|
||||
},
|
||||
expected: expected{
|
||||
model: &DataSourceModel{
|
||||
model: &dataSourceModel{
|
||||
DatabaseModel: datasource.DatabaseModel{
|
||||
Id: types.Int64Value(1),
|
||||
Name: types.StringValue("my-db"),
|
||||
|
|
@ -59,7 +59,7 @@ func TestMapFields(t *testing.T) {
|
|||
Id: utils.Ptr(int64(1)),
|
||||
Name: utils.Ptr("my-db"),
|
||||
},
|
||||
model: &DataSourceModel{
|
||||
model: &dataSourceModel{
|
||||
DatabaseModel: datasource.DatabaseModel{
|
||||
Id: types.Int64Value(1),
|
||||
ProjectId: types.StringValue("my-project"),
|
||||
|
|
@ -69,7 +69,7 @@ func TestMapFields(t *testing.T) {
|
|||
region: "eu01",
|
||||
},
|
||||
expected: expected{
|
||||
model: &DataSourceModel{
|
||||
model: &dataSourceModel{
|
||||
DatabaseModel: datasource.DatabaseModel{
|
||||
Id: types.Int64Value(1),
|
||||
Name: types.StringValue("my-db"),
|
||||
|
|
@ -86,7 +86,7 @@ func TestMapFields(t *testing.T) {
|
|||
name: "should fail on nil source",
|
||||
given: given{
|
||||
source: nil,
|
||||
model: &DataSourceModel{},
|
||||
model: &dataSourceModel{},
|
||||
},
|
||||
expected: expected{err: true},
|
||||
},
|
||||
|
|
@ -94,7 +94,7 @@ func TestMapFields(t *testing.T) {
|
|||
name: "should fail on nil source ID",
|
||||
given: given{
|
||||
source: &postgresflexalpha.ListDatabase{Id: nil},
|
||||
model: &DataSourceModel{},
|
||||
model: &dataSourceModel{},
|
||||
},
|
||||
expected: expected{err: true},
|
||||
},
|
||||
|
|
@ -128,10 +128,10 @@ func TestMapFields(t *testing.T) {
|
|||
func TestMapResourceFields(t *testing.T) {
|
||||
type given struct {
|
||||
source *postgresflexalpha.ListDatabase
|
||||
model *ResourceModel
|
||||
model *resourceModel
|
||||
}
|
||||
type expected struct {
|
||||
model *ResourceModel
|
||||
model *resourceModel
|
||||
err bool
|
||||
}
|
||||
|
||||
|
|
@ -148,10 +148,10 @@ func TestMapResourceFields(t *testing.T) {
|
|||
Name: utils.Ptr("my-db"),
|
||||
Owner: utils.Ptr("\"my-owner\""),
|
||||
},
|
||||
model: &ResourceModel{},
|
||||
model: &resourceModel{},
|
||||
},
|
||||
expected: expected{
|
||||
model: &ResourceModel{
|
||||
model: &resourceModel{
|
||||
Id: types.Int64Value(1),
|
||||
Name: types.StringValue("my-db"),
|
||||
Owner: types.StringValue("my-owner"),
|
||||
|
|
@ -163,7 +163,7 @@ func TestMapResourceFields(t *testing.T) {
|
|||
name: "should fail on nil source",
|
||||
given: given{
|
||||
source: nil,
|
||||
model: &ResourceModel{},
|
||||
model: &resourceModel{},
|
||||
},
|
||||
expected: expected{err: true},
|
||||
},
|
||||
|
|
@ -188,7 +188,7 @@ func TestMapResourceFields(t *testing.T) {
|
|||
|
||||
func TestToCreatePayload(t *testing.T) {
|
||||
type given struct {
|
||||
model *ResourceModel
|
||||
model *resourceModel
|
||||
}
|
||||
type expected struct {
|
||||
payload *postgresflexalpha.CreateDatabaseRequestPayload
|
||||
|
|
@ -203,7 +203,7 @@ func TestToCreatePayload(t *testing.T) {
|
|||
{
|
||||
name: "should convert model to payload",
|
||||
given: given{
|
||||
model: &ResourceModel{
|
||||
model: &resourceModel{
|
||||
Name: types.StringValue("my-db"),
|
||||
Owner: types.StringValue("my-owner"),
|
||||
},
|
||||
|
|
|
|||
|
|
@ -41,8 +41,13 @@ var (
|
|||
extractErrorMessage = "Extracting identity data: %v"
|
||||
)
|
||||
|
||||
// ResourceModel describes the resource data model.
|
||||
type ResourceModel = postgresflexalpha2.DatabaseModel
|
||||
// NewDatabaseResource is a helper function to simplify the provider implementation.
|
||||
func NewDatabaseResource() resource.Resource {
|
||||
return &databaseResource{}
|
||||
}
|
||||
|
||||
// resourceModel describes the resource data model.
|
||||
type resourceModel = postgresflexalpha2.DatabaseModel
|
||||
|
||||
// DatabaseResourceIdentityModel describes the resource's identity attributes.
|
||||
type DatabaseResourceIdentityModel struct {
|
||||
|
|
@ -52,11 +57,6 @@ type DatabaseResourceIdentityModel struct {
|
|||
DatabaseID types.Int64 `tfsdk:"database_id"`
|
||||
}
|
||||
|
||||
// NewDatabaseResource is a helper function to simplify the provider implementation.
|
||||
func NewDatabaseResource() resource.Resource {
|
||||
return &databaseResource{}
|
||||
}
|
||||
|
||||
// databaseResource is the resource implementation.
|
||||
type databaseResource struct {
|
||||
client *postgresflexalpha.APIClient
|
||||
|
|
@ -69,7 +69,7 @@ func (r *databaseResource) ModifyPlan(
|
|||
req resource.ModifyPlanRequest,
|
||||
resp *resource.ModifyPlanResponse,
|
||||
) { // nolint:gocritic // function signature required by Terraform
|
||||
var configModel ResourceModel
|
||||
var configModel resourceModel
|
||||
// skip initial empty configuration to avoid follow-up errors
|
||||
if req.Config.Raw.IsNull() {
|
||||
return
|
||||
|
|
@ -79,7 +79,7 @@ func (r *databaseResource) ModifyPlan(
|
|||
return
|
||||
}
|
||||
|
||||
var planModel ResourceModel
|
||||
var planModel resourceModel
|
||||
resp.Diagnostics.Append(req.Plan.Get(ctx, &planModel)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
|
|
@ -199,7 +199,7 @@ func (r *databaseResource) Create(
|
|||
req resource.CreateRequest,
|
||||
resp *resource.CreateResponse,
|
||||
) { // nolint:gocritic // function signature required by Terraform
|
||||
var model ResourceModel
|
||||
var model resourceModel
|
||||
diags := req.Plan.Get(ctx, &model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
|
|
@ -309,7 +309,7 @@ func (r *databaseResource) Read(
|
|||
req resource.ReadRequest,
|
||||
resp *resource.ReadResponse,
|
||||
) { // nolint:gocritic // function signature required by Terraform
|
||||
var model ResourceModel
|
||||
var model resourceModel
|
||||
diags := req.State.Get(ctx, &model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
|
|
@ -380,7 +380,7 @@ func (r *databaseResource) Update(
|
|||
req resource.UpdateRequest,
|
||||
resp *resource.UpdateResponse,
|
||||
) {
|
||||
var model ResourceModel
|
||||
var model resourceModel
|
||||
diags := req.Plan.Get(ctx, &model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
|
|
@ -418,7 +418,7 @@ func (r *databaseResource) Update(
|
|||
ctx = tflog.SetField(ctx, "database_id", databaseId)
|
||||
|
||||
// Retrieve values from state
|
||||
var stateModel ResourceModel
|
||||
var stateModel resourceModel
|
||||
diags = req.State.Get(ctx, &stateModel)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
|
|
@ -483,7 +483,7 @@ func (r *databaseResource) Delete(
|
|||
req resource.DeleteRequest,
|
||||
resp *resource.DeleteResponse,
|
||||
) { // nolint:gocritic // function signature required by Terraform
|
||||
var model ResourceModel
|
||||
var model resourceModel
|
||||
diags := req.State.Get(ctx, &model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
|
|
@ -591,7 +591,7 @@ func (r *databaseResource) ImportState(
|
|||
|
||||
// extractIdentityData extracts essential identifiers from the resource model, falling back to the identity model.
|
||||
func (r *databaseResource) extractIdentityData(
|
||||
model ResourceModel,
|
||||
model resourceModel,
|
||||
identity DatabaseResourceIdentityModel,
|
||||
) (projectId, region, instanceId string, databaseId int64, err error) {
|
||||
if !model.DatabaseId.IsNull() && !model.DatabaseId.IsUnknown() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue