Search in sources :

Example 61 with UserVm

use of com.cloud.uservm.UserVm in project cosmic by MissionCriticalCloud.

the class MigrateVMCmd method execute.

@Override
public void execute() {
    if (getHostId() == null && getStoragePoolId() == null) {
        throw new InvalidParameterValueException("Either hostId or storageId must be specified");
    }
    if (getHostId() != null && getStoragePoolId() != null) {
        throw new InvalidParameterValueException("Only one of hostId and storageId can be specified");
    }
    final UserVm userVm = _userVmService.getUserVm(getVirtualMachineId());
    if (userVm == null) {
        throw new InvalidParameterValueException("Unable to find the VM by id=" + getVirtualMachineId());
    }
    Host destinationHost = null;
    if (getHostId() != null) {
        destinationHost = _resourceService.getHost(getHostId());
        if (destinationHost == null) {
            throw new InvalidParameterValueException("Unable to find the host to migrate the VM, host id=" + getHostId());
        }
        if (destinationHost.getType() != Host.Type.Routing) {
            throw new InvalidParameterValueException("The specified host(" + destinationHost.getName() + ") is not suitable to migrate the VM, please specify another one");
        }
        CallContext.current().setEventDetails("VM Id: " + getVirtualMachineId() + " to host Id: " + getHostId());
    }
    StoragePool destStoragePool = null;
    if (getStoragePoolId() != null) {
        destStoragePool = _storageService.getStoragePool(getStoragePoolId());
        if (destStoragePool == null) {
            throw new InvalidParameterValueException("Unable to find the storage pool to migrate the VM");
        }
        CallContext.current().setEventDetails("VM Id: " + getVirtualMachineId() + " to storage pool Id: " + getStoragePoolId());
    }
    try {
        VirtualMachine migratedVm = null;
        if (getHostId() != null) {
            migratedVm = _userVmService.migrateVirtualMachine(getVirtualMachineId(), destinationHost);
        } else if (getStoragePoolId() != null) {
            migratedVm = _userVmService.vmStorageMigration(getVirtualMachineId(), destStoragePool);
        }
        if (migratedVm != null) {
            final UserVmResponse response = _responseGenerator.createUserVmResponse(ResponseView.Full, "virtualmachine", (UserVm) migratedVm).get(0);
            response.setResponseName(getCommandName());
            setResponseObject(response);
        } else {
            throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to migrate vm");
        }
    } catch (final ResourceUnavailableException ex) {
        s_logger.warn("Exception: ", ex);
        throw new ServerApiException(ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR, ex.getMessage());
    } catch (final ConcurrentOperationException e) {
        s_logger.warn("Exception: ", e);
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, e.getMessage());
    } catch (final ManagementServerException e) {
        s_logger.warn("Exception: ", e);
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, e.getMessage());
    } catch (final VirtualMachineMigrationException e) {
        s_logger.warn("Exception: ", e);
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, e.getMessage());
    }
}
Also used : UserVm(com.cloud.uservm.UserVm) StoragePool(com.cloud.storage.StoragePool) ServerApiException(com.cloud.api.ServerApiException) ManagementServerException(com.cloud.exception.ManagementServerException) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) Host(com.cloud.host.Host) VirtualMachineMigrationException(com.cloud.exception.VirtualMachineMigrationException) UserVmResponse(com.cloud.api.response.UserVmResponse) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) VirtualMachine(com.cloud.vm.VirtualMachine)

Example 62 with UserVm

use of com.cloud.uservm.UserVm in project cosmic by MissionCriticalCloud.

the class MigrateVirtualMachineWithVolumeCmd method execute.

@Override
public void execute() {
    final UserVm userVm = _userVmService.getUserVm(getVirtualMachineId());
    if (userVm == null) {
        throw new InvalidParameterValueException("Unable to find the VM by id=" + getVirtualMachineId());
    }
    final Host destinationHost = _resourceService.getHost(getHostId());
    if (destinationHost == null) {
        throw new InvalidParameterValueException("Unable to find the host to migrate the VM, host id =" + getHostId());
    }
    try {
        final VirtualMachine migratedVm = _userVmService.migrateVirtualMachineWithVolume(getVirtualMachineId(), destinationHost, getVolumeToPool());
        if (migratedVm != null) {
            final UserVmResponse response = _responseGenerator.createUserVmResponse(ResponseView.Full, "virtualmachine", (UserVm) migratedVm).get(0);
            response.setResponseName(getCommandName());
            setResponseObject(response);
        } else {
            throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to migrate vm");
        }
    } catch (final ResourceUnavailableException ex) {
        s_logger.warn("Exception: ", ex);
        throw new ServerApiException(ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR, ex.getMessage());
    } catch (final ConcurrentOperationException | ManagementServerException | VirtualMachineMigrationException e) {
        s_logger.warn("Exception: ", e);
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, e.getMessage());
    }
}
Also used : UserVm(com.cloud.uservm.UserVm) ServerApiException(com.cloud.api.ServerApiException) ManagementServerException(com.cloud.exception.ManagementServerException) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) Host(com.cloud.host.Host) VirtualMachineMigrationException(com.cloud.exception.VirtualMachineMigrationException) UserVmResponse(com.cloud.api.response.UserVmResponse) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) VirtualMachine(com.cloud.vm.VirtualMachine)

Example 63 with UserVm

use of com.cloud.uservm.UserVm in project cosmic by MissionCriticalCloud.

the class RemoveNicFromVMCmdByAdmin method execute.

@Override
public void execute() {
    CallContext.current().setEventDetails("Vm Id: " + getVmId() + " Nic Id: " + getNicId());
    final UserVm result = _userVmService.removeNicFromVirtualMachine(this);
    final ArrayList<VMDetails> dc = new ArrayList<>();
    dc.add(VMDetails.valueOf("nics"));
    final EnumSet<VMDetails> details = EnumSet.copyOf(dc);
    if (result != null) {
        final UserVmResponse response = _responseGenerator.createUserVmResponse(ResponseView.Full, "virtualmachine", details, result).get(0);
        response.setResponseName(getCommandName());
        setResponseObject(response);
    } else {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to remove NIC from vm, see error log for details");
    }
}
Also used : UserVm(com.cloud.uservm.UserVm) VMDetails(com.cloud.api.ApiConstants.VMDetails) ServerApiException(com.cloud.api.ServerApiException) ArrayList(java.util.ArrayList) UserVmResponse(com.cloud.api.response.UserVmResponse)

Example 64 with UserVm

use of com.cloud.uservm.UserVm in project cosmic by MissionCriticalCloud.

the class AttachIsoCmd method execute.

@Override
public void execute() {
    CallContext.current().setEventDetails("Vm Id: " + getVirtualMachineId() + " ISO ID: " + getId());
    final boolean result = _templateService.attachIso(id, virtualMachineId);
    if (result) {
        final UserVm userVm = _responseGenerator.findUserVmById(virtualMachineId);
        if (userVm != null) {
            final UserVmResponse response = _responseGenerator.createUserVmResponse(ResponseView.Restricted, "virtualmachine", userVm).get(0);
            response.setResponseName(DeployVMCmd.getResultObjectName());
            setResponseObject(response);
        } else {
            throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to attach ISO");
        }
    } else {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to attach ISO");
    }
}
Also used : UserVm(com.cloud.uservm.UserVm) ServerApiException(com.cloud.api.ServerApiException) UserVmResponse(com.cloud.api.response.UserVmResponse)

Example 65 with UserVm

use of com.cloud.uservm.UserVm in project cosmic by MissionCriticalCloud.

the class DetachIsoCmd method execute.

@Override
public void execute() {
    final boolean result = _templateService.detachIso(virtualMachineId);
    if (result) {
        final UserVm userVm = _entityMgr.findById(UserVm.class, virtualMachineId);
        final UserVmResponse response = _responseGenerator.createUserVmResponse(ResponseView.Restricted, "virtualmachine", userVm).get(0);
        response.setResponseName(DeployVMCmd.getResultObjectName());
        setResponseObject(response);
    } else {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to detach ISO");
    }
}
Also used : UserVm(com.cloud.uservm.UserVm) ServerApiException(com.cloud.api.ServerApiException) UserVmResponse(com.cloud.api.response.UserVmResponse)

Aggregations

UserVm (com.cloud.uservm.UserVm)196 ServerApiException (com.cloud.api.ServerApiException)59 UserVmResponse (com.cloud.api.response.UserVmResponse)59 ArrayList (java.util.ArrayList)54 ServerApiException (org.apache.cloudstack.api.ServerApiException)48 UserVmResponse (org.apache.cloudstack.api.response.UserVmResponse)47 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)32 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)30 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)28 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)28 Network (com.cloud.network.Network)26 Account (com.cloud.user.Account)22 InsufficientCapacityException (com.cloud.exception.InsufficientCapacityException)21 ManagementServerException (com.cloud.exception.ManagementServerException)21 HashMap (java.util.HashMap)16 ServiceOffering (com.cloud.offering.ServiceOffering)15 DataCenter (com.cloud.dc.DataCenter)14 List (java.util.List)14 ActionEvent (com.cloud.event.ActionEvent)12 VirtualMachineMigrationException (com.cloud.exception.VirtualMachineMigrationException)12