mirror of
https://github.com/apache/cloudstack.git
synced 2025-12-17 02:53:18 +01:00
CLOUDSTACK-1645 : Resources limit is not validated with update compute offering [ Instances are updated to higher CPU/Memory resources though there are no resources available @ account/domain]
This commit is contained in:
parent
98291d043d
commit
105618896b
@ -356,8 +356,9 @@ public interface UserVmService {
|
|||||||
* @param cmd
|
* @param cmd
|
||||||
* - the command specifying vmId and new serviceOfferingId
|
* - the command specifying vmId and new serviceOfferingId
|
||||||
* @return the vm
|
* @return the vm
|
||||||
|
* @throws ResourceAllocationException
|
||||||
*/
|
*/
|
||||||
UserVm upgradeVirtualMachine(UpgradeVMCmd cmd);
|
UserVm upgradeVirtualMachine(UpgradeVMCmd cmd) throws ResourceAllocationException;
|
||||||
|
|
||||||
UserVm stopVirtualMachine(long vmId, boolean forced) throws ConcurrentOperationException;
|
UserVm stopVirtualMachine(long vmId, boolean forced) throws ConcurrentOperationException;
|
||||||
|
|
||||||
|
|||||||
@ -22,12 +22,12 @@ import org.apache.cloudstack.api.ApiErrorCode;
|
|||||||
import org.apache.cloudstack.api.BaseCmd;
|
import org.apache.cloudstack.api.BaseCmd;
|
||||||
import org.apache.cloudstack.api.Parameter;
|
import org.apache.cloudstack.api.Parameter;
|
||||||
import org.apache.cloudstack.api.ServerApiException;
|
import org.apache.cloudstack.api.ServerApiException;
|
||||||
import org.apache.cloudstack.api.response.DiskOfferingResponse;
|
|
||||||
import org.apache.cloudstack.api.response.ServiceOfferingResponse;
|
import org.apache.cloudstack.api.response.ServiceOfferingResponse;
|
||||||
import org.apache.cloudstack.api.response.UserVmResponse;
|
import org.apache.cloudstack.api.response.UserVmResponse;
|
||||||
import org.apache.log4j.Logger;
|
import org.apache.log4j.Logger;
|
||||||
|
|
||||||
import com.cloud.exception.InvalidParameterValueException;
|
import com.cloud.exception.InvalidParameterValueException;
|
||||||
|
import com.cloud.exception.ResourceAllocationException;
|
||||||
import com.cloud.offering.ServiceOffering;
|
import com.cloud.offering.ServiceOffering;
|
||||||
import com.cloud.user.Account;
|
import com.cloud.user.Account;
|
||||||
import com.cloud.user.UserContext;
|
import com.cloud.user.UserContext;
|
||||||
@ -88,7 +88,7 @@ public class UpgradeVMCmd extends BaseCmd {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void execute(){
|
public void execute() throws ResourceAllocationException{
|
||||||
UserContext.current().setEventDetails("Vm Id: "+getId());
|
UserContext.current().setEventDetails("Vm Id: "+getId());
|
||||||
|
|
||||||
ServiceOffering serviceOffering = _configService.getServiceOffering(serviceOfferingId);
|
ServiceOffering serviceOffering = _configService.getServiceOffering(serviceOfferingId);
|
||||||
|
|||||||
@ -750,7 +750,7 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Use
|
|||||||
/*
|
/*
|
||||||
* TODO: cleanup eventually - Refactored API call
|
* TODO: cleanup eventually - Refactored API call
|
||||||
*/
|
*/
|
||||||
public UserVm upgradeVirtualMachine(UpgradeVMCmd cmd) {
|
public UserVm upgradeVirtualMachine(UpgradeVMCmd cmd) throws ResourceAllocationException {
|
||||||
Long vmId = cmd.getId();
|
Long vmId = cmd.getId();
|
||||||
Long svcOffId = cmd.getServiceOfferingId();
|
Long svcOffId = cmd.getServiceOfferingId();
|
||||||
Account caller = UserContext.current().getCaller();
|
Account caller = UserContext.current().getCaller();
|
||||||
@ -764,6 +764,24 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Use
|
|||||||
|
|
||||||
_accountMgr.checkAccess(caller, null, true, vmInstance);
|
_accountMgr.checkAccess(caller, null, true, vmInstance);
|
||||||
|
|
||||||
|
// Check resource limits for CPU and Memory.
|
||||||
|
ServiceOfferingVO newServiceOffering = _offeringDao.findById(svcOffId);
|
||||||
|
ServiceOfferingVO currentServiceOffering = _offeringDao.findByIdIncludingRemoved(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
|
// Check that the specified service offering ID is valid
|
||||||
_itMgr.checkIfCanUpgrade(vmInstance, svcOffId);
|
_itMgr.checkIfCanUpgrade(vmInstance, svcOffId);
|
||||||
|
|
||||||
@ -782,6 +800,18 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Use
|
|||||||
|
|
||||||
_itMgr.upgradeVmDb(vmId, svcOffId);
|
_itMgr.upgradeVmDb(vmId, svcOffId);
|
||||||
|
|
||||||
|
// 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());
|
return _vmDao.findById(vmInstance.getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user