Skip to content

Commit f91a730

Browse files
committed
Fix listPublicIpAddresses NPE on shared network router placeholder NICs (#14248)
1 parent f23766d commit f91a730

3 files changed

Lines changed: 34 additions & 2 deletions

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2311,6 +2311,10 @@ public static NicVO findByIp4AddressAndNetworkId(String ip4Address, long network
23112311
return s_nicDao.findByIp4AddressAndNetworkId(ip4Address, networkId);
23122312
}
23132313

2314+
public static NicVO findNonPlaceHolderByIp4AddressAndNetworkId(String ip4Address, long networkId) {
2315+
return s_nicDao.findNonPlaceHolderByIp4AddressAndNetworkId(ip4Address, networkId);
2316+
}
2317+
23142318
public static NicSecondaryIpVO findSecondaryIpByIp4AddressAndNetworkId(String ip4Address, long networkId) {
23152319
return s_nicSecondaryIpDao.findByIp4AddressAndNetworkId(ip4Address, networkId);
23162320
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,9 +1235,9 @@ private void setVpcIdInResponse(Long vpcId, Consumer<String> vpcUuidSetter, Cons
12351235
}
12361236
}
12371237

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

12421242
if (nic == null) { // find in nic_secondary_ips, user vm only
12431243
NicSecondaryIpVO secondaryIp =
@@ -1266,6 +1266,7 @@ private void showVmInfoForSharedNetworks(boolean forVirtualNetworks, IpAddress i
12661266
}
12671267
}
12681268
} else if (nic.getVmType() == Type.DomainRouter) {
1269+
ipResponse.setIsSystem(true);
12691270
VirtualMachine vm = ApiDBUtils.findVMInstanceById(nic.getInstanceId());
12701271
if (vm != null) {
12711272
ipResponse.setVirtualMachineId(vm.getUuid());

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.apache.cloudstack.api.response.AutoScaleVmProfileResponse;
4343
import org.apache.cloudstack.api.response.DirectDownloadCertificateResponse;
4444
import org.apache.cloudstack.api.response.GuestOSCategoryResponse;
45+
import org.apache.cloudstack.api.response.IPAddressResponse;
4546
import org.apache.cloudstack.api.response.IpQuarantineResponse;
4647
import org.apache.cloudstack.api.response.NicSecondaryIpResponse;
4748
import org.apache.cloudstack.api.response.ResourceIconResponse;
@@ -68,6 +69,7 @@
6869
import com.cloud.configuration.Resource;
6970
import com.cloud.domain.DomainVO;
7071
import com.cloud.host.HostVO;
72+
import com.cloud.network.IpAddress;
7173
import com.cloud.network.PublicIpQuarantine;
7274
import com.cloud.network.as.AutoScaleVmGroup;
7375
import com.cloud.network.as.AutoScaleVmGroupVO;
@@ -96,7 +98,9 @@
9698
import com.cloud.utils.net.Ip;
9799
import com.cloud.vm.ConsoleSessionVO;
98100
import com.cloud.vm.NicSecondaryIp;
101+
import com.cloud.vm.NicVO;
99102
import com.cloud.vm.VMInstanceVO;
103+
import com.cloud.vm.VirtualMachine;
100104
import org.apache.cloudstack.api.ResponseObject;
101105
import org.apache.cloudstack.api.response.ConsoleSessionResponse;
102106

@@ -759,4 +763,27 @@ public void createConsoleSessionResponseTestShouldReturnFullResponse() {
759763
Assert.assertEquals(expected.getVmName(), response.getVmName());
760764
}
761765
}
766+
767+
@Test
768+
public void showVmInfoForSharedNetworksTestRouterIp() {
769+
IpAddress ipAddress = Mockito.mock(IpAddress.class);
770+
when(ipAddress.getAddress()).thenReturn(new Ip("10.1.1.2"));
771+
when(ipAddress.getNetworkId()).thenReturn(1L);
772+
NicVO routerNic = new NicVO("DirectNetworkGuru", 2L, 1L, VirtualMachine.Type.DomainRouter);
773+
IPAddressResponse ipResponse = Mockito.mock(IPAddressResponse.class);
774+
775+
try (MockedStatic<ApiDBUtils> ignored = Mockito.mockStatic(ApiDBUtils.class)) {
776+
when(ApiDBUtils.findNonPlaceHolderByIp4AddressAndNetworkId("10.1.1.2", 1L)).thenReturn(routerNic);
777+
when(ApiDBUtils.findVMInstanceById(2L)).thenReturn(vmInstanceVOMock);
778+
when(vmInstanceVOMock.getUuid()).thenReturn("router-uuid");
779+
when(vmInstanceVOMock.getHostName()).thenReturn("r-2-VM");
780+
when(vmInstanceVOMock.getType()).thenReturn(VirtualMachine.Type.DomainRouter);
781+
782+
apiResponseHelper.showVmInfoForSharedNetworks(false, ipAddress, ipResponse);
783+
784+
verify(ipResponse).setIsSystem(true);
785+
verify(ipResponse).setVirtualMachineId("router-uuid");
786+
verify(ipResponse).setVirtualMachineName("r-2-VM");
787+
}
788+
}
762789
}

0 commit comments

Comments
 (0)