@@ -10,6 +10,7 @@ import (
1010 "github.com/hashicorp/terraform-plugin-framework/types/basetypes"
1111 "github.com/hashicorp/terraform-plugin-log/tflog"
1212 "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
13+ "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"
1314 "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate"
1415
1516 "github.com/hashicorp/terraform-plugin-framework/datasource/schema"
@@ -31,7 +32,8 @@ func NewInstanceDataSource() datasource.DataSource {
3132
3233// instanceDataSource is the data source implementation.
3334type instanceDataSource struct {
34- client * sqlserverflex.APIClient
35+ client * sqlserverflex.APIClient
36+ providerData core.ProviderData
3537}
3638
3739// Metadata returns the data source type name.
@@ -46,23 +48,24 @@ func (r *instanceDataSource) Configure(ctx context.Context, req datasource.Confi
4648 return
4749 }
4850
49- providerData , ok := req .ProviderData .(core.ProviderData )
51+ var ok bool
52+ r .providerData , ok = req .ProviderData .(core.ProviderData )
5053 if ! ok {
5154 core .LogAndAddError (ctx , & resp .Diagnostics , "Error configuring API client" , fmt .Sprintf ("Expected configure type stackit.ProviderData, got %T" , req .ProviderData ))
5255 return
5356 }
5457
5558 var apiClient * sqlserverflex.APIClient
5659 var err error
57- if providerData .SQLServerFlexCustomEndpoint != "" {
60+ if r . providerData .SQLServerFlexCustomEndpoint != "" {
5861 apiClient , err = sqlserverflex .NewAPIClient (
59- config .WithCustomAuth (providerData .RoundTripper ),
60- config .WithEndpoint (providerData .SQLServerFlexCustomEndpoint ),
62+ config .WithCustomAuth (r . providerData .RoundTripper ),
63+ config .WithEndpoint (r . providerData .SQLServerFlexCustomEndpoint ),
6164 )
6265 } else {
6366 apiClient , err = sqlserverflex .NewAPIClient (
64- config .WithCustomAuth (providerData .RoundTripper ),
65- config .WithRegion (providerData .Region ),
67+ config .WithCustomAuth (r . providerData .RoundTripper ),
68+ config .WithRegion (r . providerData .Region ),
6669 )
6770 }
6871
@@ -79,13 +82,14 @@ func (r *instanceDataSource) Configure(ctx context.Context, req datasource.Confi
7982func (r * instanceDataSource ) Schema (_ context.Context , _ datasource.SchemaRequest , resp * datasource.SchemaResponse ) {
8083 descriptions := map [string ]string {
8184 "main" : "SQLServer Flex instance data source schema. Must have a `region` specified in the provider configuration." ,
82- "id" : "Terraform's internal data source. ID. It is structured as \" `project_id`,`instance_id`\" ." ,
85+ "id" : "Terraform's internal data source. ID. It is structured as \" `project_id`,`region`,` instance_id`\" ." ,
8386 "instance_id" : "ID of the SQLServer Flex instance." ,
8487 "project_id" : "STACKIT project ID to which the instance is associated." ,
8588 "name" : "Instance name." ,
8689 "acl" : "The Access Control List (ACL) for the SQLServer Flex instance." ,
8790 "backup_schedule" : `The backup schedule. Should follow the cron scheduling system format (e.g. "0 0 * * *").` ,
8891 "options" : "Custom parameters for the SQLServer Flex instance." ,
92+ "region" : "The resource region. If not defined, the provider region is used." ,
8993 }
9094
9195 resp .Schema = schema.Schema {
@@ -170,6 +174,11 @@ func (r *instanceDataSource) Schema(_ context.Context, _ datasource.SchemaReques
170174 },
171175 },
172176 },
177+ "region" : schema.StringAttribute {
178+ // the region cannot be found, so it has to be passed
179+ Optional : true ,
180+ Description : descriptions ["region" ],
181+ },
173182 },
174183 }
175184}
@@ -185,9 +194,16 @@ func (r *instanceDataSource) Read(ctx context.Context, req datasource.ReadReques
185194
186195 projectId := model .ProjectId .ValueString ()
187196 instanceId := model .InstanceId .ValueString ()
197+ var region string
198+ if utils .IsUndefined (model .Region ) {
199+ region = r .providerData .Region
200+ } else {
201+ region = model .Region .ValueString ()
202+ }
188203 ctx = tflog .SetField (ctx , "project_id" , projectId )
189204 ctx = tflog .SetField (ctx , "instance_id" , instanceId )
190- instanceResp , err := r .client .GetInstance (ctx , projectId , instanceId ).Execute ()
205+ ctx = tflog .SetField (ctx , "region" , region )
206+ instanceResp , err := r .client .GetInstance (ctx , projectId , instanceId , region ).Execute ()
191207 if err != nil {
192208 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
193209 if ok && oapiErr .StatusCode == http .StatusNotFound {
@@ -222,7 +238,7 @@ func (r *instanceDataSource) Read(ctx context.Context, req datasource.ReadReques
222238 }
223239 }
224240
225- err = mapFields (ctx , instanceResp , & model , flavor , storage , options )
241+ err = mapFields (ctx , instanceResp , & model , flavor , storage , options , region )
226242 if err != nil {
227243 core .LogAndAddError (ctx , & resp .Diagnostics , "Error reading instance" , fmt .Sprintf ("Processing API payload: %v" , err ))
228244 return
0 commit comments