@@ -33,34 +33,36 @@ func NewInstanceDataSource() datasource.DataSource {
3333
3434// instanceDataSource is the data source implementation.
3535type instanceDataSource struct {
36- client * mongodbflex.APIClient
36+ client * mongodbflex.APIClient
37+ providerData core.ProviderData
3738}
3839
3940// Metadata returns the data source type name.
40- func (r * instanceDataSource ) Metadata (_ context.Context , req datasource.MetadataRequest , resp * datasource.MetadataResponse ) {
41+ func (d * instanceDataSource ) Metadata (_ context.Context , req datasource.MetadataRequest , resp * datasource.MetadataResponse ) {
4142 resp .TypeName = req .ProviderTypeName + "_mongodbflex_instance"
4243}
4344
4445// Configure adds the provider configured client to the data source.
45- func (r * instanceDataSource ) Configure (ctx context.Context , req datasource.ConfigureRequest , resp * datasource.ConfigureResponse ) {
46- providerData , ok := conversion .ParseProviderData (ctx , req .ProviderData , & resp .Diagnostics )
46+ func (d * instanceDataSource ) Configure (ctx context.Context , req datasource.ConfigureRequest , resp * datasource.ConfigureResponse ) {
47+ var ok bool
48+ d .providerData , ok = conversion .ParseProviderData (ctx , req .ProviderData , & resp .Diagnostics )
4749 if ! ok {
4850 return
4951 }
5052
51- apiClient := mongodbflexUtils .ConfigureClient (ctx , & providerData , & resp .Diagnostics )
53+ apiClient := mongodbflexUtils .ConfigureClient (ctx , & d . providerData , & resp .Diagnostics )
5254 if resp .Diagnostics .HasError () {
5355 return
5456 }
55- r .client = apiClient
57+ d .client = apiClient
5658 tflog .Info (ctx , "MongoDB Flex instance client configured" )
5759}
5860
5961// Schema defines the schema for the data source.
60- func (r * instanceDataSource ) Schema (_ context.Context , _ datasource.SchemaRequest , resp * datasource.SchemaResponse ) {
62+ func (d * instanceDataSource ) Schema (_ context.Context , _ datasource.SchemaRequest , resp * datasource.SchemaResponse ) {
6163 descriptions := map [string ]string {
6264 "main" : "MongoDB Flex instance data source schema. Must have a `region` specified in the provider configuration." ,
63- "id" : "Terraform's internal data source ID. It is structured as \" `project_id`,`instance_id`\" ." ,
65+ "id" : "Terraform's internal data source ID. It is structured as \" `project_id`,`region`,` instance_id`\" ." ,
6466 "instance_id" : "ID of the MongoDB Flex instance." ,
6567 "project_id" : "STACKIT project ID to which the instance is associated." ,
6668 "name" : "Instance name." ,
@@ -73,6 +75,7 @@ func (r *instanceDataSource) Schema(_ context.Context, _ datasource.SchemaReques
7375 "weekly_snapshot_retention_weeks" : "The number of weeks that weekly backups will be retained." ,
7476 "monthly_snapshot_retention_months" : "The number of months that monthly backups will be retained." ,
7577 "point_in_time_window_hours" : "The number of hours back in time the point-in-time recovery feature will be able to recover." ,
78+ "region" : "The resource region. If not defined, the provider region is used." ,
7679 }
7780
7881 resp .Schema = schema.Schema {
@@ -175,12 +178,18 @@ func (r *instanceDataSource) Schema(_ context.Context, _ datasource.SchemaReques
175178 },
176179 },
177180 },
181+ "region" : schema.StringAttribute {
182+ Optional : true ,
183+ // must be computed to allow for storing the override value from the provider
184+ Computed : true ,
185+ Description : descriptions ["region" ],
186+ },
178187 },
179188 }
180189}
181190
182191// Read refreshes the Terraform state with the latest data.
183- func (r * instanceDataSource ) Read (ctx context.Context , req datasource.ReadRequest , resp * datasource.ReadResponse ) { // nolint:gocritic // function signature required by Terraform
192+ func (d * instanceDataSource ) Read (ctx context.Context , req datasource.ReadRequest , resp * datasource.ReadResponse ) { // nolint:gocritic // function signature required by Terraform
184193 var model Model
185194 diags := req .Config .Get (ctx , & model )
186195 resp .Diagnostics .Append (diags ... )
@@ -189,10 +198,12 @@ func (r *instanceDataSource) Read(ctx context.Context, req datasource.ReadReques
189198 }
190199
191200 projectId := model .ProjectId .ValueString ()
201+ region := d .providerData .GetRegionWithOverride (model .Region )
192202 instanceId := model .InstanceId .ValueString ()
193203 ctx = tflog .SetField (ctx , "project_id" , projectId )
204+ ctx = tflog .SetField (ctx , "region" , region )
194205 ctx = tflog .SetField (ctx , "instance_id" , instanceId )
195- instanceResp , err := r .client .GetInstance (ctx , projectId , instanceId ).Execute ()
206+ instanceResp , err := d .client .GetInstance (ctx , projectId , instanceId , region ).Execute ()
196207 if err != nil {
197208 utils .LogError (
198209 ctx ,
@@ -233,7 +244,7 @@ func (r *instanceDataSource) Read(ctx context.Context, req datasource.ReadReques
233244 }
234245 }
235246
236- err = mapFields (ctx , instanceResp , & model , flavor , storage , options )
247+ err = mapFields (ctx , instanceResp , & model , flavor , storage , options , region )
237248 if err != nil {
238249 core .LogAndAddError (ctx , & resp .Diagnostics , "Error reading instance" , fmt .Sprintf ("Processing API payload: %v" , err ))
239250 return
0 commit comments