Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions server/src/main/java/com/cloud/vm/UserVmManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -9509,6 +9509,13 @@ public UserVm importVM(final DataCenter zone, final Host host, final VirtualMach
throw new InvalidParameterValueException("Unable to import virtual machine with invalid host");
}

// Ensure template details are loaded so that commitUserVm can copy them into the VM's details map
VMTemplateVO vmTemplateVO = null;
if (template != null) {
vmTemplateVO = _templateDao.findById(template.getId());
_templateDao.loadDetails(vmTemplateVO);
}
Comment on lines 9511 to +9517
Copy link

Copilot AI Mar 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vmTemplateVO can remain null (when template == null), but it’s used unconditionally in checkIfDynamicScalingCanBeEnabled(...), commitUserVm(...), and vmTemplateVO.getFormat(), which will throw a NullPointerException. If template is required for VM import, fail fast with a clear exception when it’s null and/or when _templateDao.findById(...) returns null; otherwise, keep using template for the ISO check (and avoid passing null into commitUserVm) by deriving an isIso boolean safely.

Suggested change
// Ensure template details are loaded so that commitUserVm can copy them into the VM's details map
VMTemplateVO vmTemplateVO = null;
if (template != null) {
vmTemplateVO = _templateDao.findById(template.getId());
_templateDao.loadDetails(vmTemplateVO);
}
if (template == null) {
throw new InvalidParameterValueException("Unable to import virtual machine without a template");
}
// Ensure template details are loaded so that commitUserVm can copy them into the VM's details map
VMTemplateVO vmTemplateVO = null;
vmTemplateVO = _templateDao.findById(template.getId());
if (vmTemplateVO == null) {
throw new InvalidParameterValueException("Unable to find template with id " + template.getId() + " for virtual machine import");
}
_templateDao.loadDetails(vmTemplateVO);

Copilot uses AI. Check for mistakes.

final long id = _vmDao.getNextInSequence(Long.class, "id");
String instanceName = StringUtils.isBlank(instanceNameInternal) ?
getInternalName(owner.getAccountId(), id) :
Expand All @@ -9521,10 +9528,10 @@ public UserVm importVM(final DataCenter zone, final Host host, final VirtualMach

final String uuidName = _uuidMgr.generateUuid(UserVm.class, null);
final Host lastHost = powerState != VirtualMachine.PowerState.PowerOn ? host : null;
final Boolean dynamicScalingEnabled = checkIfDynamicScalingCanBeEnabled(null, serviceOffering, template, zone.getId());
return commitUserVm(true, zone, host, lastHost, template, hostName, displayName, owner,
final Boolean dynamicScalingEnabled = checkIfDynamicScalingCanBeEnabled(null, serviceOffering, vmTemplateVO, zone.getId());
return commitUserVm(true, zone, host, lastHost, vmTemplateVO, hostName, displayName, owner,
null, null, userData, null, null, isDisplayVm, keyboard,
accountId, userId, serviceOffering, template.getFormat().equals(ImageFormat.ISO), sshPublicKeys, networkNicMap,
accountId, userId, serviceOffering, vmTemplateVO.getFormat().equals(ImageFormat.ISO), sshPublicKeys, networkNicMap,
Comment on lines +9531 to +9534
Copy link

Copilot AI Mar 11, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vmTemplateVO can remain null (when template == null), but it’s used unconditionally in checkIfDynamicScalingCanBeEnabled(...), commitUserVm(...), and vmTemplateVO.getFormat(), which will throw a NullPointerException. If template is required for VM import, fail fast with a clear exception when it’s null and/or when _templateDao.findById(...) returns null; otherwise, keep using template for the ISO check (and avoid passing null into commitUserVm) by deriving an isIso boolean safely.

Copilot uses AI. Check for mistakes.
id, instanceName, uuidName, hypervisorType, customParameters,
null, null, null, powerState, dynamicScalingEnabled, null, serviceOffering.getDiskOfferingId(), null, null, null, null);
});
Expand Down
Loading