sureshanaparti commented on code in PR #10560:
URL: https://github.com/apache/cloudstack/pull/10560#discussion_r1998330018


##########
server/src/main/java/com/cloud/vm/UserVmManagerImpl.java:
##########
@@ -6251,9 +6267,115 @@ public UserVm createVirtualMachine(DeployVMCmd cmd) 
throws InsufficientCapacityE
                 }
             }
         }
+
+        applyLeaseOnCreateInstance(vm, leaseDuration, leaseExpiryAction, 
svcOffering);
         return vm;
     }
 
+    protected void validateLeaseProperties(Long leaseDuration, String 
leaseExpiryAction) {
+        if (!VMLeaseManagerImpl.InstanceLeaseEnabled.value()
+                || (leaseDuration == null && 
StringUtils.isEmpty(leaseExpiryAction))) { // if both are null
+            return;
+        }
+
+        boolean bothValuesSet = true;
+        if (leaseDuration != null) {
+            if (leaseDuration == -1) {   // special condition used to disable 
lease for instance
+                return;
+            }
+            // both params have value
+            if (leaseDuration < -1) {
+                throw new InvalidParameterValueException("Invalid value given 
for leaseduration, lesser than -1 is not supported ");
+            }
+
+            if (StringUtils.isEmpty(leaseExpiryAction)) {
+                bothValuesSet = false;
+            }
+        } else {
+            bothValuesSet = false;
+        }
+
+        if (!bothValuesSet) {
+            throw new InvalidParameterValueException("Provide values for both: 
leaseduration and leaseexpiryaction");
+        }
+        try {
+            VMLeaseManager.ExpiryAction.valueOf(leaseExpiryAction);
+        } catch (IllegalArgumentException e) {
+            throw new InvalidParameterValueException("Invalid value provided 
for leaseexpiryaction, valid values are: " +
+                    
EnumUtils.listValues(VMLeaseManager.ExpiryAction.values()));
+        }
+    }
+
+    /**
+     * if feature is enabled
+     * use leaseDuration and leaseExpiryAction passed in the cmd
+     * if passed leaseDuration is -1, get leaseDuration from service_offering
+     * @param vm
+     * @param leaseDuration
+     * @param leaseExpiryAction
+     * @param serviceOfferingJoinVO
+     */
+    private void applyLeaseOnCreateInstance(UserVm vm, Long leaseDuration, 
String leaseExpiryAction, ServiceOfferingJoinVO serviceOfferingJoinVO) {
+        if (!VMLeaseManagerImpl.InstanceLeaseEnabled.value()) {
+            return;
+        }
+
+        if (leaseDuration == null) {
+            leaseDuration = serviceOfferingJoinVO.getLeaseDuration();
+        }
+        // if leaseDuration is null or -1, instance will never expire, nothing 
to be done
+        if  (leaseDuration == null || leaseDuration == -1) {
+            return;
+        }
+
+        leaseExpiryAction = Strings.isNotEmpty(leaseExpiryAction) ? 
leaseExpiryAction : serviceOfferingJoinVO.getLeaseExpiryAction();
+        if (StringUtils.isEmpty(leaseExpiryAction)) {
+            leaseExpiryAction = 
VMLeaseManager.InstanceLeaseExpiryAction.valueIn(vm.getAccountId());
+        }
+        addLeaseDetailsForInstance(vm, leaseDuration, leaseExpiryAction);
+    }
+
+
+    protected void applyLeaseOnUpdateInstance(UserVm instance, Long 
leaseDuration, String leaseExpiryAction) {

Review Comment:
   is renew lease allowed on update instance?



-- 
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