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_backup_policy_vm: support instant_restore_resource_group #20562

Merged
merged 2 commits into from
Feb 21, 2023
Merged
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
63 changes: 63 additions & 0 deletions internal/services/recoveryservices/backup_policy_vm_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/Azure/azure-sdk-for-go/services/recoveryservices/mgmt/2021-12-01/backup" // nolint: staticcheck
"github.com/Azure/go-autorest/autorest/date"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/recoveryservices/parse"
Expand Down Expand Up @@ -147,6 +148,7 @@ func resourceBackupProtectionPolicyVMCreateUpdate(d *pluginsdk.ResourceData, met
BackupManagementType: backup.ManagementTypeBasicProtectionPolicyBackupManagementTypeAzureIaasVM,
PolicyType: policyType,
SchedulePolicy: *schedulePolicy,
InstantRPDetails: expandBackupProtectionPolicyVMResourceGroup(d),
RetentionPolicy: &backup.LongTermRetentionPolicy{ // SimpleRetentionPolicy only has duration property ¯\_(ツ)_/¯
RetentionPolicyType: backup.RetentionPolicyTypeLongTermRetentionPolicy,
DailySchedule: expandBackupProtectionPolicyVMRetentionDaily(d, times),
Expand Down Expand Up @@ -269,6 +271,10 @@ func resourceBackupProtectionPolicyVMRead(d *pluginsdk.ResourceData, meta interf
d.Set("retention_yearly", nil)
}
}

if instantRPDetail := properties.InstantRPDetails; instantRPDetail != nil {
d.Set("instant_restore_resource_group", flattenBackupProtectionPolicyVMResourceGroup(*instantRPDetail))
}
}

return nil
Expand Down Expand Up @@ -403,6 +409,21 @@ func expandBackupProtectionPolicyVMSchedule(d *pluginsdk.ResourceData, times []d
return nil, nil
}

func expandBackupProtectionPolicyVMResourceGroup(d *pluginsdk.ResourceData) *backup.InstantRPAdditionalDetails {
if v, ok := d.Get("instant_restore_resource_group").([]interface{}); ok && len(v) > 0 {
rgRaw := v[0].(map[string]interface{})
output := &backup.InstantRPAdditionalDetails{
AzureBackupRGNamePrefix: utils.String(rgRaw["prefix"].(string)),
}
if suffix, ok := rgRaw["suffix"]; ok && suffix != "" {
output.AzureBackupRGNameSuffix = utils.String(suffix.(string))
}
return output
}

return nil
}

func expandBackupProtectionPolicyVMRetentionDaily(d *pluginsdk.ResourceData, times []date.Time) *backup.DailyRetentionSchedule {
if rb, ok := d.Get("retention_daily").([]interface{}); ok && len(rb) > 0 {
block := rb[0].(map[string]interface{})
Expand Down Expand Up @@ -517,6 +538,28 @@ func expandBackupProtectionPolicyVMRetentionWeeklyFormat(block map[string]interf
return &weekly
}

func flattenBackupProtectionPolicyVMResourceGroup(rpDetail backup.InstantRPAdditionalDetails) []interface{} {
if rpDetail.AzureBackupRGNamePrefix == nil {
return nil
}

block := map[string]interface{}{}

prefix := ""
if rpDetail.AzureBackupRGNamePrefix != nil {
prefix = *rpDetail.AzureBackupRGNamePrefix
}
block["prefix"] = prefix

suffix := ""
if rpDetail.AzureBackupRGNameSuffix != nil {
suffix = *rpDetail.AzureBackupRGNameSuffix
}
block["suffix"] = suffix

return []interface{}{block}
}

func flattenBackupProtectionPolicyVMSchedule(schedule *backup.SimpleSchedulePolicy) []interface{} {
block := map[string]interface{}{}

Expand Down Expand Up @@ -763,6 +806,26 @@ func resourceBackupProtectionPolicyVMSchema() map[string]*pluginsdk.Schema {
Default: "UTC",
},

"instant_restore_resource_group": {
Type: pluginsdk.TypeList,
MaxItems: 1,
Optional: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*schema.Schema{
"prefix": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"suffix": {
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: validation.StringIsNotEmpty,
},
},
},
},

"backup": {
Type: pluginsdk.TypeList,
MaxItems: 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,21 @@ func TestAccBackupProtectionPolicyVM_updateWeeklyToPartialV2(t *testing.T) {
})
}

func TestAccBackupProtectionPolicyVM_withCustomRGName(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_backup_policy_vm", "test")
r := BackupProtectionPolicyVMResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.withCustomResourceGroup(data),
Check: acceptance.ComposeAggregateTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func (t BackupProtectionPolicyVMResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := parse.BackupPolicyID(state.ID)
if err != nil {
Expand Down Expand Up @@ -865,3 +880,29 @@ resource "azurerm_backup_policy_vm" "test" {
}
`, r.template(data), data.RandomInteger, retentionRange, policyType)
}

func (r BackupProtectionPolicyVMResource) withCustomResourceGroup(data acceptance.TestData) string {
return fmt.Sprintf(`
%s

resource "azurerm_backup_policy_vm" "test" {
name = "acctest-%d"
resource_group_name = azurerm_resource_group.test.name
recovery_vault_name = azurerm_recovery_services_vault.test.name

instant_restore_resource_group {
prefix = "acctest"
suffix = "suffix"
}

backup {
frequency = "Daily"
time = "23:00"
}

retention_daily {
count = 10
}
}
`, r.template(data), data.RandomInteger)
}
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ resource "azurerm_recovery_services_vault" "test" {
resource_group_name = azurerm_resource_group.test.name
sku = "Standard"

cross_region_restore_enabled = %v
cross_region_restore_enabled = %t

soft_delete_enabled = false
}
Expand Down
2 changes: 1 addition & 1 deletion website/docs/d/backup_policy_vm.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ subcategory: "Recovery Services"
layout: "azurerm"
page_title: "Azure Resource Manager: azurerm_backup_policy_vm"
description: |-
Gets information about an existing existing VM Backup Policy.
Gets information about an existing VM Backup Policy.
---

# Data Source: azurerm_backup_policy_vm
Expand Down