Skip to content

Commit 46a3032

Browse files
server: set correct gateway when update vm nic on shared networks (#5105)
This PR fixes the issue that nic has wrong gateway after updating vm nic. Steps to reproduce the issue (1) create shared network (in advanced zone or advanced zone with sg) (2) create new shared network (with same startip/endip/netmask, but different gateway). (3) create a vm in new network (4) stop vm and update vm nic ip address Expected result: The vm has correct gateway and netmask (of second network) Actual result: The vm has wrong gateway and netmask (of first network)
1 parent 326e03b commit 46a3032

2 files changed

Lines changed: 4 additions & 3 deletions

File tree

server/src/main/java/com/cloud/vm/UserVmManagerImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1728,7 +1728,7 @@ public UserVm updateNicIpForVirtualMachine(UpdateVmNicIpCmd cmd) {
17281728
throw new InvalidParameterValueException("Allocating ip to guest nic " + nicVO.getUuid() + " failed, please choose another ip");
17291729
}
17301730

1731-
final IPAddressVO newIp = _ipAddressDao.findByIpAndDcId(dc.getId(), ipaddr);
1731+
final IPAddressVO newIp = _ipAddressDao.findByIpAndSourceNetworkId(network.getId(), ipaddr);
17321732
final Vlan vlan = _vlanDao.findById(newIp.getVlanId());
17331733
nicVO.setIPv4Gateway(vlan.getVlanGateway());
17341734
nicVO.setIPv4Netmask(vlan.getVlanNetmask());

server/src/test/java/com/cloud/vm/UserVmManagerTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,7 @@ public void testUpdateVmNicIpSuccess2() throws Exception {
604604

605605
NicVO nic = new NicVO("nic", 1L, 2L, VirtualMachine.Type.User);
606606
when(_nicDao.findById(anyLong())).thenReturn(nic);
607+
nic.setIPv4Address("10.10.10.9");
607608
when(_vmDao.findById(anyLong())).thenReturn(_vmMock);
608609
when(_networkDao.findById(anyLong())).thenReturn(_networkMock);
609610
doReturn(9L).when(_networkMock).getNetworkOfferingId();
@@ -630,9 +631,9 @@ public void testUpdateVmNicIpSuccess2() throws Exception {
630631
when(vlan.getVlanNetmask()).thenReturn("255.255.255.0");
631632

632633
when(_ipAddrMgr.allocatePublicIpForGuestNic(Mockito.eq(_networkMock), nullable(Long.class), Mockito.eq(_accountMock), anyString())).thenReturn("10.10.10.10");
633-
lenient().when(_ipAddressDao.findByIpAndSourceNetworkId(anyLong(), anyString())).thenReturn(null);
634+
when(_ipAddressDao.findByIpAndSourceNetworkId(anyLong(), eq("10.10.10.10"))).thenReturn(newIp);
635+
when(_ipAddressDao.findByIpAndSourceNetworkId(anyLong(), eq("10.10.10.9"))).thenReturn(null);
634636
when(_nicDao.persist(any(NicVO.class))).thenReturn(nic);
635-
when(_ipAddressDao.findByIpAndDcId(anyLong(), anyString())).thenReturn(newIp);
636637
when(_vlanDao.findById(anyLong())).thenReturn(vlan);
637638

638639
Account caller = new AccountVO("testaccount", 1, "networkdomain", (short)0, UUID.randomUUID().toString());

0 commit comments

Comments
 (0)