99 "github.com/hashicorp/terraform-plugin-framework/schema/validator"
1010 "github.com/hashicorp/terraform-plugin-log/tflog"
1111 "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
12+ "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"
1213 "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate"
1314
1415 "github.com/hashicorp/terraform-plugin-framework/datasource/schema"
@@ -29,7 +30,8 @@ func NewDatabaseDataSource() datasource.DataSource {
2930
3031// databaseDataSource is the data source implementation.
3132type databaseDataSource struct {
32- client * postgresflex.APIClient
33+ client * postgresflex.APIClient
34+ providerData core.ProviderData
3335}
3436
3537// Metadata returns the data source type name.
@@ -44,23 +46,24 @@ func (r *databaseDataSource) Configure(ctx context.Context, req datasource.Confi
4446 return
4547 }
4648
47- providerData , ok := req .ProviderData .(core.ProviderData )
49+ var ok bool
50+ r .providerData , ok = req .ProviderData .(core.ProviderData )
4851 if ! ok {
4952 core .LogAndAddError (ctx , & resp .Diagnostics , "Error configuring API client" , fmt .Sprintf ("Expected configure type stackit.ProviderData, got %T" , req .ProviderData ))
5053 return
5154 }
5255
5356 var apiClient * postgresflex.APIClient
5457 var err error
55- if providerData .PostgresFlexCustomEndpoint != "" {
58+ if r . providerData .PostgresFlexCustomEndpoint != "" {
5659 apiClient , err = postgresflex .NewAPIClient (
57- config .WithCustomAuth (providerData .RoundTripper ),
58- config .WithEndpoint (providerData .PostgresFlexCustomEndpoint ),
60+ config .WithCustomAuth (r . providerData .RoundTripper ),
61+ config .WithEndpoint (r . providerData .PostgresFlexCustomEndpoint ),
5962 )
6063 } else {
6164 apiClient , err = postgresflex .NewAPIClient (
62- config .WithCustomAuth (providerData .RoundTripper ),
63- config .WithRegion (providerData .GetRegion ()),
65+ config .WithCustomAuth (r . providerData .RoundTripper ),
66+ config .WithRegion (r . providerData .GetRegion ()),
6467 )
6568 }
6669
@@ -77,12 +80,13 @@ func (r *databaseDataSource) Configure(ctx context.Context, req datasource.Confi
7780func (r * databaseDataSource ) Schema (_ context.Context , _ datasource.SchemaRequest , resp * datasource.SchemaResponse ) {
7881 descriptions := map [string ]string {
7982 "main" : "Postgres Flex database resource schema. Must have a `region` specified in the provider configuration." ,
80- "id" : "Terraform's internal resource ID. It is structured as \" `project_id`,`instance_id`,`database_id`\" ." ,
83+ "id" : "Terraform's internal resource ID. It is structured as \" `project_id`,`region`,` instance_id`,`database_id`\" ." ,
8184 "database_id" : "Database ID." ,
8285 "instance_id" : "ID of the Postgres Flex instance." ,
8386 "project_id" : "STACKIT project ID to which the instance is associated." ,
8487 "name" : "Database name." ,
8588 "owner" : "Username of the database owner." ,
89+ "region" : "The resource region. If not defined, the provider region is used." ,
8690 }
8791
8892 resp .Schema = schema.Schema {
@@ -123,6 +127,11 @@ func (r *databaseDataSource) Schema(_ context.Context, _ datasource.SchemaReques
123127 Description : descriptions ["owner" ],
124128 Computed : true ,
125129 },
130+ "region" : schema.StringAttribute {
131+ // the region cannot be found, so it has to be passed
132+ Optional : true ,
133+ Description : descriptions ["region" ],
134+ },
126135 },
127136 }
128137}
@@ -138,11 +147,18 @@ func (r *databaseDataSource) Read(ctx context.Context, req datasource.ReadReques
138147 projectId := model .ProjectId .ValueString ()
139148 instanceId := model .InstanceId .ValueString ()
140149 databaseId := model .DatabaseId .ValueString ()
150+ var region string
151+ if utils .IsUndefined (model .Region ) {
152+ region = r .providerData .GetRegion ()
153+ } else {
154+ region = model .Region .ValueString ()
155+ }
141156 ctx = tflog .SetField (ctx , "project_id" , projectId )
142157 ctx = tflog .SetField (ctx , "instance_id" , instanceId )
143158 ctx = tflog .SetField (ctx , "database_id" , databaseId )
159+ ctx = tflog .SetField (ctx , "region" , region )
144160
145- databaseResp , err := getDatabase (ctx , r .client , projectId , instanceId , databaseId )
161+ databaseResp , err := getDatabase (ctx , r .client , projectId , region , instanceId , databaseId )
146162 if err != nil {
147163 oapiErr , ok := err .(* oapierror.GenericOpenAPIError ) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped
148164 if ok && oapiErr .StatusCode == http .StatusNotFound {
@@ -153,7 +169,7 @@ func (r *databaseDataSource) Read(ctx context.Context, req datasource.ReadReques
153169 }
154170
155171 // Map response body to schema and populate Computed attribute values
156- err = mapFields (databaseResp , & model )
172+ err = mapFields (databaseResp , & model , region )
157173 if err != nil {
158174 core .LogAndAddError (ctx , & resp .Diagnostics , "Error reading database" , fmt .Sprintf ("Processing API payload: %v" , err ))
159175 return
0 commit comments