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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue