From 4830d6196de3f924b2f28c8e564fed12b3212963 Mon Sep 17 00:00:00 2001 From: Andre Harms Date: Wed, 4 Feb 2026 20:18:46 +0100 Subject: [PATCH] feat: extend database data source schema with project_id, instance_id, region, and tf_id attributes --- .../postgresflexalpha/database/datasource.go | 47 ++++++++++++++++--- .../database_data_source_gen.go | 22 --------- 2 files changed, 40 insertions(+), 29 deletions(-) diff --git a/stackit/internal/services/postgresflexalpha/database/datasource.go b/stackit/internal/services/postgresflexalpha/database/datasource.go index 45bd305c..28d8e51d 100644 --- a/stackit/internal/services/postgresflexalpha/database/datasource.go +++ b/stackit/internal/services/postgresflexalpha/database/datasource.go @@ -5,7 +5,9 @@ import ( "fmt" "net/http" + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" "github.com/hashicorp/terraform-plugin-framework/diag" + "github.com/hashicorp/terraform-plugin-framework/types" "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/conversion" 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" @@ -18,6 +20,15 @@ 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 + ProjectId types.String `tfsdk:"project_id"` + InstanceId types.String `tfsdk:"instance_id"` + Region types.String `tfsdk:"region"` + TerraformID types.String `tfsdk:"tf_id"` +} + // Ensure the implementation satisfies the expected interfaces. var ( _ datasource.DataSource = &databaseDataSource{} @@ -65,7 +76,30 @@ func (r *databaseDataSource) Configure( // Schema defines the schema for the data source. func (r *databaseDataSource) Schema(ctx context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { - resp.Schema = postgresflexalpha2.DatabaseResourceSchema(ctx) + + 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, + } + s.Attributes["instance_id"] = schema.StringAttribute{ + Description: "ID of the PostgresFlex instance.", + MarkdownDescription: "ID of the PostgresFlex instance.", + Required: true, + } + s.Attributes["region"] = schema.StringAttribute{ + Description: "Region of the PostgresFlex instance.", + MarkdownDescription: "Region of the PostgresFlex instance.", + Optional: true, + } + s.Attributes["tf_id"] = schema.StringAttribute{ + Description: "Terraform's internal resource ID. It is structured as \\\"`project_id`,`region`,`instance_id`,`id`\\\".\",", + Optional: true, + Computed: true, + } + + resp.Schema = s } // Read fetches the data for the data source. @@ -74,7 +108,7 @@ func (r *databaseDataSource) Read( req datasource.ReadRequest, resp *datasource.ReadResponse, ) { // nolint:gocritic // function signature required by Terraform - var model postgresflexalpha2.DatabaseModel + var model DataSourceModel diags := req.Config.Get(ctx, &model) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { @@ -123,11 +157,10 @@ func (r *databaseDataSource) Read( tflog.Info(ctx, "Postgres Flex database read") } -// getDatabase retrieves a single database by either ID or name. -// It adds a diagnostic if the configuration is invalid. +// getDatabaseByNameOrID retrieves a single database by ensuring either a unique ID or name is provided. func (r *databaseDataSource) getDatabaseByNameOrID( ctx context.Context, - model *postgresflexalpha2.DatabaseModel, + model *DataSourceModel, projectId, region, instanceId string, diags *diag.Diagnostics, ) (*postgresflexalpha.ListDatabase, error) { @@ -144,7 +177,7 @@ func (r *databaseDataSource) getDatabaseByNameOrID( if isIdSet { databaseId := model.Id.ValueInt64() - ctx = tflog.SetField(ctx, "database_id", databaseId) + ctx = tflog.SetField(ctx, "id", databaseId) return getDatabaseById(ctx, r.client, projectId, region, instanceId, databaseId) } @@ -153,7 +186,7 @@ func (r *databaseDataSource) getDatabaseByNameOrID( return getDatabaseByName(ctx, r.client, projectId, region, instanceId, databaseName) } -// handleReadError logs and adds a structured error to diagnostics for API errors. +// handleReadError centralizes API error handling for the Read operation. func handleReadError(ctx context.Context, diags *diag.Diagnostics, err error, projectId, instanceId string) { utils.LogError( ctx, diff --git a/stackit/internal/services/postgresflexalpha/database/datasources_gen/database_data_source_gen.go b/stackit/internal/services/postgresflexalpha/database/datasources_gen/database_data_source_gen.go index a890b593..cf4b987c 100644 --- a/stackit/internal/services/postgresflexalpha/database/datasources_gen/database_data_source_gen.go +++ b/stackit/internal/services/postgresflexalpha/database/datasources_gen/database_data_source_gen.go @@ -5,9 +5,7 @@ package postgresflexalpha import ( "context" - "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" "github.com/hashicorp/terraform-plugin-framework/datasource/schema" - "github.com/hashicorp/terraform-plugin-framework/schema/validator" "github.com/hashicorp/terraform-plugin-framework/types" "github.com/hashicorp/terraform-plugin-framework/datasource/schema" @@ -43,26 +41,6 @@ func DatabaseDataSourceSchema(ctx context.Context) schema.Schema { Description: "The owner of the database.", MarkdownDescription: "The owner of the database.", }, - "instance_id": schema.StringAttribute{ - Required: true, - Description: "The ID of the instance.", - MarkdownDescription: "The ID of the instance.", - }, - "project_id": schema.StringAttribute{ - Required: true, - Description: "The STACKIT project ID.", - MarkdownDescription: "The STACKIT project ID.", - }, - "region": schema.StringAttribute{ - Required: true, - Description: "The region which should be addressed", - MarkdownDescription: "The region which should be addressed", - Validators: []validator.String{ - stringvalidator.OneOf( - "eu01", - ), - }, - }, }, } }