Pearl1594 commented on code in PR #13101:
URL: https://github.com/apache/cloudstack/pull/13101#discussion_r3191541923


##########
server/src/main/java/com/cloud/template/TemplateManagerImpl.java:
##########
@@ -1402,20 +1453,143 @@ private boolean attachISOToVM(long vmId, long isoId, 
boolean attach, boolean for
         return (a != null && a.getResult());
     }
 
-    private boolean attachISOToVM(long vmId, long userId, long isoId, boolean 
attach, boolean forced, boolean isVirtualRouter) {
+    boolean attachISOToVM(long vmId, long userId, long isoId, boolean attach, 
boolean forced, boolean isVirtualRouter) {
         UserVmVO vm = _userVmDao.findById(vmId);
         VMTemplateVO iso = _tmpltDao.findById(isoId);
 
-        boolean success = attachISOToVM(vmId, isoId, attach, forced, 
isVirtualRouter);
-        if (success && attach && !isVirtualRouter) {
+        int targetSlot = attach ? chooseAttachSlot(vmId, vm) : 
findAttachedSlot(vmId, vm, isoId);
+        boolean success = attachISOToVM(vmId, isoId, targetSlot, attach, 
forced, isVirtualRouter);
+        if (!success || isVirtualRouter) {
+            return success;
+        }
+        if (attach) {
+            persistIsoAttachment(vmId, vm, iso, targetSlot);
+        } else {
+            persistIsoDetachment(vmId, vm, isoId, targetSlot);
+        }
+        return success;
+    }
+
+    private int chooseAttachSlot(long vmId, UserVmVO vm) {
+        if (vm.getIsoId() == null) {
+            return CDROM_PRIMARY_DEVICE_SEQ;
+        }
+        VmIsoMapVO highest = highestCdromMapEntry(vmId);
+        return highest == null ? CDROM_PRIMARY_DEVICE_SEQ + 1 : 
highest.getDeviceSeq() + 1;

Review Comment:
   Is we aren't using highestCdromMapEntry() elsewhere I was wondering if we 
could just do:
   
   ```suggestion
        return _vmIsoMapDao.listByVmId(vmId).stream()
       .mapToInt(VmIsoMapVO::getDeviceSeq)
       .max()
       .orElse(CDROM_PRIMARY_DEVICE_SEQ) + 1;
   ```
   feel free to ignore.



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