Skip to content

Commit

Permalink
azurerm_backup_policy_vm support instant_restore_retention_days p…
Browse files Browse the repository at this point in the history
…roperty (#8822)

Co-authored-by: kt <[email protected]>
Co-authored-by: jackofallops <[email protected]>
  • Loading branch information
3 people authored Oct 29, 2020
1 parent 7e261f9 commit 17f4bb6
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ func resourceArmBackupProtectionPolicyVM() *schema.Resource {

"resource_group_name": azure.SchemaResourceGroupName(),

"instant_restore_retention_days": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
ValidateFunc: validation.IntBetween(1, 5),
},

"recovery_vault_name": {
Type: schema.TypeString,
Required: true,
Expand Down Expand Up @@ -319,21 +326,28 @@ func resourceArmBackupProtectionPolicyVMCreateUpdate(d *schema.ResourceData, met
return fmt.Errorf("The Azure API has recently changed behaviour so that provisioning a `count` for the `retention_daily` field can no longer be less than 7 days for new/updates to existing Backup Policies. Please ensure that `count` is less than 7, currently %d", d.Get("retention_daily.0.count").(int))
}

policy := backup.ProtectionPolicyResource{
Tags: tags.Expand(t),
Properties: &backup.AzureIaaSVMProtectionPolicy{
TimeZone: utils.String(d.Get("timezone").(string)),
BackupManagementType: backup.BackupManagementTypeAzureIaasVM,
SchedulePolicy: expandArmBackupProtectionPolicyVMSchedule(d, times),
RetentionPolicy: &backup.LongTermRetentionPolicy{ // SimpleRetentionPolicy only has duration property ¯\_(ツ)_/¯
RetentionPolicyType: backup.RetentionPolicyTypeLongTermRetentionPolicy,
DailySchedule: expandArmBackupProtectionPolicyVMRetentionDaily(d, times),
WeeklySchedule: expandArmBackupProtectionPolicyVMRetentionWeekly(d, times),
MonthlySchedule: expandArmBackupProtectionPolicyVMRetentionMonthly(d, times),
YearlySchedule: expandArmBackupProtectionPolicyVMRetentionYearly(d, times),
},
vmProtectionPolicyProperties := &backup.AzureIaaSVMProtectionPolicy{
TimeZone: utils.String(d.Get("timezone").(string)),
BackupManagementType: backup.BackupManagementTypeAzureIaasVM,
SchedulePolicy: expandArmBackupProtectionPolicyVMSchedule(d, times),
RetentionPolicy: &backup.LongTermRetentionPolicy{ // SimpleRetentionPolicy only has duration property ¯\_(ツ)_/¯
RetentionPolicyType: backup.RetentionPolicyTypeLongTermRetentionPolicy,
DailySchedule: expandArmBackupProtectionPolicyVMRetentionDaily(d, times),
WeeklySchedule: expandArmBackupProtectionPolicyVMRetentionWeekly(d, times),
MonthlySchedule: expandArmBackupProtectionPolicyVMRetentionMonthly(d, times),
YearlySchedule: expandArmBackupProtectionPolicyVMRetentionYearly(d, times),
},
}

if d.HasChange("instant_restore_retention_days") {
vmProtectionPolicyProperties.InstantRpRetentionRangeInDays = utils.Int32(int32(d.Get("instant_restore_retention_days").(int)))
}

policy := backup.ProtectionPolicyResource{
Tags: tags.Expand(t),
Properties: vmProtectionPolicyProperties,
}

if _, err = client.CreateOrUpdate(ctx, vaultName, resourceGroup, policyName, policy); err != nil {
return fmt.Errorf("Error creating/updating Azure Backup Protection Policy %q (Resource Group %q): %+v", policyName, resourceGroup, err)
}
Expand Down Expand Up @@ -381,6 +395,7 @@ func resourceArmBackupProtectionPolicyVMRead(d *schema.ResourceData, meta interf

if properties, ok := resp.Properties.AsAzureIaaSVMProtectionPolicy(); ok && properties != nil {
d.Set("timezone", properties.TimeZone)
d.Set("instant_restore_retention_days", properties.InstantRpRetentionRangeInDays)

if schedule, ok := properties.SchedulePolicy.AsSimpleSchedulePolicy(); ok && schedule != nil {
if err := d.Set("backup", flattenArmBackupProtectionPolicyVMSchedule(schedule)); err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,39 @@ func TestAccAzureRMBackupProtectionPolicyVM_basicDaily(t *testing.T) {
})
}

func TestAccAzureRMBackupProtectionPolicyVM_withInstantRestoreRetentionRangeUpdate(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_backup_policy_vm", "test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
CheckDestroy: testCheckAzureRMBackupProtectionPolicyVmDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMBackupProtectionPolicyVM_basicDaily(data),
Check: resource.ComposeAggregateTestCheckFunc(
testCheckAzureRMBackupProtectionPolicyVmExists(data.ResourceName),
),
},
data.ImportStep(),
{
Config: testAccAzureRMBackupProtectionPolicyVM_basicDailyWithInstantRestoreRetentionRange(data),
Check: resource.ComposeAggregateTestCheckFunc(
testCheckAzureRMBackupProtectionPolicyVmExists(data.ResourceName),
),
},
data.ImportStep(),
{
Config: testAccAzureRMBackupProtectionPolicyVM_basicDaily(data),
Check: resource.ComposeAggregateTestCheckFunc(
testCheckAzureRMBackupProtectionPolicyVmExists(data.ResourceName),
),
},
data.ImportStep(),
},
})
}

func TestAccAzureRMBackupProtectionPolicyVM_requiresImport(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_backup_policy_vm", "test")

Expand Down Expand Up @@ -535,3 +568,25 @@ resource "azurerm_backup_policy_vm" "test" {
}
`, template, data.RandomInteger)
}

func testAccAzureRMBackupProtectionPolicyVM_basicDailyWithInstantRestoreRetentionRange(data acceptance.TestData) string {
template := testAccAzureRMBackupProtectionPolicyVM_template(data)
return fmt.Sprintf(`
%s
resource "azurerm_backup_policy_vm" "test" {
name = "acctest-BPVM-%d"
resource_group_name = azurerm_resource_group.test.name
recovery_vault_name = azurerm_recovery_services_vault.test.name
instant_restore_retention_days = 5
backup {
frequency = "Daily"
time = "23:00"
}
retention_daily {
count = 10
}
}
`, template, data.RandomInteger)
}
2 changes: 2 additions & 0 deletions website/docs/r/backup_policy_vm.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ The following arguments are supported:

* `timezone` - (Optional) Specifies the timezone. Defaults to `UTC`

* `instant_restore_retention_days` - (Optional) Specifies the instant restore retention range in days.

* `retention_daily` - (Optional) Configures the policy daily retention as documented in the `retention_daily` block below. Required when backup frequency is `Daily`.

* `retention_weekly` - (Optional) Configures the policy weekly retention as documented in the `retention_weekly` block below. Required when backup frequency is `Weekly`.
Expand Down

0 comments on commit 17f4bb6

Please sign in to comment.