Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -1531,6 +1531,8 @@ public boolean deleteBackup(final Long backupId, final Boolean forced) {

validateBackupForZone(backup.getZoneId());
accountManager.checkAccess(CallContext.current().getCallingAccount(), null, true, vm == null ? backup : vm);

checkForPendingBackupJobs(backup);
final BackupOffering offering = backupOfferingDao.findByIdIncludingRemoved(backup.getBackupOfferingId());
if (offering == null) {
throw new CloudRuntimeException(String.format("Backup offering with ID [%s] does not exist.", backup.getBackupOfferingId()));
Expand All @@ -1551,6 +1553,18 @@ public boolean deleteBackup(final Long backupId, final Boolean forced) {
throw new CloudRuntimeException("Failed to delete the backup");
}

private void checkForPendingBackupJobs(final BackupVO backup) {
String backupUuid = backup.getUuid();
long pendingJobs = asyncJobManager.countPendingJobs(backupUuid,
CreateVMFromBackupCmd.class.getName(),
CreateVMFromBackupCmdByAdmin.class.getName(),
RestoreBackupCmd.class.getName(),
RestoreVolumeFromBackupAndAttachToVMCmd.class.getName());
if (pendingJobs > 0) {
throw new CloudRuntimeException("Cannot delete Backup while a create Instance from Backup or restore Backup operation is in progress, please try again later.");
}
}

/**
* Get the pair: hostIp, datastoreUuid in which to restore the volume, based on the VM to be attached information
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
import org.apache.cloudstack.framework.config.impl.ConfigDepotImpl;
import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao;
import org.apache.cloudstack.storage.datastore.db.StoragePoolVO;
import org.apache.cloudstack.framework.jobs.AsyncJobManager;
import org.apache.cloudstack.framework.jobs.impl.AsyncJobVO;
import org.junit.After;
import org.junit.Assert;
Expand Down Expand Up @@ -241,6 +242,9 @@ public class BackupManagerTest {
@Mock
private GuestOSDao _guestOSDao;

@Mock
AsyncJobManager asyncJobManager;

private Gson gson;

private String[] hostPossibleValues = {"127.0.0.1", "hostname"};
Expand Down Expand Up @@ -1489,6 +1493,7 @@ public void testDeleteBackupVmNotFound() {
when(backup.getAccountId()).thenReturn(accountId);
when(backup.getBackupOfferingId()).thenReturn(backupOfferingId);
when(backup.getSize()).thenReturn(100L);
when(backup.getUuid()).thenReturn("backup-uuid");

overrideBackupFrameworkConfigValue();

Expand Down Expand Up @@ -1523,6 +1528,31 @@ public void testDeleteBackupVmNotFound() {
}
}

@Test(expected = CloudRuntimeException.class)
public void testDeleteBackupBlockedByPendingJobs() {
Long backupId = 1L;
Long vmId = 2L;

BackupVO backup = mock(BackupVO.class);
when(backup.getVmId()).thenReturn(vmId);
when(backup.getUuid()).thenReturn("backup-uuid");
when(backup.getZoneId()).thenReturn(1L);
when(backupDao.findByIdIncludingRemoved(backupId)).thenReturn(backup);

VMInstanceVO vm = mock(VMInstanceVO.class);
when(vmInstanceDao.findByIdIncludingRemoved(vmId)).thenReturn(vm);

overrideBackupFrameworkConfigValue();

when(asyncJobManager.countPendingJobs("backup-uuid",
"org.apache.cloudstack.api.command.user.vm.CreateVMFromBackupCmd",
"org.apache.cloudstack.api.command.admin.vm.CreateVMFromBackupCmdByAdmin",
"org.apache.cloudstack.api.command.user.backup.RestoreBackupCmd",
"org.apache.cloudstack.api.command.user.backup.RestoreVolumeFromBackupAndAttachToVMCmd")).thenReturn(1L);

backupManager.deleteBackup(backupId, false);
}

@Test
public void testNewBackupResponse() {
Long vmId = 1L;
Expand Down
Loading