Skip to content

Commit d4fcd0f

Browse files
committed
Get the router's real NIC when the shared network IP lookup returns its placeholder
1 parent f91a730 commit d4fcd0f

2 files changed

Lines changed: 37 additions & 6 deletions

File tree

‎server/src/main/java/com/cloud/api/ApiResponseHelper.java‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,7 +1237,7 @@ private void setVpcIdInResponse(Long vpcId, Consumer<String> vpcUuidSetter, Cons
12371237

12381238
protected void showVmInfoForSharedNetworks(boolean forVirtualNetworks, IpAddress ipAddr, IPAddressResponse ipResponse) {
12391239
if (!forVirtualNetworks) {
1240-
NicVO nic = ApiDBUtils.findNonPlaceHolderByIp4AddressAndNetworkId(ipAddr.getAddress().toString(), ipAddr.getNetworkId());
1240+
NicVO nic = ApiDBUtils.findByIp4AddressAndNetworkId(ipAddr.getAddress().toString(), ipAddr.getNetworkId());
12411241

12421242
if (nic == null) { // find in nic_secondary_ips, user vm only
12431243
NicSecondaryIpVO secondaryIp =
@@ -1267,11 +1267,18 @@ protected void showVmInfoForSharedNetworks(boolean forVirtualNetworks, IpAddress
12671267
}
12681268
} else if (nic.getVmType() == Type.DomainRouter) {
12691269
ipResponse.setIsSystem(true);
1270-
VirtualMachine vm = ApiDBUtils.findVMInstanceById(nic.getInstanceId());
1271-
if (vm != null) {
1272-
ipResponse.setVirtualMachineId(vm.getUuid());
1273-
ipResponse.setVirtualMachineName(vm.getHostName());
1274-
ipResponse.setVirtualMachineType(vm.getType().toString());
1270+
// The lookup above returns either the router's nic or its placeholder, whichever the db returns first.
1271+
NicVO routerNic = nic;
1272+
if (nic.getReservationStrategy() == Nic.ReservationStrategy.PlaceHolder) { // found a placeholder nic, get the router's real nic
1273+
routerNic = ApiDBUtils.findNonPlaceHolderByIp4AddressAndNetworkId(ipAddr.getAddress().toString(), ipAddr.getNetworkId());
1274+
}
1275+
if (routerNic != null) { // continue if we have the router's real nic, i.e. the router exists
1276+
VirtualMachine vm = ApiDBUtils.findVMInstanceById(routerNic.getInstanceId());
1277+
if (vm != null) {
1278+
ipResponse.setVirtualMachineId(vm.getUuid());
1279+
ipResponse.setVirtualMachineName(vm.getHostName());
1280+
ipResponse.setVirtualMachineType(vm.getType().toString());
1281+
}
12751282
}
12761283
} else if (nic.getVmType().isUsedBySystem()) {
12771284
ipResponse.setIsSystem(true);

‎server/src/test/java/com/cloud/api/ApiResponseHelperTest.java‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
import com.cloud.user.dao.UserDataDao;
9898
import com.cloud.utils.net.Ip;
9999
import com.cloud.vm.ConsoleSessionVO;
100+
import com.cloud.vm.Nic;
100101
import com.cloud.vm.NicSecondaryIp;
101102
import com.cloud.vm.NicVO;
102103
import com.cloud.vm.VMInstanceVO;
@@ -769,10 +770,13 @@ public void showVmInfoForSharedNetworksTestRouterIp() {
769770
IpAddress ipAddress = Mockito.mock(IpAddress.class);
770771
when(ipAddress.getAddress()).thenReturn(new Ip("10.1.1.2"));
771772
when(ipAddress.getNetworkId()).thenReturn(1L);
773+
NicVO placeholderNic = new NicVO(null, null, 1L, VirtualMachine.Type.DomainRouter);
774+
placeholderNic.setReservationStrategy(Nic.ReservationStrategy.PlaceHolder);
772775
NicVO routerNic = new NicVO("DirectNetworkGuru", 2L, 1L, VirtualMachine.Type.DomainRouter);
773776
IPAddressResponse ipResponse = Mockito.mock(IPAddressResponse.class);
774777

775778
try (MockedStatic<ApiDBUtils> ignored = Mockito.mockStatic(ApiDBUtils.class)) {
779+
when(ApiDBUtils.findByIp4AddressAndNetworkId("10.1.1.2", 1L)).thenReturn(placeholderNic);
776780
when(ApiDBUtils.findNonPlaceHolderByIp4AddressAndNetworkId("10.1.1.2", 1L)).thenReturn(routerNic);
777781
when(ApiDBUtils.findVMInstanceById(2L)).thenReturn(vmInstanceVOMock);
778782
when(vmInstanceVOMock.getUuid()).thenReturn("router-uuid");
@@ -786,4 +790,24 @@ public void showVmInfoForSharedNetworksTestRouterIp() {
786790
verify(ipResponse).setVirtualMachineName("r-2-VM");
787791
}
788792
}
793+
794+
@Test
795+
public void showVmInfoForSharedNetworksTestRouterPlaceholderOnly() {
796+
IpAddress ipAddress = Mockito.mock(IpAddress.class);
797+
when(ipAddress.getAddress()).thenReturn(new Ip("10.1.1.2"));
798+
when(ipAddress.getNetworkId()).thenReturn(1L);
799+
NicVO placeholderNic = new NicVO(null, null, 1L, VirtualMachine.Type.DomainRouter);
800+
placeholderNic.setReservationStrategy(Nic.ReservationStrategy.PlaceHolder);
801+
IPAddressResponse ipResponse = Mockito.mock(IPAddressResponse.class);
802+
803+
try (MockedStatic<ApiDBUtils> ignored = Mockito.mockStatic(ApiDBUtils.class)) {
804+
when(ApiDBUtils.findNonPlaceHolderByIp4AddressAndNetworkId("10.1.1.2", 1L)).thenReturn(null);
805+
when(ApiDBUtils.findByIp4AddressAndNetworkId("10.1.1.2", 1L)).thenReturn(placeholderNic);
806+
807+
apiResponseHelper.showVmInfoForSharedNetworks(false, ipAddress, ipResponse);
808+
809+
verify(ipResponse).setIsSystem(true);
810+
verify(ipResponse, Mockito.never()).setVirtualMachineId(Mockito.anyString());
811+
}
812+
}
789813
}

0 commit comments

Comments
 (0)