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 2 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
348 changes: 118 additions & 230 deletions internal/service/advancedclustertpf/plan_modifier.go

Large diffs are not rendered by default.

71 changes: 66 additions & 5 deletions internal/service/advancedclustertpf/plan_modifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var (
repSpec1 = tfjsonpath.New("replication_specs").AtSliceIndex(1)
regionConfig0 = repSpec0.AtMapKey("region_configs").AtSliceIndex(0)
regionConfig1 = repSpec1.AtMapKey("region_configs").AtSliceIndex(0)
advConfig = tfjsonpath.New("advanced_configuration")
mockConfig = unit.MockConfigAdvancedClusterTPF
)

Expand Down Expand Up @@ -69,9 +70,69 @@ func TestPlanChecksClusterTwoRepSpecsWithAutoScalingAndSpecs(t *testing.T) {
},
}
)
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.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.ExpectKnownValue(resourceName, regionConfig0.AtMapKey("read_only_specs").AtMapKey("disk_size_gb"), knownvalue.Int64Exact(99)),
plancheck.ExpectKnownValue(resourceName, regionConfig0.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)
}
16 changes: 2 additions & 14 deletions internal/service/advancedclustertpf/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,22 +98,10 @@ type rs struct {
// 1. UseStateForUnknown always copies the state for unknown values. However, that leads to `Error: Provider produced inconsistent result after apply` in some cases (see implementation below).
// 2. Adding the different UseStateForUnknown is very verbose.
func (r *rs) ModifyPlan(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) {
if req.State.Raw.IsNull() || req.Plan.Raw.IsNull() || req.Plan.Raw.IsFullyKnown() { // Return early unless it is an Update
if req.State.Raw.IsNull() || req.Plan.Raw.IsNull() || resp.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
@@ -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