Skip to content

Commit

Permalink
update code
Browse files Browse the repository at this point in the history
  • Loading branch information
ziyeqf committed Dec 8, 2023
1 parent 8ed382a commit ad42736
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
2 changes: 1 addition & 1 deletion internal/provider/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ func expandFeatures(input []interface{}) features.UserFeatures {
if len(items) > 0 && items[0] != nil {
appConfRaw := items[0].(map[string]interface{})
if v, ok := appConfRaw["recover_soft_deleted_backup_protected_vm"]; ok {
featuresMap.RecoveryServicesVault.RecoverSoftDeletedProtectedVm = v.(bool)
featuresMap.RecoveryServicesVault.RecoverSoftDeletedBackupProtected = v.(bool)
}
}
}
Expand Down
24 changes: 14 additions & 10 deletions internal/services/recoveryservices/backup_protected_vm_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,15 @@ func resourceRecoveryServicesBackupProtectedVMCreateUpdate(d *pluginsdk.Resource
}

if !response.WasNotFound(existing.HttpResponse) {
if meta.(*clients.Client).Features.RecoveryServicesVault.RecoverSoftDeletedProtectedVm {
if err := resourceRecoveryServicesVaultBackupProtectedVMrecoverSoftDeleted(ctx, client, opClient, id, existing.Model); err != nil {
isSoftDeleted := false
if meta.(*clients.Client).Features.RecoveryServicesVault.RecoverSoftDeletedBackupProtected {
isSoftDeleted, err = resourceRecoveryServicesVaultBackupProtectedVMrecoverSoftDeleted(ctx, client, opClient, id, existing.Model)
if err != nil {
return fmt.Errorf("recovering soft deleted %s: %+v", id, err)
}
}

if d.IsNewResource() {
if !isSoftDeleted && d.IsNewResource() {
return tf.ImportAsExistsError("azurerm_backup_protected_vm", id.ID())
}
}
Expand Down Expand Up @@ -424,39 +426,41 @@ func expandDiskLunList(input []interface{}) []interface{} {
return result
}

func resourceRecoveryServicesVaultBackupProtectedVMrecoverSoftDeleted(ctx context.Context, client *protecteditems.ProtectedItemsClient, opClient *backup.ProtectedItemOperationResultsClient, id protecteditems.ProtectedItemId, model *protecteditems.ProtectedItemResource) error {
func resourceRecoveryServicesVaultBackupProtectedVMrecoverSoftDeleted(ctx context.Context, client *protecteditems.ProtectedItemsClient, opClient *backup.ProtectedItemOperationResultsClient, id protecteditems.ProtectedItemId, model *protecteditems.ProtectedItemResource) (isSoftDeleted bool, err error) {
isSoftDeleted = false
if model == nil {
return fmt.Errorf("model was nil")
return isSoftDeleted, fmt.Errorf("model was nil")
}

if model.Properties == nil {
return fmt.Errorf("properties was nil")
return isSoftDeleted, fmt.Errorf("properties was nil")
}

if prop, ok := model.Properties.(protecteditems.AzureIaaSComputeVMProtectedItem); ok {
if prop.IsScheduledForDeferredDelete != nil && *prop.IsScheduledForDeferredDelete {
isSoftDeleted = true
resp, err := client.CreateOrUpdate(ctx, id, protecteditems.ProtectedItemResource{
Properties: &protecteditems.AzureIaaSComputeVMProtectedItem{
IsRehydrate: pointer.To(true),
},
})
if err != nil {
return fmt.Errorf("issuing request for %s: %+v", id, err)
return isSoftDeleted, fmt.Errorf("issuing request for %s: %+v", id, err)
}

operationId, err := parseBackupOperationId(resp.HttpResponse)
if err != nil {
return err
return isSoftDeleted, err
}

if err = resourceRecoveryServicesBackupProtectedVMWaitForStateCreateUpdate(ctx, opClient, id, operationId); err != nil {
return err
return isSoftDeleted, err
}

}
}

return nil
return isSoftDeleted, nil
}

func resourceRecoveryServicesBackupProtectedVMSchema() map[string]*pluginsdk.Schema {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,10 @@ func TestAccBackupProtectedVm_recoverSoftDeletedVM(t *testing.T) {
),
},
data.ImportStep(),
{
// to disable soft delete feature
Config: r.basic(data),
},
{
// vault cannot be deleted unless we unregister all backups
Config: r.base(data),
Expand Down Expand Up @@ -957,7 +961,11 @@ resource "azurerm_backup_protected_vm" "test" {
func (r BackupProtectedVmResource) basicWithSoftDeleted(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
features {
recovery_services_vault {
recover_soft_deleted_backup_protected_vm = true
}
}
}
%s
Expand Down

0 comments on commit ad42736

Please sign in to comment.