Skip to content

Commit df78800

Browse files
Ensure the detail value is not null when updating the vnc access details of the vms with failed migrations on host maintenance
- Fixes the Constraint error Column 'value' cannot be null
1 parent 288f9a9 commit df78800

3 files changed

Lines changed: 26 additions & 4 deletions

File tree

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtGetVncPortCommandWrapper.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public Answer execute(final GetVncPortCommand command, final LibvirtComputingRes
4141
final Integer vncPort = libvirtComputingResource.getVncPort(conn, command.getName());
4242
return new GetVncPortAnswer(command, libvirtComputingResource.getPrivateIp(), 5900 + vncPort);
4343
} catch (final LibvirtException e) {
44+
logger.error("Failed to get vnc port for the vm: {} due to {}", command.getName(), e.getMessage());
4445
return new GetVncPortAnswer(command, e.toString());
4546
}
4647
}

server/src/main/java/com/cloud/resource/ResourceManagerImpl.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1825,10 +1825,29 @@ public Host cancelHostAsDegraded(final CancelHostAsDegradedCmd cmd) throws NoTra
18251825
protected void setKVMVncAccess(long hostId, List<VMInstanceVO> vms) {
18261826
for (VMInstanceVO vm : vms) {
18271827
GetVncPortAnswer vmVncPortAnswer = (GetVncPortAnswer) _agentMgr.easySend(hostId, new GetVncPortCommand(vm.getId(), vm.getInstanceName()));
1828-
if (vmVncPortAnswer != null) {
1829-
vmInstanceDetailsDao.addDetail(vm.getId(), VmDetailConstants.KVM_VNC_ADDRESS, vmVncPortAnswer.getAddress(), true);
1830-
vmInstanceDetailsDao.addDetail(vm.getId(), VmDetailConstants.KVM_VNC_PORT, String.valueOf(vmVncPortAnswer.getPort()), true);
1831-
}
1828+
updateVncAccessDetailForVM(vm, vmVncPortAnswer, hostId);
1829+
}
1830+
}
1831+
1832+
private void updateVncAccessDetailForVM(VMInstanceVO vm, GetVncPortAnswer vmVncPortAnswer, long hostId) {
1833+
if (vm == null || vmVncPortAnswer == null) {
1834+
logger.warn("VM or VNC port answer is null. Cannot update VNC access details.");
1835+
return;
1836+
}
1837+
1838+
if (!vmVncPortAnswer.getResult()) {
1839+
logger.warn("Failed to get VNC port for VM {} on host {}. Details: {}", vm, hostId, vmVncPortAnswer.getDetails());
1840+
return;
1841+
}
1842+
1843+
String vncAddress = vmVncPortAnswer.getAddress();
1844+
String vncPort = String.valueOf(vmVncPortAnswer.getPort());
1845+
if (org.apache.commons.lang3.StringUtils.isNotBlank(vmVncPortAnswer.getAddress()) && org.apache.commons.lang3.StringUtils.isNotBlank(vncPort)) {
1846+
logger.info("Setting VNC access details for VM {} on host {} to address: {}, port: {}", vm, hostId, vncAddress, vncPort);
1847+
vmInstanceDetailsDao.addDetail(vm.getId(), VmDetailConstants.KVM_VNC_ADDRESS, vncAddress, true);
1848+
vmInstanceDetailsDao.addDetail(vm.getId(), VmDetailConstants.KVM_VNC_PORT, vncPort, true);
1849+
} else {
1850+
logger.warn("Unable to set VNC access details for VM {} on host {} as the address or port is blank. Address: {}, Port: {}", vm, hostId, vncAddress, vncPort);
18321851
}
18331852
}
18341853

server/src/test/java/com/cloud/resource/ResourceManagerImplTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,10 @@ public void setup() throws Exception {
210210
actionEventUtilsMocked = Mockito.mockStatic(ActionEventUtils.class);
211211
BDDMockito.given(ActionEventUtils.onCompletedActionEvent(anyLong(), anyLong(), anyString(), anyString(), anyString(), anyLong(), anyString(), anyLong()))
212212
.willReturn(1L);
213+
when(getVncPortAnswerVm1.getResult()).thenReturn(true);
213214
when(getVncPortAnswerVm1.getAddress()).thenReturn(vm1VncAddress);
214215
when(getVncPortAnswerVm1.getPort()).thenReturn(vm1VncPort);
216+
when(getVncPortAnswerVm2.getResult()).thenReturn(true);
215217
when(getVncPortAnswerVm2.getAddress()).thenReturn(vm2VncAddress);
216218
when(getVncPortAnswerVm2.getPort()).thenReturn(vm2VncPort);
217219
getVncPortCommandMockedConstruction = Mockito.mockConstruction(GetVncPortCommand.class, (mock,context) -> {

0 commit comments

Comments
 (0)