Skip to content

refactor: Uses new plan modify logic common functions in advanced_cluster TPF #3218

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: CLOUDP-307851_common_plan_modifier_logic
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ test: fmtcheck ## Run unit tests
@$(eval export MONGODB_ATLAS_ORG_ID?=111111111111111111111111)
@$(eval export MONGODB_ATLAS_PROJECT_ID?=111111111111111111111111)
@$(eval export MONGODB_ATLAS_CLUSTER_NAME?=mocked-cluster)
go test ./... -timeout=120s -parallel=$(PARALLEL_GO_TEST) -race
go test ./... -timeout=180s -parallel=$(PARALLEL_GO_TEST) -race

.PHONY: testmact
testmact: ## Run MacT tests (mocked acc tests)
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ func newReplicationSpec20240805(ctx context.Context, input types.List, diags *di
resp := make([]admin.ReplicationSpec20240805, len(input.Elements()))
for i := range elements {
item := &elements[i]
// Choosing to never send ZoneId in PATCH request. It is inferred by the backend.
resp[i] = admin.ReplicationSpec20240805{
Id: conversion.NilForUnknownOrEmptyString(item.ExternalId),
ZoneId: conversion.NilForUnknownOrEmptyString(item.ZoneId),
RegionConfigs: newCloudRegionConfig20240805(ctx, item.RegionConfigs, diags),
ZoneName: conversion.StringPtr(resolveZoneNameOrUseDefault(item)),
}
Expand Down
353 changes: 128 additions & 225 deletions internal/service/advancedclustertpf/plan_modifier.go

Large diffs are not rendered by default.

125 changes: 105 additions & 20 deletions internal/service/advancedclustertpf/plan_modifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import (
)

var (
repSpec0 = tfjsonpath.New("replication_specs").AtSliceIndex(0)
repSpec1 = tfjsonpath.New("replication_specs").AtSliceIndex(1)
regionConfig0 = repSpec0.AtMapKey("region_configs").AtSliceIndex(0)
regionConfig1 = repSpec1.AtMapKey("region_configs").AtSliceIndex(0)
mockConfig = unit.MockConfigAdvancedClusterTPF
repSpec0 = tfjsonpath.New("replication_specs").AtSliceIndex(0)
repSpec1 = tfjsonpath.New("replication_specs").AtSliceIndex(1)
regionConfig0_0 = repSpec0.AtMapKey("region_configs").AtSliceIndex(0)
regionConfig1_0 = repSpec1.AtMapKey("region_configs").AtSliceIndex(0)
regionConfig1_1 = repSpec1.AtMapKey("region_configs").AtSliceIndex(1)
advConfig = tfjsonpath.New("advanced_configuration")
mockConfig = unit.MockConfigAdvancedClusterTPF
)

func autoScalingKnownValue(computeEnabled, diskEnabled, scaleDown bool, minInstanceSize, maxInstanceSize string) knownvalue.Check {
Expand Down Expand Up @@ -51,27 +53,110 @@ func TestPlanChecksClusterTwoRepSpecsWithAutoScalingAndSpecs(t *testing.T) {
Checks: []plancheck.PlanCheck{
plancheck.ExpectResourceAction(resourceName, plancheck.ResourceActionUpdate),
// checks regionConfig0
plancheck.ExpectKnownValue(resourceName, regionConfig0.AtMapKey("read_only_specs"), specInstanceSizeNodeCount("M10", 2)),
plancheck.ExpectKnownValue(resourceName, regionConfig0.AtMapKey("electable_specs"), specInstanceSizeNodeCount("M10", 5)),
plancheck.ExpectKnownValue(resourceName, regionConfig0.AtMapKey("auto_scaling"), autoScalingEnabled),
plancheck.ExpectKnownValue(resourceName, regionConfig0.AtMapKey("analytics_auto_scaling"), autoScalingEnabled),
plancheck.ExpectUnknownValue(resourceName, regionConfig0.AtMapKey("analytics_specs")), // analytics specs was defined in region_configs.0 but not in region_configs.1
plancheck.ExpectKnownValue(resourceName, regionConfig0_0.AtMapKey("read_only_specs"), specInstanceSizeNodeCount("M10", 2)),
plancheck.ExpectKnownValue(resourceName, regionConfig0_0.AtMapKey("electable_specs"), specInstanceSizeNodeCount("M10", 5)),
plancheck.ExpectKnownValue(resourceName, regionConfig0_0.AtMapKey("auto_scaling"), autoScalingEnabled),
plancheck.ExpectKnownValue(resourceName, regionConfig0_0.AtMapKey("analytics_auto_scaling"), autoScalingEnabled),
plancheck.ExpectUnknownValue(resourceName, regionConfig0_0.AtMapKey("analytics_specs")), // analytics specs was defined in region_configs.0 but not in region_configs.1
plancheck.ExpectUnknownValue(resourceName, repSpec0.AtMapKey("id")),

// checks regionConfig1
plancheck.ExpectKnownValue(resourceName, regionConfig1.AtMapKey("read_only_specs"), specInstanceSizeNodeCount("M20", 1)),
plancheck.ExpectKnownValue(resourceName, regionConfig1.AtMapKey("electable_specs"), specInstanceSizeNodeCount("M20", 3)),
plancheck.ExpectKnownValue(resourceName, regionConfig1.AtMapKey("auto_scaling"), autoScalingEnabled),
plancheck.ExpectKnownValue(resourceName, regionConfig1.AtMapKey("analytics_auto_scaling"), autoScalingEnabled),
plancheck.ExpectKnownValue(resourceName, regionConfig1.AtMapKey("analytics_specs"), knownvalue.NotNull()),
plancheck.ExpectKnownValue(resourceName, regionConfig1_0.AtMapKey("read_only_specs"), specInstanceSizeNodeCount("M20", 1)),
plancheck.ExpectKnownValue(resourceName, regionConfig1_0.AtMapKey("electable_specs"), specInstanceSizeNodeCount("M20", 3)),
plancheck.ExpectKnownValue(resourceName, regionConfig1_0.AtMapKey("auto_scaling"), autoScalingEnabled),
plancheck.ExpectKnownValue(resourceName, regionConfig1_0.AtMapKey("analytics_auto_scaling"), autoScalingEnabled),
plancheck.ExpectKnownValue(resourceName, regionConfig1_0.AtMapKey("analytics_specs"), knownvalue.NotNull()),
plancheck.ExpectUnknownValue(resourceName, repSpec1.AtMapKey("id")),
},
},
}
)
for _, testCase := range testCases {
t.Run(testCase.ConfigFilename, func(t *testing.T) {
unit.MockPlanChecksAndRun(t, baseConfig.WithPlanCheckTest(testCase))
})
}
unit.RunPlanCheckTests(t, baseConfig, testCases)
}

func TestMockPlanChecks_ClusterReplicasetOneRegion(t *testing.T) {
var (
baseConfig = unit.NewMockPlanChecksConfig(t, &mockConfig, unit.ImportNameClusterReplicasetOneRegion)
resourceName = baseConfig.ResourceName
testCases = []unit.PlanCheckTest{
{
ConfigFilename: "main_mongo_db_major_version_changed.tf",
Checks: []plancheck.PlanCheck{
plancheck.ExpectResourceAction(resourceName, plancheck.ResourceActionUpdate),
plancheck.ExpectUnknownValue(resourceName, tfjsonpath.New("mongo_db_version")),
},
},
{
ConfigFilename: "main_backup_enabled.tf",
Checks: []plancheck.PlanCheck{
plancheck.ExpectResourceAction(resourceName, plancheck.ResourceActionUpdate),
// should use state values inside replication_specs as no changes are made to replication_specs
plancheck.ExpectKnownValue(resourceName, repSpec0.AtMapKey("id"), knownvalue.NotNull()),
plancheck.ExpectKnownValue(resourceName, repSpec0.AtMapKey("zone_name"), knownvalue.NotNull()),
plancheck.ExpectKnownValue(resourceName, repSpec0.AtMapKey("zone_id"), knownvalue.NotNull()),
plancheck.ExpectKnownValue(resourceName, regionConfig0_0.AtMapKey("electable_specs").AtMapKey("ebs_volume_type"), knownvalue.NotNull()),
},
},
{
ConfigFilename: "main_electable_disk_size_changed.tf",
Checks: []plancheck.PlanCheck{
plancheck.ExpectResourceAction(resourceName, plancheck.ResourceActionUpdate),
plancheck.ExpectUnknownValue(resourceName, tfjsonpath.New("disk_size_gb")),
plancheck.ExpectUnknownValue(resourceName, regionConfig0_0.AtMapKey("read_only_specs").AtMapKey("disk_size_gb")), // disk_size_gb is changed so always marked as unknown
plancheck.ExpectKnownValue(resourceName, regionConfig0_0.AtMapKey("electable_specs").AtMapKey("disk_size_gb"), knownvalue.Int64Exact(99)),
},
},
{
ConfigFilename: "main_tls_cipher_config_mode_changed.tf",
Checks: []plancheck.PlanCheck{
plancheck.ExpectResourceAction(resourceName, plancheck.ResourceActionUpdate),
plancheck.ExpectUnknownValue(resourceName, advConfig.AtMapKey("custom_openssl_cipher_config_tls12")),
plancheck.ExpectKnownValue(resourceName, advConfig.AtMapKey("javascript_enabled"), knownvalue.Bool(true)),
},
},
{
ConfigFilename: "main_cluster_type_changed.tf",
Checks: []plancheck.PlanCheck{
plancheck.ExpectResourceAction(resourceName, plancheck.ResourceActionUpdate),
plancheck.ExpectUnknownValue(resourceName, tfjsonpath.New("config_server_type")),
plancheck.ExpectUnknownValue(resourceName, tfjsonpath.New("config_server_management_mode")),
plancheck.ExpectKnownValue(resourceName, tfjsonpath.New("bi_connector_config"), knownvalue.ObjectExact(map[string]knownvalue.Check{
"enabled": knownvalue.Bool(false),
"read_preference": knownvalue.StringExact("secondary"),
})),
},
},
{
ConfigFilename: "main_pin_fcv.tf",
Checks: []plancheck.PlanCheck{
plancheck.ExpectResourceAction(resourceName, plancheck.ResourceActionUpdate),
plancheck.ExpectUnknownValue(resourceName, tfjsonpath.New("pinned_fcv").AtMapKey("version")),
},
},
}
)
unit.RunPlanCheckTests(t, baseConfig, testCases)
}

func TestPlanChecksClusterTwoRepSpecsMultipleRegions(t *testing.T) {
var (
baseConfig = unit.NewMockPlanChecksConfig(t, &mockConfig, unit.ImportNameTwoRepSpecsMultipleRegions)
resourceName = baseConfig.ResourceName
testCases = []unit.PlanCheckTest{
{
ConfigFilename: "main_specs_removed.tf",
Checks: []plancheck.PlanCheck{
plancheck.ExpectResourceAction(resourceName, plancheck.ResourceActionUpdate),
plancheck.ExpectKnownValue(resourceName, regionConfig0_0.AtMapKey("read_only_specs"), specInstanceSizeNodeCount("M10", 2)), // copied from state
plancheck.ExpectKnownValue(resourceName, regionConfig0_0.AtMapKey("electable_specs"), specInstanceSizeNodeCount("M10", 5)), // copied from state
plancheck.ExpectKnownValue(resourceName, regionConfig1_0.AtMapKey("read_only_specs"), specInstanceSizeNodeCount("M20", 1)), // copied from plan in same region_config
plancheck.ExpectKnownValue(resourceName, regionConfig1_1.AtMapKey("read_only_specs"), specInstanceSizeNodeCount("M20", 2)), // copied from plan in different region_config
plancheck.ExpectUnknownValue(resourceName, regionConfig1_1.AtMapKey("read_only_specs").AtMapKey("disk_iops")), // auto_scaling.compute_enabled is used, so disk_iops should be unknown
plancheck.ExpectUnknownValue(resourceName, regionConfig1_1.AtMapKey("read_only_specs").AtMapKey("disk_size_gb")), // auto_scaling.disk_enabled is used, so disk_size_gb should be unknown
plancheck.ExpectUnknownValue(resourceName, regionConfig1_1.AtMapKey("electable_specs")), // node_count = 0, should not be copied
},
},
}
)
unit.RunPlanCheckTests(t, baseConfig, testCases)
}
14 changes: 1 addition & 13 deletions internal/service/advancedclustertpf/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,19 +101,7 @@ func (r *rs) ModifyPlan(ctx context.Context, req resource.ModifyPlanRequest, res
if req.State.Raw.IsNull() || req.Plan.Raw.IsNull() || req.Plan.Raw.IsFullyKnown() { // Return early unless it is an Update
return
}
var plan, state TFModel
diags := &resp.Diagnostics
diags.Append(req.Plan.Get(ctx, &plan)...)
diags.Append(req.State.Get(ctx, &state)...)
if diags.HasError() {
return
}

useStateForUnknowns(ctx, diags, &state, &plan)
if diags.HasError() {
return
}
diags.Append(resp.Plan.Set(ctx, plan)...)
unknownReplacements(ctx, &req.State, &resp.Plan, &resp.Diagnostics)
}

func (r *rs) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ steps:
create = "6000s"
}
}

data "mongodbatlas_advanced_cluster" "test" {
project_id = mongodbatlas_advanced_cluster.test.project_id
name = mongodbatlas_advanced_cluster.test.name
use_replication_spec_per_shard = true
depends_on = [mongodbatlas_advanced_cluster.test]
}

data "mongodbatlas_advanced_clusters" "test" {
use_replication_spec_per_shard = true
project_id = mongodbatlas_advanced_cluster.test.project_id
depends_on = [mongodbatlas_advanced_cluster.test]
}
diff_requests: []
request_responses:
- path: /api/atlas/v2/groups/{groupId}/clusters/{clusterName}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
resource "mongodbatlas_advanced_cluster" "test" {
project_id = "111111111111111111111111"
name = "mocked-cluster"
cluster_type = "SHARDED"

replication_specs = [{
region_configs = [{
auto_scaling = {
compute_enabled = false
compute_scale_down_enabled = false
disk_gb_enabled = true
}
electable_specs = {
disk_size_gb = 10
instance_size = "M10"
node_count = 3
}
priority = 7
provider_name = "AWS"
region_name = "US_EAST_1"
}]
}]
timeouts = {
create = "6000s"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
resource "mongodbatlas_advanced_cluster" "test" {
project_id = "111111111111111111111111"
name = "mocked-cluster"
cluster_type = "REPLICASET"

replication_specs = [{
region_configs = [{
auto_scaling = {
compute_enabled = false
compute_scale_down_enabled = false
disk_gb_enabled = true
}
electable_specs = {
disk_size_gb = 99
instance_size = "M10"
node_count = 3
}
priority = 7
provider_name = "AWS"
region_name = "US_EAST_1"
}]
}]
timeouts = {
create = "6000s"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
resource "mongodbatlas_advanced_cluster" "test" {
project_id = "111111111111111111111111"
name = "mocked-cluster"
cluster_type = "REPLICASET"

pinned_fcv = {
expiration_date = "2025-04-03T00:00:00Z"
}

replication_specs = [{
region_configs = [{
auto_scaling = {
compute_enabled = false
compute_scale_down_enabled = false
disk_gb_enabled = true
}
electable_specs = {
disk_size_gb = 10
instance_size = "M10"
node_count = 3
}
priority = 7
provider_name = "AWS"
region_name = "US_EAST_1"
}]
}]
timeouts = {
create = "6000s"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
resource "mongodbatlas_advanced_cluster" "test" {
project_id = "111111111111111111111111"
name = "mocked-cluster"
cluster_type = "REPLICASET"

replication_specs = [{
region_configs = [{
auto_scaling = {
compute_enabled = false
compute_scale_down_enabled = false
disk_gb_enabled = true
}
electable_specs = {
disk_size_gb = 10
instance_size = "M10"
node_count = 3
}
priority = 7
provider_name = "AWS"
region_name = "US_EAST_1"
}]
}]
timeouts = {
create = "6000s"
}
advanced_configuration = {
tls_cipher_config_mode = "CUSTOM"
}
}
Loading