server: Prevent NPE if hypervisor's capabilities are null (#5029)

If the hypervisor's capabilities are null, CloudRuntimeException will be thrown;
Format the error message.
This commit is contained in:
slavkap 2021-05-21 12:49:04 +03:00 committed by GitHub
parent c6ba3d1bea
commit d47e273329
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6201,22 +6201,16 @@ public class UserVmManagerImpl extends ManagerBase implements UserVmManager, Vir
HypervisorCapabilitiesVO capabilities = _hypervisorCapabilitiesDao.findByHypervisorTypeAndVersion(srcHost.getHypervisorType(), srcHost.getHypervisorVersion());
if (capabilities == null && HypervisorType.KVM.equals(srcHost.getHypervisorType())) {
if (capabilities == null) {
if (!HypervisorType.KVM.equals(srcHost.getHypervisorType())) {
throw new CloudRuntimeException(String.format("Cannot migrate VM with storage, as the capabilities are not found for the hypervisor %s with version %s", srcHost.getHypervisorType(), srcHost.getHypervisorVersion()));
}
List<HypervisorCapabilitiesVO> lstHypervisorCapabilities = _hypervisorCapabilitiesDao.listAllByHypervisorType(HypervisorType.KVM);
if (lstHypervisorCapabilities != null) {
for (HypervisorCapabilitiesVO hypervisorCapabilities : lstHypervisorCapabilities) {
if (hypervisorCapabilities.isStorageMotionSupported()) {
capabilities = hypervisorCapabilities;
break;
}
}
}
}
if (!capabilities.isStorageMotionSupported()) {
throw new CloudRuntimeException("Migration with storage isn't supported on hypervisor " + srcHost.getHypervisorType() + " of version " + srcHost.getHypervisorVersion());
capabilities = lstHypervisorCapabilities.stream().filter(hvCapabilities -> hvCapabilities.isStorageMotionSupported()).findAny()
.orElseThrow(() -> new CloudRuntimeException(String.format("Cannot migrate VM with storage, as the capabilities are not found for the hypervisor %s with version %s", srcHost.getHypervisorType(), srcHost.getHypervisorVersion())));
} else if (!capabilities.isStorageMotionSupported()) {
throw new CloudRuntimeException(String.format("Migration with storage isn't supported on hypervisor %s of version %s", srcHost.getHypervisorType(), srcHost.getHypervisorVersion()));
}
// Check if destination host is up.