Skip to content

Commit 637dc07

Browse files
EspenAlbertlantolikanchana-mongodb
authored
fix: Fixes inconsistent result when using a multi-region cluster by always using a single spec (#2685)
* fix: Initial workaround for multiple specs issuing a warning of spec missmatch * fix: can only use multiple specs in data source (schema limitation) * test: Add a test to confirm multi-region cluster can be used with search deployment * chore: changelog file * chore: fix test enum value * chore: revert timeout changes * Update .changelog/2685.txt Co-authored-by: kanchana-mongodb <[email protected]> --------- Co-authored-by: Leo Antoli <[email protected]> Co-authored-by: kanchana-mongodb <[email protected]>
1 parent d903111 commit 637dc07

File tree

6 files changed

+73
-18
lines changed

6 files changed

+73
-18
lines changed

.changelog/2685.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
resource/mongodbatlas_search_deployment: Fixes inconsistent result for a multi-region cluster that always uses a single spec.
3+
```

internal/service/searchdeployment/data_source_search_deployment.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (d *searchDeploymentDS) Read(ctx context.Context, req datasource.ReadReques
4444
return
4545
}
4646

47-
newSearchDeploymentModel, diagnostics := NewTFSearchDeployment(ctx, clusterName, deploymentResp, nil)
47+
newSearchDeploymentModel, diagnostics := NewTFSearchDeployment(ctx, clusterName, deploymentResp, nil, true)
4848
resp.Diagnostics.Append(diagnostics...)
4949
if resp.Diagnostics.HasError() {
5050
return

internal/service/searchdeployment/model_search_deployment.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func NewSearchDeploymentReq(ctx context.Context, searchDeploymentPlan *TFSearchD
2525
}
2626
}
2727

28-
func NewTFSearchDeployment(ctx context.Context, clusterName string, deployResp *admin.ApiSearchDeploymentResponse, timeout *timeouts.Value) (*TFSearchDeploymentRSModel, diag.Diagnostics) {
28+
func NewTFSearchDeployment(ctx context.Context, clusterName string, deployResp *admin.ApiSearchDeploymentResponse, timeout *timeouts.Value, allowMultipleSpecs bool) (*TFSearchDeploymentRSModel, diag.Diagnostics) {
2929
result := TFSearchDeploymentRSModel{
3030
ID: types.StringPointerValue(deployResp.Id),
3131
ClusterName: types.StringValue(clusterName),
@@ -37,13 +37,16 @@ func NewTFSearchDeployment(ctx context.Context, clusterName string, deployResp *
3737
result.Timeouts = *timeout
3838
}
3939

40-
specsList, diagnostics := types.ListValueFrom(ctx, SpecObjectType, newTFSpecsModel(deployResp.GetSpecs()))
40+
specs := deployResp.GetSpecs()
41+
if !allowMultipleSpecs && len(specs) > 1 {
42+
specs = specs[:1]
43+
}
44+
specsList, diagnostics := types.ListValueFrom(ctx, SpecObjectType, newTFSpecsModel(specs))
4145
if diagnostics.HasError() {
4246
return nil, diagnostics
4347
}
44-
4548
result.Specs = specsList
46-
return &result, nil
49+
return &result, diagnostics
4750
}
4851

4952
func newTFSpecsModel(specs []admin.ApiSearchDeploymentSpec) []TFSearchNodeSpecModel {

internal/service/searchdeployment/model_search_deployment_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func TestSearchDeploymentSDKToTFModel(t *testing.T) {
5555

5656
for _, tc := range testCases {
5757
t.Run(tc.name, func(t *testing.T) {
58-
resultModel, diags := searchdeployment.NewTFSearchDeployment(context.Background(), tc.clusterName, tc.SDKResp, nil)
58+
resultModel, diags := searchdeployment.NewTFSearchDeployment(context.Background(), tc.clusterName, tc.SDKResp, nil, false)
5959
if diags.HasError() {
6060
t.Errorf("unexpected errors found: %s", diags.Errors()[0].Summary())
6161
}

internal/service/searchdeployment/resource_search_deployment.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func (r *searchDeploymentRS) Create(ctx context.Context, req resource.CreateRequ
7575
resp.Diagnostics.AddError("error during search deployment creation", err.Error())
7676
return
7777
}
78-
newSearchNodeModel, diagnostics := NewTFSearchDeployment(ctx, clusterName, deploymentResp, &searchDeploymentPlan.Timeouts)
78+
newSearchNodeModel, diagnostics := NewTFSearchDeployment(ctx, clusterName, deploymentResp, &searchDeploymentPlan.Timeouts, false)
7979
resp.Diagnostics.Append(diagnostics...)
8080
if resp.Diagnostics.HasError() {
8181
return
@@ -103,7 +103,7 @@ func (r *searchDeploymentRS) Read(ctx context.Context, req resource.ReadRequest,
103103
return
104104
}
105105

106-
newSearchNodeModel, diagnostics := NewTFSearchDeployment(ctx, clusterName, deploymentResp, &searchDeploymentPlan.Timeouts)
106+
newSearchNodeModel, diagnostics := NewTFSearchDeployment(ctx, clusterName, deploymentResp, &searchDeploymentPlan.Timeouts, false)
107107
resp.Diagnostics.Append(diagnostics...)
108108
if resp.Diagnostics.HasError() {
109109
return
@@ -138,7 +138,7 @@ func (r *searchDeploymentRS) Update(ctx context.Context, req resource.UpdateRequ
138138
resp.Diagnostics.AddError("error during search deployment update", err.Error())
139139
return
140140
}
141-
newSearchNodeModel, diagnostics := NewTFSearchDeployment(ctx, clusterName, deploymentResp, &searchDeploymentPlan.Timeouts)
141+
newSearchNodeModel, diagnostics := NewTFSearchDeployment(ctx, clusterName, deploymentResp, &searchDeploymentPlan.Timeouts, false)
142142
resp.Diagnostics.Append(diagnostics...)
143143
if resp.Diagnostics.HasError() {
144144
return

internal/service/searchdeployment/resource_search_deployment_test.go

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,68 @@ import (
1111
"github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/acc"
1212
)
1313

14+
const (
15+
resourceID = "mongodbatlas_search_deployment.test"
16+
dataSourceID = "data.mongodbatlas_search_deployment.test"
17+
)
18+
1419
func TestAccSearchDeployment_basic(t *testing.T) {
1520
var (
16-
resourceName = "mongodbatlas_search_deployment.test"
17-
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
18-
projectName = acc.RandomProjectName()
19-
clusterName = acc.RandomClusterName()
21+
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
22+
projectName = acc.RandomProjectName()
23+
clusterName = acc.RandomClusterName()
2024
)
2125
resource.ParallelTest(t, resource.TestCase{
2226
PreCheck: func() { acc.PreCheckBasic(t) },
2327
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
2428
CheckDestroy: checkDestroy,
2529
Steps: []resource.TestStep{
26-
newSearchNodeTestStep(resourceName, orgID, projectName, clusterName, "S20_HIGHCPU_NVME", 3),
27-
newSearchNodeTestStep(resourceName, orgID, projectName, clusterName, "S30_HIGHCPU_NVME", 4),
30+
newSearchNodeTestStep(resourceID, orgID, projectName, clusterName, "S20_HIGHCPU_NVME", 3),
31+
newSearchNodeTestStep(resourceID, orgID, projectName, clusterName, "S30_HIGHCPU_NVME", 4),
2832
{
2933
Config: configBasic(orgID, projectName, clusterName, "S30_HIGHCPU_NVME", 4),
30-
ResourceName: resourceName,
31-
ImportStateIdFunc: importStateIDFunc(resourceName),
34+
ResourceName: resourceID,
35+
ImportStateIdFunc: importStateIDFunc(resourceID),
3236
ImportState: true,
3337
ImportStateVerify: true,
3438
},
3539
},
3640
})
3741
}
3842

43+
func TestAccSearchDeployment_multiRegion(t *testing.T) {
44+
var (
45+
clusterInfo = acc.GetClusterInfo(t, &acc.ClusterRequest{
46+
ClusterName: "multi-region-cluster",
47+
ReplicationSpecs: []acc.ReplicationSpecRequest{
48+
{
49+
Region: "US_EAST_1",
50+
ExtraRegionConfigs: []acc.ReplicationSpecRequest{
51+
{Region: "US_WEST_2", Priority: 6, InstanceSize: "M10", NodeCount: 2},
52+
},
53+
},
54+
},
55+
})
56+
)
57+
resource.ParallelTest(t, resource.TestCase{
58+
PreCheck: func() { acc.PreCheckBasic(t) },
59+
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
60+
CheckDestroy: checkDestroy,
61+
Steps: []resource.TestStep{
62+
{
63+
Config: clusterInfo.TerraformStr + configSearchDeployment(clusterInfo.ProjectID, clusterInfo.TerraformNameRef, "S20_HIGHCPU_NVME", 2),
64+
Check: resource.ComposeAggregateTestCheckFunc(
65+
checkExists(resourceID),
66+
resource.TestCheckResourceAttr(resourceID, "specs.#", "1"),
67+
),
68+
},
69+
},
70+
})
71+
}
72+
3973
func newSearchNodeTestStep(resourceName, orgID, projectName, clusterName, instanceSize string, searchNodeCount int) resource.TestStep {
4074
resourceChecks := searchNodeChecks(resourceName, clusterName, instanceSize, searchNodeCount)
41-
dataSourceChecks := searchNodeChecks(fmt.Sprintf("data.%s", resourceName), clusterName, instanceSize, searchNodeCount)
75+
dataSourceChecks := searchNodeChecks(dataSourceID, clusterName, instanceSize, searchNodeCount)
4276
return resource.TestStep{
4377
Config: configBasic(orgID, projectName, clusterName, instanceSize, searchNodeCount),
4478
Check: resource.ComposeAggregateTestCheckFunc(append(resourceChecks, dataSourceChecks...)...),
@@ -80,6 +114,21 @@ func configBasic(orgID, projectName, clusterName, instanceSize string, searchNod
80114
`, clusterConfig, instanceSize, searchNodeCount)
81115
}
82116

117+
func configSearchDeployment(projectID, clusterNameRef, instanceSize string, searchNodeCount int) string {
118+
return fmt.Sprintf(`
119+
resource "mongodbatlas_search_deployment" "test" {
120+
project_id = %[1]q
121+
cluster_name = %[2]s
122+
specs = [
123+
{
124+
instance_size = %[3]q
125+
node_count = %[4]d
126+
}
127+
]
128+
}
129+
`, projectID, clusterNameRef, instanceSize, searchNodeCount)
130+
}
131+
83132
func advancedClusterConfig(orgID, projectName, clusterName string) string {
84133
return fmt.Sprintf(`
85134
resource "mongodbatlas_project" "test" {

0 commit comments

Comments
 (0)