diff --git a/docs/data-sources/mongodbflex_instance.md b/docs/data-sources/mongodbflex_instance.md index b665e60f..47ad14ae 100644 --- a/docs/data-sources/mongodbflex_instance.md +++ b/docs/data-sources/mongodbflex_instance.md @@ -27,12 +27,16 @@ data "stackit_mongodbflex_instance" "example" { - `instance_id` (String) ID of the MongoDB Flex instance. - `project_id` (String) STACKIT project ID to which the instance is associated. +### Optional + +- `region` (String) The resource region. If not defined, the provider region is used. + ### Read-Only - `acl` (List of String) The Access Control List (ACL) for the MongoDB Flex instance. - `backup_schedule` (String) The backup schedule. Should follow the cron scheduling system format (e.g. "0 0 * * *"). - `flavor` (Attributes) (see [below for nested schema](#nestedatt--flavor)) -- `id` (String) Terraform's internal data source ID. It is structured as "`project_id`,`instance_id`". +- `id` (String) Terraform's internal data source ID. It is structured as "`project_id`,`region`,`instance_id`". - `name` (String) Instance name. - `options` (Attributes) Custom parameters for the MongoDB Flex instance. (see [below for nested schema](#nestedatt--options)) - `replicas` (Number) diff --git a/docs/data-sources/mongodbflex_user.md b/docs/data-sources/mongodbflex_user.md index 326e65bb..4489321f 100644 --- a/docs/data-sources/mongodbflex_user.md +++ b/docs/data-sources/mongodbflex_user.md @@ -29,11 +29,15 @@ data "stackit_mongodbflex_user" "example" { - `project_id` (String) STACKIT project ID to which the instance is associated. - `user_id` (String) User ID. +### Optional + +- `region` (String) The resource region. If not defined, the provider region is used. + ### Read-Only - `database` (String) - `host` (String) -- `id` (String) Terraform's internal data source. ID. It is structured as "`project_id`,`instance_id`,`user_id`". +- `id` (String) Terraform's internal data source. ID. It is structured as "`project_id`,`region`,`instance_id`,`user_id`". - `port` (Number) - `roles` (Set of String) - `username` (String) diff --git a/docs/resources/mongodbflex_instance.md b/docs/resources/mongodbflex_instance.md index e0dc6a17..b09b8ced 100644 --- a/docs/resources/mongodbflex_instance.md +++ b/docs/resources/mongodbflex_instance.md @@ -51,9 +51,13 @@ resource "stackit_mongodbflex_instance" "example" { - `storage` (Attributes) (see [below for nested schema](#nestedatt--storage)) - `version` (String) +### Optional + +- `region` (String) The resource region. If not defined, the provider region is used. + ### Read-Only -- `id` (String) Terraform's internal resource ID. It is structured as "`project_id`,`instance_id`". +- `id` (String) Terraform's internal resource ID. It is structured as "`project_id`,`region`,`instance_id`". - `instance_id` (String) ID of the MongoDB Flex instance. diff --git a/docs/resources/mongodbflex_user.md b/docs/resources/mongodbflex_user.md index 517c718b..c2a7d234 100644 --- a/docs/resources/mongodbflex_user.md +++ b/docs/resources/mongodbflex_user.md @@ -34,6 +34,7 @@ resource "stackit_mongodbflex_user" "example" { ### Optional +- `region` (String) The resource region. If not defined, the provider region is used. - `username` (String) ### Read-Only diff --git a/go.mod b/go.mod index 659b7229..f4217dae 100644 --- a/go.mod +++ b/go.mod @@ -21,7 +21,7 @@ require ( github.com/stackitcloud/stackit-sdk-go/services/logme v0.25.0 github.com/stackitcloud/stackit-sdk-go/services/mariadb v0.25.0 github.com/stackitcloud/stackit-sdk-go/services/modelserving v0.5.0 - github.com/stackitcloud/stackit-sdk-go/services/mongodbflex v1.2.1 + github.com/stackitcloud/stackit-sdk-go/services/mongodbflex v1.4.0 github.com/stackitcloud/stackit-sdk-go/services/objectstorage v1.3.0 github.com/stackitcloud/stackit-sdk-go/services/observability v0.8.0 github.com/stackitcloud/stackit-sdk-go/services/opensearch v0.24.0 diff --git a/go.sum b/go.sum index 629a7f41..a7d301b0 100644 --- a/go.sum +++ b/go.sum @@ -172,8 +172,8 @@ github.com/stackitcloud/stackit-sdk-go/services/mariadb v0.25.0 h1:vxk6ztgzUIPMk github.com/stackitcloud/stackit-sdk-go/services/mariadb v0.25.0/go.mod h1:Pb8IEV5/jP8k75dVcN5cn3kP7PHTy/4KXXKpG76oj4U= github.com/stackitcloud/stackit-sdk-go/services/modelserving v0.5.0 h1:WMnCRB+zEWK5qq3d+MT5+nysuyXs+694/kSxgVYaXfk= github.com/stackitcloud/stackit-sdk-go/services/modelserving v0.5.0/go.mod h1:3p68DR66MNYgc2YcMF7B23MmySVShQ6g7f+7EkZfpPY= -github.com/stackitcloud/stackit-sdk-go/services/mongodbflex v1.2.1 h1:TWz7qJ4Mg5pquDXODSZ1dzhS95ZYn3w1aKjuRU2VqCg= -github.com/stackitcloud/stackit-sdk-go/services/mongodbflex v1.2.1/go.mod h1:U1Zf/S9IuDvRJq1tRKFT/bsJd4qxYzwtukqX3TL++Mw= +github.com/stackitcloud/stackit-sdk-go/services/mongodbflex v1.4.0 h1:il4vBOejtX0//CWuY6CDpFfPpDIvin5V9QIaKvyXV/M= +github.com/stackitcloud/stackit-sdk-go/services/mongodbflex v1.4.0/go.mod h1:U1Zf/S9IuDvRJq1tRKFT/bsJd4qxYzwtukqX3TL++Mw= github.com/stackitcloud/stackit-sdk-go/services/objectstorage v1.3.0 h1:bK5FDpSGfUKDXNkqOoiqOU9hua2YfcdYsGS4zQQ9wg0= github.com/stackitcloud/stackit-sdk-go/services/objectstorage v1.3.0/go.mod h1:Q2SJXlZTksvNAb1QnpGNfDSw/OMPN9uopaKuptUGhO8= github.com/stackitcloud/stackit-sdk-go/services/observability v0.8.0 h1:8v9Iupkc3meXcVPMpteXDz07YzY2M5jud50vEtAvbs8= diff --git a/stackit/internal/services/mongodbflex/instance/datasource.go b/stackit/internal/services/mongodbflex/instance/datasource.go index e85dac22..3ce92a3f 100644 --- a/stackit/internal/services/mongodbflex/instance/datasource.go +++ b/stackit/internal/services/mongodbflex/instance/datasource.go @@ -33,34 +33,36 @@ func NewInstanceDataSource() datasource.DataSource { // instanceDataSource is the data source implementation. type instanceDataSource struct { - client *mongodbflex.APIClient + client *mongodbflex.APIClient + providerData core.ProviderData } // Metadata returns the data source type name. -func (r *instanceDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { +func (d *instanceDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { resp.TypeName = req.ProviderTypeName + "_mongodbflex_instance" } // Configure adds the provider configured client to the data source. -func (r *instanceDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { - providerData, ok := conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics) +func (d *instanceDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { + var ok bool + d.providerData, ok = conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics) if !ok { return } - apiClient := mongodbflexUtils.ConfigureClient(ctx, &providerData, &resp.Diagnostics) + apiClient := mongodbflexUtils.ConfigureClient(ctx, &d.providerData, &resp.Diagnostics) if resp.Diagnostics.HasError() { return } - r.client = apiClient + d.client = apiClient tflog.Info(ctx, "MongoDB Flex instance client configured") } // Schema defines the schema for the data source. -func (r *instanceDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { +func (d *instanceDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { descriptions := map[string]string{ "main": "MongoDB Flex instance data source schema. Must have a `region` specified in the provider configuration.", - "id": "Terraform's internal data source ID. It is structured as \"`project_id`,`instance_id`\".", + "id": "Terraform's internal data source ID. It is structured as \"`project_id`,`region`,`instance_id`\".", "instance_id": "ID of the MongoDB Flex instance.", "project_id": "STACKIT project ID to which the instance is associated.", "name": "Instance name.", @@ -73,6 +75,7 @@ func (r *instanceDataSource) Schema(_ context.Context, _ datasource.SchemaReques "weekly_snapshot_retention_weeks": "The number of weeks that weekly backups will be retained.", "monthly_snapshot_retention_months": "The number of months that monthly backups will be retained.", "point_in_time_window_hours": "The number of hours back in time the point-in-time recovery feature will be able to recover.", + "region": "The resource region. If not defined, the provider region is used.", } resp.Schema = schema.Schema{ @@ -175,12 +178,18 @@ func (r *instanceDataSource) Schema(_ context.Context, _ datasource.SchemaReques }, }, }, + "region": schema.StringAttribute{ + Optional: true, + // must be computed to allow for storing the override value from the provider + Computed: true, + Description: descriptions["region"], + }, }, } } // Read refreshes the Terraform state with the latest data. -func (r *instanceDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { // nolint:gocritic // function signature required by Terraform +func (d *instanceDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { // nolint:gocritic // function signature required by Terraform var model Model diags := req.Config.Get(ctx, &model) resp.Diagnostics.Append(diags...) @@ -189,10 +198,12 @@ func (r *instanceDataSource) Read(ctx context.Context, req datasource.ReadReques } projectId := model.ProjectId.ValueString() + region := d.providerData.GetRegionWithOverride(model.Region) instanceId := model.InstanceId.ValueString() ctx = tflog.SetField(ctx, "project_id", projectId) + ctx = tflog.SetField(ctx, "region", region) ctx = tflog.SetField(ctx, "instance_id", instanceId) - instanceResp, err := r.client.GetInstance(ctx, projectId, instanceId).Execute() + instanceResp, err := d.client.GetInstance(ctx, projectId, instanceId, region).Execute() if err != nil { utils.LogError( ctx, @@ -233,7 +244,7 @@ func (r *instanceDataSource) Read(ctx context.Context, req datasource.ReadReques } } - err = mapFields(ctx, instanceResp, &model, flavor, storage, options) + err = mapFields(ctx, instanceResp, &model, flavor, storage, options, region) if err != nil { core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading instance", fmt.Sprintf("Processing API payload: %v", err)) return diff --git a/stackit/internal/services/mongodbflex/instance/resource.go b/stackit/internal/services/mongodbflex/instance/resource.go index 0d7537e7..c8aeeef2 100644 --- a/stackit/internal/services/mongodbflex/instance/resource.go +++ b/stackit/internal/services/mongodbflex/instance/resource.go @@ -39,6 +39,7 @@ var ( _ resource.Resource = &instanceResource{} _ resource.ResourceWithConfigure = &instanceResource{} _ resource.ResourceWithImportState = &instanceResource{} + _ resource.ResourceWithModifyPlan = &instanceResource{} ) type Model struct { @@ -53,6 +54,7 @@ type Model struct { Storage types.Object `tfsdk:"storage"` Version types.String `tfsdk:"version"` Options types.Object `tfsdk:"options"` + Region types.String `tfsdk:"region"` } // Struct corresponding to Model.Flavor @@ -110,7 +112,8 @@ func NewInstanceResource() resource.Resource { // instanceResource is the resource implementation. type instanceResource struct { - client *mongodbflex.APIClient + client *mongodbflex.APIClient + providerData core.ProviderData } // Metadata returns the resource type name. @@ -120,12 +123,13 @@ func (r *instanceResource) Metadata(_ context.Context, req resource.MetadataRequ // Configure adds the provider configured client to the resource. func (r *instanceResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { - providerData, ok := conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics) + var ok bool + r.providerData, ok = conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics) if !ok { return } - apiClient := mongodbflexUtils.ConfigureClient(ctx, &providerData, &resp.Diagnostics) + apiClient := mongodbflexUtils.ConfigureClient(ctx, &r.providerData, &resp.Diagnostics) if resp.Diagnostics.HasError() { return } @@ -133,13 +137,43 @@ func (r *instanceResource) Configure(ctx context.Context, req resource.Configure tflog.Info(ctx, "MongoDB Flex instance client configured") } +// ModifyPlan implements resource.ResourceWithModifyPlan. +// Use the modifier to set the effective region in the current plan. +func (r *instanceResource) ModifyPlan(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { // nolint:gocritic // function signature required by Terraform + var configModel Model + // skip initial empty configuration to avoid follow-up errors + if req.Config.Raw.IsNull() { + return + } + resp.Diagnostics.Append(req.Config.Get(ctx, &configModel)...) + if resp.Diagnostics.HasError() { + return + } + + var planModel Model + resp.Diagnostics.Append(req.Plan.Get(ctx, &planModel)...) + if resp.Diagnostics.HasError() { + return + } + + utils.AdaptRegion(ctx, configModel.Region, &planModel.Region, r.providerData.GetRegion(), resp) + if resp.Diagnostics.HasError() { + return + } + + resp.Diagnostics.Append(resp.Plan.Set(ctx, planModel)...) + if resp.Diagnostics.HasError() { + return + } +} + // Schema defines the schema for the resource. func (r *instanceResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { typeOptions := []string{"Replica", "Sharded", "Single"} descriptions := map[string]string{ "main": "MongoDB Flex instance resource schema. Must have a `region` specified in the provider configuration.", - "id": "Terraform's internal resource ID. It is structured as \"`project_id`,`instance_id`\".", + "id": "Terraform's internal resource ID. It is structured as \"`project_id`,`region`,`instance_id`\".", "instance_id": "ID of the MongoDB Flex instance.", "project_id": "STACKIT project ID to which the instance is associated.", "name": "Instance name.", @@ -152,6 +186,7 @@ func (r *instanceResource) Schema(_ context.Context, _ resource.SchemaRequest, r "weekly_snapshot_retention_weeks": "The number of weeks that weekly backups will be retained.", "monthly_snapshot_retention_months": "The number of months that monthly backups will be retained.", "point_in_time_window_hours": "The number of hours back in time the point-in-time recovery feature will be able to recover.", + "region": "The resource region. If not defined, the provider region is used.", } resp.Schema = schema.Schema{ @@ -298,6 +333,15 @@ func (r *instanceResource) Schema(_ context.Context, _ resource.SchemaRequest, r }, }, }, + "region": schema.StringAttribute{ + Optional: true, + // must be computed to allow for storing the override value from the provider + Computed: true, + Description: descriptions["region"], + PlanModifiers: []planmodifier.String{ + stringplanmodifier.RequiresReplace(), + }, + }, }, } } @@ -312,7 +356,9 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques return } projectId := model.ProjectId.ValueString() + region := r.providerData.GetRegionWithOverride(model.Region) ctx = tflog.SetField(ctx, "project_id", projectId) + ctx = tflog.SetField(ctx, "region", region) var acl []string if !(model.ACL.IsNull() || model.ACL.IsUnknown()) { @@ -329,7 +375,7 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques if resp.Diagnostics.HasError() { return } - err := loadFlavorId(ctx, r.client, &model, flavor) + err := loadFlavorId(ctx, r.client, &model, flavor, region) if err != nil { core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", fmt.Sprintf("Loading flavor ID: %v", err)) return @@ -360,7 +406,7 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques return } // Create new instance - createResp, err := r.client.CreateInstance(ctx, projectId).CreateInstancePayload(*payload).Execute() + createResp, err := r.client.CreateInstance(ctx, projectId, region).CreateInstancePayload(*payload).Execute() if err != nil { core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", fmt.Sprintf("Calling API: %v", err)) return @@ -385,14 +431,14 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques if resp.Diagnostics.HasError() { return } - waitResp, err := wait.CreateInstanceWaitHandler(ctx, r.client, projectId, instanceId).WaitWithContext(ctx) + waitResp, err := wait.CreateInstanceWaitHandler(ctx, r.client, projectId, instanceId, region).WaitWithContext(ctx) if err != nil { core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", fmt.Sprintf("Instance creation waiting: %v", err)) return } // Map response body to schema - err = mapFields(ctx, waitResp, &model, flavor, storage, options) + err = mapFields(ctx, waitResp, &model, flavor, storage, options, region) if err != nil { core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", fmt.Sprintf("Processing API payload: %v", err)) return @@ -409,7 +455,7 @@ func (r *instanceResource) Create(ctx context.Context, req resource.CreateReques core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", fmt.Sprintf("Creating API payload: %v", err)) return } - backupScheduleOptions, err := r.client.UpdateBackupSchedule(ctx, projectId, instanceId).UpdateBackupSchedulePayload(*backupScheduleOptionsPayload).Execute() + backupScheduleOptions, err := r.client.UpdateBackupSchedule(ctx, projectId, instanceId, region).UpdateBackupSchedulePayload(*backupScheduleOptionsPayload).Execute() if err != nil { core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", fmt.Sprintf("Updating options: %v", err)) return @@ -439,8 +485,10 @@ func (r *instanceResource) Read(ctx context.Context, req resource.ReadRequest, r return } projectId := model.ProjectId.ValueString() + region := r.providerData.GetRegionWithOverride(model.Region) instanceId := model.InstanceId.ValueString() ctx = tflog.SetField(ctx, "project_id", projectId) + ctx = tflog.SetField(ctx, "region", region) ctx = tflog.SetField(ctx, "instance_id", instanceId) var flavor = &flavorModel{} @@ -469,7 +517,7 @@ func (r *instanceResource) Read(ctx context.Context, req resource.ReadRequest, r } } - instanceResp, err := r.client.GetInstance(ctx, projectId, instanceId).Execute() + instanceResp, err := r.client.GetInstance(ctx, projectId, instanceId, region).Execute() if err != nil { 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 if ok && oapiErr.StatusCode == http.StatusNotFound { @@ -481,7 +529,7 @@ func (r *instanceResource) Read(ctx context.Context, req resource.ReadRequest, r } // Map response body to schema - err = mapFields(ctx, instanceResp, &model, flavor, storage, options) + err = mapFields(ctx, instanceResp, &model, flavor, storage, options, region) if err != nil { core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading instance", fmt.Sprintf("Processing API payload: %v", err)) return @@ -505,8 +553,10 @@ func (r *instanceResource) Update(ctx context.Context, req resource.UpdateReques return } projectId := model.ProjectId.ValueString() + region := r.providerData.GetRegionWithOverride(model.Region) instanceId := model.InstanceId.ValueString() ctx = tflog.SetField(ctx, "project_id", projectId) + ctx = tflog.SetField(ctx, "region", region) ctx = tflog.SetField(ctx, "instance_id", instanceId) var acl []string @@ -524,7 +574,7 @@ func (r *instanceResource) Update(ctx context.Context, req resource.UpdateReques if resp.Diagnostics.HasError() { return } - err := loadFlavorId(ctx, r.client, &model, flavor) + err := loadFlavorId(ctx, r.client, &model, flavor, region) if err != nil { core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating instance", fmt.Sprintf("Loading flavor ID: %v", err)) return @@ -555,19 +605,19 @@ func (r *instanceResource) Update(ctx context.Context, req resource.UpdateReques return } // Update existing instance - _, err = r.client.PartialUpdateInstance(ctx, projectId, instanceId).PartialUpdateInstancePayload(*payload).Execute() + _, err = r.client.PartialUpdateInstance(ctx, projectId, instanceId, region).PartialUpdateInstancePayload(*payload).Execute() if err != nil { core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating instance", err.Error()) return } - waitResp, err := wait.UpdateInstanceWaitHandler(ctx, r.client, projectId, instanceId).WaitWithContext(ctx) + waitResp, err := wait.UpdateInstanceWaitHandler(ctx, r.client, projectId, instanceId, region).WaitWithContext(ctx) if err != nil { core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating instance", fmt.Sprintf("Instance update waiting: %v", err)) return } // Map response body to schema - err = mapFields(ctx, waitResp, &model, flavor, storage, options) + err = mapFields(ctx, waitResp, &model, flavor, storage, options, region) if err != nil { core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating instance", fmt.Sprintf("Processing API payload: %v", err)) return @@ -578,7 +628,7 @@ func (r *instanceResource) Update(ctx context.Context, req resource.UpdateReques core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating instance", fmt.Sprintf("Creating API payload: %v", err)) return } - backupScheduleOptions, err := r.client.UpdateBackupSchedule(ctx, projectId, instanceId).UpdateBackupSchedulePayload(*backupScheduleOptionsPayload).Execute() + backupScheduleOptions, err := r.client.UpdateBackupSchedule(ctx, projectId, instanceId, region).UpdateBackupSchedulePayload(*backupScheduleOptionsPayload).Execute() if err != nil { core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating instance", fmt.Sprintf("Updating options: %v", err)) return @@ -608,17 +658,19 @@ func (r *instanceResource) Delete(ctx context.Context, req resource.DeleteReques return } projectId := model.ProjectId.ValueString() + region := r.providerData.GetRegionWithOverride(model.Region) instanceId := model.InstanceId.ValueString() ctx = tflog.SetField(ctx, "project_id", projectId) + ctx = tflog.SetField(ctx, "region", region) ctx = tflog.SetField(ctx, "instance_id", instanceId) // Delete existing instance - err := r.client.DeleteInstance(ctx, projectId, instanceId).Execute() + err := r.client.DeleteInstance(ctx, projectId, instanceId, region).Execute() if err != nil { core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting instance", fmt.Sprintf("Calling API: %v", err)) return } - _, err = wait.DeleteInstanceWaitHandler(ctx, r.client, projectId, instanceId).WaitWithContext(ctx) + _, err = wait.DeleteInstanceWaitHandler(ctx, r.client, projectId, instanceId, region).WaitWithContext(ctx) if err != nil { core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting instance", fmt.Sprintf("Instance deletion waiting: %v", err)) return @@ -636,20 +688,21 @@ func (r *instanceResource) Delete(ctx context.Context, req resource.DeleteReques func (r *instanceResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { idParts := strings.Split(req.ID, core.Separator) - if len(idParts) != 2 || idParts[0] == "" || idParts[1] == "" { + if len(idParts) != 3 || idParts[0] == "" || idParts[1] == "" || idParts[2] == "" { core.LogAndAddError(ctx, &resp.Diagnostics, "Error importing instance", - fmt.Sprintf("Expected import identifier with format: [project_id],[instance_id] Got: %q", req.ID), + fmt.Sprintf("Expected import identifier with format: [project_id],[region],[instance_id] Got: %q", req.ID), ) return } resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("project_id"), idParts[0])...) - resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("instance_id"), idParts[1])...) + resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("region"), idParts[1])...) + resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("instance_id"), idParts[2])...) tflog.Info(ctx, "MongoDB Flex instance state imported") } -func mapFields(ctx context.Context, resp *mongodbflex.GetInstanceResponse, model *Model, flavor *flavorModel, storage *storageModel, options *optionsModel) error { +func mapFields(ctx context.Context, resp *mongodbflex.GetInstanceResponse, model *Model, flavor *flavorModel, storage *storageModel, options *optionsModel, region string) error { if resp == nil { return fmt.Errorf("response input is nil") } @@ -785,7 +838,8 @@ func mapFields(ctx context.Context, resp *mongodbflex.GetInstanceResponse, model model.BackupSchedule = types.StringPointerValue(instance.BackupSchedule) } - model.Id = utils.BuildInternalTerraformId(model.ProjectId.ValueString(), instanceId) + model.Id = utils.BuildInternalTerraformId(model.ProjectId.ValueString(), region, instanceId) + model.Region = types.StringValue(region) model.InstanceId = types.StringValue(instanceId) model.Name = types.StringPointerValue(instance.Name) model.ACL = aclList @@ -849,7 +903,7 @@ func toCreatePayload(model *Model, acl []string, flavor *flavorModel, storage *s } return &mongodbflex.CreateInstancePayload{ - Acl: &mongodbflex.ACL{ + Acl: &mongodbflex.CreateInstancePayloadAcl{ Items: &acl, }, BackupSchedule: conversion.StringValueToPointer(model.BackupSchedule), @@ -956,10 +1010,10 @@ func toUpdateBackupScheduleOptionsPayload(ctx context.Context, model *Model, con } type mongoDBFlexClient interface { - ListFlavorsExecute(ctx context.Context, projectId string) (*mongodbflex.ListFlavorsResponse, error) + ListFlavorsExecute(ctx context.Context, projectId, region string) (*mongodbflex.ListFlavorsResponse, error) } -func loadFlavorId(ctx context.Context, client mongoDBFlexClient, model *Model, flavor *flavorModel) error { +func loadFlavorId(ctx context.Context, client mongoDBFlexClient, model *Model, flavor *flavorModel, region string) error { if model == nil { return fmt.Errorf("nil model") } @@ -976,7 +1030,7 @@ func loadFlavorId(ctx context.Context, client mongoDBFlexClient, model *Model, f } projectId := model.ProjectId.ValueString() - res, err := client.ListFlavorsExecute(ctx, projectId) + res, err := client.ListFlavorsExecute(ctx, projectId, region) if err != nil { return fmt.Errorf("listing mongodbflex flavors: %w", err) } diff --git a/stackit/internal/services/mongodbflex/instance/resource_test.go b/stackit/internal/services/mongodbflex/instance/resource_test.go index a2e4e933..61f6f54e 100644 --- a/stackit/internal/services/mongodbflex/instance/resource_test.go +++ b/stackit/internal/services/mongodbflex/instance/resource_test.go @@ -5,6 +5,8 @@ import ( "fmt" "testing" + "github.com/google/uuid" + "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/hashicorp/terraform-plugin-framework/attr" @@ -13,12 +15,21 @@ import ( "github.com/stackitcloud/stackit-sdk-go/services/mongodbflex" ) +const ( + testRegion = "eu02" +) + +var ( + projectId = uuid.NewString() + instanceId = uuid.NewString() +) + type mongoDBFlexClientMocked struct { returnError bool listFlavorsResp *mongodbflex.ListFlavorsResponse } -func (c *mongoDBFlexClientMocked) ListFlavorsExecute(_ context.Context, _ string) (*mongodbflex.ListFlavorsResponse, error) { +func (c *mongoDBFlexClientMocked) ListFlavorsExecute(_ context.Context, _, _ string) (*mongodbflex.ListFlavorsResponse, error) { if c.returnError { return nil, fmt.Errorf("get flavors failed") } @@ -34,14 +45,15 @@ func TestMapFields(t *testing.T) { flavor *flavorModel storage *storageModel options *optionsModel + region string expected Model isValid bool }{ { "default_values", Model{ - InstanceId: types.StringValue("iid"), - ProjectId: types.StringValue("pid"), + InstanceId: types.StringValue(instanceId), + ProjectId: types.StringValue(projectId), }, &mongodbflex.GetInstanceResponse{ Item: &mongodbflex.Instance{}, @@ -49,10 +61,11 @@ func TestMapFields(t *testing.T) { &flavorModel{}, &storageModel{}, &optionsModel{}, + testRegion, Model{ - Id: types.StringValue("pid,iid"), - InstanceId: types.StringValue("iid"), - ProjectId: types.StringValue("pid"), + Id: types.StringValue(fmt.Sprintf("%s,%s,%s", projectId, testRegion, instanceId)), + InstanceId: types.StringValue(instanceId), + ProjectId: types.StringValue(projectId), Name: types.StringNull(), ACL: types.ListNull(types.StringType), BackupSchedule: types.StringNull(), @@ -76,14 +89,15 @@ func TestMapFields(t *testing.T) { "point_in_time_window_hours": types.Int64Null(), }), Version: types.StringNull(), + Region: types.StringValue(testRegion), }, true, }, { "simple_values", Model{ - InstanceId: types.StringValue("iid"), - ProjectId: types.StringValue("pid"), + InstanceId: types.StringValue(instanceId), + ProjectId: types.StringValue(projectId), }, &mongodbflex.GetInstanceResponse{ Item: &mongodbflex.Instance{ @@ -101,7 +115,7 @@ func TestMapFields(t *testing.T) { Id: utils.Ptr("flavor_id"), Memory: utils.Ptr(int64(34)), }, - Id: utils.Ptr("iid"), + Id: utils.Ptr(instanceId), Name: utils.Ptr("name"), Replicas: utils.Ptr(int64(56)), Status: mongodbflex.INSTANCESTATUS_READY.Ptr(), @@ -123,10 +137,11 @@ func TestMapFields(t *testing.T) { &flavorModel{}, &storageModel{}, &optionsModel{}, + testRegion, Model{ - Id: types.StringValue("pid,iid"), - InstanceId: types.StringValue("iid"), - ProjectId: types.StringValue("pid"), + Id: types.StringValue(fmt.Sprintf("%s,%s,%s", projectId, testRegion, instanceId)), + InstanceId: types.StringValue(instanceId), + ProjectId: types.StringValue(projectId), Name: types.StringValue("name"), ACL: types.ListValueMust(types.StringType, []attr.Value{ types.StringValue("ip1"), @@ -153,6 +168,7 @@ func TestMapFields(t *testing.T) { "monthly_snapshot_retention_months": types.Int64Value(8), "point_in_time_window_hours": types.Int64Value(9), }), + Region: types.StringValue(testRegion), Version: types.StringValue("version"), }, true, @@ -160,8 +176,8 @@ func TestMapFields(t *testing.T) { { "simple_values_no_flavor_and_storage", Model{ - InstanceId: types.StringValue("iid"), - ProjectId: types.StringValue("pid"), + InstanceId: types.StringValue(instanceId), + ProjectId: types.StringValue(projectId), }, &mongodbflex.GetInstanceResponse{ Item: &mongodbflex.Instance{ @@ -174,7 +190,7 @@ func TestMapFields(t *testing.T) { }, BackupSchedule: utils.Ptr("schedule"), Flavor: nil, - Id: utils.Ptr("iid"), + Id: utils.Ptr(instanceId), Name: utils.Ptr("name"), Replicas: utils.Ptr(int64(56)), Status: mongodbflex.INSTANCESTATUS_READY.Ptr(), @@ -201,10 +217,11 @@ func TestMapFields(t *testing.T) { &optionsModel{ Type: types.StringValue("type"), }, + testRegion, Model{ - Id: types.StringValue("pid,iid"), - InstanceId: types.StringValue("iid"), - ProjectId: types.StringValue("pid"), + Id: types.StringValue(fmt.Sprintf("%s,%s,%s", projectId, testRegion, instanceId)), + InstanceId: types.StringValue(instanceId), + ProjectId: types.StringValue(projectId), Name: types.StringValue("name"), ACL: types.ListValueMust(types.StringType, []attr.Value{ types.StringValue("ip1"), @@ -231,6 +248,7 @@ func TestMapFields(t *testing.T) { "monthly_snapshot_retention_months": types.Int64Value(8), "point_in_time_window_hours": types.Int64Value(9), }), + Region: types.StringValue(testRegion), Version: types.StringValue("version"), }, true, @@ -238,8 +256,8 @@ func TestMapFields(t *testing.T) { { "acls_unordered", Model{ - InstanceId: types.StringValue("iid"), - ProjectId: types.StringValue("pid"), + InstanceId: types.StringValue(instanceId), + ProjectId: types.StringValue(projectId), ACL: types.ListValueMust(types.StringType, []attr.Value{ types.StringValue("ip2"), types.StringValue(""), @@ -257,7 +275,7 @@ func TestMapFields(t *testing.T) { }, BackupSchedule: utils.Ptr("schedule"), Flavor: nil, - Id: utils.Ptr("iid"), + Id: utils.Ptr(instanceId), Name: utils.Ptr("name"), Replicas: utils.Ptr(int64(56)), Status: mongodbflex.INSTANCESTATUS_READY.Ptr(), @@ -284,10 +302,11 @@ func TestMapFields(t *testing.T) { &optionsModel{ Type: types.StringValue("type"), }, + testRegion, Model{ - Id: types.StringValue("pid,iid"), - InstanceId: types.StringValue("iid"), - ProjectId: types.StringValue("pid"), + Id: types.StringValue(fmt.Sprintf("%s,%s,%s", projectId, testRegion, instanceId)), + InstanceId: types.StringValue(instanceId), + ProjectId: types.StringValue(projectId), Name: types.StringValue("name"), ACL: types.ListValueMust(types.StringType, []attr.Value{ types.StringValue("ip2"), @@ -314,6 +333,7 @@ func TestMapFields(t *testing.T) { "monthly_snapshot_retention_months": types.Int64Value(8), "point_in_time_window_hours": types.Int64Value(9), }), + Region: types.StringValue(testRegion), Version: types.StringValue("version"), }, true, @@ -321,33 +341,35 @@ func TestMapFields(t *testing.T) { { "nil_response", Model{ - InstanceId: types.StringValue("iid"), - ProjectId: types.StringValue("pid"), + InstanceId: types.StringValue(instanceId), + ProjectId: types.StringValue(projectId), }, nil, &flavorModel{}, &storageModel{}, &optionsModel{}, + testRegion, Model{}, false, }, { "no_resource_id", Model{ - InstanceId: types.StringValue("iid"), - ProjectId: types.StringValue("pid"), + InstanceId: types.StringValue(instanceId), + ProjectId: types.StringValue(projectId), }, &mongodbflex.GetInstanceResponse{}, &flavorModel{}, &storageModel{}, &optionsModel{}, + testRegion, Model{}, false, }, } for _, tt := range tests { t.Run(tt.description, func(t *testing.T) { - err := mapFields(context.Background(), tt.input, &tt.state, tt.flavor, tt.storage, tt.options) + err := mapFields(context.Background(), tt.input, &tt.state, tt.flavor, tt.storage, tt.options, tt.region) if !tt.isValid && err == nil { t.Fatalf("Should have failed") } @@ -454,7 +476,7 @@ func TestToCreatePayload(t *testing.T) { &storageModel{}, &optionsModel{}, &mongodbflex.CreateInstancePayload{ - Acl: &mongodbflex.ACL{ + Acl: &mongodbflex.CreateInstancePayloadAcl{ Items: &[]string{}, }, Storage: &mongodbflex.Storage{}, @@ -485,7 +507,7 @@ func TestToCreatePayload(t *testing.T) { Type: types.StringValue("type"), }, &mongodbflex.CreateInstancePayload{ - Acl: &mongodbflex.ACL{ + Acl: &mongodbflex.CreateInstancePayloadAcl{ Items: &[]string{ "ip_1", "ip_2", @@ -526,7 +548,7 @@ func TestToCreatePayload(t *testing.T) { Type: types.StringNull(), }, &mongodbflex.CreateInstancePayload{ - Acl: &mongodbflex.ACL{ + Acl: &mongodbflex.CreateInstancePayloadAcl{ Items: &[]string{ "", }, @@ -933,7 +955,7 @@ func TestLoadFlavorId(t *testing.T) { RAM: types.Int64Value(8), }, &mongodbflex.ListFlavorsResponse{ - Flavors: &[]mongodbflex.HandlersInfraFlavor{ + Flavors: &[]mongodbflex.InstanceFlavor{ { Id: utils.Ptr("fid-1"), Cpu: utils.Ptr(int64(2)), @@ -958,7 +980,7 @@ func TestLoadFlavorId(t *testing.T) { RAM: types.Int64Value(8), }, &mongodbflex.ListFlavorsResponse{ - Flavors: &[]mongodbflex.HandlersInfraFlavor{ + Flavors: &[]mongodbflex.InstanceFlavor{ { Id: utils.Ptr("fid-1"), Cpu: utils.Ptr(int64(2)), @@ -989,7 +1011,7 @@ func TestLoadFlavorId(t *testing.T) { RAM: types.Int64Value(8), }, &mongodbflex.ListFlavorsResponse{ - Flavors: &[]mongodbflex.HandlersInfraFlavor{ + Flavors: &[]mongodbflex.InstanceFlavor{ { Id: utils.Ptr("fid-1"), Cpu: utils.Ptr(int64(1)), @@ -1047,13 +1069,13 @@ func TestLoadFlavorId(t *testing.T) { listFlavorsResp: tt.mockedResp, } model := &Model{ - ProjectId: types.StringValue("pid"), + ProjectId: types.StringValue(projectId), } flavorModel := &flavorModel{ CPU: tt.inputFlavor.CPU, RAM: tt.inputFlavor.RAM, } - err := loadFlavorId(context.Background(), client, model, flavorModel) + err := loadFlavorId(context.Background(), client, model, flavorModel, testRegion) if !tt.isValid && err == nil { t.Fatalf("Should have failed") } diff --git a/stackit/internal/services/mongodbflex/mongodbflex_acc_test.go b/stackit/internal/services/mongodbflex/mongodbflex_acc_test.go index 97776e2c..0ab9598f 100644 --- a/stackit/internal/services/mongodbflex/mongodbflex_acc_test.go +++ b/stackit/internal/services/mongodbflex/mongodbflex_acc_test.go @@ -29,8 +29,8 @@ var instanceResource = map[string]string{ "replicas": "3", "storage_class": "premium-perf2-mongodb", "storage_size": "10", - "version": "6.0", - "version_updated": "7.0", + "version": "7.0", + "version_updated": "8.0", "options_type": "Replica", "flavor_id": "2.4", "backup_schedule": "00 6 * * *", @@ -217,12 +217,16 @@ func TestAccMongoDBFlexFlexResource(t *testing.T) { if !ok { return "", fmt.Errorf("couldn't find resource stackit_mongodbflex_instance.instance") } + region, ok := r.Primary.Attributes["region"] + if !ok { + return "", fmt.Errorf("couldn't find attribute region") + } instanceId, ok := r.Primary.Attributes["instance_id"] if !ok { return "", fmt.Errorf("couldn't find attribute instance_id") } - return fmt.Sprintf("%s,%s", testutil.ProjectId, instanceId), nil + return fmt.Sprintf("%s,%s,%s", testutil.ProjectId, region, instanceId), nil }, ImportState: true, ImportStateVerify: true, @@ -244,6 +248,10 @@ func TestAccMongoDBFlexFlexResource(t *testing.T) { if !ok { return "", fmt.Errorf("couldn't find resource stackit_mongodbflex_user.user") } + region, ok := r.Primary.Attributes["region"] + if !ok { + return "", fmt.Errorf("couldn't find attribute region") + } instanceId, ok := r.Primary.Attributes["instance_id"] if !ok { return "", fmt.Errorf("couldn't find attribute instance_id") @@ -253,7 +261,7 @@ func TestAccMongoDBFlexFlexResource(t *testing.T) { return "", fmt.Errorf("couldn't find attribute user_id") } - return fmt.Sprintf("%s,%s,%s", testutil.ProjectId, instanceId, userId), nil + return fmt.Sprintf("%s,%s,%s,%s", testutil.ProjectId, region, instanceId, userId), nil }, ImportState: true, ImportStateVerify: true, @@ -293,9 +301,7 @@ func testAccCheckMongoDBFlexDestroy(s *terraform.State) error { var client *mongodbflex.APIClient var err error if testutil.MongoDBFlexCustomEndpoint == "" { - client, err = mongodbflex.NewAPIClient( - config.WithRegion("eu01"), - ) + client, err = mongodbflex.NewAPIClient() } else { client, err = mongodbflex.NewAPIClient( config.WithEndpoint(testutil.MongoDBFlexCustomEndpoint), @@ -310,12 +316,12 @@ func testAccCheckMongoDBFlexDestroy(s *terraform.State) error { if rs.Type != "stackit_mongodbflex_instance" { continue } - // instance terraform ID: = "[project_id],[instance_id]" - instanceId := strings.Split(rs.Primary.ID, core.Separator)[1] + // instance terraform ID: = "[project_id],[region],[instance_id]" + instanceId := strings.Split(rs.Primary.ID, core.Separator)[2] instancesToDestroy = append(instancesToDestroy, instanceId) } - instancesResp, err := client.ListInstances(ctx, testutil.ProjectId).Tag("").Execute() + instancesResp, err := client.ListInstances(ctx, testutil.ProjectId, testutil.Region).Tag("").Execute() if err != nil { return fmt.Errorf("getting instancesResp: %w", err) } @@ -326,11 +332,11 @@ func testAccCheckMongoDBFlexDestroy(s *terraform.State) error { continue } if utils.Contains(instancesToDestroy, *items[i].Id) { - err := client.DeleteInstanceExecute(ctx, testutil.ProjectId, *items[i].Id) + err := client.DeleteInstanceExecute(ctx, testutil.ProjectId, *items[i].Id, testutil.Region) if err != nil { return fmt.Errorf("destroying instance %s during CheckDestroy: %w", *items[i].Id, err) } - _, err = wait.DeleteInstanceWaitHandler(ctx, client, testutil.ProjectId, *items[i].Id).WaitWithContext(ctx) + _, err = wait.DeleteInstanceWaitHandler(ctx, client, testutil.ProjectId, *items[i].Id, testutil.Region).WaitWithContext(ctx) if err != nil { return fmt.Errorf("destroying instance %s during CheckDestroy: waiting for deletion %w", *items[i].Id, err) } diff --git a/stackit/internal/services/mongodbflex/user/datasource.go b/stackit/internal/services/mongodbflex/user/datasource.go index b6d8c8a0..3a7e9f02 100644 --- a/stackit/internal/services/mongodbflex/user/datasource.go +++ b/stackit/internal/services/mongodbflex/user/datasource.go @@ -36,6 +36,7 @@ type DataSourceModel struct { Roles types.Set `tfsdk:"roles"` Host types.String `tfsdk:"host"` Port types.Int64 `tfsdk:"port"` + Region types.String `tfsdk:"region"` } // NewUserDataSource is a helper function to simplify the provider implementation. @@ -45,37 +46,40 @@ func NewUserDataSource() datasource.DataSource { // userDataSource is the data source implementation. type userDataSource struct { - client *mongodbflex.APIClient + client *mongodbflex.APIClient + providerData core.ProviderData } // Metadata returns the data source type name. -func (r *userDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { +func (d *userDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { resp.TypeName = req.ProviderTypeName + "_mongodbflex_user" } // Configure adds the provider configured client to the data source. -func (r *userDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { - providerData, ok := conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics) +func (d *userDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { + var ok bool + d.providerData, ok = conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics) if !ok { return } - apiClient := mongodbflexUtils.ConfigureClient(ctx, &providerData, &resp.Diagnostics) + apiClient := mongodbflexUtils.ConfigureClient(ctx, &d.providerData, &resp.Diagnostics) if resp.Diagnostics.HasError() { return } - r.client = apiClient + d.client = apiClient tflog.Info(ctx, "MongoDB Flex user client configured") } // Schema defines the schema for the data source. -func (r *userDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { +func (d *userDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { descriptions := map[string]string{ "main": "MongoDB Flex user data source schema. Must have a `region` specified in the provider configuration.", - "id": "Terraform's internal data source. ID. It is structured as \"`project_id`,`instance_id`,`user_id`\".", + "id": "Terraform's internal data source. ID. It is structured as \"`project_id`,`region`,`instance_id`,`user_id`\".", "user_id": "User ID.", "instance_id": "ID of the MongoDB Flex instance.", "project_id": "STACKIT project ID to which the instance is associated.", + "region": "The resource region. If not defined, the provider region is used.", } resp.Schema = schema.Schema{ @@ -124,12 +128,18 @@ func (r *userDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, r "port": schema.Int64Attribute{ Computed: true, }, + "region": schema.StringAttribute{ + Optional: true, + // must be computed to allow for storing the override value from the provider + Computed: true, + Description: descriptions["region"], + }, }, } } // Read refreshes the Terraform state with the latest data. -func (r *userDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { // nolint:gocritic // function signature required by Terraform +func (d *userDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { // nolint:gocritic // function signature required by Terraform var model DataSourceModel diags := req.Config.Get(ctx, &model) resp.Diagnostics.Append(diags...) @@ -137,13 +147,15 @@ func (r *userDataSource) Read(ctx context.Context, req datasource.ReadRequest, r return } projectId := model.ProjectId.ValueString() + region := d.providerData.GetRegionWithOverride(model.Region) instanceId := model.InstanceId.ValueString() userId := model.UserId.ValueString() ctx = tflog.SetField(ctx, "project_id", projectId) + ctx = tflog.SetField(ctx, "region", region) ctx = tflog.SetField(ctx, "instance_id", instanceId) ctx = tflog.SetField(ctx, "user_id", userId) - recordSetResp, err := r.client.GetUser(ctx, projectId, instanceId, userId).Execute() + recordSetResp, err := d.client.GetUser(ctx, projectId, instanceId, userId, region).Execute() if err != nil { utils.LogError( ctx, @@ -160,7 +172,7 @@ func (r *userDataSource) Read(ctx context.Context, req datasource.ReadRequest, r } // Map response body to schema and populate Computed attribute values - err = mapDataSourceFields(recordSetResp, &model) + err = mapDataSourceFields(recordSetResp, &model, region) if err != nil { core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading user", fmt.Sprintf("Processing API payload: %v", err)) return @@ -175,7 +187,7 @@ func (r *userDataSource) Read(ctx context.Context, req datasource.ReadRequest, r tflog.Info(ctx, "MongoDB Flex user read") } -func mapDataSourceFields(userResp *mongodbflex.GetUserResponse, model *DataSourceModel) error { +func mapDataSourceFields(userResp *mongodbflex.GetUserResponse, model *DataSourceModel, region string) error { if userResp == nil || userResp.Item == nil { return fmt.Errorf("response is nil") } @@ -192,7 +204,7 @@ func mapDataSourceFields(userResp *mongodbflex.GetUserResponse, model *DataSourc } else { return fmt.Errorf("user id not present") } - model.Id = utils.BuildInternalTerraformId(model.ProjectId.ValueString(), model.InstanceId.ValueString(), userId) + model.Id = utils.BuildInternalTerraformId(model.ProjectId.ValueString(), region, model.InstanceId.ValueString(), userId) model.UserId = types.StringValue(userId) model.Username = types.StringPointerValue(user.Username) model.Database = types.StringPointerValue(user.Database) diff --git a/stackit/internal/services/mongodbflex/user/datasource_test.go b/stackit/internal/services/mongodbflex/user/datasource_test.go index e96d1f6f..e5ce87cf 100644 --- a/stackit/internal/services/mongodbflex/user/datasource_test.go +++ b/stackit/internal/services/mongodbflex/user/datasource_test.go @@ -1,6 +1,7 @@ package mongodbflex import ( + "fmt" "testing" "github.com/google/go-cmp/cmp" @@ -14,6 +15,7 @@ func TestMapDataSourceFields(t *testing.T) { tests := []struct { description string input *mongodbflex.GetUserResponse + region string expected DataSourceModel isValid bool }{ @@ -22,11 +24,12 @@ func TestMapDataSourceFields(t *testing.T) { &mongodbflex.GetUserResponse{ Item: &mongodbflex.InstanceResponseUser{}, }, + testRegion, DataSourceModel{ - Id: types.StringValue("pid,iid,uid"), - UserId: types.StringValue("uid"), - InstanceId: types.StringValue("iid"), - ProjectId: types.StringValue("pid"), + Id: types.StringValue(fmt.Sprintf("%s,%s,%s,%s", projectId, testRegion, instanceId, userId)), + UserId: types.StringValue(userId), + InstanceId: types.StringValue(instanceId), + ProjectId: types.StringValue(projectId), Username: types.StringNull(), Database: types.StringNull(), Roles: types.SetNull(types.StringType), @@ -50,11 +53,12 @@ func TestMapDataSourceFields(t *testing.T) { Port: utils.Ptr(int64(1234)), }, }, + testRegion, DataSourceModel{ - Id: types.StringValue("pid,iid,uid"), - UserId: types.StringValue("uid"), - InstanceId: types.StringValue("iid"), - ProjectId: types.StringValue("pid"), + Id: types.StringValue(fmt.Sprintf("%s,%s,%s,%s", projectId, testRegion, instanceId, userId)), + UserId: types.StringValue(userId), + InstanceId: types.StringValue(instanceId), + ProjectId: types.StringValue(projectId), Username: types.StringValue("username"), Database: types.StringValue("database"), Roles: types.SetValueMust(types.StringType, []attr.Value{ @@ -71,7 +75,7 @@ func TestMapDataSourceFields(t *testing.T) { "null_fields_and_int_conversions", &mongodbflex.GetUserResponse{ Item: &mongodbflex.InstanceResponseUser{ - Id: utils.Ptr("uid"), + Id: utils.Ptr(userId), Roles: &[]string{}, Username: nil, Database: nil, @@ -79,11 +83,12 @@ func TestMapDataSourceFields(t *testing.T) { Port: utils.Ptr(int64(2123456789)), }, }, + testRegion, DataSourceModel{ - Id: types.StringValue("pid,iid,uid"), - UserId: types.StringValue("uid"), - InstanceId: types.StringValue("iid"), - ProjectId: types.StringValue("pid"), + Id: types.StringValue(fmt.Sprintf("%s,%s,%s,%s", projectId, testRegion, instanceId, userId)), + UserId: types.StringValue(userId), + InstanceId: types.StringValue(instanceId), + ProjectId: types.StringValue(projectId), Username: types.StringNull(), Database: types.StringNull(), Roles: types.SetValueMust(types.StringType, []attr.Value{}), @@ -95,12 +100,14 @@ func TestMapDataSourceFields(t *testing.T) { { "nil_response", nil, + testRegion, DataSourceModel{}, false, }, { "nil_response_2", &mongodbflex.GetUserResponse{}, + testRegion, DataSourceModel{}, false, }, @@ -109,6 +116,7 @@ func TestMapDataSourceFields(t *testing.T) { &mongodbflex.GetUserResponse{ Item: &mongodbflex.InstanceResponseUser{}, }, + testRegion, DataSourceModel{}, false, }, @@ -120,7 +128,7 @@ func TestMapDataSourceFields(t *testing.T) { InstanceId: tt.expected.InstanceId, UserId: tt.expected.UserId, } - err := mapDataSourceFields(tt.input, state) + err := mapDataSourceFields(tt.input, state, tt.region) if !tt.isValid && err == nil { t.Fatalf("Should have failed") } diff --git a/stackit/internal/services/mongodbflex/user/resource.go b/stackit/internal/services/mongodbflex/user/resource.go index 6be17603..4a763e17 100644 --- a/stackit/internal/services/mongodbflex/user/resource.go +++ b/stackit/internal/services/mongodbflex/user/resource.go @@ -32,6 +32,7 @@ var ( _ resource.Resource = &userResource{} _ resource.ResourceWithConfigure = &userResource{} _ resource.ResourceWithImportState = &userResource{} + _ resource.ResourceWithModifyPlan = &userResource{} ) type Model struct { @@ -46,6 +47,7 @@ type Model struct { Host types.String `tfsdk:"host"` Port types.Int64 `tfsdk:"port"` Uri types.String `tfsdk:"uri"` + Region types.String `tfsdk:"region"` } // NewUserResource is a helper function to simplify the provider implementation. @@ -55,7 +57,8 @@ func NewUserResource() resource.Resource { // userResource is the resource implementation. type userResource struct { - client *mongodbflex.APIClient + client *mongodbflex.APIClient + providerData core.ProviderData } // Metadata returns the resource type name. @@ -65,12 +68,13 @@ func (r *userResource) Metadata(_ context.Context, req resource.MetadataRequest, // Configure adds the provider configured client to the resource. func (r *userResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { - providerData, ok := conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics) + var ok bool + r.providerData, ok = conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics) if !ok { return } - apiClient := mongodbflexUtils.ConfigureClient(ctx, &providerData, &resp.Diagnostics) + apiClient := mongodbflexUtils.ConfigureClient(ctx, &r.providerData, &resp.Diagnostics) if resp.Diagnostics.HasError() { return } @@ -78,6 +82,36 @@ func (r *userResource) Configure(ctx context.Context, req resource.ConfigureRequ tflog.Info(ctx, "MongoDB Flex user client configured") } +// ModifyPlan implements resource.ResourceWithModifyPlan. +// Use the modifier to set the effective region in the current plan. +func (r *userResource) ModifyPlan(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { // nolint:gocritic // function signature required by Terraform + var configModel Model + // skip initial empty configuration to avoid follow-up errors + if req.Config.Raw.IsNull() { + return + } + resp.Diagnostics.Append(req.Config.Get(ctx, &configModel)...) + if resp.Diagnostics.HasError() { + return + } + + var planModel Model + resp.Diagnostics.Append(req.Plan.Get(ctx, &planModel)...) + if resp.Diagnostics.HasError() { + return + } + + utils.AdaptRegion(ctx, configModel.Region, &planModel.Region, r.providerData.GetRegion(), resp) + if resp.Diagnostics.HasError() { + return + } + + resp.Diagnostics.Append(resp.Plan.Set(ctx, planModel)...) + if resp.Diagnostics.HasError() { + return + } +} + // Schema defines the schema for the resource. func (r *userResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { descriptions := map[string]string{ @@ -87,6 +121,7 @@ func (r *userResource) Schema(_ context.Context, _ resource.SchemaRequest, resp "instance_id": "ID of the MongoDB Flex instance.", "project_id": "STACKIT project ID to which the instance is associated.", "roles": "Database access levels for the user. Some of the possible values are: [`read`, `readWrite`, `readWriteAnyDatabase`]", + "region": "The resource region. If not defined, the provider region is used.", } resp.Schema = schema.Schema{ @@ -166,6 +201,15 @@ func (r *userResource) Schema(_ context.Context, _ resource.SchemaRequest, resp Computed: true, Sensitive: true, }, + "region": schema.StringAttribute{ + Optional: true, + // must be computed to allow for storing the override value from the provider + Computed: true, + Description: descriptions["region"], + PlanModifiers: []planmodifier.String{ + stringplanmodifier.RequiresReplace(), + }, + }, }, } } @@ -179,8 +223,10 @@ func (r *userResource) Create(ctx context.Context, req resource.CreateRequest, r return } projectId := model.ProjectId.ValueString() + region := r.providerData.GetRegionWithOverride(model.Region) instanceId := model.InstanceId.ValueString() ctx = tflog.SetField(ctx, "project_id", projectId) + ctx = tflog.SetField(ctx, "region", region) ctx = tflog.SetField(ctx, "instance_id", instanceId) var roles []string @@ -199,7 +245,7 @@ func (r *userResource) Create(ctx context.Context, req resource.CreateRequest, r return } // Create new user - userResp, err := r.client.CreateUser(ctx, projectId, instanceId).CreateUserPayload(*payload).Execute() + userResp, err := r.client.CreateUser(ctx, projectId, instanceId, region).CreateUserPayload(*payload).Execute() if err != nil { core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating user", fmt.Sprintf("Calling API: %v", err)) return @@ -212,7 +258,7 @@ func (r *userResource) Create(ctx context.Context, req resource.CreateRequest, r ctx = tflog.SetField(ctx, "user_id", userId) // Map response body to schema - err = mapFieldsCreate(userResp, &model) + err = mapFieldsCreate(userResp, &model, region) if err != nil { core.LogAndAddError(ctx, &resp.Diagnostics, "Error creating user", fmt.Sprintf("Processing API payload: %v", err)) return @@ -235,13 +281,15 @@ func (r *userResource) Read(ctx context.Context, req resource.ReadRequest, resp return } projectId := model.ProjectId.ValueString() + region := r.providerData.GetRegionWithOverride(model.Region) instanceId := model.InstanceId.ValueString() userId := model.UserId.ValueString() ctx = tflog.SetField(ctx, "project_id", projectId) + ctx = tflog.SetField(ctx, "region", region) ctx = tflog.SetField(ctx, "instance_id", instanceId) ctx = tflog.SetField(ctx, "user_id", userId) - recordSetResp, err := r.client.GetUser(ctx, projectId, instanceId, userId).Execute() + recordSetResp, err := r.client.GetUser(ctx, projectId, instanceId, userId, region).Execute() if err != nil { 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 if ok && oapiErr.StatusCode == http.StatusNotFound { @@ -253,7 +301,7 @@ func (r *userResource) Read(ctx context.Context, req resource.ReadRequest, resp } // Map response body to schema - err = mapFields(recordSetResp, &model) + err = mapFields(recordSetResp, &model, region) if err != nil { core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading user", fmt.Sprintf("Processing API payload: %v", err)) return @@ -278,9 +326,11 @@ func (r *userResource) Update(ctx context.Context, req resource.UpdateRequest, r return } projectId := model.ProjectId.ValueString() + region := r.providerData.GetRegionWithOverride(model.Region) instanceId := model.InstanceId.ValueString() userId := model.UserId.ValueString() ctx = tflog.SetField(ctx, "project_id", projectId) + ctx = tflog.SetField(ctx, "region", region) ctx = tflog.SetField(ctx, "instance_id", instanceId) ctx = tflog.SetField(ctx, "user_id", userId) @@ -309,20 +359,20 @@ func (r *userResource) Update(ctx context.Context, req resource.UpdateRequest, r } // Update existing instance - err = r.client.UpdateUser(ctx, projectId, instanceId, userId).UpdateUserPayload(*payload).Execute() + err = r.client.UpdateUser(ctx, projectId, instanceId, userId, region).UpdateUserPayload(*payload).Execute() if err != nil { core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating user", err.Error()) return } - userResp, err := r.client.GetUser(ctx, projectId, instanceId, userId).Execute() + userResp, err := r.client.GetUser(ctx, projectId, instanceId, userId, region).Execute() if err != nil { core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating user", fmt.Sprintf("Calling API: %v", err)) return } // Map response body to schema - err = mapFields(userResp, &stateModel) + err = mapFields(userResp, &stateModel, region) if err != nil { core.LogAndAddError(ctx, &resp.Diagnostics, "Error updating user", fmt.Sprintf("Processing API payload: %v", err)) return @@ -348,14 +398,16 @@ func (r *userResource) Delete(ctx context.Context, req resource.DeleteRequest, r } projectId := model.ProjectId.ValueString() + region := r.providerData.GetRegionWithOverride(model.Region) instanceId := model.InstanceId.ValueString() userId := model.UserId.ValueString() ctx = tflog.SetField(ctx, "project_id", projectId) + ctx = tflog.SetField(ctx, "region", region) ctx = tflog.SetField(ctx, "instance_id", instanceId) ctx = tflog.SetField(ctx, "user_id", userId) // Delete user - err := r.client.DeleteUser(ctx, projectId, instanceId, userId).Execute() + err := r.client.DeleteUser(ctx, projectId, instanceId, userId, region).Execute() if err != nil { core.LogAndAddError(ctx, &resp.Diagnostics, "Error deleting user", fmt.Sprintf("Calling API: %v", err)) return @@ -367,17 +419,18 @@ func (r *userResource) Delete(ctx context.Context, req resource.DeleteRequest, r // The expected format of the resource import identifier is: project_id,zone_id,record_set_id func (r *userResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { idParts := strings.Split(req.ID, core.Separator) - if len(idParts) != 3 || idParts[0] == "" || idParts[1] == "" || idParts[2] == "" { + if len(idParts) != 4 || idParts[0] == "" || idParts[1] == "" || idParts[2] == "" || idParts[3] == "" { core.LogAndAddError(ctx, &resp.Diagnostics, "Error importing user", - fmt.Sprintf("Expected import identifier with format [project_id],[instance_id],[user_id], got %q", req.ID), + fmt.Sprintf("Expected import identifier with format [project_id],[region],[instance_id],[user_id], got %q", req.ID), ) return } resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("project_id"), idParts[0])...) - resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("instance_id"), idParts[1])...) - resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("user_id"), idParts[2])...) + resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("region"), idParts[1])...) + resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("instance_id"), idParts[2])...) + resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("user_id"), idParts[3])...) core.LogAndAddWarning(ctx, &resp.Diagnostics, "MongoDB Flex user imported with empty password and empty uri", "The user password and uri are not imported as they are only available upon creation of a new user. The password and uri fields will be empty.", @@ -385,7 +438,7 @@ func (r *userResource) ImportState(ctx context.Context, req resource.ImportState tflog.Info(ctx, "MongoDB Flex user state imported") } -func mapFieldsCreate(userResp *mongodbflex.CreateUserResponse, model *Model) error { +func mapFieldsCreate(userResp *mongodbflex.CreateUserResponse, model *Model, region string) error { if userResp == nil || userResp.Item == nil { return fmt.Errorf("response is nil") } @@ -398,7 +451,8 @@ func mapFieldsCreate(userResp *mongodbflex.CreateUserResponse, model *Model) err return fmt.Errorf("user id not present") } userId := *user.Id - model.Id = utils.BuildInternalTerraformId(model.ProjectId.ValueString(), model.InstanceId.ValueString(), userId) + model.Id = utils.BuildInternalTerraformId(model.ProjectId.ValueString(), region, model.InstanceId.ValueString(), userId) + model.Region = types.StringValue(region) model.UserId = types.StringValue(userId) model.Username = types.StringPointerValue(user.Username) model.Database = types.StringPointerValue(user.Database) @@ -427,7 +481,7 @@ func mapFieldsCreate(userResp *mongodbflex.CreateUserResponse, model *Model) err return nil } -func mapFields(userResp *mongodbflex.GetUserResponse, model *Model) error { +func mapFields(userResp *mongodbflex.GetUserResponse, model *Model, region string) error { if userResp == nil || userResp.Item == nil { return fmt.Errorf("response is nil") } @@ -444,7 +498,8 @@ func mapFields(userResp *mongodbflex.GetUserResponse, model *Model) error { } else { return fmt.Errorf("user id not present") } - model.Id = utils.BuildInternalTerraformId(model.ProjectId.ValueString(), model.InstanceId.ValueString(), userId) + model.Id = utils.BuildInternalTerraformId(model.ProjectId.ValueString(), region, model.InstanceId.ValueString(), userId) + model.Region = types.StringValue(region) model.UserId = types.StringValue(userId) model.Username = types.StringPointerValue(user.Username) model.Database = types.StringPointerValue(user.Database) diff --git a/stackit/internal/services/mongodbflex/user/resource_test.go b/stackit/internal/services/mongodbflex/user/resource_test.go index cc888fff..53f31ef9 100644 --- a/stackit/internal/services/mongodbflex/user/resource_test.go +++ b/stackit/internal/services/mongodbflex/user/resource_test.go @@ -1,8 +1,11 @@ package mongodbflex import ( + "fmt" "testing" + "github.com/google/uuid" + "github.com/google/go-cmp/cmp" "github.com/hashicorp/terraform-plugin-framework/attr" "github.com/hashicorp/terraform-plugin-framework/types" @@ -10,10 +13,21 @@ import ( "github.com/stackitcloud/stackit-sdk-go/services/mongodbflex" ) +const ( + testRegion = "eu02" +) + +var ( + projectId = uuid.NewString() + instanceId = uuid.NewString() + userId = uuid.NewString() +) + func TestMapFieldsCreate(t *testing.T) { tests := []struct { description string input *mongodbflex.CreateUserResponse + region string expected Model isValid bool }{ @@ -21,15 +35,16 @@ func TestMapFieldsCreate(t *testing.T) { "default_values", &mongodbflex.CreateUserResponse{ Item: &mongodbflex.User{ - Id: utils.Ptr("uid"), + Id: utils.Ptr(userId), Password: utils.Ptr(""), }, }, + testRegion, Model{ - Id: types.StringValue("pid,iid,uid"), - UserId: types.StringValue("uid"), - InstanceId: types.StringValue("iid"), - ProjectId: types.StringValue("pid"), + Id: types.StringValue(fmt.Sprintf("%s,%s,%s,%s", projectId, testRegion, instanceId, userId)), + UserId: types.StringValue(userId), + InstanceId: types.StringValue(instanceId), + ProjectId: types.StringValue(projectId), Username: types.StringNull(), Database: types.StringNull(), Roles: types.SetNull(types.StringType), @@ -37,6 +52,7 @@ func TestMapFieldsCreate(t *testing.T) { Host: types.StringNull(), Port: types.Int64Null(), Uri: types.StringNull(), + Region: types.StringValue(testRegion), }, true, }, @@ -44,7 +60,7 @@ func TestMapFieldsCreate(t *testing.T) { "simple_values", &mongodbflex.CreateUserResponse{ Item: &mongodbflex.User{ - Id: utils.Ptr("uid"), + Id: utils.Ptr(userId), Roles: &[]string{ "role_1", "role_2", @@ -58,11 +74,12 @@ func TestMapFieldsCreate(t *testing.T) { Uri: utils.Ptr("uri"), }, }, + testRegion, Model{ - Id: types.StringValue("pid,iid,uid"), - UserId: types.StringValue("uid"), - InstanceId: types.StringValue("iid"), - ProjectId: types.StringValue("pid"), + Id: types.StringValue(fmt.Sprintf("%s,%s,%s,%s", projectId, testRegion, instanceId, userId)), + UserId: types.StringValue(userId), + InstanceId: types.StringValue(instanceId), + ProjectId: types.StringValue(projectId), Username: types.StringValue("username"), Database: types.StringValue("database"), Roles: types.SetValueMust(types.StringType, []attr.Value{ @@ -74,6 +91,7 @@ func TestMapFieldsCreate(t *testing.T) { Host: types.StringValue("host"), Port: types.Int64Value(1234), Uri: types.StringValue("uri"), + Region: types.StringValue(testRegion), }, true, }, @@ -81,7 +99,7 @@ func TestMapFieldsCreate(t *testing.T) { "null_fields_and_int_conversions", &mongodbflex.CreateUserResponse{ Item: &mongodbflex.User{ - Id: utils.Ptr("uid"), + Id: utils.Ptr(userId), Roles: &[]string{}, Username: nil, Database: nil, @@ -91,11 +109,12 @@ func TestMapFieldsCreate(t *testing.T) { Uri: nil, }, }, + testRegion, Model{ - Id: types.StringValue("pid,iid,uid"), - UserId: types.StringValue("uid"), - InstanceId: types.StringValue("iid"), - ProjectId: types.StringValue("pid"), + Id: types.StringValue(fmt.Sprintf("%s,%s,%s,%s", projectId, testRegion, instanceId, userId)), + UserId: types.StringValue(userId), + InstanceId: types.StringValue(instanceId), + ProjectId: types.StringValue(projectId), Username: types.StringNull(), Database: types.StringNull(), Roles: types.SetValueMust(types.StringType, []attr.Value{}), @@ -103,18 +122,21 @@ func TestMapFieldsCreate(t *testing.T) { Host: types.StringNull(), Port: types.Int64Value(2123456789), Uri: types.StringNull(), + Region: types.StringValue(testRegion), }, true, }, { "nil_response", nil, + testRegion, Model{}, false, }, { "nil_response_2", &mongodbflex.CreateUserResponse{}, + testRegion, Model{}, false, }, @@ -123,6 +145,7 @@ func TestMapFieldsCreate(t *testing.T) { &mongodbflex.CreateUserResponse{ Item: &mongodbflex.User{}, }, + testRegion, Model{}, false, }, @@ -130,9 +153,10 @@ func TestMapFieldsCreate(t *testing.T) { "no_password", &mongodbflex.CreateUserResponse{ Item: &mongodbflex.User{ - Id: utils.Ptr("uid"), + Id: utils.Ptr(userId), }, }, + testRegion, Model{}, false, }, @@ -143,7 +167,7 @@ func TestMapFieldsCreate(t *testing.T) { ProjectId: tt.expected.ProjectId, InstanceId: tt.expected.InstanceId, } - err := mapFieldsCreate(tt.input, state) + err := mapFieldsCreate(tt.input, state, tt.region) if !tt.isValid && err == nil { t.Fatalf("Should have failed") } @@ -164,6 +188,7 @@ func TestMapFields(t *testing.T) { tests := []struct { description string input *mongodbflex.GetUserResponse + region string expected Model isValid bool }{ @@ -172,16 +197,18 @@ func TestMapFields(t *testing.T) { &mongodbflex.GetUserResponse{ Item: &mongodbflex.InstanceResponseUser{}, }, + testRegion, Model{ - Id: types.StringValue("pid,iid,uid"), - UserId: types.StringValue("uid"), - InstanceId: types.StringValue("iid"), - ProjectId: types.StringValue("pid"), + Id: types.StringValue(fmt.Sprintf("%s,%s,%s,%s", projectId, testRegion, instanceId, userId)), + UserId: types.StringValue(userId), + InstanceId: types.StringValue(instanceId), + ProjectId: types.StringValue(projectId), Username: types.StringNull(), Database: types.StringNull(), Roles: types.SetNull(types.StringType), Host: types.StringNull(), Port: types.Int64Null(), + Region: types.StringValue(testRegion), }, true, }, @@ -200,11 +227,12 @@ func TestMapFields(t *testing.T) { Port: utils.Ptr(int64(1234)), }, }, + testRegion, Model{ - Id: types.StringValue("pid,iid,uid"), - UserId: types.StringValue("uid"), - InstanceId: types.StringValue("iid"), - ProjectId: types.StringValue("pid"), + Id: types.StringValue(fmt.Sprintf("%s,%s,%s,%s", projectId, testRegion, instanceId, userId)), + UserId: types.StringValue(userId), + InstanceId: types.StringValue(instanceId), + ProjectId: types.StringValue(projectId), Username: types.StringValue("username"), Database: types.StringValue("database"), Roles: types.SetValueMust(types.StringType, []attr.Value{ @@ -212,8 +240,9 @@ func TestMapFields(t *testing.T) { types.StringValue("role_2"), types.StringValue(""), }), - Host: types.StringValue("host"), - Port: types.Int64Value(1234), + Host: types.StringValue("host"), + Port: types.Int64Value(1234), + Region: types.StringValue(testRegion), }, true, }, @@ -221,7 +250,7 @@ func TestMapFields(t *testing.T) { "null_fields_and_int_conversions", &mongodbflex.GetUserResponse{ Item: &mongodbflex.InstanceResponseUser{ - Id: utils.Ptr("uid"), + Id: utils.Ptr(userId), Roles: &[]string{}, Username: nil, Database: nil, @@ -229,28 +258,32 @@ func TestMapFields(t *testing.T) { Port: utils.Ptr(int64(2123456789)), }, }, + testRegion, Model{ - Id: types.StringValue("pid,iid,uid"), - UserId: types.StringValue("uid"), - InstanceId: types.StringValue("iid"), - ProjectId: types.StringValue("pid"), + Id: types.StringValue(fmt.Sprintf("%s,%s,%s,%s", projectId, testRegion, instanceId, userId)), + UserId: types.StringValue(userId), + InstanceId: types.StringValue(instanceId), + ProjectId: types.StringValue(projectId), Username: types.StringNull(), Database: types.StringNull(), Roles: types.SetValueMust(types.StringType, []attr.Value{}), Host: types.StringNull(), Port: types.Int64Value(2123456789), + Region: types.StringValue(testRegion), }, true, }, { "nil_response", nil, + testRegion, Model{}, false, }, { "nil_response_2", &mongodbflex.GetUserResponse{}, + testRegion, Model{}, false, }, @@ -259,6 +292,7 @@ func TestMapFields(t *testing.T) { &mongodbflex.GetUserResponse{ Item: &mongodbflex.InstanceResponseUser{}, }, + testRegion, Model{}, false, }, @@ -270,7 +304,7 @@ func TestMapFields(t *testing.T) { InstanceId: tt.expected.InstanceId, UserId: tt.expected.UserId, } - err := mapFields(tt.input, state) + err := mapFields(tt.input, state, tt.region) if !tt.isValid && err == nil { t.Fatalf("Should have failed") } diff --git a/stackit/internal/services/mongodbflex/utils/util.go b/stackit/internal/services/mongodbflex/utils/util.go index 9abd99da..e1c805c1 100644 --- a/stackit/internal/services/mongodbflex/utils/util.go +++ b/stackit/internal/services/mongodbflex/utils/util.go @@ -18,9 +18,8 @@ func ConfigureClient(ctx context.Context, providerData *core.ProviderData, diags } if providerData.MongoDBFlexCustomEndpoint != "" { apiClientConfigOptions = append(apiClientConfigOptions, config.WithEndpoint(providerData.MongoDBFlexCustomEndpoint)) - } else { - apiClientConfigOptions = append(apiClientConfigOptions, config.WithRegion(providerData.GetRegion())) } + apiClient, err := mongodbflex.NewAPIClient(apiClientConfigOptions...) if err != nil { core.LogAndAddError(ctx, diags, "Error configuring API client", fmt.Sprintf("Configuring client: %v. This is an error related to the provider configuration, not to the resource configuration", err)) diff --git a/stackit/internal/services/mongodbflex/utils/util_test.go b/stackit/internal/services/mongodbflex/utils/util_test.go index 7f068a5a..d268d9af 100644 --- a/stackit/internal/services/mongodbflex/utils/util_test.go +++ b/stackit/internal/services/mongodbflex/utils/util_test.go @@ -45,7 +45,6 @@ func TestConfigureClient(t *testing.T) { }, expected: func() *mongodbflex.APIClient { apiClient, err := mongodbflex.NewAPIClient( - config.WithRegion("eu01"), utils.UserAgentConfigOption(testVersion), ) if err != nil {