Skip to content

Commit 189fa1e

Browse files
authored
feat(mongodbflex): region adjustment (#914)
relates to STACKITTPR-282
1 parent 6555a99 commit 189fa1e

File tree

16 files changed

+388
-175
lines changed

16 files changed

+388
-175
lines changed

docs/data-sources/mongodbflex_instance.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,16 @@ data "stackit_mongodbflex_instance" "example" {
2727
- `instance_id` (String) ID of the MongoDB Flex instance.
2828
- `project_id` (String) STACKIT project ID to which the instance is associated.
2929

30+
### Optional
31+
32+
- `region` (String) The resource region. If not defined, the provider region is used.
33+
3034
### Read-Only
3135

3236
- `acl` (List of String) The Access Control List (ACL) for the MongoDB Flex instance.
3337
- `backup_schedule` (String) The backup schedule. Should follow the cron scheduling system format (e.g. "0 0 * * *").
3438
- `flavor` (Attributes) (see [below for nested schema](#nestedatt--flavor))
35-
- `id` (String) Terraform's internal data source ID. It is structured as "`project_id`,`instance_id`".
39+
- `id` (String) Terraform's internal data source ID. It is structured as "`project_id`,`region`,`instance_id`".
3640
- `name` (String) Instance name.
3741
- `options` (Attributes) Custom parameters for the MongoDB Flex instance. (see [below for nested schema](#nestedatt--options))
3842
- `replicas` (Number)

docs/data-sources/mongodbflex_user.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,15 @@ data "stackit_mongodbflex_user" "example" {
2929
- `project_id` (String) STACKIT project ID to which the instance is associated.
3030
- `user_id` (String) User ID.
3131

32+
### Optional
33+
34+
- `region` (String) The resource region. If not defined, the provider region is used.
35+
3236
### Read-Only
3337

3438
- `database` (String)
3539
- `host` (String)
36-
- `id` (String) Terraform's internal data source. ID. It is structured as "`project_id`,`instance_id`,`user_id`".
40+
- `id` (String) Terraform's internal data source. ID. It is structured as "`project_id`,`region`,`instance_id`,`user_id`".
3741
- `port` (Number)
3842
- `roles` (Set of String)
3943
- `username` (String)

docs/resources/mongodbflex_instance.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,13 @@ resource "stackit_mongodbflex_instance" "example" {
5151
- `storage` (Attributes) (see [below for nested schema](#nestedatt--storage))
5252
- `version` (String)
5353

54+
### Optional
55+
56+
- `region` (String) The resource region. If not defined, the provider region is used.
57+
5458
### Read-Only
5559

56-
- `id` (String) Terraform's internal resource ID. It is structured as "`project_id`,`instance_id`".
60+
- `id` (String) Terraform's internal resource ID. It is structured as "`project_id`,`region`,`instance_id`".
5761
- `instance_id` (String) ID of the MongoDB Flex instance.
5862

5963
<a id="nestedatt--flavor"></a>

docs/resources/mongodbflex_user.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ resource "stackit_mongodbflex_user" "example" {
3434

3535
### Optional
3636

37+
- `region` (String) The resource region. If not defined, the provider region is used.
3738
- `username` (String)
3839

3940
### Read-Only

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ require (
2121
github.com/stackitcloud/stackit-sdk-go/services/logme v0.25.0
2222
github.com/stackitcloud/stackit-sdk-go/services/mariadb v0.25.0
2323
github.com/stackitcloud/stackit-sdk-go/services/modelserving v0.5.0
24-
github.com/stackitcloud/stackit-sdk-go/services/mongodbflex v1.2.1
24+
github.com/stackitcloud/stackit-sdk-go/services/mongodbflex v1.4.0
2525
github.com/stackitcloud/stackit-sdk-go/services/objectstorage v1.3.0
2626
github.com/stackitcloud/stackit-sdk-go/services/observability v0.8.0
2727
github.com/stackitcloud/stackit-sdk-go/services/opensearch v0.24.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ github.com/stackitcloud/stackit-sdk-go/services/mariadb v0.25.0 h1:vxk6ztgzUIPMk
172172
github.com/stackitcloud/stackit-sdk-go/services/mariadb v0.25.0/go.mod h1:Pb8IEV5/jP8k75dVcN5cn3kP7PHTy/4KXXKpG76oj4U=
173173
github.com/stackitcloud/stackit-sdk-go/services/modelserving v0.5.0 h1:WMnCRB+zEWK5qq3d+MT5+nysuyXs+694/kSxgVYaXfk=
174174
github.com/stackitcloud/stackit-sdk-go/services/modelserving v0.5.0/go.mod h1:3p68DR66MNYgc2YcMF7B23MmySVShQ6g7f+7EkZfpPY=
175-
github.com/stackitcloud/stackit-sdk-go/services/mongodbflex v1.2.1 h1:TWz7qJ4Mg5pquDXODSZ1dzhS95ZYn3w1aKjuRU2VqCg=
176-
github.com/stackitcloud/stackit-sdk-go/services/mongodbflex v1.2.1/go.mod h1:U1Zf/S9IuDvRJq1tRKFT/bsJd4qxYzwtukqX3TL++Mw=
175+
github.com/stackitcloud/stackit-sdk-go/services/mongodbflex v1.4.0 h1:il4vBOejtX0//CWuY6CDpFfPpDIvin5V9QIaKvyXV/M=
176+
github.com/stackitcloud/stackit-sdk-go/services/mongodbflex v1.4.0/go.mod h1:U1Zf/S9IuDvRJq1tRKFT/bsJd4qxYzwtukqX3TL++Mw=
177177
github.com/stackitcloud/stackit-sdk-go/services/objectstorage v1.3.0 h1:bK5FDpSGfUKDXNkqOoiqOU9hua2YfcdYsGS4zQQ9wg0=
178178
github.com/stackitcloud/stackit-sdk-go/services/objectstorage v1.3.0/go.mod h1:Q2SJXlZTksvNAb1QnpGNfDSw/OMPN9uopaKuptUGhO8=
179179
github.com/stackitcloud/stackit-sdk-go/services/observability v0.8.0 h1:8v9Iupkc3meXcVPMpteXDz07YzY2M5jud50vEtAvbs8=

stackit/internal/services/mongodbflex/instance/datasource.go

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,34 +33,36 @@ func NewInstanceDataSource() datasource.DataSource {
3333

3434
// instanceDataSource is the data source implementation.
3535
type 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

Comments
 (0)