Pearl1594 commented on a change in pull request #5428:
URL: https://github.com/apache/cloudstack/pull/5428#discussion_r707466582



##########
File path: server/src/main/java/com/cloud/vm/UserVmManagerImpl.java
##########
@@ -2575,6 +2575,53 @@ protected void runInContext() {
         }
     }
 
+    private void verifyVmLimits(UserVmVO vmInstance, Map<String,String> 
details) {
+        Long newCpu = Long.valueOf(details.get(VmDetailConstants.CPU_NUMBER) 
!= null ? details.get(VmDetailConstants.CPU_NUMBER) : "0");
+        Long newMemory = Long.valueOf(details.get(VmDetailConstants.MEMORY) != 
null ? details.get(VmDetailConstants.MEMORY) : "0");
+        long currentCpu = 0L;
+        long currentMemory = 0L;
+        List<UserVmDetailVO> vmDetails = 
userVmDetailsDao.listDetails(vmInstance.getId());
+        for (UserVmDetailVO detailVO : vmDetails) {
+            if (VmDetailConstants.CPU_NUMBER.equals(detailVO.getName())) {
+                currentCpu = Long.parseLong(detailVO.getValue());
+            }
+            if (VmDetailConstants.MEMORY.equals(detailVO.getName())) {
+                currentMemory = Long.parseLong(detailVO.getValue());
+            }
+        }
+        Account owner = _accountDao.findById(vmInstance.getAccountId());
+        if (owner == null) {
+            throw new InvalidParameterValueException("The owner of " + 
vmInstance + " does not exist: " + vmInstance.getAccountId());
+        }
+
+        if (VirtualMachineManager.ResoureCountRunningVMsonly.value()) {
+            return;
+        }
+
+        try {
+            if (newCpu > currentCpu) {
+                _resourceLimitMgr.checkResourceLimit(owner, ResourceType.cpu, 
newCpu - currentCpu);
+            }
+            if (newMemory > currentMemory) {
+                _resourceLimitMgr.checkResourceLimit(owner, 
ResourceType.memory, newMemory - currentMemory);
+            }
+        } catch (ResourceAllocationException e) {
+            throw new CloudRuntimeException("Failed to update VM settings", e);
+        }
+
+        if (newCpu > currentCpu) {
+            _resourceLimitMgr.incrementResourceCount(owner.getAccountId(), 
ResourceType.cpu, newCpu - currentCpu);
+        } else {

Review comment:
       Ideally, it shouldn't be zero - and the UI handles the check, but via 
cmk it is possible to pass 0 as the cpu number or any other parameter, and it 
fails eventually during deployment for example on VMWare:
   ```
   2021-09-13 15:42:13,633 INFO  [c.c.h.v.u.VmwareHelper] 
(DirectAgent-3:ctx-9536c4ac 10.0.35.165, job-288/job-291, cmd: StartCommand) 
(logid:9ffa5f01) [ignored]failed to get message for exception: A specified 
parameter was not correct: configSpec.numCPUs
   2021-09-13 15:42:13,633 WARN  [c.c.h.v.r.VmwareResource] 
(DirectAgent-3:ctx-9536c4ac 10.0.35.165, job-288/job-291, cmd: StartCommand) 
(logid:9ffa5f01) StartCommand failed due to Exception: 
java.lang.RuntimeException
   Message: A specified parameter was not correct: configSpec.numCPUs
   
   java.lang.RuntimeException: A specified parameter was not correct: 
configSpec.numCPUs
        at 
com.cloud.hypervisor.vmware.util.VmwareClient.waitForTask(VmwareClient.java:426)
        at 
com.cloud.hypervisor.vmware.mo.VirtualMachineMO.configureVm(VirtualMachineMO.java:1142)
        at 
com.cloud.hypervisor.vmware.resource.VmwareResource.execute(VmwareResource.java:2390)
        at 
com.cloud.hypervisor.vmware.resource.VmwareResource.executeRequest(VmwareResource.java:555)
   ```
   




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to