Search in sources :

Example 46 with ServiceOffering

use of com.cloud.offering.ServiceOffering in project cloudstack by apache.

the class UserVmManagerImpl method upgradeStoppedVirtualMachine.

private UserVm upgradeStoppedVirtualMachine(Long vmId, Long svcOffId, Map<String, String> customParameters) throws ResourceAllocationException {
    Account caller = CallContext.current().getCallingAccount();
    // Verify input parameters
    //UserVmVO vmInstance = _vmDao.findById(vmId);
    VMInstanceVO vmInstance = _vmInstanceDao.findById(vmId);
    if (vmInstance == null) {
        throw new InvalidParameterValueException("unable to find a virtual machine with id " + vmId);
    }
    _accountMgr.checkAccess(caller, null, true, vmInstance);
    // Check resource limits for CPU and Memory.
    ServiceOfferingVO newServiceOffering = _offeringDao.findById(svcOffId);
    if (newServiceOffering.isDynamic()) {
        newServiceOffering.setDynamicFlag(true);
        validateCustomParameters(newServiceOffering, customParameters);
        newServiceOffering = _offeringDao.getcomputeOffering(newServiceOffering, customParameters);
    }
    ServiceOfferingVO currentServiceOffering = _offeringDao.findByIdIncludingRemoved(vmInstance.getId(), vmInstance.getServiceOfferingId());
    int newCpu = newServiceOffering.getCpu();
    int newMemory = newServiceOffering.getRamSize();
    int currentCpu = currentServiceOffering.getCpu();
    int currentMemory = currentServiceOffering.getRamSize();
    if (newCpu > currentCpu) {
        _resourceLimitMgr.checkResourceLimit(caller, ResourceType.cpu, newCpu - currentCpu);
    }
    if (newMemory > currentMemory) {
        _resourceLimitMgr.checkResourceLimit(caller, ResourceType.memory, newMemory - currentMemory);
    }
    // Check that the specified service offering ID is valid
    _itMgr.checkIfCanUpgrade(vmInstance, newServiceOffering);
    // Check if the new service offering can be applied to vm instance
    ServiceOffering newSvcOffering = _offeringDao.findById(svcOffId);
    Account owner = _accountMgr.getActiveAccountById(vmInstance.getAccountId());
    _accountMgr.checkAccess(owner, newSvcOffering);
    _itMgr.upgradeVmDb(vmId, svcOffId);
    if (newServiceOffering.isDynamic()) {
        //save the custom values to the database.
        saveCustomOfferingDetails(vmId, newServiceOffering);
    }
    if (currentServiceOffering.isDynamic() && !newServiceOffering.isDynamic()) {
        removeCustomOfferingDetails(vmId);
    }
    // Increment or decrement CPU and Memory count accordingly.
    if (newCpu > currentCpu) {
        _resourceLimitMgr.incrementResourceCount(caller.getAccountId(), ResourceType.cpu, new Long(newCpu - currentCpu));
    } else if (currentCpu > newCpu) {
        _resourceLimitMgr.decrementResourceCount(caller.getAccountId(), ResourceType.cpu, new Long(currentCpu - newCpu));
    }
    if (newMemory > currentMemory) {
        _resourceLimitMgr.incrementResourceCount(caller.getAccountId(), ResourceType.memory, new Long(newMemory - currentMemory));
    } else if (currentMemory > newMemory) {
        _resourceLimitMgr.decrementResourceCount(caller.getAccountId(), ResourceType.memory, new Long(currentMemory - newMemory));
    }
    return _vmDao.findById(vmInstance.getId());
}
Also used : Account(com.cloud.user.Account) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ServiceOffering(com.cloud.offering.ServiceOffering) ServiceOfferingVO(com.cloud.service.ServiceOfferingVO)

Aggregations

ServiceOffering (com.cloud.offering.ServiceOffering)46 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)18 Account (com.cloud.user.Account)14 ArrayList (java.util.ArrayList)13 DataCenter (com.cloud.dc.DataCenter)11 VirtualMachine (com.cloud.vm.VirtualMachine)9 ServerApiException (org.apache.cloudstack.api.ServerApiException)8 ServerApiException (com.cloud.api.ServerApiException)5 VirtualMachineTemplate (com.cloud.template.VirtualMachineTemplate)5 UserVm (com.cloud.uservm.UserVm)5 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)5 HashMap (java.util.HashMap)5 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)4 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)4 Host (com.cloud.host.Host)4 HostVO (com.cloud.host.HostVO)4 DiskOffering (com.cloud.offering.DiskOffering)4 ServiceOfferingVO (com.cloud.service.ServiceOfferingVO)4 VMTemplateVO (com.cloud.storage.VMTemplateVO)4 ServiceOfferingResponse (com.cloud.api.response.ServiceOfferingResponse)3