116116import org .apache .commons .codec .binary .Base64 ;
117117import org .apache .commons .collections .CollectionUtils ;
118118import org .apache .commons .collections .MapUtils ;
119+ import org .apache .commons .lang .math .NumberUtils ;
119120import org .apache .commons .lang3 .StringUtils ;
120121import org .apache .log4j .Logger ;
121122import org .springframework .beans .factory .annotation .Autowired ;
@@ -627,15 +628,15 @@ private void resourceLimitCheck(Account owner, Boolean displayVm, Long cpu, Long
627628 }
628629
629630 protected void resourceCountIncrement (long accountId , Boolean displayVm , Long cpu , Long memory ) {
630- if (! VirtualMachineManager .ResoureCountRunningVMsonly .value ()) {
631+ if (! VirtualMachineManager .ResourceCountRunningVMsonly .value ()) {
631632 _resourceLimitMgr .incrementResourceCount (accountId , ResourceType .user_vm , displayVm );
632633 _resourceLimitMgr .incrementResourceCount (accountId , ResourceType .cpu , displayVm , cpu );
633634 _resourceLimitMgr .incrementResourceCount (accountId , ResourceType .memory , displayVm , memory );
634635 }
635636 }
636637
637638 protected void resourceCountDecrement (long accountId , Boolean displayVm , Long cpu , Long memory ) {
638- if (! VirtualMachineManager .ResoureCountRunningVMsonly .value ()) {
639+ if (! VirtualMachineManager .ResourceCountRunningVMsonly .value ()) {
639640 _resourceLimitMgr .decrementResourceCount (accountId , ResourceType .user_vm , displayVm );
640641 _resourceLimitMgr .decrementResourceCount (accountId , ResourceType .cpu , displayVm , cpu );
641642 _resourceLimitMgr .decrementResourceCount (accountId , ResourceType .memory , displayVm , memory );
@@ -1112,7 +1113,7 @@ public UserVm upgradeVirtualMachine(UpgradeVMCmd cmd) throws ResourceAllocationE
11121113 int currentMemory = currentServiceOffering .getRamSize ();
11131114
11141115 Account owner = _accountMgr .getActiveAccountById (vmInstance .getAccountId ());
1115- if (! VirtualMachineManager .ResoureCountRunningVMsonly .value ()) {
1116+ if (! VirtualMachineManager .ResourceCountRunningVMsonly .value ()) {
11161117 if (newCpu > currentCpu ) {
11171118 _resourceLimitMgr .checkResourceLimit (owner , ResourceType .cpu , newCpu - currentCpu );
11181119 }
@@ -1129,7 +1130,7 @@ public UserVm upgradeVirtualMachine(UpgradeVMCmd cmd) throws ResourceAllocationE
11291130 _itMgr .upgradeVmDb (vmId , newServiceOffering , currentServiceOffering );
11301131
11311132 // Increment or decrement CPU and Memory count accordingly.
1132- if (! VirtualMachineManager .ResoureCountRunningVMsonly .value ()) {
1133+ if (! VirtualMachineManager .ResourceCountRunningVMsonly .value ()) {
11331134 if (newCpu > currentCpu ) {
11341135 _resourceLimitMgr .incrementResourceCount (owner .getAccountId (), ResourceType .cpu , new Long (newCpu - currentCpu ));
11351136 } else if (currentCpu > newCpu ) {
@@ -1233,7 +1234,7 @@ private UserVm upgradeStoppedVirtualMachine(Long vmId, Long svcOffId, Map<String
12331234 int currentMemory = currentServiceOffering .getRamSize ();
12341235
12351236 Account owner = _accountMgr .getActiveAccountById (vmInstance .getAccountId ());
1236- if (! VirtualMachineManager .ResoureCountRunningVMsonly .value ()) {
1237+ if (! VirtualMachineManager .ResourceCountRunningVMsonly .value ()) {
12371238 if (newCpu > currentCpu ) {
12381239 _resourceLimitMgr .checkResourceLimit (owner , ResourceType .cpu , newCpu - currentCpu );
12391240 }
@@ -1269,7 +1270,7 @@ private UserVm upgradeStoppedVirtualMachine(Long vmId, Long svcOffId, Map<String
12691270 _itMgr .upgradeVmDb (vmId , newServiceOffering , currentServiceOffering );
12701271
12711272 // Increment or decrement CPU and Memory count accordingly.
1272- if (! VirtualMachineManager .ResoureCountRunningVMsonly .value ()) {
1273+ if (! VirtualMachineManager .ResourceCountRunningVMsonly .value ()) {
12731274 if (newCpu > currentCpu ) {
12741275 _resourceLimitMgr .incrementResourceCount (owner .getAccountId (), ResourceType .cpu , new Long (newCpu - currentCpu ));
12751276 } else if (currentCpu > newCpu ) {
@@ -2178,7 +2179,7 @@ public UserVm recoverVirtualMachine(RecoverVMCmd cmd) throws ResourceAllocationE
21782179
21792180 // First check that the maximum number of UserVMs, CPU and Memory limit for the given
21802181 // accountId will not be exceeded
2181- if (! VirtualMachineManager .ResoureCountRunningVMsonly .value ()) {
2182+ if (! VirtualMachineManager .ResourceCountRunningVMsonly .value ()) {
21822183 resourceLimitCheck (account , vm .isDisplayVm (), new Long (serviceOffering .getCpu ()), new Long (serviceOffering .getRamSize ()));
21832184 }
21842185
@@ -2584,6 +2585,53 @@ protected void runInContext() {
25842585 }
25852586 }
25862587
2588+ private void verifyVmLimits (UserVmVO vmInstance , Map <String , String > details ) {
2589+ Account owner = _accountDao .findById (vmInstance .getAccountId ());
2590+ if (owner == null ) {
2591+ throw new InvalidParameterValueException ("The owner of " + vmInstance + " does not exist: " + vmInstance .getAccountId ());
2592+ }
2593+
2594+ long newCpu = NumberUtils .toLong (details .get (VmDetailConstants .CPU_NUMBER ));
2595+ long newMemory = NumberUtils .toLong (details .get (VmDetailConstants .MEMORY ));
2596+ ServiceOfferingVO currentServiceOffering = _serviceOfferingDao .findByIdIncludingRemoved (vmInstance .getId (), vmInstance .getServiceOfferingId ());
2597+ ServiceOfferingVO svcOffering = _serviceOfferingDao .findById (vmInstance .getServiceOfferingId ());
2598+ boolean isDynamic = currentServiceOffering .isDynamic ();
2599+ if (isDynamic ) {
2600+ Map <String , String > customParameters = new HashMap <>();
2601+ customParameters .put (VmDetailConstants .CPU_NUMBER , String .valueOf (newCpu ));
2602+ customParameters .put (VmDetailConstants .MEMORY , String .valueOf (newMemory ));
2603+ validateCustomParameters (svcOffering , customParameters );
2604+ }
2605+ if (VirtualMachineManager .ResourceCountRunningVMsonly .value ()) {
2606+ return ;
2607+ }
2608+ long currentCpu = currentServiceOffering .getCpu ();
2609+ long currentMemory = currentServiceOffering .getRamSize ();
2610+
2611+ try {
2612+ if (newCpu > currentCpu ) {
2613+ _resourceLimitMgr .checkResourceLimit (owner , ResourceType .cpu , newCpu - currentCpu );
2614+ }
2615+ if (newMemory > currentMemory ) {
2616+ _resourceLimitMgr .checkResourceLimit (owner , ResourceType .memory , newMemory - currentMemory );
2617+ }
2618+ } catch (ResourceAllocationException e ) {
2619+ s_logger .error (String .format ("Failed to updated VM due to: %s" , e .getLocalizedMessage ()));
2620+ throw new InvalidParameterValueException (e .getLocalizedMessage ());
2621+ }
2622+
2623+ if (newCpu > currentCpu ) {
2624+ _resourceLimitMgr .incrementResourceCount (owner .getAccountId (), ResourceType .cpu , newCpu - currentCpu );
2625+ } else if (newCpu > 0 && currentCpu > newCpu ){
2626+ _resourceLimitMgr .decrementResourceCount (owner .getAccountId (), ResourceType .cpu , currentCpu - newCpu );
2627+ }
2628+ if (newMemory > currentMemory ) {
2629+ _resourceLimitMgr .incrementResourceCount (owner .getAccountId (), ResourceType .memory , newMemory - currentMemory );
2630+ } else if (newMemory > 0 && currentMemory > newMemory ){
2631+ _resourceLimitMgr .decrementResourceCount (owner .getAccountId (), ResourceType .memory , currentMemory - newMemory );
2632+ }
2633+ }
2634+
25872635 @ Override
25882636 @ ActionEvent (eventType = EventTypes .EVENT_VM_UPDATE , eventDescription = "updating Vm" )
25892637 public UserVm updateVirtualMachine (UpdateVMCmd cmd ) throws ResourceUnavailableException , InsufficientCapacityException {
@@ -2657,6 +2705,8 @@ public UserVm updateVirtualMachine(UpdateVMCmd cmd) throws ResourceUnavailableEx
26572705 }
26582706 }
26592707 }
2708+
2709+ verifyVmLimits (vmInstance , details );
26602710 vmInstance .setDetails (details );
26612711 _vmDao .saveDetails (vmInstance );
26622712 }
@@ -2679,9 +2729,9 @@ protected void updateDisplayVmFlag(Boolean isDisplayVm, Long id, UserVmVO vmInst
26792729 // Resource limit changes
26802730 ServiceOffering offering = _serviceOfferingDao .findByIdIncludingRemoved (vmInstance .getId (), vmInstance .getServiceOfferingId ());
26812731 if (isDisplayVm ) {
2682- resourceCountIncrement (vmInstance .getAccountId (), true , new Long (offering .getCpu ()), new Long (offering .getRamSize ()));
2732+ resourceCountIncrement (vmInstance .getAccountId (), true , Long . valueOf (offering .getCpu ()), Long . valueOf (offering .getRamSize ()));
26832733 } else {
2684- resourceCountDecrement (vmInstance .getAccountId (), true , new Long (offering .getCpu ()), new Long (offering .getRamSize ()));
2734+ resourceCountDecrement (vmInstance .getAccountId (), true , Long . valueOf (offering .getCpu ()), Long . valueOf (offering .getRamSize ()));
26852735 }
26862736
26872737 // Usage
@@ -3747,7 +3797,7 @@ private UserVm createVirtualMachine(DataCenter zone, ServiceOffering serviceOffe
37473797 }
37483798 size += _diskOfferingDao .findById (diskOfferingId ).getDiskSize ();
37493799 }
3750- if (! VirtualMachineManager .ResoureCountRunningVMsonly .value ()) {
3800+ if (! VirtualMachineManager .ResourceCountRunningVMsonly .value ()) {
37513801 resourceLimitCheck (owner , isDisplayVm , new Long (offering .getCpu ()), new Long (offering .getRamSize ()));
37523802 }
37533803
@@ -4974,12 +5024,11 @@ public Pair<UserVmVO, Map<VirtualMachineProfile.Param, Object>> startVirtualMach
49745024 if (owner .getState () == Account .State .disabled ) {
49755025 throw new PermissionDeniedException ("The owner of " + vm + " is disabled: " + vm .getAccountId ());
49765026 }
4977- if (VirtualMachineManager .ResoureCountRunningVMsonly .value ()) {
5027+ if (VirtualMachineManager .ResourceCountRunningVMsonly .value ()) {
49785028 // check if account/domain is with in resource limits to start a new vm
49795029 ServiceOfferingVO offering = _serviceOfferingDao .findById (vm .getId (), vm .getServiceOfferingId ());
4980- resourceLimitCheck (owner , vm .isDisplayVm (), new Long (offering .getCpu ()), new Long (offering .getRamSize ()));
5030+ resourceLimitCheck (owner , vm .isDisplayVm (), Long . valueOf (offering .getCpu ()), Long . valueOf (offering .getRamSize ()));
49815031 }
4982-
49835032 // check if vm is security group enabled
49845033 if (_securityGroupMgr .isVmSecurityGroupEnabled (vmId ) && _securityGroupMgr .getSecurityGroupsForVm (vmId ).isEmpty ()
49855034 && !_securityGroupMgr .isVmMappedToDefaultSecurityGroup (vmId ) && _networkModel .canAddDefaultSecurityGroup ()) {
@@ -6672,7 +6721,7 @@ public UserVm moveVMToUser(final AssignVMCmd cmd) throws ResourceAllocationExcep
66726721 removeInstanceFromInstanceGroup (cmd .getVmId ());
66736722
66746723 // VV 2: check if account/domain is with in resource limits to create a new vm
6675- if (! VirtualMachineManager .ResoureCountRunningVMsonly .value ()) {
6724+ if (! VirtualMachineManager .ResourceCountRunningVMsonly .value ()) {
66766725 resourceLimitCheck (newAccount , vm .isDisplayVm (), new Long (offering .getCpu ()), new Long (offering .getRamSize ()));
66776726 }
66786727
@@ -6730,7 +6779,7 @@ public void doInTransactionWithoutResult(TransactionStatus status) {
67306779 }
67316780
67326781 //update resource count of new account
6733- if (! VirtualMachineManager .ResoureCountRunningVMsonly .value ()) {
6782+ if (! VirtualMachineManager .ResourceCountRunningVMsonly .value ()) {
67346783 resourceCountIncrement (newAccount .getAccountId (), vm .isDisplayVm (), new Long (offering .getCpu ()), new Long (offering .getRamSize ()));
67356784 }
67366785
0 commit comments