Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions server/src/main/java/com/cloud/api/ApiDBUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2351,6 +2351,10 @@ public static NicVO findByIp4AddressAndNetworkId(String ip4Address, long network
return s_nicDao.findByIp4AddressAndNetworkId(ip4Address, networkId);
}

public static NicVO findNonPlaceHolderByIp4AddressAndNetworkId(String ip4Address, long networkId) {
return s_nicDao.findNonPlaceHolderByIp4AddressAndNetworkId(ip4Address, networkId);
}

public static NicSecondaryIpVO findSecondaryIpByIp4AddressAndNetworkId(String ip4Address, long networkId) {
return s_nicSecondaryIpDao.findByIp4AddressAndNetworkId(ip4Address, networkId);
}
Expand Down
2 changes: 1 addition & 1 deletion server/src/main/java/com/cloud/api/ApiResponseHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -1263,7 +1263,7 @@ private void setVpcIdInResponse(Long vpcId, Consumer<String> vpcUuidSetter, Cons

private void showVmInfoForSharedNetworks(boolean forVirtualNetworks, IpAddress ipAddr, IPAddressResponse ipResponse) {
if (!forVirtualNetworks) {
NicVO nic = ApiDBUtils.findByIp4AddressAndNetworkId(ipAddr.getAddress().toString(), ipAddr.getNetworkId());
NicVO nic = ApiDBUtils.findNonPlaceHolderByIp4AddressAndNetworkId(ipAddr.getAddress().toString(), ipAddr.getNetworkId());

if (nic == null) { // find in nic_secondary_ips, user vm only
NicSecondaryIpVO secondaryIp =
Expand Down
28 changes: 28 additions & 0 deletions server/src/test/java/com/cloud/api/ApiResponseHelperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import org.apache.cloudstack.api.response.ConsoleSessionResponse;
import org.apache.cloudstack.api.response.DirectDownloadCertificateResponse;
import org.apache.cloudstack.api.response.GuestOSCategoryResponse;
import org.apache.cloudstack.api.response.IPAddressResponse;
import org.apache.cloudstack.api.response.IpQuarantineResponse;
import org.apache.cloudstack.api.response.NicSecondaryIpResponse;
import org.apache.cloudstack.api.response.ResourceIconResponse;
Expand All @@ -64,6 +65,7 @@
import com.cloud.configuration.Resource;
import com.cloud.domain.DomainVO;
import com.cloud.host.HostVO;
import com.cloud.network.IpAddress;
import com.cloud.network.Networks;
import com.cloud.network.PhysicalNetworkTrafficType;
import com.cloud.network.PublicIpQuarantine;
Expand Down Expand Up @@ -96,7 +98,9 @@
import com.cloud.utils.net.Ip;
import com.cloud.vm.ConsoleSessionVO;
import com.cloud.vm.NicSecondaryIp;
import com.cloud.vm.NicVO;
import com.cloud.vm.VMInstanceVO;
import com.cloud.vm.VirtualMachine;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
Expand Down Expand Up @@ -800,4 +804,28 @@ public void createConsoleSessionResponseTestShouldReturnFullResponse() {
Assert.assertEquals(expected.getVmName(), response.getVmName());
}
}

@Test
public void showVmInfoForSharedNetworksTestSkipsPlaceHolderNic() {
IpAddress ipAddress = Mockito.mock(IpAddress.class);
Mockito.when(ipAddress.getAddress()).thenReturn(new Ip("10.1.1.10"));
Mockito.when(ipAddress.getNetworkId()).thenReturn(1L);
NicVO nic = Mockito.mock(NicVO.class);
Mockito.when(nic.getVmType()).thenReturn(VirtualMachine.Type.DomainRouter);
Mockito.when(nic.getInstanceId()).thenReturn(2L);
Mockito.when(vmInstanceVOMock.getUuid()).thenReturn("router-uuid");
Mockito.when(vmInstanceVOMock.getHostName()).thenReturn("r-2-VM");
Mockito.when(vmInstanceVOMock.getType()).thenReturn(VirtualMachine.Type.DomainRouter);
IPAddressResponse response = new IPAddressResponse();

try (MockedStatic<ApiDBUtils> apiDBUtilsStaticMock = Mockito.mockStatic(ApiDBUtils.class)) {
apiDBUtilsStaticMock.when(() -> ApiDBUtils.findNonPlaceHolderByIp4AddressAndNetworkId("10.1.1.10", 1L)).thenReturn(nic);
apiDBUtilsStaticMock.when(() -> ApiDBUtils.findVMInstanceById(2L)).thenReturn(vmInstanceVOMock);

ReflectionTestUtils.invokeMethod(apiResponseHelper, "showVmInfoForSharedNetworks", false, ipAddress, response);

apiDBUtilsStaticMock.verify(() -> ApiDBUtils.findByIp4AddressAndNetworkId(Mockito.anyString(), Mockito.anyLong()), Mockito.never());
Assert.assertEquals("router-uuid", ReflectionTestUtils.getField(response, "virtualMachineId"));
}
}
}
Loading