Skip to content
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

azurerm_site_recovery_protection_container_mapping - support automatic_update_extension.automatic_update_extensions_enabled and automatic_update_extension.automation_account_id properties #19710

Merged
merged 16 commits into from
Jan 30, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func resourceSiteRecoveryProtectionContainerMapping() *pluginsdk.Resource {
return &pluginsdk.Resource{
Create: resourceSiteRecoveryContainerMappingCreate,
Read: resourceSiteRecoveryContainerMappingRead,
Update: nil,
Update: resourceSiteRecoveryContainerMappingUpdate,
Delete: resourceSiteRecoveryServicesContainerMappingDelete,
Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
_, err := parse.ReplicationProtectionContainerMappingsID(id)
Expand Down Expand Up @@ -77,6 +77,18 @@ func resourceSiteRecoveryProtectionContainerMapping() *pluginsdk.Resource {
ValidateFunc: azure.ValidateResourceID,
DiffSuppressFunc: suppress.CaseDifference,
},
"automatic_update_extensions_enabled": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: false,
},
"automation_account_id": {
Type: pluginsdk.TypeString,
Optional: true,
ForceNew: true,
RequiredWith: []string{"automatic_update_extensions_enabled"},
ziyeqf marked this conversation as resolved.
Show resolved Hide resolved
ValidateFunc: azure.ValidateResourceID,
},
},
}
}
Expand Down Expand Up @@ -111,9 +123,24 @@ func resourceSiteRecoveryContainerMappingCreate(d *pluginsdk.ResourceData, meta
Properties: &siterecovery.CreateProtectionContainerMappingInputProperties{
TargetProtectionContainerID: &targetContainerId,
PolicyID: &policyId,
ProviderSpecificInput: siterecovery.ReplicationProviderSpecificContainerMappingInput{},
},
}

autoUpdateEnabledValue := siterecovery.Disabled
if d.Get("automatic_update_extensions_enabled").(bool) {
autoUpdateEnabledValue = siterecovery.Enabled
}

if autoUpdateEnabledValue == siterecovery.Enabled {
parameters.Properties.ProviderSpecificInput = siterecovery.A2AContainerMappingInput{
InstanceType: siterecovery.InstanceTypeBasicReplicationProviderSpecificContainerMappingInputInstanceTypeA2A,
AgentAutoUpdateStatus: autoUpdateEnabledValue,
AutomationAccountArmID: utils.String(d.Get("automation_account_id").(string)),
}
} else {
parameters.Properties.ProviderSpecificInput = siterecovery.ReplicationProviderSpecificContainerMappingInput{}
}

future, err := client.Create(ctx, fabricName, protectionContainerName, name, parameters)
if err != nil {
return fmt.Errorf("creating site recovery protection container mapping %s (vault %s): %+v", name, vaultName, err)
Expand All @@ -132,6 +159,75 @@ func resourceSiteRecoveryContainerMappingCreate(d *pluginsdk.ResourceData, meta
return resourceSiteRecoveryContainerMappingRead(d, meta)
}

func resourceSiteRecoveryContainerMappingUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
resGroup := d.Get("resource_group_name").(string)
vaultName := d.Get("recovery_vault_name").(string)

client := meta.(*clients.Client).RecoveryServices.ContainerMappingClient(resGroup, vaultName)
ctx, cancel := timeouts.ForUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.ReplicationProtectionContainerMappingsID(d.Id())
if err != nil {
return err
}

resp, err := client.Get(ctx, id.ReplicationFabricName, id.ReplicationProtectionContainerName, id.ReplicationProtectionContainerMappingName)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
d.SetId("")
return nil
}
return fmt.Errorf("making Read request on site recovery protection container mapping %s : %+v", id.String(), err)
}

if resp.Properties == nil {
return fmt.Errorf("making Read request on site recovery protection container mapping %s : `Properties` is nil", id.String())
}

detail, ok := resp.Properties.ProviderSpecificDetails.AsA2AProtectionContainerMappingDetails()
if !ok {
return fmt.Errorf("update %s: type mismatch", id)
}

updateInput := siterecovery.A2AUpdateContainerMappingInput{
AgentAutoUpdateStatus: detail.AgentAutoUpdateStatus,
AutomationAccountArmID: detail.AutomationAccountArmID,
}

if d.HasChange("automatic_update_extensions_enabled") {
if d.Get("automatic_update_extensions_enabled").(bool) {
updateInput.AgentAutoUpdateStatus = siterecovery.Enabled
} else {
updateInput.AgentAutoUpdateStatus = siterecovery.Disabled
}
}

if d.HasChange("automation_account_id") {
if v, ok := d.GetOk("automation_account_id"); ok {
updateInput.AutomationAccountArmID = utils.String(v.(string))
} else {
updateInput.AutomationAccountArmID = nil
}
}

update := siterecovery.UpdateProtectionContainerMappingInput{
Properties: &siterecovery.UpdateProtectionContainerMappingInputProperties{
ProviderSpecificInput: updateInput,
},
}

future, err := client.Update(ctx, id.ReplicationFabricName, id.ReplicationProtectionContainerName, id.ReplicationProtectionContainerMappingName, update)
if err != nil {
return fmt.Errorf("update %s: %+v", id, err)
}
if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("update %s: %+v", id, err)
}

return resourceSiteRecoveryContainerMappingRead(d, meta)
}

func resourceSiteRecoveryContainerMappingRead(d *pluginsdk.ResourceData, meta interface{}) error {
id, err := parse.ReplicationProtectionContainerMappingsID(d.Id())
if err != nil {
Expand All @@ -158,6 +254,12 @@ func resourceSiteRecoveryContainerMappingRead(d *pluginsdk.ResourceData, meta in
d.Set("name", resp.Name)
d.Set("recovery_replication_policy_id", resp.Properties.PolicyID)
d.Set("recovery_target_protection_container_id", resp.Properties.TargetProtectionContainerID)

if detail, ok := resp.Properties.ProviderSpecificDetails.AsA2AProtectionContainerMappingDetails(); ok {
d.Set("automatic_update_extensions_enabled", detail.AgentAutoUpdateStatus == siterecovery.Enabled)
d.Set("automation_account_id", detail.AutomationAccountArmID)
}

return nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,35 @@ func TestAccSiteRecoveryProtectionContainerMapping_basic(t *testing.T) {
})
}

func TestAccSiteRecoveryProtectionContainerMapping_withAutoUpdateExtension(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_site_recovery_protection_container_mapping", "test")
r := SiteRecoveryProtectionContainerMappingResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.autoUpdateExtension(data, true),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.autoUpdateExtension(data, false),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.autoUpdateExtension(data, true),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func (SiteRecoveryProtectionContainerMappingResource) basic(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
Expand Down Expand Up @@ -99,6 +128,89 @@ resource "azurerm_site_recovery_protection_container_mapping" "test" {
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, data.RandomInteger, data.Locations.Secondary, data.RandomInteger, data.RandomInteger, data.RandomInteger, data.RandomInteger)
}

func (SiteRecoveryProtectionContainerMappingResource) autoUpdateExtension(data acceptance.TestData, enabled bool) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "test1" {
name = "acctestRG-recovery-%d-1"
location = "%s"
}

resource "azurerm_automation_account" "test" {
name = "acctestAutomation-%d"
location = azurerm_resource_group.test1.location
resource_group_name = azurerm_resource_group.test1.name

sku_name = "Basic"

tags = {
Environment = "Test"
}
}

resource "azurerm_recovery_services_vault" "test" {
name = "acctest-vault-%d"
location = azurerm_resource_group.test1.location
resource_group_name = azurerm_resource_group.test1.name
sku = "Standard"

soft_delete_enabled = false
}

resource "azurerm_site_recovery_fabric" "test1" {
resource_group_name = azurerm_resource_group.test1.name
recovery_vault_name = azurerm_recovery_services_vault.test.name
name = "acctest-fabric1-%d"
location = azurerm_resource_group.test1.location
}

resource "azurerm_site_recovery_fabric" "test2" {
resource_group_name = azurerm_resource_group.test1.name
recovery_vault_name = azurerm_recovery_services_vault.test.name
name = "acctest-fabric2-%d"
location = "%s"
depends_on = [azurerm_site_recovery_fabric.test1]
}

resource "azurerm_site_recovery_protection_container" "test1" {
resource_group_name = azurerm_resource_group.test1.name
recovery_vault_name = azurerm_recovery_services_vault.test.name
recovery_fabric_name = azurerm_site_recovery_fabric.test1.name
name = "acctest-protection-cont1-%d"
}

resource "azurerm_site_recovery_protection_container" "test2" {
resource_group_name = azurerm_resource_group.test1.name
recovery_vault_name = azurerm_recovery_services_vault.test.name
recovery_fabric_name = azurerm_site_recovery_fabric.test2.name
name = "acctest-protection-cont2-%d"
}

resource "azurerm_site_recovery_replication_policy" "test" {
resource_group_name = azurerm_resource_group.test1.name
recovery_vault_name = azurerm_recovery_services_vault.test.name
name = "acctest-policy-%d"
recovery_point_retention_in_minutes = 24 * 60
application_consistent_snapshot_frequency_in_minutes = 4 * 60
}

resource "azurerm_site_recovery_protection_container_mapping" "test" {
resource_group_name = azurerm_resource_group.test1.name
recovery_vault_name = azurerm_recovery_services_vault.test.name
recovery_fabric_name = azurerm_site_recovery_fabric.test1.name
recovery_source_protection_container_name = azurerm_site_recovery_protection_container.test1.name
recovery_target_protection_container_id = azurerm_site_recovery_protection_container.test2.id
recovery_replication_policy_id = azurerm_site_recovery_replication_policy.test.id
name = "mapping-%d"
automatic_update_extensions_enabled = %v
automation_account_id = azurerm_automation_account.test.id
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, data.RandomInteger, data.RandomInteger, data.Locations.Secondary, data.RandomInteger, data.RandomInteger, data.RandomInteger, data.RandomInteger, enabled)
}

func (t SiteRecoveryProtectionContainerMappingResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := parse.ReplicationProtectionContainerMappingsID(state.ID)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ The following arguments are supported:

* `recovery_replication_policy_id` - (Required) Id of the policy to use for this mapping. Changing this forces a new resource to be created.

* `automatic_update_extensions_enabled` - (Optional) Should the Mobility service installed on Azure virtual machines be automatically updated. Defaults to `false`.

~> **Note:** The setting applies to all Azure VMs protected in the same container. For more details see [this document](https://learn.microsoft.com/en-us/azure/site-recovery/azure-to-azure-autoupdate#enable-automatic-updates)

* `automation_account_id` - (Optional) The automation account ID which holds the automatic update runbook and authenticates to Azure resources.

~> **Note:** `automation_account_id` is required when `automatic_update_extensions_enabled` is specified.

## Attributes Reference

In addition to the arguments above, the following attributes are exported:
Expand All @@ -115,5 +123,5 @@ The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/l
Site Recovery Protection Container Mappings can be imported using the `resource id`, e.g.

```shell
terraform import azurerm_site_recovery_protection_container_mapping.mymapping /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resource-group-name/providers/Microsoft.RecoveryServices/vaults/recovery-vault-name
terraform import azurerm_site_recovery_protection_container_mapping.mymapping /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resource-group-name/providers/Microsoft.RecoveryServices/vaults/recovery-vault-name/replicationFabrics/fabric-name/replicationProtectionContainers/container-name/replicationProtectionContainerMappings/mapping-name
```