Search in sources :

Example 6 with UserVm

use of com.cloud.uservm.UserVm in project CloudStack-archive by CloudStack-extras.

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");
    }
    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());
        }
        UserContext.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");
        }
        UserContext.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) {
            UserVmResponse response = _responseGenerator.createUserVmResponse("virtualmachine", (UserVm) migratedVm).get(0);
            response.setResponseName(getCommandName());
            this.setResponseObject(response);
        } else {
            throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to migrate vm");
        }
    } catch (ResourceUnavailableException ex) {
        s_logger.warn("Exception: ", ex);
        throw new ServerApiException(BaseCmd.RESOURCE_UNAVAILABLE_ERROR, ex.getMessage());
    } catch (ConcurrentOperationException e) {
        s_logger.warn("Exception: ", e);
        throw new ServerApiException(BaseCmd.INTERNAL_ERROR, e.getMessage());
    } catch (ManagementServerException e) {
        s_logger.warn("Exception: ", e);
        throw new ServerApiException(BaseCmd.INTERNAL_ERROR, e.getMessage());
    } catch (VirtualMachineMigrationException e) {
        s_logger.warn("Exception: ", e);
        throw new ServerApiException(BaseCmd.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.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 7 with UserVm

use of com.cloud.uservm.UserVm in project CloudStack-archive by CloudStack-extras.

the class DeployVMCmd method create.

@Override
public void create() throws ResourceAllocationException {
    try {
        //Verify that all objects exist before passing them to the service
        Account owner = _accountService.getActiveAccountById(getEntityOwnerId());
        DataCenter zone = _configService.getZone(zoneId);
        if (zone == null) {
            throw new InvalidParameterValueException("Unable to find zone by id=" + zoneId);
        }
        ServiceOffering serviceOffering = _configService.getServiceOffering(serviceOfferingId);
        if (serviceOffering == null) {
            throw new InvalidParameterValueException("Unable to find service offering: " + serviceOfferingId);
        }
        VirtualMachineTemplate template = _templateService.getTemplate(templateId);
        // Make sure a valid template ID was specified
        if (template == null) {
            throw new InvalidParameterValueException("Unable to use template " + templateId);
        }
        if (diskOfferingId != null) {
            DiskOffering diskOffering = _configService.getDiskOffering(diskOfferingId);
            if (diskOffering == null) {
                throw new InvalidParameterValueException("Unable to find disk offering " + diskOfferingId);
            }
        }
        UserVm vm = null;
        if (getHypervisor() == HypervisorType.BareMetal) {
            vm = _bareMetalVmService.createVirtualMachine(this);
        } else {
            if (zone.getNetworkType() == NetworkType.Basic) {
                if (getNetworkIds() != null) {
                    throw new InvalidParameterValueException("Can't specify network Ids in Basic zone");
                } else {
                    vm = _userVmService.createBasicSecurityGroupVirtualMachine(zone, serviceOffering, template, getSecurityGroupIdList(), owner, name, displayName, diskOfferingId, size, group, getHypervisor(), userData, sshKeyPairName, getIpToNetworkMap(), ipAddress, keyboard);
                }
            } else {
                if (zone.isSecurityGroupEnabled()) {
                    vm = _userVmService.createAdvancedSecurityGroupVirtualMachine(zone, serviceOffering, template, getNetworkIds(), getSecurityGroupIdList(), owner, name, displayName, diskOfferingId, size, group, getHypervisor(), userData, sshKeyPairName, getIpToNetworkMap(), ipAddress, keyboard);
                } else {
                    if (getSecurityGroupIdList() != null && !getSecurityGroupIdList().isEmpty()) {
                        throw new InvalidParameterValueException("Can't create vm with security groups; security group feature is not enabled per zone");
                    }
                    vm = _userVmService.createAdvancedVirtualMachine(zone, serviceOffering, template, getNetworkIds(), owner, name, displayName, diskOfferingId, size, group, getHypervisor(), userData, sshKeyPairName, getIpToNetworkMap(), ipAddress, keyboard);
                }
            }
        }
        if (vm != null) {
            setEntityId(vm.getId());
        } else {
            throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to deploy vm");
        }
    } catch (InsufficientCapacityException ex) {
        s_logger.info(ex);
        s_logger.trace(ex);
        throw new ServerApiException(BaseCmd.INSUFFICIENT_CAPACITY_ERROR, ex.getMessage());
    } catch (ResourceUnavailableException ex) {
        s_logger.warn("Exception: ", ex);
        throw new ServerApiException(BaseCmd.RESOURCE_UNAVAILABLE_ERROR, ex.getMessage());
    } catch (ConcurrentOperationException ex) {
        s_logger.warn("Exception: ", ex);
        throw new ServerApiException(BaseCmd.INTERNAL_ERROR, ex.getMessage());
    }
}
Also used : Account(com.cloud.user.Account) UserVm(com.cloud.uservm.UserVm) DataCenter(com.cloud.dc.DataCenter) DiskOffering(com.cloud.offering.DiskOffering) VirtualMachineTemplate(com.cloud.template.VirtualMachineTemplate) ServerApiException(com.cloud.api.ServerApiException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ServiceOffering(com.cloud.offering.ServiceOffering) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException)

Example 8 with UserVm

use of com.cloud.uservm.UserVm in project CloudStack-archive by CloudStack-extras.

the class DeployVMCmd method execute.

@Override
public void execute() {
    UserVm result;
    if (getStartVm()) {
        try {
            UserContext.current().setEventDetails("Vm Id: " + getEntityId());
            if (getHypervisor() == HypervisorType.BareMetal) {
                result = _bareMetalVmService.startVirtualMachine(this);
            } else {
                result = _userVmService.startVirtualMachine(this);
            }
        } catch (ResourceUnavailableException ex) {
            s_logger.warn("Exception: ", ex);
            throw new ServerApiException(BaseCmd.RESOURCE_UNAVAILABLE_ERROR, ex.getMessage());
        } catch (ConcurrentOperationException ex) {
            s_logger.warn("Exception: ", ex);
            throw new ServerApiException(BaseCmd.INTERNAL_ERROR, ex.getMessage());
        } catch (InsufficientCapacityException ex) {
            s_logger.info(ex);
            s_logger.trace(ex);
            throw new ServerApiException(BaseCmd.INSUFFICIENT_CAPACITY_ERROR, ex.getMessage());
        }
    } else {
        result = _userVmService.getUserVm(getEntityId());
    }
    if (result != null) {
        UserVmResponse response = _responseGenerator.createUserVmResponse("virtualmachine", result).get(0);
        response.setResponseName(getCommandName());
        this.setResponseObject(response);
    } else {
        throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to deploy vm");
    }
}
Also used : UserVm(com.cloud.uservm.UserVm) ServerApiException(com.cloud.api.ServerApiException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) ConcurrentOperationException(com.cloud.exception.ConcurrentOperationException) UserVmResponse(com.cloud.api.response.UserVmResponse)

Example 9 with UserVm

use of com.cloud.uservm.UserVm in project CloudStack-archive by CloudStack-extras.

the class DestroyVMCmd method execute.

@Override
public void execute() throws ResourceUnavailableException, ConcurrentOperationException {
    UserContext.current().setEventDetails("Vm Id: " + getId());
    UserVm result;
    if (_userVmService.getHypervisorTypeOfUserVM(getId()) == HypervisorType.BareMetal) {
        result = _bareMetalVmService.destroyVm(this);
    } else {
        result = _userVmService.destroyVm(this);
    }
    if (result != null) {
        UserVmResponse response = _responseGenerator.createUserVmResponse("virtualmachine", result).get(0);
        response.setResponseName("virtualmachine");
        this.setResponseObject(response);
    } else {
        throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to destroy vm");
    }
}
Also used : UserVm(com.cloud.uservm.UserVm) ServerApiException(com.cloud.api.ServerApiException) UserVmResponse(com.cloud.api.response.UserVmResponse)

Example 10 with UserVm

use of com.cloud.uservm.UserVm in project CloudStack-archive by CloudStack-extras.

the class DetachIsoCmd method execute.

@Override
public void execute() {
    boolean result = _templateService.detachIso(virtualMachineId);
    if (result) {
        UserVm userVm = _entityMgr.findById(UserVm.class, virtualMachineId);
        UserVmResponse response = _responseGenerator.createUserVmResponse("virtualmachine", userVm).get(0);
        response.setResponseName(DeployVMCmd.getResultObjectName());
        this.setResponseObject(response);
    } else {
        throw new ServerApiException(BaseCmd.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)98 ServerApiException (org.apache.cloudstack.api.ServerApiException)45 UserVmResponse (org.apache.cloudstack.api.response.UserVmResponse)45 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)20 ArrayList (java.util.ArrayList)19 ServerApiException (com.cloud.api.ServerApiException)15 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)15 UserVmResponse (com.cloud.api.response.UserVmResponse)14 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)14 Network (com.cloud.network.Network)13 Account (com.cloud.user.Account)10 InsufficientCapacityException (com.cloud.exception.InsufficientCapacityException)9 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)9 VMDetails (org.apache.cloudstack.api.ApiConstants.VMDetails)9 Test (org.junit.Test)9 DataCenter (com.cloud.dc.DataCenter)6 ManagementServerException (com.cloud.exception.ManagementServerException)6 VirtualMachineMigrationException (com.cloud.exception.VirtualMachineMigrationException)6 DB (com.cloud.utils.db.DB)6 TransactionStatus (com.cloud.utils.db.TransactionStatus)6