Skip to content
Open
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 @@ -2311,6 +2311,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
5 changes: 3 additions & 2 deletions server/src/main/java/com/cloud/api/ApiResponseHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -1235,9 +1235,9 @@ private void setVpcIdInResponse(Long vpcId, Consumer<String> vpcUuidSetter, Cons
}
}

private void showVmInfoForSharedNetworks(boolean forVirtualNetworks, IpAddress ipAddr, IPAddressResponse ipResponse) {
protected 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 Expand Up @@ -1266,6 +1266,7 @@ private void showVmInfoForSharedNetworks(boolean forVirtualNetworks, IpAddress i
}
}
} else if (nic.getVmType() == Type.DomainRouter) {
ipResponse.setIsSystem(true);
VirtualMachine vm = ApiDBUtils.findVMInstanceById(nic.getInstanceId());
if (vm != null) {
ipResponse.setVirtualMachineId(vm.getUuid());
Expand Down
27 changes: 27 additions & 0 deletions server/src/test/java/com/cloud/api/ApiResponseHelperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.apache.cloudstack.api.response.AutoScaleVmProfileResponse;
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 @@ -68,6 +69,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.PublicIpQuarantine;
import com.cloud.network.as.AutoScaleVmGroup;
import com.cloud.network.as.AutoScaleVmGroupVO;
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 org.apache.cloudstack.api.ResponseObject;
import org.apache.cloudstack.api.response.ConsoleSessionResponse;

Expand Down Expand Up @@ -759,4 +763,27 @@ public void createConsoleSessionResponseTestShouldReturnFullResponse() {
Assert.assertEquals(expected.getVmName(), response.getVmName());
}
}

@Test
public void showVmInfoForSharedNetworksTestRouterIp() {
IpAddress ipAddress = Mockito.mock(IpAddress.class);
when(ipAddress.getAddress()).thenReturn(new Ip("10.1.1.2"));
when(ipAddress.getNetworkId()).thenReturn(1L);
NicVO routerNic = new NicVO("DirectNetworkGuru", 2L, 1L, VirtualMachine.Type.DomainRouter);
IPAddressResponse ipResponse = Mockito.mock(IPAddressResponse.class);

try (MockedStatic<ApiDBUtils> ignored = Mockito.mockStatic(ApiDBUtils.class)) {
when(ApiDBUtils.findNonPlaceHolderByIp4AddressAndNetworkId("10.1.1.2", 1L)).thenReturn(routerNic);
when(ApiDBUtils.findVMInstanceById(2L)).thenReturn(vmInstanceVOMock);
when(vmInstanceVOMock.getUuid()).thenReturn("router-uuid");
when(vmInstanceVOMock.getHostName()).thenReturn("r-2-VM");
when(vmInstanceVOMock.getType()).thenReturn(VirtualMachine.Type.DomainRouter);

apiResponseHelper.showVmInfoForSharedNetworks(false, ipAddress, ipResponse);

verify(ipResponse).setIsSystem(true);
verify(ipResponse).setVirtualMachineId("router-uuid");
verify(ipResponse).setVirtualMachineName("r-2-VM");
}
}
}