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 03b73834a94e..a8ff00c40ff3 100644 --- a/api/src/main/java/org/apache/cloudstack/api/ApiConstants.java +++ b/api/src/main/java/org/apache/cloudstack/api/ApiConstants.java @@ -1407,7 +1407,7 @@ public String toString() { } public enum HostDetails { - all, capacity, events, stats, min; + all, capacity, core, events, stats, min; } public enum VMDetails { diff --git a/api/src/main/java/org/apache/cloudstack/api/response/HostResponse.java b/api/src/main/java/org/apache/cloudstack/api/response/HostResponse.java index 5d085d1bee05..324cc31177a7 100644 --- a/api/src/main/java/org/apache/cloudstack/api/response/HostResponse.java +++ b/api/src/main/java/org/apache/cloudstack/api/response/HostResponse.java @@ -197,6 +197,10 @@ public class HostResponse extends BaseResponseWithAnnotations { @Param(description = "the virtual machine id for host type ConsoleProxy and SecondaryStorageVM", since = "4.21.0") private String virtualMachineId; + @SerializedName("msid") + @Param(description = "(only for details=core) the msid of the host's management server") + private Long msId; + @SerializedName(ApiConstants.MANAGEMENT_SERVER_ID) @Param(description = "The management server ID of the host") private String managementServerId; @@ -488,6 +492,14 @@ public void setVirtualMachineId(String virtualMachineId) { this.virtualMachineId = virtualMachineId; } + public Long getMsId() { + return msId; + } + + public void setMsId(Long msId) { + this.msId = msId; + } + public void setManagementServerId(String managementServerId) { this.managementServerId = managementServerId; } diff --git a/server/src/main/java/com/cloud/api/query/dao/HostJoinDaoImpl.java b/server/src/main/java/com/cloud/api/query/dao/HostJoinDaoImpl.java index e7265a7e3b9a..7e149020cb00 100644 --- a/server/src/main/java/com/cloud/api/query/dao/HostJoinDaoImpl.java +++ b/server/src/main/java/com/cloud/api/query/dao/HostJoinDaoImpl.java @@ -131,21 +131,16 @@ private void setNewHostResponseBase(HostJoinVO host, EnumSet detail hostResponse.setHypervisor(hypervisorType); } hostResponse.setHostType(host.getType()); - if (host.getType().equals(Host.Type.ConsoleProxy) || host.getType().equals(Host.Type.SecondaryStorageVM)) { + if (!details.contains(HostDetails.core) && (host.getType().equals(Host.Type.ConsoleProxy) || host.getType().equals(Host.Type.SecondaryStorageVM))) { VMInstanceVO vm = virtualMachineDao.findVMByInstanceNameIncludingRemoved(host.getName()); if (vm != null) { hostResponse.setVirtualMachineId(vm.getUuid()); } } hostResponse.setLastPinged(new Date(host.getLastPinged())); - Long mshostId = host.getManagementServerId(); - if (mshostId != null) { - ManagementServerHostVO managementServer = managementServerHostDao.findByMsid(host.getManagementServerId()); - if (managementServer != null) { - hostResponse.setManagementServerId(managementServer.getUuid()); - hostResponse.setManagementServerName(managementServer.getName()); - } - } + + setManagementServerResponse(hostResponse, host, details); + hostResponse.setName(host.getName()); hostResponse.setPodId(host.getPodUuid()); hostResponse.setRemoved(host.getRemoved()); @@ -155,41 +150,44 @@ private void setNewHostResponseBase(HostJoinVO host, EnumSet detail hostResponse.setVersion(host.getVersion()); hostResponse.setCreated(host.getCreated()); - List gpuGroups = ApiDBUtils.getGpuGroups(host.getId()); - if (gpuGroups != null && !gpuGroups.isEmpty()) { - List gpus = new ArrayList(); - long gpuRemaining = 0; - long gpuTotal = 0; - for (HostGpuGroupsVO entry : gpuGroups) { - GpuResponse gpuResponse = new GpuResponse(); - gpuResponse.setGpuGroupName(entry.getGroupName()); - List vgpuTypes = ApiDBUtils.getVgpus(entry.getId()); - if (vgpuTypes != null && !vgpuTypes.isEmpty()) { - List vgpus = new ArrayList(); - for (VGPUTypesVO vgpuType : vgpuTypes) { - VgpuResponse vgpuResponse = new VgpuResponse(); - vgpuResponse.setName(vgpuType.getVgpuType()); - vgpuResponse.setVideoRam(vgpuType.getVideoRam()); - vgpuResponse.setMaxHeads(vgpuType.getMaxHeads()); - vgpuResponse.setMaxResolutionX(vgpuType.getMaxResolutionX()); - vgpuResponse.setMaxResolutionY(vgpuType.getMaxResolutionY()); - vgpuResponse.setMaxVgpuPerPgpu(vgpuType.getMaxVgpuPerPgpu()); - vgpuResponse.setRemainingCapacity(vgpuType.getRemainingCapacity()); - vgpuResponse.setmaxCapacity(vgpuType.getMaxCapacity()); - vgpus.add(vgpuResponse); - gpuRemaining += vgpuType.getRemainingCapacity(); - gpuTotal += vgpuType.getMaxCapacity(); + if (!details.contains(HostDetails.core)) { + List gpuGroups = ApiDBUtils.getGpuGroups(host.getId()); + if (gpuGroups != null && !gpuGroups.isEmpty()) { + List gpus = new ArrayList(); + long gpuRemaining = 0; + long gpuTotal = 0; + for (HostGpuGroupsVO entry : gpuGroups) { + GpuResponse gpuResponse = new GpuResponse(); + gpuResponse.setGpuGroupName(entry.getGroupName()); + List vgpuTypes = ApiDBUtils.getVgpus(entry.getId()); + if (vgpuTypes != null && !vgpuTypes.isEmpty()) { + List vgpus = new ArrayList(); + for (VGPUTypesVO vgpuType : vgpuTypes) { + VgpuResponse vgpuResponse = new VgpuResponse(); + vgpuResponse.setName(vgpuType.getVgpuType()); + vgpuResponse.setVideoRam(vgpuType.getVideoRam()); + vgpuResponse.setMaxHeads(vgpuType.getMaxHeads()); + vgpuResponse.setMaxResolutionX(vgpuType.getMaxResolutionX()); + vgpuResponse.setMaxResolutionY(vgpuType.getMaxResolutionY()); + vgpuResponse.setMaxVgpuPerPgpu(vgpuType.getMaxVgpuPerPgpu()); + vgpuResponse.setRemainingCapacity(vgpuType.getRemainingCapacity()); + vgpuResponse.setmaxCapacity(vgpuType.getMaxCapacity()); + vgpus.add(vgpuResponse); + gpuRemaining += vgpuType.getRemainingCapacity(); + gpuTotal += vgpuType.getMaxCapacity(); + } + gpuResponse.setVgpu(vgpus); } - gpuResponse.setVgpu(vgpus); + gpus.add(gpuResponse); } - gpus.add(gpuResponse); + hostResponse.setGpuTotal(gpuTotal); + hostResponse.setGpuUsed(gpuTotal - gpuRemaining); + hostResponse.setGpuGroup(gpus); } - hostResponse.setGpuTotal(gpuTotal); - hostResponse.setGpuUsed(gpuTotal - gpuRemaining); - hostResponse.setGpuGroup(gpus); } - if (details.contains(HostDetails.all) || details.contains(HostDetails.capacity) || details.contains(HostDetails.stats) || details.contains(HostDetails.events)) { + if (details.contains(HostDetails.all) || details.contains(HostDetails.capacity) || details.contains(HostDetails.core) || + details.contains(HostDetails.stats) || details.contains(HostDetails.events)) { hostResponse.setOsCategoryId(host.getOsCategoryUuid()); hostResponse.setOsCategoryName(host.getOsCategoryName()); hostResponse.setZoneName(host.getZoneName()); @@ -202,43 +200,45 @@ private void setNewHostResponseBase(HostJoinVO host, EnumSet detail DecimalFormat decimalFormat = new DecimalFormat("#.##"); if (host.getType() == Host.Type.Routing) { - float cpuOverprovisioningFactor = ApiDBUtils.getCpuOverprovisioningFactor(host.getClusterId()); - if (details.contains(HostDetails.all) || details.contains(HostDetails.capacity)) { - // set allocated capacities - Long mem = host.getMemReservedCapacity() + host.getMemUsedCapacity(); - Long cpu = host.getCpuReservedCapacity() + host.getCpuUsedCapacity(); - - Float memWithOverprovisioning = host.getTotalMemory() * ApiDBUtils.getMemOverprovisioningFactor(host.getClusterId()); - hostResponse.setMemoryTotal(memWithOverprovisioning.longValue()); - hostResponse.setMemWithOverprovisioning(decimalFormat.format(memWithOverprovisioning)); - hostResponse.setMemoryAllocated(mem); - hostResponse.setMemoryAllocatedBytes(mem); - hostResponse.setMemoryAllocatedPercentage(calculateResourceAllocatedPercentage(mem, memWithOverprovisioning)); - + if (details.contains(HostDetails.all) || details.contains(HostDetails.capacity) || details.contains(HostDetails.core)) { String hostTags = host.getTag(); hostResponse.setHostTags(hostTags); - hostResponse.setIsTagARule(host.getIsTagARule()); - hostResponse.setHaHost(containsHostHATag(hostTags)); hostResponse.setExplicitHostTags(host.getExplicitTag()); hostResponse.setImplicitHostTags(host.getImplicitTag()); - - hostResponse.setHypervisorVersion(host.getHypervisorVersion()); - if (host.getArch() != null) { - hostResponse.setArch(host.getArch().getType()); - } - hostResponse.setStorageAccessGroups(host.getStorageAccessGroups()); hostResponse.setClusterStorageAccessGroups(host.getClusterStorageAccessGroups()); hostResponse.setPodStorageAccessGroups(host.getPodStorageAccessGroups()); hostResponse.setZoneStorageAccessGroups(host.getZoneStorageAccessGroups()); + hostResponse.setHypervisorVersion(host.getHypervisorVersion()); + + if (!details.contains(HostDetails.core)) { + float cpuOverprovisioningFactor = ApiDBUtils.getCpuOverprovisioningFactor(host.getClusterId()); + // set allocated capacities + Long mem = host.getMemReservedCapacity() + host.getMemUsedCapacity(); + Long cpu = host.getCpuReservedCapacity() + host.getCpuUsedCapacity(); + + Float memWithOverprovisioning = host.getTotalMemory() * ApiDBUtils.getMemOverprovisioningFactor(host.getClusterId()); + hostResponse.setMemoryTotal(memWithOverprovisioning.longValue()); + hostResponse.setMemWithOverprovisioning(decimalFormat.format(memWithOverprovisioning)); + hostResponse.setMemoryAllocated(mem); + hostResponse.setMemoryAllocatedBytes(mem); + hostResponse.setMemoryAllocatedPercentage(calculateResourceAllocatedPercentage(mem, memWithOverprovisioning)); + + hostResponse.setIsTagARule(host.getIsTagARule()); + hostResponse.setHaHost(containsHostHATag(hostTags)); - float cpuWithOverprovisioning = host.getCpus() * host.getSpeed() * cpuOverprovisioningFactor; - hostResponse.setCpuAllocatedValue(cpu); - String cpuAllocated = calculateResourceAllocatedPercentage(cpu, cpuWithOverprovisioning); - hostResponse.setCpuAllocated(cpuAllocated); - hostResponse.setCpuAllocatedPercentage(cpuAllocated); - hostResponse.setCpuAllocatedWithOverprovisioning(cpuAllocated); - hostResponse.setCpuWithOverprovisioning(decimalFormat.format(cpuWithOverprovisioning)); + if (host.getArch() != null) { + hostResponse.setArch(host.getArch().getType()); + } + + float cpuWithOverprovisioning = host.getCpus() * host.getSpeed() * cpuOverprovisioningFactor; + hostResponse.setCpuAllocatedValue(cpu); + String cpuAllocated = calculateResourceAllocatedPercentage(cpu, cpuWithOverprovisioning); + hostResponse.setCpuAllocated(cpuAllocated); + hostResponse.setCpuAllocatedPercentage(cpuAllocated); + hostResponse.setCpuAllocatedWithOverprovisioning(cpuAllocated); + hostResponse.setCpuWithOverprovisioning(decimalFormat.format(cpuWithOverprovisioning)); + } } if (details.contains(HostDetails.all) || details.contains(HostDetails.stats)) { @@ -257,27 +257,29 @@ private void setNewHostResponseBase(HostJoinVO host, EnumSet detail } } - Map hostDetails = hostDetailsDao.findDetails(host.getId()); - if (hostDetails != null) { - if (hostDetails.containsKey(Host.HOST_UEFI_ENABLE)) { - hostResponse.setUefiCapability(Boolean.parseBoolean((String) hostDetails.get(Host.HOST_UEFI_ENABLE))); - } else { - hostResponse.setUefiCapability(new Boolean(false)); + if (!details.contains(HostDetails.core)) { + Map hostDetails = hostDetailsDao.findDetails(host.getId()); + if (hostDetails != null) { + if (hostDetails.containsKey(Host.HOST_UEFI_ENABLE)) { + hostResponse.setUefiCapability(Boolean.parseBoolean((String) hostDetails.get(Host.HOST_UEFI_ENABLE))); + } else { + hostResponse.setUefiCapability(new Boolean(false)); + } } - } - if (details.contains(HostDetails.all) && - Arrays.asList(Hypervisor.HypervisorType.KVM, - Hypervisor.HypervisorType.Custom, - Hypervisor.HypervisorType.External).contains(host.getHypervisorType())) { - //only kvm has the requirement to return host details - try { - hostResponse.setDetails(hostDetails, host.getHypervisorType()); - } catch (Exception e) { - logger.debug("failed to get host details", e); + if (details.contains(HostDetails.all) && + Arrays.asList(Hypervisor.HypervisorType.KVM, + Hypervisor.HypervisorType.Custom, + Hypervisor.HypervisorType.External).contains(host.getHypervisorType())) { + //only kvm has the requirement to return host details + try { + hostResponse.setDetails(hostDetails, host.getHypervisorType()); + } catch (Exception e) { + logger.debug("failed to get host details", e); + } } } - } else if (host.getType() == Host.Type.SecondaryStorage) { + } else if (host.getType() == Host.Type.SecondaryStorage && !details.contains(HostDetails.core)) { StorageStats secStorageStats = ApiDBUtils.getSecondaryStorageStatistics(host.getId()); if (secStorageStats != null) { hostResponse.setDiskSizeTotal(secStorageStats.getCapacityBytes()); @@ -285,7 +287,16 @@ private void setNewHostResponseBase(HostJoinVO host, EnumSet detail } } - hostResponse.setLocalStorageActive(ApiDBUtils.isLocalStorageActiveOnHost(host.getId())); + if (!details.contains(HostDetails.core)) { + hostResponse.setLocalStorageActive(ApiDBUtils.isLocalStorageActiveOnHost(host.getId())); + hostResponse.setHostHAResponse(haConfigDao.findHAResource(host.getId(), HAResource.ResourceType.Host)); + hostResponse.setOutOfBandManagementResponse(outOfBandManagementDao.findByHost(host.getId())); + hostResponse.setHasAnnotation(annotationDao.hasAnnotations(host.getUuid(), AnnotationService.EntityType.HOST.name(), + accountManager.isRootAdmin(CallContext.current().getCallingAccount().getId()))); + hostResponse.setAnnotation(host.getAnnotation()); + hostResponse.setLastAnnotated(host.getLastAnnotated()); + hostResponse.setUsername(host.getUsername()); + } if (details.contains(HostDetails.all) || details.contains(HostDetails.events)) { Set possibleEvents = host.getStatus().getPossibleEvents(); @@ -303,8 +314,6 @@ private void setNewHostResponseBase(HostJoinVO host, EnumSet detail } } - hostResponse.setHostHAResponse(haConfigDao.findHAResource(host.getId(), HAResource.ResourceType.Host)); - hostResponse.setOutOfBandManagementResponse(outOfBandManagementDao.findByHost(host.getId())); hostResponse.setResourceState(host.getResourceState().toString()); // set async job @@ -312,15 +321,25 @@ private void setNewHostResponseBase(HostJoinVO host, EnumSet detail hostResponse.setJobId(host.getJobUuid()); hostResponse.setJobStatus(host.getJobStatus()); } - hostResponse.setHasAnnotation(annotationDao.hasAnnotations(host.getUuid(), AnnotationService.EntityType.HOST.name(), - accountManager.isRootAdmin(CallContext.current().getCallingAccount().getId()))); - hostResponse.setAnnotation(host.getAnnotation()); - hostResponse.setLastAnnotated(host.getLastAnnotated ()); - hostResponse.setUsername(host.getUsername()); hostResponse.setObjectName("host"); } + private void setManagementServerResponse(HostResponse hostResponse, HostJoinVO host, EnumSet details) { + if (host.getManagementServerId() != null) { + if (details.size() == 1 && details.contains(HostDetails.core)) { + // msid is returned as-is; callers resolve it to avoid a per-host lookup + hostResponse.setMsId(host.getManagementServerId()); + } else { + ManagementServerHostVO managementServer = managementServerHostDao.findByMsid(host.getManagementServerId()); + if (managementServer != null) { + hostResponse.setManagementServerId(managementServer.getUuid()); + hostResponse.setManagementServerName(managementServer.getName()); + } + } + } + } + @Override public HostResponse newMinimalHostResponse(HostJoinVO host) { HostResponse hostResponse = new HostResponse();