Skip to content

Commit

Permalink
update code
Browse files Browse the repository at this point in the history
  • Loading branch information
ziyeqf committed Feb 22, 2023
1 parent ce5343e commit ce80e45
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
30 changes: 17 additions & 13 deletions internal/services/recoveryservices/backup_protected_vm_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ func resourceRecoveryServicesBackupProtectedVMCreateUpdate(d *pluginsdk.Resource
vmId := d.Get("source_vm_id").(string)
policyId := d.Get("backup_policy_id").(string)

if d.IsNewResource() && policyId == "" {
return fmt.Errorf("`backup_policy_id` must be specified when creating")
}

// get VM name from id
parsedVmId, err := vmParse.VirtualMachineID(vmId)
if err != nil {
Expand Down Expand Up @@ -106,18 +110,21 @@ func resourceRecoveryServicesBackupProtectedVMCreateUpdate(d *pluginsdk.Resource
},
}

requireAdditionalUpdate := false
if d.Get("protection_stopped").(bool) && d.IsNewResource() { // it only needs an additional update for new resource.
requireAdditionalUpdate = true
}
requireAdditionalUpdate := d.Get("protection_stopped").(bool)
skipNormalUpdate := d.Get("protection_stopped").(bool) && !d.IsNewResource()

if _, err = client.CreateOrUpdate(ctx, vaultName, resourceGroup, "Azure", containerName, protectedItemName, item); err != nil {
return fmt.Errorf("creating/updating Azure Backup Protected VM %q (Resource Group %q): %+v", protectedItemName, resourceGroup, err)
}
if !skipNormalUpdate {
if _, err = client.CreateOrUpdate(ctx, vaultName, resourceGroup, "Azure", containerName, protectedItemName, item); err != nil {
return fmt.Errorf("creating/updating Azure Backup Protected VM %q (Resource Group %q): %+v", protectedItemName, resourceGroup, err)
}

resp, err := resourceRecoveryServicesBackupProtectedVMWaitForStateCreateUpdate(ctx, client, vaultName, resourceGroup, containerName, protectedItemName, d)
if err != nil {
return err
resp, err := resourceRecoveryServicesBackupProtectedVMWaitForStateCreateUpdate(ctx, client, vaultName, resourceGroup, containerName, protectedItemName, d)
if err != nil {
return err
}

id := strings.Replace(*resp.ID, "Subscriptions", "subscriptions", 1) // This code is a workaround for this bug https://github.com/Azure/azure-sdk-for-go/issues/2824
d.SetId(id)
}

if requireAdditionalUpdate {
Expand Down Expand Up @@ -150,9 +157,6 @@ func resourceRecoveryServicesBackupProtectedVMCreateUpdate(d *pluginsdk.Resource

}

id := strings.Replace(*resp.ID, "Subscriptions", "subscriptions", 1) // This code is a workaround for this bug https://github.com/Azure/azure-sdk-for-go/issues/2824
d.SetId(id)

return resourceRecoveryServicesBackupProtectedVMRead(d, meta)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,12 @@ resource "azurerm_virtual_machine" "test" {
enabled = true
storage_uri = azurerm_storage_account.test.primary_blob_endpoint
}
lifecycle {
ignore_changes = [
tags, identity
]
}
}
resource "azurerm_recovery_services_vault" "test" {
Expand Down Expand Up @@ -725,7 +731,7 @@ resource "azurerm_backup_protected_vm" "test" {
recovery_vault_name = azurerm_recovery_services_vault.test.name
source_vm_id = azurerm_virtual_machine.test.id
include_disk_luns = [0]
include_disk_luns = [0]
protection_stopped = true
}
`, r.base(data))
Expand Down
2 changes: 1 addition & 1 deletion website/docs/r/backup_protected_vm.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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` - (Optional) Specifies the id of the backup policy to use. Required when `protection_stopped` is not specified.
* `backup_policy_id` - (Optional) Specifies the id of the backup policy to use. Required in creation or when `protection_stopped` is not specified.

* `exclude_disk_luns` - (Optional) A list of Disks' Logical Unit Numbers(LUN) to be excluded for VM Protection.

Expand Down

0 comments on commit ce80e45

Please sign in to comment.