From ce5343ee65aef2d372c069b0d28b57769423f432 Mon Sep 17 00:00:00 2001 From: ziyeqf Date: Wed, 22 Feb 2023 17:04:30 +0800 Subject: [PATCH] update `backup_policy_id` to `Optional` --- .../backup_protected_vm_resource.go | 16 +++++++++------- .../backup_protected_vm_resource_test.go | 9 ++++++++- website/docs/r/backup_protected_vm.html.markdown | 2 +- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/internal/services/recoveryservices/backup_protected_vm_resource.go b/internal/services/recoveryservices/backup_protected_vm_resource.go index 8fe66e025d38..84c9c1d08329 100644 --- a/internal/services/recoveryservices/backup_protected_vm_resource.go +++ b/internal/services/recoveryservices/backup_protected_vm_resource.go @@ -106,9 +106,9 @@ func resourceRecoveryServicesBackupProtectedVMCreateUpdate(d *pluginsdk.Resource }, } - requireAdditionalupdate := false + requireAdditionalUpdate := false if d.Get("protection_stopped").(bool) && d.IsNewResource() { // it only needs an additional update for new resource. - requireAdditionalupdate = true + requireAdditionalUpdate = true } if _, err = client.CreateOrUpdate(ctx, vaultName, resourceGroup, "Azure", containerName, protectedItemName, item); err != nil { @@ -120,7 +120,7 @@ func resourceRecoveryServicesBackupProtectedVMCreateUpdate(d *pluginsdk.Resource return err } - if requireAdditionalupdate { + if requireAdditionalUpdate { updateInput := backup.ProtectedItemResource{ Properties: &backup.AzureIaaSComputeVMProtectedItem{ ProtectionState: backup.ProtectionStateProtectionStopped, @@ -405,8 +405,9 @@ func resourceRecoveryServicesBackupProtectedVMSchema() map[string]*pluginsdk.Sch "backup_policy_id": { Type: pluginsdk.TypeString, - Required: true, + Optional: true, ValidateFunc: azure.ValidateResourceID, + ExactlyOneOf: []string{"backup_policy_id", "protection_stopped"}, }, "exclude_disk_luns": { @@ -430,9 +431,10 @@ func resourceRecoveryServicesBackupProtectedVMSchema() map[string]*pluginsdk.Sch }, "protection_stopped": { - Type: pluginsdk.TypeBool, - Optional: true, - Computed: true, + Type: pluginsdk.TypeBool, + Optional: true, + Computed: true, + ExactlyOneOf: []string{"backup_policy_id", "protection_stopped"}, }, } } diff --git a/internal/services/recoveryservices/backup_protected_vm_resource_test.go b/internal/services/recoveryservices/backup_protected_vm_resource_test.go index 5532b04a9d33..d2e73ec54726 100644 --- a/internal/services/recoveryservices/backup_protected_vm_resource_test.go +++ b/internal/services/recoveryservices/backup_protected_vm_resource_test.go @@ -206,6 +206,14 @@ func TestAccBackupProtectedVm_protectionStopped(t *testing.T) { r := BackupProtectedVmResource{} data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.basic(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + check.That(data.ResourceName).Key("resource_group_name").Exists(), + ), + }, + data.ImportStep(), { Config: r.protectionStopped(data), Check: acceptance.ComposeTestCheckFunc( @@ -716,7 +724,6 @@ resource "azurerm_backup_protected_vm" "test" { resource_group_name = azurerm_resource_group.test.name recovery_vault_name = azurerm_recovery_services_vault.test.name source_vm_id = azurerm_virtual_machine.test.id - backup_policy_id = azurerm_backup_policy_vm.test.id include_disk_luns = [0] protection_stopped = true diff --git a/website/docs/r/backup_protected_vm.html.markdown b/website/docs/r/backup_protected_vm.html.markdown index 15f44cfc1dd8..2a2415410e58 100644 --- a/website/docs/r/backup_protected_vm.html.markdown +++ b/website/docs/r/backup_protected_vm.html.markdown @@ -62,7 +62,7 @@ The following arguments are supported: ~> **NOTE:** After creation, the `source_vm_id` property can be removed without forcing a new resource to be created; however, setting it to a different ID will create a new resource. This allows the source vm to be deleted without having to remove the backup. -* `backup_policy_id` - (Required) Specifies the id of the backup policy to use. +* `backup_policy_id` - (Optional) Specifies the id of the backup policy to use. Required when `protection_stopped` is not specified. * `exclude_disk_luns` - (Optional) A list of Disks' Logical Unit Numbers(LUN) to be excluded for VM Protection.