use of com.cloud.legacymodel.communication.command.RevertToVMSnapshotCommand in project cosmic by MissionCriticalCloud.
the class DefaultVMSnapshotStrategy method revertVMSnapshot.
@Override
public boolean revertVMSnapshot(final VMSnapshot vmSnapshot) {
final VMSnapshotVO vmSnapshotVO = (VMSnapshotVO) vmSnapshot;
final UserVmVO userVm = userVmDao.findById(vmSnapshot.getVmId());
try {
vmSnapshotHelper.vmSnapshotStateTransitTo(vmSnapshotVO, VMSnapshot.Event.RevertRequested);
} catch (final NoTransitionException e) {
throw new CloudRuntimeException(e.getMessage());
}
boolean result = false;
try {
final VMSnapshotVO snapshot = vmSnapshotDao.findById(vmSnapshotVO.getId());
final List<VolumeObjectTO> volumeTOs = vmSnapshotHelper.getVolumeTOList(userVm.getId());
final String vmInstanceName = userVm.getInstanceName();
final VMSnapshotTO parent = vmSnapshotHelper.getSnapshotWithParents(snapshot).getParent();
final VMSnapshotTO vmSnapshotTO = new VMSnapshotTO(snapshot.getId(), snapshot.getName(), snapshot.getType(), snapshot.getCreated().getTime(), snapshot.getDescription(), snapshot.getCurrent(), parent, true);
final Long hostId = vmSnapshotHelper.pickRunningHost(vmSnapshot.getVmId());
final GuestOSVO guestOS = guestOSDao.findById(userVm.getGuestOSId());
final RevertToVMSnapshotCommand revertToSnapshotCommand = new RevertToVMSnapshotCommand(vmInstanceName, userVm.getUuid(), vmSnapshotTO, volumeTOs, guestOS.getDisplayName());
final HostVO host = hostDao.findById(hostId);
final GuestOSHypervisorVO guestOsMapping = guestOsHypervisorDao.findByOsIdAndHypervisor(guestOS.getId(), host.getHypervisorType().toString(), host.getHypervisorVersion());
if (guestOsMapping == null) {
revertToSnapshotCommand.setPlatformEmulator(null);
} else {
revertToSnapshotCommand.setPlatformEmulator(guestOsMapping.getGuestOsName());
}
final RevertToVMSnapshotAnswer answer = (RevertToVMSnapshotAnswer) agentMgr.send(hostId, revertToSnapshotCommand);
if (answer != null && answer.getResult()) {
processAnswer(vmSnapshotVO, userVm, answer, hostId);
result = true;
} else {
String errMsg = "Revert VM: " + userVm.getInstanceName() + " to snapshot: " + vmSnapshotVO.getName() + " failed";
if (answer != null && answer.getDetails() != null) {
errMsg = errMsg + " due to " + answer.getDetails();
}
s_logger.error(errMsg);
throw new CloudRuntimeException(errMsg);
}
} catch (final OperationTimedoutException e) {
s_logger.debug("Failed to revert vm snapshot", e);
throw new CloudRuntimeException(e.getMessage());
} catch (final AgentUnavailableException e) {
s_logger.debug("Failed to revert vm snapshot", e);
throw new CloudRuntimeException(e.getMessage());
} finally {
if (!result) {
try {
vmSnapshotHelper.vmSnapshotStateTransitTo(vmSnapshot, VMSnapshot.Event.OperationFailed);
} catch (final NoTransitionException e1) {
s_logger.error("Cannot set vm snapshot state due to: " + e1.getMessage());
}
}
}
return result;
}
use of com.cloud.legacymodel.communication.command.RevertToVMSnapshotCommand in project cosmic by MissionCriticalCloud.
the class NotAValidCommand method testRevertToVMSnapshotCommand.
@Test
public void testRevertToVMSnapshotCommand() {
final Connection conn = Mockito.mock(Connection.class);
final VMSnapshotTO snapshotTO = Mockito.mock(VMSnapshotTO.class);
final List<VolumeObjectTO> volumeTOs = new ArrayList<>();
final RevertToVMSnapshotCommand vmSnapshot = new RevertToVMSnapshotCommand("Test", "uuid", snapshotTO, volumeTOs, "Debian");
final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
assertNotNull(wrapper);
when(this.citrixResourceBase.getConnection()).thenReturn(conn);
final Answer answer = wrapper.execute(vmSnapshot, this.citrixResourceBase);
verify(this.citrixResourceBase, times(1)).getConnection();
assertFalse(answer.getResult());
}
Aggregations