From 41e8ad74875480e205ae1048a068a2b28ab32627 Mon Sep 17 00:00:00 2001 From: Abhishek Kumar Date: Thu, 8 Jun 2023 18:35:59 +0530 Subject: [PATCH] api,server,ui: vr,systemvm in public ip response (#7403) Signed-off-by: Abhishek Kumar Co-authored-by: Wei Zhou --- .../apache/cloudstack/api/ApiConstants.java | 1 + .../api/response/IPAddressResponse.java | 12 ++++- .../java/com/cloud/api/ApiResponseHelper.java | 53 ++++++++++++++----- ui/src/components/view/ListView.vue | 10 +++- ui/src/config/section/network.js | 2 +- ui/src/views/network/IpAddressesTab.vue | 10 +++- 6 files changed, 70 insertions(+), 18 deletions(-) diff --git a/api/src/main/java/org/apache/cloudstack/api/ApiConstants.java b/api/src/main/java/org/apache/cloudstack/api/ApiConstants.java index 76c0830527f..9dc4e587cfa 100644 --- a/api/src/main/java/org/apache/cloudstack/api/ApiConstants.java +++ b/api/src/main/java/org/apache/cloudstack/api/ApiConstants.java @@ -460,6 +460,7 @@ public class ApiConstants { public static final String VIRTUAL_MACHINE_NAME = "virtualmachinename"; public static final String VIRTUAL_MACHINE_ID_IP = "vmidipmap"; public static final String VIRTUAL_MACHINE_COUNT = "virtualmachinecount"; + public static final String VIRTUAL_MACHINE_TYPE = "virtualmachinetype"; public static final String VIRTUAL_MACHINES = "virtualmachines"; public static final String USAGE_ID = "usageid"; public static final String USAGE_TYPE = "usagetype"; diff --git a/api/src/main/java/org/apache/cloudstack/api/response/IPAddressResponse.java b/api/src/main/java/org/apache/cloudstack/api/response/IPAddressResponse.java index 13497d89308..2ad0a4fad0d 100644 --- a/api/src/main/java/org/apache/cloudstack/api/response/IPAddressResponse.java +++ b/api/src/main/java/org/apache/cloudstack/api/response/IPAddressResponse.java @@ -96,15 +96,19 @@ public class IPAddressResponse extends BaseResponseWithAnnotations implements Co private Boolean isSystem; @SerializedName(ApiConstants.VIRTUAL_MACHINE_ID) - @Param(description = "virtual machine id the ip address is assigned to (not null only for static nat Ip)") + @Param(description = "virtual machine id the ip address is assigned to") private String virtualMachineId; + @SerializedName(ApiConstants.VIRTUAL_MACHINE_TYPE) + @Param(description = "virtual machine type the ip address is assigned to", since = "4.19.0") + private String virtualMachineType; + @SerializedName("vmipaddress") @Param(description = "virtual machine (dnat) ip address (not null only for static nat Ip)") private String virtualMachineIp; @SerializedName("virtualmachinename") - @Param(description = "virtual machine name the ip address is assigned to (not null only for static nat Ip)") + @Param(description = "virtual machine name the ip address is assigned to") private String virtualMachineName; @SerializedName("virtualmachinedisplayname") @@ -232,6 +236,10 @@ public class IPAddressResponse extends BaseResponseWithAnnotations implements Co this.virtualMachineId = virtualMachineId; } + public void setVirtualMachineType(String virtualMachineType) { + this.virtualMachineType = virtualMachineType; + } + public void setVirtualMachineIp(String virtualMachineIp) { this.virtualMachineIp = virtualMachineIp; } diff --git a/server/src/main/java/com/cloud/api/ApiResponseHelper.java b/server/src/main/java/com/cloud/api/ApiResponseHelper.java index 8fffebb3303..11db312ebdf 100644 --- a/server/src/main/java/com/cloud/api/ApiResponseHelper.java +++ b/server/src/main/java/com/cloud/api/ApiResponseHelper.java @@ -199,6 +199,7 @@ import org.apache.cloudstack.usage.Usage; import org.apache.cloudstack.usage.UsageService; import org.apache.cloudstack.usage.UsageTypes; import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; import org.apache.log4j.Logger; @@ -935,6 +936,42 @@ public class ApiResponseHelper implements ResponseGenerator { return userIpAddresVO != null ? userIpAddresVO.isForSystemVms() : false; } + private void addVmDetailsInIpResponse(IPAddressResponse response, IpAddress ipAddress) { + if (ipAddress.getAllocatedToAccountId() != null && ipAddress.getAllocatedToAccountId() == Account.ACCOUNT_ID_SYSTEM) { + NicVO nic = ApiDBUtils.findByIp4AddressAndNetworkId(ipAddress.getAddress().toString(), ipAddress.getNetworkId()); + if (nic != null) { + addSystemVmInfoToIpResponse(nic, response); + } + } + if (ipAddress.getAssociatedWithVmId() != null) { + addUserVmDetailsInIpResponse(response, ipAddress); + } + } + + private void addSystemVmInfoToIpResponse(NicVO nic, IPAddressResponse ipResponse) { + final boolean isAdmin = Account.Type.ADMIN.equals(CallContext.current().getCallingAccount().getType()); + if (!isAdmin) { + return; + } + VirtualMachine vm = ApiDBUtils.findVMInstanceById(nic.getInstanceId()); + if (vm == null) { + return; + } + ipResponse.setVirtualMachineId(vm.getUuid()); + ipResponse.setVirtualMachineName(vm.getHostName()); + ipResponse.setVirtualMachineType(vm.getType().toString()); + } + + private void addUserVmDetailsInIpResponse(IPAddressResponse response, IpAddress ipAddress) { + UserVm userVm = ApiDBUtils.findUserVmById(ipAddress.getAssociatedWithVmId()); + if (userVm != null) { + response.setVirtualMachineId(userVm.getUuid()); + response.setVirtualMachineName(userVm.getHostName()); + response.setVirtualMachineType(userVm.getType().toString()); + response.setVirtualMachineDisplayName(ObjectUtils.firstNonNull(userVm.getDisplayName(), userVm.getHostName())); + } + } + @Override public IPAddressResponse createIPAddressResponse(ResponseView view, IpAddress ipAddr) { VlanVO vlan = ApiDBUtils.findVlanById(ipAddr.getVlanId()); @@ -963,18 +1000,7 @@ public class ApiResponseHelper implements ResponseGenerator { ipResponse.setForVirtualNetwork(forVirtualNetworks); ipResponse.setStaticNat(ipAddr.isOneToOneNat()); - if (ipAddr.getAssociatedWithVmId() != null) { - UserVm vm = ApiDBUtils.findUserVmById(ipAddr.getAssociatedWithVmId()); - if (vm != null) { - ipResponse.setVirtualMachineId(vm.getUuid()); - ipResponse.setVirtualMachineName(vm.getHostName()); - if (vm.getDisplayName() != null) { - ipResponse.setVirtualMachineDisplayName(vm.getDisplayName()); - } else { - ipResponse.setVirtualMachineDisplayName(vm.getHostName()); - } - } - } + addVmDetailsInIpResponse(ipResponse, ipAddr); if (ipAddr.getVmIp() != null) { ipResponse.setVirtualMachineIp(ipAddr.getVmIp()); } @@ -1092,8 +1118,9 @@ public class ApiResponseHelper implements ResponseGenerator { ipResponse.setVirtualMachineDisplayName(vm.getHostName()); } } - } else if (nic.getVmType() == VirtualMachine.Type.DomainRouter) { + } else if (nic.getVmType().isUsedBySystem()) { ipResponse.setIsSystem(true); + addSystemVmInfoToIpResponse(nic, ipResponse); } } } diff --git a/ui/src/components/view/ListView.vue b/ui/src/components/view/ListView.vue index a9163ce63cd..294087d5f7a 100644 --- a/ui/src/components/view/ListView.vue +++ b/ui/src/components/view/ListView.vue @@ -153,7 +153,7 @@ {{ text }}