use of com.cloud.legacymodel.exceptions.CloudException in project cosmic by MissionCriticalCloud.
the class UserVmManagerImpl method stopVirtualMachine.
@Override
@ActionEvent(eventType = EventTypes.EVENT_VM_STOP, eventDescription = "stopping Vm", async = true)
public UserVm stopVirtualMachine(final long vmId, final boolean forced) throws ConcurrentOperationException {
// Input validation
final Account caller = CallContext.current().getCallingAccount();
final Long userId = CallContext.current().getCallingUserId();
// if account is removed, return error
if (caller != null && caller.getRemoved() != null) {
throw new PermissionDeniedException("The account " + caller.getUuid() + " is removed");
}
final UserVmVO vm = _vmDao.findById(vmId);
if (vm == null) {
throw new InvalidParameterValueException("unable to find a virtual machine with id " + vmId);
}
_userDao.findById(userId);
final boolean status;
try {
final VirtualMachineEntity vmEntity = _orchSrvc.getVirtualMachine(vm.getUuid());
if (forced || vm.getState() == State.Paused) {
status = vmEntity.stopForced(Long.toString(userId));
} else {
status = vmEntity.stop(Long.toString(userId));
}
if (status) {
return _vmDao.findById(vmId);
} else {
return null;
}
} catch (final ResourceUnavailableException e) {
throw new CloudRuntimeException("Unable to contact the agent to stop the virtual machine " + vm, e);
} catch (final CloudException e) {
throw new CloudRuntimeException("Unable to contact the agent to stop the virtual machine " + vm, e);
}
}
use of com.cloud.legacymodel.exceptions.CloudException in project cosmic by MissionCriticalCloud.
the class UserVmManagerImpl method destroyVm.
@Override
public UserVm destroyVm(final long vmId) throws ResourceUnavailableException, ConcurrentOperationException {
// Account caller = CallContext.current().getCallingAccount();
// Long userId = CallContext.current().getCallingUserId();
final Long userId = 2L;
// Verify input parameters
final UserVmVO vm = _vmDao.findById(vmId);
if (vm == null || vm.getRemoved() != null) {
final InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find a virtual machine with specified vmId");
throw ex;
}
if (vm.getState() == State.Destroyed || vm.getState() == State.Expunging) {
s_logger.trace("Vm id=" + vmId + " is already destroyed");
return vm;
}
final boolean status;
final State vmState = vm.getState();
try {
final VirtualMachineEntity vmEntity = _orchSrvc.getVirtualMachine(vm.getUuid());
status = vmEntity.destroy(Long.toString(userId));
} catch (final CloudException e) {
final CloudRuntimeException ex = new CloudRuntimeException("Unable to destroy with specified vmId", e);
ex.addProxyObject(vm.getUuid(), "vmId");
throw ex;
}
if (status) {
// Mark the account's volumes as destroyed
final List<VolumeVO> volumes = _volsDao.findByInstance(vmId);
if (vmState != State.Error) {
// Get serviceOffering for Virtual Machine
final ServiceOfferingVO offering = _serviceOfferingDao.findByIdIncludingRemoved(vm.getId(), vm.getServiceOfferingId());
// Update Resource Count for the given account
resourceCountDecrement(vm.getAccountId(), vm.isDisplayVm(), new Long(offering.getCpu()), new Long(offering.getRamSize()));
}
return _vmDao.findById(vmId);
} else {
final CloudRuntimeException ex = new CloudRuntimeException("Failed to destroy vm with specified vmId");
ex.addProxyObject(vm.getUuid(), "vmId");
throw ex;
}
}
use of com.cloud.legacymodel.exceptions.CloudException in project cosmic by MissionCriticalCloud.
the class UserVmManagerImpl method stopVirtualMachine.
@Override
public boolean stopVirtualMachine(final long userId, final long vmId) {
boolean status;
if (s_logger.isDebugEnabled()) {
s_logger.debug("Stopping vm=" + vmId);
}
final UserVmVO vm = _vmDao.findById(vmId);
if (vm == null || vm.getRemoved() != null) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("VM is either removed or deleted.");
}
return true;
}
_userDao.findById(userId);
try {
final VirtualMachineEntity vmEntity = _orchSrvc.getVirtualMachine(vm.getUuid());
status = vmEntity.stop(Long.toString(userId));
} catch (final ResourceUnavailableException e) {
s_logger.debug("Unable to stop due to ", e);
status = false;
} catch (final CloudException e) {
throw new CloudRuntimeException("Unable to contact the agent to stop the virtual machine " + vm, e);
}
return status;
}
Aggregations