diff --git a/internal/services/recoveryservices/site_recovery_replication_recovery_plan_resource.go b/internal/services/recoveryservices/site_recovery_replication_recovery_plan_resource.go index 13b1d43a167b..0c591ba060cf 100644 --- a/internal/services/recoveryservices/site_recovery_replication_recovery_plan_resource.go +++ b/internal/services/recoveryservices/site_recovery_replication_recovery_plan_resource.go @@ -517,14 +517,43 @@ func (r SiteRecoveryReplicationRecoveryPlanResource) Update() sdk.ResourceFunc { return fmt.Errorf("parse Site reocvery replication plan id: %+v", err) } - recoveryPlanGroup, err := expandRecoveryGroup(model.RecoveryGroup) + resp, err := client.Get(ctx, *id) + if err != nil { + return fmt.Errorf("retrieving %s: %+v", *id, err) + } + + var groupValue []replicationrecoveryplans.RecoveryPlanGroup + if resp.Model == nil { + return fmt.Errorf("retrieving %s: model is nil", *id) + } + + if resp.Model.Properties == nil { + return fmt.Errorf("retrieving %s: properties is nil", *id) + } + + if resp.Model.Properties.Groups == nil { + return fmt.Errorf("retrieving %s: groups is nil", *id) + } + + groupValue = *resp.Model.Properties.Groups + + if metadata.ResourceData.HasChange("recovery_group") { + groupValue, err = expandRecoveryGroup(model.RecoveryGroup) + } + + if metadata.ResourceData.HasChange("boot_recovery_group") || + metadata.ResourceData.HasChange("failover_recovery_group") || + metadata.ResourceData.HasChange("shutdown_recovery_group") { + groupValue, err = expandRecoveryGroupNew(model.ShutdownRecoveryGroup, model.FailoverRecoveryGroup, model.BootRecoveryGroup) + } + if err != nil { - return fmt.Errorf("when expanding recovery group: %s", err) + return fmt.Errorf("expanding recovery group: %+v", err) } parameters := replicationrecoveryplans.UpdateRecoveryPlanInput{ Properties: &replicationrecoveryplans.UpdateRecoveryPlanInputProperties{ - Groups: &recoveryPlanGroup, + Groups: &groupValue, }, } diff --git a/internal/services/recoveryservices/site_recovery_replication_recovery_plan_resource_test.go b/internal/services/recoveryservices/site_recovery_replication_recovery_plan_resource_test.go index 6d4da320c80e..4f867da1a1eb 100644 --- a/internal/services/recoveryservices/site_recovery_replication_recovery_plan_resource_test.go +++ b/internal/services/recoveryservices/site_recovery_replication_recovery_plan_resource_test.go @@ -85,6 +85,38 @@ func TestAccSiteRecoveryReplicationRecoveryPlan_withMultiActions(t *testing.T) { }) } +func TestAccSiteRecoveryReplicationRecoveryPlan_updateWithmultiActions(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_site_recovery_replication_recovery_plan", "test") + r := SiteRecoveryReplicationRecoveryPlan{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.withMultiActions(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + // to check the actions are in the correct order + check.That(data.ResourceName).Key("boot_recovery_group.0.pre_action.0.name").HasValue("testPreAction1"), + check.That(data.ResourceName).Key("boot_recovery_group.0.pre_action.1.name").HasValue("testPreAction2"), + check.That(data.ResourceName).Key("boot_recovery_group.0.post_action.0.name").HasValue("testPostAction1"), + check.That(data.ResourceName).Key("boot_recovery_group.0.post_action.1.name").HasValue("testPostAction2"), + ), + }, + data.ImportStep(), + { + Config: r.updateWithMultiActions(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + // to check the actions are in the correct order + check.That(data.ResourceName).Key("boot_recovery_group.0.pre_action.0.name").HasValue("testPreAction1-new"), + check.That(data.ResourceName).Key("boot_recovery_group.0.pre_action.1.name").HasValue("testPreAction2-new"), + check.That(data.ResourceName).Key("boot_recovery_group.0.post_action.0.name").HasValue("testPostAction1-new"), + check.That(data.ResourceName).Key("boot_recovery_group.0.post_action.1.name").HasValue("testPostAction2-new"), + ), + }, + data.ImportStep(), + }) +} + func TestAccSiteRecoveryReplicationRecoveryPlan_withZones(t *testing.T) { data := acceptance.BuildTestData(t, "azurerm_site_recovery_replication_recovery_plan", "test") r := SiteRecoveryReplicationRecoveryPlan{} @@ -513,6 +545,59 @@ resource "azurerm_site_recovery_replication_recovery_plan" "test" { `, r.template(data), data.RandomInteger) } +func (r SiteRecoveryReplicationRecoveryPlan) updateWithMultiActions(data acceptance.TestData) string { + return fmt.Sprintf(` +%s + +resource "azurerm_site_recovery_replication_recovery_plan" "test" { + name = "acctest-%[2]d" + recovery_vault_id = azurerm_recovery_services_vault.test.id + source_recovery_fabric_id = azurerm_site_recovery_fabric.test1.id + target_recovery_fabric_id = azurerm_site_recovery_fabric.test2.id + + shutdown_recovery_group {} + + failover_recovery_group {} + + boot_recovery_group { + replicated_protected_items = [azurerm_site_recovery_replicated_vm.test.id] + pre_action { + name = "testPreAction1-new" + type = "ManualActionDetails" + fail_over_directions = ["PrimaryToRecovery"] + fail_over_types = ["TestFailover"] + manual_action_instruction = "test instruction" + } + + pre_action { + name = "testPreAction2-new" + type = "ManualActionDetails" + fail_over_directions = ["PrimaryToRecovery"] + fail_over_types = ["TestFailover"] + manual_action_instruction = "test instruction" + } + + post_action { + name = "testPostAction1-new" + type = "ManualActionDetails" + fail_over_directions = ["PrimaryToRecovery"] + fail_over_types = ["TestFailover"] + manual_action_instruction = "test instruction" + } + + post_action { + name = "testPostAction2-new" + type = "ManualActionDetails" + fail_over_directions = ["PrimaryToRecovery"] + fail_over_types = ["TestFailover"] + manual_action_instruction = "test instruction" + } + } + +} +`, r.template(data), data.RandomInteger) +} + func (r SiteRecoveryReplicationRecoveryPlan) withZones(data acceptance.TestData) string { return fmt.Sprintf(` %s