feat: enhance database resource model with additional attributes and refactor data source schema
This commit is contained in:
parent
c4891b770d
commit
1284bcc8a3
6 changed files with 66 additions and 80 deletions
|
|
@ -5,18 +5,15 @@ import (
|
|||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/datasource"
|
||||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
|
||||
"github.com/hashicorp/terraform-plugin-framework/diag"
|
||||
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
"github.com/hashicorp/terraform-plugin-log/tflog"
|
||||
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/conversion"
|
||||
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/core"
|
||||
postgresflexalpha2 "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/postgresflexalpha/database/datasources_gen"
|
||||
postgresflexUtils "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/postgresflexalpha/utils"
|
||||
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/validate"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/datasource"
|
||||
"github.com/hashicorp/terraform-plugin-log/tflog"
|
||||
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/core"
|
||||
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/utils"
|
||||
|
||||
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/postgresflexalpha"
|
||||
|
|
@ -25,10 +22,6 @@ import (
|
|||
// DataSourceModel maps the data source schema data.
|
||||
type DataSourceModel struct {
|
||||
postgresflexalpha2.DatabaseModel
|
||||
ProjectId types.String `tfsdk:"project_id"`
|
||||
InstanceId types.String `tfsdk:"instance_id"`
|
||||
Region types.String `tfsdk:"region"`
|
||||
DatabaseID types.Int64 `tfsdk:"database_id"`
|
||||
TerraformID types.String `tfsdk:"id"`
|
||||
}
|
||||
|
||||
|
|
@ -80,34 +73,7 @@ func (r *databaseDataSource) Configure(
|
|||
// Schema defines the schema for the data source.
|
||||
func (r *databaseDataSource) Schema(ctx context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
|
||||
|
||||
s := postgresflexalpha2.DatabaseResourceSchema(ctx)
|
||||
s.Attributes["project_id"] = schema.StringAttribute{
|
||||
Description: "STACKIT project ID to which the instance is associated.",
|
||||
MarkdownDescription: "STACKIT project ID to which the instance is associated.",
|
||||
Required: true,
|
||||
Validators: []validator.String{
|
||||
validate.UUID(),
|
||||
validate.NoSeparator(),
|
||||
},
|
||||
}
|
||||
s.Attributes["instance_id"] = schema.StringAttribute{
|
||||
Description: "ID of the PostgresFlex instance.",
|
||||
MarkdownDescription: "ID of the PostgresFlex instance.",
|
||||
Required: true,
|
||||
Validators: []validator.String{
|
||||
validate.UUID(),
|
||||
validate.NoSeparator(),
|
||||
},
|
||||
}
|
||||
s.Attributes["region"] = schema.StringAttribute{
|
||||
Description: "Region of the PostgresFlex instance.",
|
||||
MarkdownDescription: "Region of the PostgresFlex instance.",
|
||||
Optional: true,
|
||||
}
|
||||
s.Attributes["database_id"] = schema.Int64Attribute{
|
||||
Description: "The ID of the database.",
|
||||
Required: true,
|
||||
}
|
||||
s := postgresflexalpha2.DatabaseDataSourceSchema(ctx)
|
||||
s.Attributes["id"] = schema.StringAttribute{
|
||||
Description: "Terraform's internal resource ID. It is structured as \\\"`project_id`,`region`,`instance_id`," +
|
||||
"`database_id`\\\".\",",
|
||||
|
|
@ -179,7 +145,7 @@ func (r *databaseDataSource) getDatabaseByNameOrID(
|
|||
projectId, region, instanceId string,
|
||||
diags *diag.Diagnostics,
|
||||
) (*postgresflexalpha.ListDatabase, error) {
|
||||
isIdSet := !model.DatabaseID.IsNull() && !model.DatabaseID.IsUnknown()
|
||||
isIdSet := !model.DatabaseId.IsNull() && !model.DatabaseId.IsUnknown()
|
||||
isNameSet := !model.Name.IsNull() && !model.Name.IsUnknown()
|
||||
|
||||
if (isIdSet && isNameSet) || (!isIdSet && !isNameSet) {
|
||||
|
|
@ -191,7 +157,7 @@ func (r *databaseDataSource) getDatabaseByNameOrID(
|
|||
}
|
||||
|
||||
if isIdSet {
|
||||
databaseId := model.DatabaseID.ValueInt64()
|
||||
databaseId := model.DatabaseId.ValueInt64()
|
||||
ctx = tflog.SetField(ctx, "database_id", databaseId)
|
||||
return getDatabaseById(ctx, r.client, projectId, region, instanceId, databaseId)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,9 +2,11 @@ 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.
|
||||
|
|
@ -24,8 +26,8 @@ func mapFields(
|
|||
}
|
||||
|
||||
var databaseId int64
|
||||
if model.Id.ValueInt64() != 0 {
|
||||
databaseId = model.Id.ValueInt64()
|
||||
if model.DatabaseId.ValueInt64() != 0 {
|
||||
databaseId = model.DatabaseId.ValueInt64()
|
||||
} else if source.Id != nil {
|
||||
databaseId = *source.Id
|
||||
} else {
|
||||
|
|
@ -33,10 +35,18 @@ func mapFields(
|
|||
}
|
||||
|
||||
model.Id = types.Int64Value(databaseId)
|
||||
model.DatabaseID = 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
|
||||
}
|
||||
|
|
@ -63,7 +73,7 @@ func mapResourceFields(source *postgresflexalpha.ListDatabase, model *ResourceMo
|
|||
}
|
||||
|
||||
model.Id = types.Int64Value(databaseId)
|
||||
model.DatabaseID = types.Int64Value(databaseId)
|
||||
model.DatabaseId = types.Int64Value(databaseId)
|
||||
model.Name = types.StringValue(source.GetName())
|
||||
model.Owner = types.StringPointerValue(cleanString(source.Owner))
|
||||
return nil
|
||||
|
|
|
|||
|
|
@ -8,7 +8,6 @@ import (
|
|||
"github.com/stackitcloud/stackit-sdk-go/core/utils"
|
||||
"tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/postgresflexalpha"
|
||||
datasource "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/postgresflexalpha/database/datasources_gen"
|
||||
resource "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/postgresflexalpha/database/resources_gen"
|
||||
)
|
||||
|
||||
func TestMapFields(t *testing.T) {
|
||||
|
|
@ -41,12 +40,15 @@ func TestMapFields(t *testing.T) {
|
|||
expected: expected{
|
||||
model: &DataSourceModel{
|
||||
DatabaseModel: datasource.DatabaseModel{
|
||||
Id: types.Int64Value(1),
|
||||
Name: types.StringValue("my-db"),
|
||||
Owner: types.StringValue("my-owner"),
|
||||
Id: types.Int64Value(1),
|
||||
Name: types.StringValue("my-db"),
|
||||
Owner: types.StringValue("my-owner"),
|
||||
Region: types.StringValue("eu01"),
|
||||
DatabaseId: types.Int64Value(1),
|
||||
InstanceId: types.StringValue("my-instance"),
|
||||
ProjectId: types.StringValue("my-project"),
|
||||
},
|
||||
Region: types.StringValue("eu01"),
|
||||
DatabaseID: types.Int64Value(1),
|
||||
TerraformID: types.StringValue("my-project,eu01,my-instance,1"),
|
||||
},
|
||||
},
|
||||
},
|
||||
|
|
@ -59,7 +61,9 @@ func TestMapFields(t *testing.T) {
|
|||
},
|
||||
model: &DataSourceModel{
|
||||
DatabaseModel: datasource.DatabaseModel{
|
||||
Id: types.Int64Value(1),
|
||||
Id: types.Int64Value(1),
|
||||
ProjectId: types.StringValue("my-project"),
|
||||
InstanceId: types.StringValue("my-instance"),
|
||||
},
|
||||
},
|
||||
region: "eu01",
|
||||
|
|
@ -69,10 +73,12 @@ func TestMapFields(t *testing.T) {
|
|||
DatabaseModel: datasource.DatabaseModel{
|
||||
Id: types.Int64Value(1),
|
||||
Name: types.StringValue("my-db"),
|
||||
Owner: types.StringNull(),
|
||||
Owner: types.StringNull(), DatabaseId: types.Int64Value(1),
|
||||
Region: types.StringValue("eu01"),
|
||||
InstanceId: types.StringValue("my-instance"),
|
||||
ProjectId: types.StringValue("my-project"),
|
||||
},
|
||||
DatabaseID: types.Int64Value(1),
|
||||
Region: types.StringValue("eu01"),
|
||||
TerraformID: types.StringValue("my-project,eu01,my-instance,1"),
|
||||
},
|
||||
},
|
||||
},
|
||||
|
|
@ -146,12 +152,10 @@ func TestMapResourceFields(t *testing.T) {
|
|||
},
|
||||
expected: expected{
|
||||
model: &ResourceModel{
|
||||
DatabaseModel: resource.DatabaseModel{
|
||||
Id: types.Int64Value(1),
|
||||
Name: types.StringValue("my-db"),
|
||||
Owner: types.StringValue("my-owner"),
|
||||
},
|
||||
DatabaseID: types.Int64Value(1),
|
||||
Id: types.Int64Value(1),
|
||||
Name: types.StringValue("my-db"),
|
||||
Owner: types.StringValue("my-owner"),
|
||||
DatabaseId: types.Int64Value(1),
|
||||
},
|
||||
},
|
||||
},
|
||||
|
|
@ -200,10 +204,8 @@ func TestToCreatePayload(t *testing.T) {
|
|||
name: "should convert model to payload",
|
||||
given: given{
|
||||
model: &ResourceModel{
|
||||
DatabaseModel: resource.DatabaseModel{
|
||||
Name: types.StringValue("my-db"),
|
||||
Owner: types.StringValue("my-owner"),
|
||||
},
|
||||
Name: types.StringValue("my-db"),
|
||||
Owner: types.StringValue("my-owner"),
|
||||
},
|
||||
},
|
||||
expected: expected{
|
||||
|
|
|
|||
|
|
@ -42,14 +42,7 @@ var (
|
|||
)
|
||||
|
||||
// ResourceModel describes the resource data model.
|
||||
type ResourceModel struct {
|
||||
postgresflexalpha2.DatabaseModel
|
||||
ProjectID types.String `tfsdk:"project_id"`
|
||||
Region types.String `tfsdk:"region"`
|
||||
InstanceID types.String `tfsdk:"instance_id"`
|
||||
DatabaseID types.Int64 `tfsdk:"database_id"`
|
||||
TerraformID types.String `tfsdk:"id"`
|
||||
}
|
||||
type ResourceModel = postgresflexalpha2.DatabaseModel
|
||||
|
||||
// DatabaseResourceIdentityModel describes the resource's identity attributes.
|
||||
type DatabaseResourceIdentityModel struct {
|
||||
|
|
@ -289,9 +282,21 @@ func (r *databaseResource) Create(
|
|||
)
|
||||
return
|
||||
}
|
||||
|
||||
// Write identity attributes to state
|
||||
identityData.ProjectID = types.StringValue(projectId)
|
||||
identityData.Region = types.StringValue(region)
|
||||
identityData.InstanceID = types.StringValue(instanceId)
|
||||
identityData.DatabaseID = types.Int64Value(databaseId)
|
||||
|
||||
resp.Diagnostics.Append(resp.Identity.Set(ctx, &identityData)...)
|
||||
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
||||
// Set state to fully populated data
|
||||
diags = resp.State.Set(ctx, model)
|
||||
resp.Diagnostics.Append(diags...)
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, model)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
|
|
@ -589,8 +594,8 @@ func (r *databaseResource) extractIdentityData(
|
|||
model ResourceModel,
|
||||
identity DatabaseResourceIdentityModel,
|
||||
) (projectId, region, instanceId string, databaseId int64, err error) {
|
||||
if !model.DatabaseID.IsNull() && !model.DatabaseID.IsUnknown() {
|
||||
databaseId = model.DatabaseID.ValueInt64()
|
||||
if !model.DatabaseId.IsNull() && !model.DatabaseId.IsUnknown() {
|
||||
databaseId = model.DatabaseId.ValueInt64()
|
||||
} else {
|
||||
if identity.DatabaseID.IsNull() || identity.DatabaseID.IsUnknown() {
|
||||
return "", "", "", 0, fmt.Errorf("database_id not found in config")
|
||||
|
|
@ -598,8 +603,8 @@ func (r *databaseResource) extractIdentityData(
|
|||
databaseId = identity.DatabaseID.ValueInt64()
|
||||
}
|
||||
|
||||
if !model.ProjectID.IsNull() && !model.ProjectID.IsUnknown() {
|
||||
projectId = model.ProjectID.ValueString()
|
||||
if !model.ProjectId.IsNull() && !model.ProjectId.IsUnknown() {
|
||||
projectId = model.ProjectId.ValueString()
|
||||
} else {
|
||||
if identity.ProjectID.IsNull() || identity.ProjectID.IsUnknown() {
|
||||
return "", "", "", 0, fmt.Errorf("project_id not found in config")
|
||||
|
|
@ -616,8 +621,8 @@ func (r *databaseResource) extractIdentityData(
|
|||
region = r.providerData.GetRegionWithOverride(identity.Region)
|
||||
}
|
||||
|
||||
if !model.InstanceID.IsNull() && !model.InstanceID.IsUnknown() {
|
||||
instanceId = model.InstanceID.ValueString()
|
||||
if !model.InstanceId.IsNull() && !model.InstanceId.IsUnknown() {
|
||||
instanceId = model.InstanceId.ValueString()
|
||||
} else {
|
||||
if identity.InstanceID.IsNull() || identity.InstanceID.IsUnknown() {
|
||||
return "", "", "", 0, fmt.Errorf("instance_id not found in config")
|
||||
|
|
|
|||
|
|
@ -7,6 +7,9 @@ import (
|
|||
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
|
||||
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
|
||||
"github.com/hashicorp/terraform-plugin-go/tftypes"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue