Skip to content

Commit 135922f

Browse files
nvazquezabennatan
andauthored
listHosts: add 'core' details (#13444)
* listHosts: add 'core' details * Include msid response and integrate core details into the base response method Co-authored-by: abennatan <a_bennatan@apple.com>
1 parent 348ceaf commit 135922f

3 files changed

Lines changed: 124 additions & 93 deletions

File tree

api/src/main/java/org/apache/cloudstack/api/ApiConstants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1407,7 +1407,7 @@ public String toString() {
14071407
}
14081408

14091409
public enum HostDetails {
1410-
all, capacity, events, stats, min;
1410+
all, capacity, core, events, stats, min;
14111411
}
14121412

14131413
public enum VMDetails {

api/src/main/java/org/apache/cloudstack/api/response/HostResponse.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,10 @@ public class HostResponse extends BaseResponseWithAnnotations {
197197
@Param(description = "the virtual machine id for host type ConsoleProxy and SecondaryStorageVM", since = "4.21.0")
198198
private String virtualMachineId;
199199

200+
@SerializedName("msid")
201+
@Param(description = "(only for details=core) the msid of the host's management server")
202+
private Long msId;
203+
200204
@SerializedName(ApiConstants.MANAGEMENT_SERVER_ID)
201205
@Param(description = "The management server ID of the host")
202206
private String managementServerId;
@@ -488,6 +492,14 @@ public void setVirtualMachineId(String virtualMachineId) {
488492
this.virtualMachineId = virtualMachineId;
489493
}
490494

495+
public Long getMsId() {
496+
return msId;
497+
}
498+
499+
public void setMsId(Long msId) {
500+
this.msId = msId;
501+
}
502+
491503
public void setManagementServerId(String managementServerId) {
492504
this.managementServerId = managementServerId;
493505
}

server/src/main/java/com/cloud/api/query/dao/HostJoinDaoImpl.java

Lines changed: 111 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -131,21 +131,16 @@ private void setNewHostResponseBase(HostJoinVO host, EnumSet<HostDetails> detail
131131
hostResponse.setHypervisor(hypervisorType);
132132
}
133133
hostResponse.setHostType(host.getType());
134-
if (host.getType().equals(Host.Type.ConsoleProxy) || host.getType().equals(Host.Type.SecondaryStorageVM)) {
134+
if (!details.contains(HostDetails.core) && (host.getType().equals(Host.Type.ConsoleProxy) || host.getType().equals(Host.Type.SecondaryStorageVM))) {
135135
VMInstanceVO vm = virtualMachineDao.findVMByInstanceNameIncludingRemoved(host.getName());
136136
if (vm != null) {
137137
hostResponse.setVirtualMachineId(vm.getUuid());
138138
}
139139
}
140140
hostResponse.setLastPinged(new Date(host.getLastPinged()));
141-
Long mshostId = host.getManagementServerId();
142-
if (mshostId != null) {
143-
ManagementServerHostVO managementServer = managementServerHostDao.findByMsid(host.getManagementServerId());
144-
if (managementServer != null) {
145-
hostResponse.setManagementServerId(managementServer.getUuid());
146-
hostResponse.setManagementServerName(managementServer.getName());
147-
}
148-
}
141+
142+
setManagementServerResponse(hostResponse, host, details);
143+
149144
hostResponse.setName(host.getName());
150145
hostResponse.setPodId(host.getPodUuid());
151146
hostResponse.setRemoved(host.getRemoved());
@@ -155,41 +150,44 @@ private void setNewHostResponseBase(HostJoinVO host, EnumSet<HostDetails> detail
155150
hostResponse.setVersion(host.getVersion());
156151
hostResponse.setCreated(host.getCreated());
157152

158-
List<HostGpuGroupsVO> gpuGroups = ApiDBUtils.getGpuGroups(host.getId());
159-
if (gpuGroups != null && !gpuGroups.isEmpty()) {
160-
List<GpuResponse> gpus = new ArrayList<GpuResponse>();
161-
long gpuRemaining = 0;
162-
long gpuTotal = 0;
163-
for (HostGpuGroupsVO entry : gpuGroups) {
164-
GpuResponse gpuResponse = new GpuResponse();
165-
gpuResponse.setGpuGroupName(entry.getGroupName());
166-
List<VGPUTypesVO> vgpuTypes = ApiDBUtils.getVgpus(entry.getId());
167-
if (vgpuTypes != null && !vgpuTypes.isEmpty()) {
168-
List<VgpuResponse> vgpus = new ArrayList<VgpuResponse>();
169-
for (VGPUTypesVO vgpuType : vgpuTypes) {
170-
VgpuResponse vgpuResponse = new VgpuResponse();
171-
vgpuResponse.setName(vgpuType.getVgpuType());
172-
vgpuResponse.setVideoRam(vgpuType.getVideoRam());
173-
vgpuResponse.setMaxHeads(vgpuType.getMaxHeads());
174-
vgpuResponse.setMaxResolutionX(vgpuType.getMaxResolutionX());
175-
vgpuResponse.setMaxResolutionY(vgpuType.getMaxResolutionY());
176-
vgpuResponse.setMaxVgpuPerPgpu(vgpuType.getMaxVgpuPerPgpu());
177-
vgpuResponse.setRemainingCapacity(vgpuType.getRemainingCapacity());
178-
vgpuResponse.setmaxCapacity(vgpuType.getMaxCapacity());
179-
vgpus.add(vgpuResponse);
180-
gpuRemaining += vgpuType.getRemainingCapacity();
181-
gpuTotal += vgpuType.getMaxCapacity();
153+
if (!details.contains(HostDetails.core)) {
154+
List<HostGpuGroupsVO> gpuGroups = ApiDBUtils.getGpuGroups(host.getId());
155+
if (gpuGroups != null && !gpuGroups.isEmpty()) {
156+
List<GpuResponse> gpus = new ArrayList<GpuResponse>();
157+
long gpuRemaining = 0;
158+
long gpuTotal = 0;
159+
for (HostGpuGroupsVO entry : gpuGroups) {
160+
GpuResponse gpuResponse = new GpuResponse();
161+
gpuResponse.setGpuGroupName(entry.getGroupName());
162+
List<VGPUTypesVO> vgpuTypes = ApiDBUtils.getVgpus(entry.getId());
163+
if (vgpuTypes != null && !vgpuTypes.isEmpty()) {
164+
List<VgpuResponse> vgpus = new ArrayList<VgpuResponse>();
165+
for (VGPUTypesVO vgpuType : vgpuTypes) {
166+
VgpuResponse vgpuResponse = new VgpuResponse();
167+
vgpuResponse.setName(vgpuType.getVgpuType());
168+
vgpuResponse.setVideoRam(vgpuType.getVideoRam());
169+
vgpuResponse.setMaxHeads(vgpuType.getMaxHeads());
170+
vgpuResponse.setMaxResolutionX(vgpuType.getMaxResolutionX());
171+
vgpuResponse.setMaxResolutionY(vgpuType.getMaxResolutionY());
172+
vgpuResponse.setMaxVgpuPerPgpu(vgpuType.getMaxVgpuPerPgpu());
173+
vgpuResponse.setRemainingCapacity(vgpuType.getRemainingCapacity());
174+
vgpuResponse.setmaxCapacity(vgpuType.getMaxCapacity());
175+
vgpus.add(vgpuResponse);
176+
gpuRemaining += vgpuType.getRemainingCapacity();
177+
gpuTotal += vgpuType.getMaxCapacity();
178+
}
179+
gpuResponse.setVgpu(vgpus);
182180
}
183-
gpuResponse.setVgpu(vgpus);
181+
gpus.add(gpuResponse);
184182
}
185-
gpus.add(gpuResponse);
183+
hostResponse.setGpuTotal(gpuTotal);
184+
hostResponse.setGpuUsed(gpuTotal - gpuRemaining);
185+
hostResponse.setGpuGroup(gpus);
186186
}
187-
hostResponse.setGpuTotal(gpuTotal);
188-
hostResponse.setGpuUsed(gpuTotal - gpuRemaining);
189-
hostResponse.setGpuGroup(gpus);
190187
}
191-
if (details.contains(HostDetails.all) || details.contains(HostDetails.capacity) || details.contains(HostDetails.stats) || details.contains(HostDetails.events)) {
192188

189+
if (details.contains(HostDetails.all) || details.contains(HostDetails.capacity) || details.contains(HostDetails.core) ||
190+
details.contains(HostDetails.stats) || details.contains(HostDetails.events)) {
193191
hostResponse.setOsCategoryId(host.getOsCategoryUuid());
194192
hostResponse.setOsCategoryName(host.getOsCategoryName());
195193
hostResponse.setZoneName(host.getZoneName());
@@ -202,43 +200,45 @@ private void setNewHostResponseBase(HostJoinVO host, EnumSet<HostDetails> detail
202200

203201
DecimalFormat decimalFormat = new DecimalFormat("#.##");
204202
if (host.getType() == Host.Type.Routing) {
205-
float cpuOverprovisioningFactor = ApiDBUtils.getCpuOverprovisioningFactor(host.getClusterId());
206-
if (details.contains(HostDetails.all) || details.contains(HostDetails.capacity)) {
207-
// set allocated capacities
208-
Long mem = host.getMemReservedCapacity() + host.getMemUsedCapacity();
209-
Long cpu = host.getCpuReservedCapacity() + host.getCpuUsedCapacity();
210-
211-
Float memWithOverprovisioning = host.getTotalMemory() * ApiDBUtils.getMemOverprovisioningFactor(host.getClusterId());
212-
hostResponse.setMemoryTotal(memWithOverprovisioning.longValue());
213-
hostResponse.setMemWithOverprovisioning(decimalFormat.format(memWithOverprovisioning));
214-
hostResponse.setMemoryAllocated(mem);
215-
hostResponse.setMemoryAllocatedBytes(mem);
216-
hostResponse.setMemoryAllocatedPercentage(calculateResourceAllocatedPercentage(mem, memWithOverprovisioning));
217-
203+
if (details.contains(HostDetails.all) || details.contains(HostDetails.capacity) || details.contains(HostDetails.core)) {
218204
String hostTags = host.getTag();
219205
hostResponse.setHostTags(hostTags);
220-
hostResponse.setIsTagARule(host.getIsTagARule());
221-
hostResponse.setHaHost(containsHostHATag(hostTags));
222206
hostResponse.setExplicitHostTags(host.getExplicitTag());
223207
hostResponse.setImplicitHostTags(host.getImplicitTag());
224-
225-
hostResponse.setHypervisorVersion(host.getHypervisorVersion());
226-
if (host.getArch() != null) {
227-
hostResponse.setArch(host.getArch().getType());
228-
}
229-
230208
hostResponse.setStorageAccessGroups(host.getStorageAccessGroups());
231209
hostResponse.setClusterStorageAccessGroups(host.getClusterStorageAccessGroups());
232210
hostResponse.setPodStorageAccessGroups(host.getPodStorageAccessGroups());
233211
hostResponse.setZoneStorageAccessGroups(host.getZoneStorageAccessGroups());
212+
hostResponse.setHypervisorVersion(host.getHypervisorVersion());
213+
214+
if (!details.contains(HostDetails.core)) {
215+
float cpuOverprovisioningFactor = ApiDBUtils.getCpuOverprovisioningFactor(host.getClusterId());
216+
// set allocated capacities
217+
Long mem = host.getMemReservedCapacity() + host.getMemUsedCapacity();
218+
Long cpu = host.getCpuReservedCapacity() + host.getCpuUsedCapacity();
219+
220+
Float memWithOverprovisioning = host.getTotalMemory() * ApiDBUtils.getMemOverprovisioningFactor(host.getClusterId());
221+
hostResponse.setMemoryTotal(memWithOverprovisioning.longValue());
222+
hostResponse.setMemWithOverprovisioning(decimalFormat.format(memWithOverprovisioning));
223+
hostResponse.setMemoryAllocated(mem);
224+
hostResponse.setMemoryAllocatedBytes(mem);
225+
hostResponse.setMemoryAllocatedPercentage(calculateResourceAllocatedPercentage(mem, memWithOverprovisioning));
226+
227+
hostResponse.setIsTagARule(host.getIsTagARule());
228+
hostResponse.setHaHost(containsHostHATag(hostTags));
234229

235-
float cpuWithOverprovisioning = host.getCpus() * host.getSpeed() * cpuOverprovisioningFactor;
236-
hostResponse.setCpuAllocatedValue(cpu);
237-
String cpuAllocated = calculateResourceAllocatedPercentage(cpu, cpuWithOverprovisioning);
238-
hostResponse.setCpuAllocated(cpuAllocated);
239-
hostResponse.setCpuAllocatedPercentage(cpuAllocated);
240-
hostResponse.setCpuAllocatedWithOverprovisioning(cpuAllocated);
241-
hostResponse.setCpuWithOverprovisioning(decimalFormat.format(cpuWithOverprovisioning));
230+
if (host.getArch() != null) {
231+
hostResponse.setArch(host.getArch().getType());
232+
}
233+
234+
float cpuWithOverprovisioning = host.getCpus() * host.getSpeed() * cpuOverprovisioningFactor;
235+
hostResponse.setCpuAllocatedValue(cpu);
236+
String cpuAllocated = calculateResourceAllocatedPercentage(cpu, cpuWithOverprovisioning);
237+
hostResponse.setCpuAllocated(cpuAllocated);
238+
hostResponse.setCpuAllocatedPercentage(cpuAllocated);
239+
hostResponse.setCpuAllocatedWithOverprovisioning(cpuAllocated);
240+
hostResponse.setCpuWithOverprovisioning(decimalFormat.format(cpuWithOverprovisioning));
241+
}
242242
}
243243

244244
if (details.contains(HostDetails.all) || details.contains(HostDetails.stats)) {
@@ -257,35 +257,46 @@ private void setNewHostResponseBase(HostJoinVO host, EnumSet<HostDetails> detail
257257
}
258258
}
259259

260-
Map<String, String> hostDetails = hostDetailsDao.findDetails(host.getId());
261-
if (hostDetails != null) {
262-
if (hostDetails.containsKey(Host.HOST_UEFI_ENABLE)) {
263-
hostResponse.setUefiCapability(Boolean.parseBoolean((String) hostDetails.get(Host.HOST_UEFI_ENABLE)));
264-
} else {
265-
hostResponse.setUefiCapability(new Boolean(false));
260+
if (!details.contains(HostDetails.core)) {
261+
Map<String, String> hostDetails = hostDetailsDao.findDetails(host.getId());
262+
if (hostDetails != null) {
263+
if (hostDetails.containsKey(Host.HOST_UEFI_ENABLE)) {
264+
hostResponse.setUefiCapability(Boolean.parseBoolean((String) hostDetails.get(Host.HOST_UEFI_ENABLE)));
265+
} else {
266+
hostResponse.setUefiCapability(new Boolean(false));
267+
}
266268
}
267-
}
268-
if (details.contains(HostDetails.all) &&
269-
Arrays.asList(Hypervisor.HypervisorType.KVM,
270-
Hypervisor.HypervisorType.Custom,
271-
Hypervisor.HypervisorType.External).contains(host.getHypervisorType())) {
272-
//only kvm has the requirement to return host details
273-
try {
274-
hostResponse.setDetails(hostDetails, host.getHypervisorType());
275-
} catch (Exception e) {
276-
logger.debug("failed to get host details", e);
269+
if (details.contains(HostDetails.all) &&
270+
Arrays.asList(Hypervisor.HypervisorType.KVM,
271+
Hypervisor.HypervisorType.Custom,
272+
Hypervisor.HypervisorType.External).contains(host.getHypervisorType())) {
273+
//only kvm has the requirement to return host details
274+
try {
275+
hostResponse.setDetails(hostDetails, host.getHypervisorType());
276+
} catch (Exception e) {
277+
logger.debug("failed to get host details", e);
278+
}
277279
}
278280
}
279281

280-
} else if (host.getType() == Host.Type.SecondaryStorage) {
282+
} else if (host.getType() == Host.Type.SecondaryStorage && !details.contains(HostDetails.core)) {
281283
StorageStats secStorageStats = ApiDBUtils.getSecondaryStorageStatistics(host.getId());
282284
if (secStorageStats != null) {
283285
hostResponse.setDiskSizeTotal(secStorageStats.getCapacityBytes());
284286
hostResponse.setDiskSizeAllocated(secStorageStats.getByteUsed());
285287
}
286288
}
287289

288-
hostResponse.setLocalStorageActive(ApiDBUtils.isLocalStorageActiveOnHost(host.getId()));
290+
if (!details.contains(HostDetails.core)) {
291+
hostResponse.setLocalStorageActive(ApiDBUtils.isLocalStorageActiveOnHost(host.getId()));
292+
hostResponse.setHostHAResponse(haConfigDao.findHAResource(host.getId(), HAResource.ResourceType.Host));
293+
hostResponse.setOutOfBandManagementResponse(outOfBandManagementDao.findByHost(host.getId()));
294+
hostResponse.setHasAnnotation(annotationDao.hasAnnotations(host.getUuid(), AnnotationService.EntityType.HOST.name(),
295+
accountManager.isRootAdmin(CallContext.current().getCallingAccount().getId())));
296+
hostResponse.setAnnotation(host.getAnnotation());
297+
hostResponse.setLastAnnotated(host.getLastAnnotated());
298+
hostResponse.setUsername(host.getUsername());
299+
}
289300

290301
if (details.contains(HostDetails.all) || details.contains(HostDetails.events)) {
291302
Set<com.cloud.host.Status.Event> possibleEvents = host.getStatus().getPossibleEvents();
@@ -303,24 +314,32 @@ private void setNewHostResponseBase(HostJoinVO host, EnumSet<HostDetails> detail
303314
}
304315
}
305316

306-
hostResponse.setHostHAResponse(haConfigDao.findHAResource(host.getId(), HAResource.ResourceType.Host));
307-
hostResponse.setOutOfBandManagementResponse(outOfBandManagementDao.findByHost(host.getId()));
308317
hostResponse.setResourceState(host.getResourceState().toString());
309318

310319
// set async job
311320
if (host.getJobId() != null) {
312321
hostResponse.setJobId(host.getJobUuid());
313322
hostResponse.setJobStatus(host.getJobStatus());
314323
}
315-
hostResponse.setHasAnnotation(annotationDao.hasAnnotations(host.getUuid(), AnnotationService.EntityType.HOST.name(),
316-
accountManager.isRootAdmin(CallContext.current().getCallingAccount().getId())));
317-
hostResponse.setAnnotation(host.getAnnotation());
318-
hostResponse.setLastAnnotated(host.getLastAnnotated ());
319-
hostResponse.setUsername(host.getUsername());
320324

321325
hostResponse.setObjectName("host");
322326
}
323327

328+
private void setManagementServerResponse(HostResponse hostResponse, HostJoinVO host, EnumSet<HostDetails> details) {
329+
if (host.getManagementServerId() != null) {
330+
if (details.size() == 1 && details.contains(HostDetails.core)) {
331+
// msid is returned as-is; callers resolve it to avoid a per-host lookup
332+
hostResponse.setMsId(host.getManagementServerId());
333+
} else {
334+
ManagementServerHostVO managementServer = managementServerHostDao.findByMsid(host.getManagementServerId());
335+
if (managementServer != null) {
336+
hostResponse.setManagementServerId(managementServer.getUuid());
337+
hostResponse.setManagementServerName(managementServer.getName());
338+
}
339+
}
340+
}
341+
}
342+
324343
@Override
325344
public HostResponse newMinimalHostResponse(HostJoinVO host) {
326345
HostResponse hostResponse = new HostResponse();

0 commit comments

Comments
 (0)