feat(mongodbflex): region adjustment (#914)

relates to STACKITTPR-282
This commit is contained in:
Ruben Hönle 2025-07-21 14:37:47 +02:00 committed by GitHub
parent 6555a99a6d
commit 189fa1ece7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 388 additions and 175 deletions

View file

@ -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

View file

@ -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)
}

View file

@ -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")
}