@@ -1256,23 +1256,7 @@ public boolean detachIso(long vmId, Long isoParamId, Boolean... extraParams) {
12561256 } else {
12571257 Long primaryIsoId = ((UserVm ) virtualMachine ).getIsoId ();
12581258 List <VmIsoMapVO > extras = _vmIsoMapDao .listByVmId (vmId );
1259- if (isoParamId != null ) {
1260- boolean attached = (primaryIsoId != null && primaryIsoId .equals (isoParamId ))
1261- || extras .stream ().anyMatch (r -> r .getIsoId () == isoParamId );
1262- if (!attached ) {
1263- throw new InvalidParameterValueException ("The specified ISO is not attached to this Instance." );
1264- }
1265- isoId = isoParamId ;
1266- } else {
1267- int totalAttached = (primaryIsoId != null ? 1 : 0 ) + extras .size ();
1268- if (totalAttached == 0 ) {
1269- throw new InvalidParameterValueException ("The specified instance has no ISO attached to it." );
1270- } else if (totalAttached > 1 ) {
1271- throw new InvalidParameterValueException ("Instance has more than one ISO attached; specify the 'id' parameter to choose which to detach." );
1272- } else {
1273- isoId = primaryIsoId != null ? primaryIsoId : extras .get (0 ).getIsoId ();
1274- }
1275- }
1259+ isoId = resolveIsoIdForDetach (primaryIsoId , extras , isoParamId );
12761260 }
12771261 if (isoId == null ) {
12781262 throw new InvalidParameterValueException ("The specified instance has no ISO attached to it." );
@@ -1359,9 +1343,7 @@ public boolean attachIso(long isoId, long vmId, Boolean... extraParams) {
13591343 }
13601344 if (!isVirtualRouter ) {
13611345 Long primaryIsoId = ((UserVm ) vm ).getIsoId ();
1362- boolean alreadyAttached = (primaryIsoId != null && primaryIsoId .equals (isoId ))
1363- || _vmIsoMapDao .findByVmIdIsoId (vmId , isoId ) != null ;
1364- if (alreadyAttached ) {
1346+ if (isIsoAlreadyAttached (vmId , primaryIsoId , isoId )) {
13651347 throw new InvalidParameterValueException ("The specified ISO is already attached to this Instance." );
13661348 }
13671349 int effectiveMax = effectiveMaxCdroms (vm );
@@ -1507,6 +1489,32 @@ VmIsoMapVO highestCdromMapEntry(long vmId) {
15071489 return highest ;
15081490 }
15091491
1492+ Long resolveIsoIdForDetach (Long primaryIsoId , List <VmIsoMapVO > extras , Long isoParamId ) {
1493+ if (isoParamId != null ) {
1494+ boolean attached = (primaryIsoId != null && primaryIsoId .equals (isoParamId ))
1495+ || extras .stream ().anyMatch (r -> r .getIsoId () == isoParamId );
1496+ if (!attached ) {
1497+ throw new InvalidParameterValueException ("The specified ISO is not attached to this Instance." );
1498+ }
1499+ return isoParamId ;
1500+ }
1501+ int totalAttached = (primaryIsoId != null ? 1 : 0 ) + extras .size ();
1502+ if (totalAttached == 0 ) {
1503+ throw new InvalidParameterValueException ("The specified instance has no ISO attached to it." );
1504+ }
1505+ if (totalAttached > 1 ) {
1506+ throw new InvalidParameterValueException ("Instance has more than one ISO attached; specify the 'id' parameter to choose which to detach." );
1507+ }
1508+ return primaryIsoId != null ? primaryIsoId : extras .get (0 ).getIsoId ();
1509+ }
1510+
1511+ boolean isIsoAlreadyAttached (long vmId , Long primaryIsoId , long isoId ) {
1512+ if (primaryIsoId != null && primaryIsoId .equals (isoId )) {
1513+ return true ;
1514+ }
1515+ return _vmIsoMapDao .findByVmIdIsoId (vmId , isoId ) != null ;
1516+ }
1517+
15101518 private int effectiveMaxCdroms (VirtualMachine vm ) {
15111519 int globalCap = VmCdromMaxCount .value ();
15121520 // i440fx/IDE: hda is root, our slot scheme puts cdroms at hdc/hdd → 2 max on KVM.
0 commit comments