Skip to content

Commit e394f1c

Browse files
committed
enhance ISO attachment validation to handle multiple ISOs and prevent duplicates
1 parent 2ff3ce6 commit e394f1c

1 file changed

Lines changed: 15 additions & 6 deletions

File tree

server/src/main/java/com/cloud/template/TemplateManagerImpl.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,12 +1263,15 @@ public boolean detachIso(long vmId, Long isoParamId, Boolean... extraParams) {
12631263
throw new InvalidParameterValueException("The specified ISO is not attached to this Instance.");
12641264
}
12651265
isoId = isoParamId;
1266-
} else if (primaryIsoId == null && extras.isEmpty()) {
1267-
throw new InvalidParameterValueException("The specified instance has no ISO attached to it.");
1268-
} else if (!extras.isEmpty()) {
1269-
throw new InvalidParameterValueException("Instance has more than one ISO attached; specify the 'id' parameter to choose which to detach.");
12701266
} else {
1271-
isoId = primaryIsoId;
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+
}
12721275
}
12731276
}
12741277
if (isoId == null) {
@@ -1355,8 +1358,14 @@ public boolean attachIso(long isoId, long vmId, Boolean... extraParams) {
13551358
throw new InvalidParameterValueException("Cannot attach VMware tools drivers to incompatible hypervisor " + vm.getHypervisorType());
13561359
}
13571360
if (!isVirtualRouter) {
1361+
Long primaryIsoId = ((UserVm) vm).getIsoId();
1362+
boolean alreadyAttached = (primaryIsoId != null && primaryIsoId.equals(isoId))
1363+
|| _vmIsoMapDao.findByVmIdIsoId(vmId, isoId) != null;
1364+
if (alreadyAttached) {
1365+
throw new InvalidParameterValueException("The specified ISO is already attached to this Instance.");
1366+
}
13581367
int effectiveMax = effectiveMaxCdroms(vm);
1359-
int attached = (((UserVm) vm).getIsoId() != null ? 1 : 0) + _vmIsoMapDao.listByVmId(vmId).size();
1368+
int attached = (primaryIsoId != null ? 1 : 0) + _vmIsoMapDao.listByVmId(vmId).size();
13601369
if (attached >= effectiveMax) {
13611370
throw new InvalidParameterValueException(String.format(
13621371
"Instance has reached the maximum of %d attached CD-ROM(s); detach one before attaching another.", effectiveMax));

0 commit comments

Comments
 (0)