Skip to content

Commit

Permalink
feat: add tests and mock data for symmetric sharded old schema config…
Browse files Browse the repository at this point in the history
…uration+Handle MongoDBMajor version formatting
  • Loading branch information
EspenAlbert committed Nov 30, 2024
1 parent e98c982 commit 13b7826
Show file tree
Hide file tree
Showing 15 changed files with 780 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ import (
"go.mongodb.org/atlas-sdk/v20241113001/admin"
)

func newLegacyModel20240530OnlyReplicationSpecs(specs *[]admin.ReplicationSpec20240805, zoneNameNumShards map[string]int64) *admin20240530.AdvancedClusterDescription {
func newLegacyModel20240530OnlyReplicationSpecsAndDiskGB(specs *[]admin.ReplicationSpec20240805, zoneNameNumShards map[string]int64, oldDiskGB *float64) *admin20240530.AdvancedClusterDescription {
newDiskGB := findRegionRootDiskSize(specs)
if oldDiskGB != nil && newDiskGB != nil && (*newDiskGB-*oldDiskGB) < 0.01 {
newDiskGB = nil
}
return &admin20240530.AdvancedClusterDescription{
DiskSizeGB: findRegionRootDiskSize(specs),
DiskSizeGB: newDiskGB,
ReplicationSpecs: convertReplicationSpecs20240805to20240530(specs, zoneNameNumShards),
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ func NewAtlasReq(ctx context.Context, input *TFModel, diags *diag.Diagnostics) *
if !ok {
diags.AddError("error converting AcceptDataRisksAndForceReplicaSetReconfig", fmt.Sprintf("not a valid time: %s", input.AcceptDataRisksAndForceReplicaSetReconfig.ValueString()))
}
majorVersion := conversion.NilForUnknown(input.MongoDBMajorVersion, input.MongoDBMajorVersion.ValueStringPointer())
if majorVersion != nil {
majorVersionFormatted := FormatMongoDBMajorVersion(*majorVersion)
majorVersion = &majorVersionFormatted
}
return &admin.ClusterDescription20240805{
AcceptDataRisksAndForceReplicaSetReconfig: acceptDataRisksAndForceReplicaSetReconfig,
BackupEnabled: conversion.NilForUnknown(input.BackupEnabled, input.BackupEnabled.ValueBoolPointer()),
Expand All @@ -25,7 +30,7 @@ func NewAtlasReq(ctx context.Context, input *TFModel, diags *diag.Diagnostics) *
EncryptionAtRestProvider: conversion.NilForUnknown(input.EncryptionAtRestProvider, input.EncryptionAtRestProvider.ValueStringPointer()),
GlobalClusterSelfManagedSharding: conversion.NilForUnknown(input.GlobalClusterSelfManagedSharding, input.GlobalClusterSelfManagedSharding.ValueBoolPointer()),
Labels: newComponentLabel(ctx, input.Labels, diags),
MongoDBMajorVersion: conversion.NilForUnknown(input.MongoDBMajorVersion, input.MongoDBMajorVersion.ValueStringPointer()),
MongoDBMajorVersion: majorVersion,
Name: input.Name.ValueStringPointer(),
Paused: conversion.NilForUnknown(input.Paused, input.Paused.ValueBoolPointer()),
PitEnabled: conversion.NilForUnknown(input.PitEnabled, input.PitEnabled.ValueBoolPointer()),
Expand Down
3 changes: 2 additions & 1 deletion internal/service/advancedclustertpf/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ func (r *rs) updateLegacyReplicationSpecs(ctx context.Context, state, plan *TFMo
if diags.HasError() {
return false
}
legacyPatch := newLegacyModel20240530OnlyReplicationSpecs(specChanges, numShardsPlan)
legacyPatch := newLegacyModel20240530OnlyReplicationSpecsAndDiskGB(specChanges, numShardsPlan, state.DiskSizeGB.ValueFloat64Pointer())
api20240530 := r.Client.AtlasV220240530.ClustersApi
api20240530.UpdateCluster(ctx, plan.ProjectID.ValueString(), plan.Name.ValueString(), legacyPatch)
_, _, err := api20240530.UpdateCluster(ctx, plan.ProjectID.ValueString(), plan.Name.ValueString(), legacyPatch).Execute()
Expand Down Expand Up @@ -372,5 +372,6 @@ func (r *rs) convertClusterAddAdvConfig(ctx context.Context, legacyAdvConfig *ad
if diags.HasError() {
return nil
}
overrideKnowTPFIssueFields(modelIn, modelOut)
return modelOut
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,20 @@ package advancedclustertpf
import (
"context"
"fmt"
"strings"

"github.com/spf13/cast"
admin20240530 "go.mongodb.org/atlas-sdk/v20240530005/admin"
"go.mongodb.org/atlas-sdk/v20241113001/admin"
)

func FormatMongoDBMajorVersion(version string) string {
if strings.Contains(version, ".") {
return version
}
return fmt.Sprintf("%.1f", cast.ToFloat32(version))
}

func getReplicationSpecIDsFromOldAPI(ctx context.Context, projectID, clusterName string, api admin20240530.ClustersApi) (map[string]string, error) {
clusterOldAPI, _, err := api.GetCluster(ctx, projectID, clusterName).Execute()
if apiError, ok := admin20240530.AsError(err); ok {
Expand Down
7 changes: 7 additions & 0 deletions internal/service/advancedclustertpf/resource_compatiblity.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ import (
"go.mongodb.org/atlas-sdk/v20241113001/admin"
)

func overrideKnowTPFIssueFields(modelIn, modelOut *TFModel) {
beforeVersion := conversion.NilForUnknown(modelIn.MongoDBMajorVersion, modelIn.MongoDBMajorVersion.ValueStringPointer())
if beforeVersion != nil && !modelIn.MongoDBMajorVersion.Equal(modelOut.MongoDBMajorVersion) {
modelOut.MongoDBMajorVersion = types.StringPointerValue(beforeVersion)
}
}

func findNumShardsUpdates(ctx context.Context, state, plan *TFModel, diags *diag.Diagnostics) map[string]int64 {
if !usingLegacySchema(ctx, plan.ReplicationSpecs, diags) {
return nil
Expand Down
10 changes: 10 additions & 0 deletions internal/service/advancedclustertpf/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,13 @@ func TestAccClusterAdvancedClusterConfig_symmetricShardedOldSchemaDiskSizeGBAtEl
testCase := tc.SymmetricShardedOldSchemaDiskSizeGBAtElectableLevel(t, orgID, projectName, clusterName)
resource.ParallelTest(t, *testCase)
}

func TestAccClusterAdvancedClusterConfig_symmetricShardedOldSchema(t *testing.T) {
var (
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
projectName = acc.RandomProjectName()
clusterName = acc.RandomClusterName()
)
testCase := tc.SymmetricShardedOldSchema(t, orgID, projectName, clusterName)
resource.ParallelTest(t, *testCase)
}
15 changes: 15 additions & 0 deletions internal/service/advancedclustertpf/resource_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,18 @@ func TestMockClusterAdvancedClusterConfig_symmetricShardedOldSchemaDiskSizeGBAtE
testCase := tc.SymmetricShardedOldSchemaDiskSizeGBAtElectableLevel(t, orgID, projectName, clusterName)
unit.MockTestCaseAndRun(t, vars, &unit.MockHTTPDataConfig{AllowMissingRequests: true, AllowReReadGet: true}, testCase)
}

func TestMockClusterAdvancedClusterConfig_symmetricShardedOldSchema(t *testing.T) {
var (
clusterName = "test-acc-tf-c-6025103075771235151"
projectName = "test-acc-tf-p-7889034782442569766"
orgID = "65def6ce0f722a1507105aa5"
vars = map[string]string{
"groupId": projectID,
"clusterName": clusterName,
}
)
shortenRetries()
testCase := tc.SymmetricShardedOldSchema(t, orgID, projectName, clusterName)
unit.MockTestCaseAndRun(t, vars, &unit.MockHTTPDataConfig{AllowMissingRequests: true, AllowReReadGet: true}, testCase)
}
Loading

0 comments on commit 13b7826

Please sign in to comment.