Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/4400.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/mongodbatlas_advanced_cluster: Fixes perpetual plan drift on `mongo_db_major_version` after out-of-band major version upgrade
Comment thread
AgustinBettati marked this conversation as resolved.
Outdated
```
8 changes: 7 additions & 1 deletion internal/service/advancedcluster/resource_compatiblity.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ func overrideAttributesWithPrevStateValue(modelIn, modelOut *TFModel) {
return
}
beforeVersion := conversion.NilForUnknown(modelIn.MongoDBMajorVersion, modelIn.MongoDBMajorVersion.ValueStringPointer())
if beforeVersion != nil && !modelIn.MongoDBMajorVersion.Equal(modelOut.MongoDBMajorVersion) {
afterVersion := conversion.NilForUnknown(modelOut.MongoDBMajorVersion, modelOut.MongoDBMajorVersion.ValueStringPointer())
// Only preserve prior state value for formatting differences (e.g. "8" vs "8.0"), not real version transitions.
Copy link
Copy Markdown

@augmentcode augmentcode Bot Apr 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

internal/service/advancedcluster/resource_compatiblity.go:99 — This comment says the override is only for formatting differences, but ShouldUsePreviousMongoDBMajorVersion also preserves the prior value when the minor version changes within the same major (e.g., CONTINUOUS returning 8.2). Consider clarifying the comment so future readers don’t assume this logic is formatting-only.

Severity: low

Fix This in Augment

🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.

if beforeVersion != nil && afterVersion != nil && ShouldUsePreviousMongoDBMajorVersion(*beforeVersion, *afterVersion) {
modelOut.MongoDBMajorVersion = types.StringPointerValue(beforeVersion)
}
overrideMapStringWithPrevStateValue(&modelIn.Labels, &modelOut.Labels)
Expand All @@ -109,6 +111,10 @@ func overrideAttributesWithPrevStateValue(modelIn, modelOut *TFModel) {
modelOut.UseEffectiveFields = modelIn.UseEffectiveFields
}

func ShouldUsePreviousMongoDBMajorVersion(beforeVersion, afterVersion string) bool {
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function helps preserve the prior state value when the major component is the same, which handles two cases:

  1. Formatting differences: user sets "8" but Atlas returns "8.0" which is the same version, but different format. This is to preserve SDKv2 behaviour
  2. version_release_system = "CONTINUOUS": Atlas can return a minor bump (e.g. "8.2") that is not user-controlled. Letting it through causes: provider produced an unexpected new value: .mongo_db_major_version: was cty.StringVal("8.0"), but now cty.StringVal("8.2"). `

The reason why step 2 is implemented, is to avoid unit test failure

return beforeVersion != afterVersion && FormatMongoDBMajorVersion(beforeVersion) == FormatMongoDBMajorVersion(afterVersion)
}

func overrideMapStringWithPrevStateValue(mapIn, mapOut *types.Map) {
if mapIn == nil || mapOut == nil || len(mapOut.Elements()) > 0 {
return
Expand Down
36 changes: 36 additions & 0 deletions internal/service/advancedcluster/resource_compatiblity_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package advancedcluster_test

import (
"testing"

"github.com/mongodb/terraform-provider-mongodbatlas/internal/service/advancedcluster"
"github.com/stretchr/testify/assert"
)

func TestAdvancedCluster_overrideMongoDBMajorVersion(t *testing.T) {
Comment thread
marcabreracast marked this conversation as resolved.
Outdated
testCases := []struct {
name string
before string
after string
expected bool
}{
{
name: "keeps previous version when only formatting differs",
before: "8",
after: "8.0",
expected: true,
},
{
name: "uses API value when major version actually changed",
before: "7.0",
after: "8.0",
expected: false,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.expected, advancedcluster.ShouldUsePreviousMongoDBMajorVersion(tc.before, tc.after))
})
}
}
Loading