Skip to content

Commit 83caf10

Browse files
committed
Refactor
1 parent d9928c5 commit 83caf10

File tree

2 files changed

+20
-87
lines changed

2 files changed

+20
-87
lines changed

server/src/main/java/com/cloud/server/ManagementServerImpl.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3038,28 +3038,41 @@ public Pair<List<? extends GuestOSHypervisor>, Integer> listGuestOSMappingByCrit
30383038
throw new InvalidParameterValueException("Hypervisor version parameter cannot be used without specifying a hypervisor : XenServer, KVM or VMware");
30393039
}
30403040

3041-
final SearchCriteria<GuestOSHypervisorVO> sc = _guestOSHypervisorDao.createSearchCriteria();
3041+
SearchBuilder<GuestOSHypervisorVO> sb = _guestOSHypervisorDao.createSearchBuilder();
3042+
sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
3043+
sb.and("guestOsName", sb.entity().getGuestOsName(), SearchCriteria.Op.LIKE);
3044+
sb.and("hypervisorType", sb.entity().getHypervisorType(), SearchCriteria.Op.LIKE);
3045+
sb.and("hypervisorVersion", sb.entity().getHypervisorVersion(), SearchCriteria.Op.LIKE);
3046+
sb.and(guestOsId, sb.entity().getGuestOsId(), SearchCriteria.Op.EQ);
3047+
SearchBuilder<GuestOSVO> guestOSSearch = _guestOSDao.createSearchBuilder();
3048+
guestOSSearch.and("display", guestOSSearch.entity().isDisplay(), SearchCriteria.Op.LIKE);
3049+
sb.join("guestOSSearch", guestOSSearch, sb.entity().getGuestOsId(), guestOSSearch.entity().getId(), JoinBuilder.JoinType.INNER);
3050+
3051+
final SearchCriteria<GuestOSHypervisorVO> sc = sb.create();
30423052

30433053
if (id != null) {
3044-
sc.addAnd("id", SearchCriteria.Op.EQ, id);
3054+
sc.setParameters("id", SearchCriteria.Op.EQ, id);
30453055
}
30463056

30473057
if (osTypeId != null) {
3048-
sc.addAnd(guestOsId, SearchCriteria.Op.EQ, osTypeId);
3058+
sc.setParameters(guestOsId, osTypeId);
30493059
}
30503060

30513061
if (osNameForHypervisor != null) {
3052-
sc.addAnd("guestOsName", SearchCriteria.Op.LIKE, "%" + osNameForHypervisor + "%");
3062+
sc.setParameters("guestOsName", "%" + osNameForHypervisor + "%");
30533063
}
30543064

30553065
if (hypervisor != null) {
3056-
sc.addAnd("hypervisorType", SearchCriteria.Op.LIKE, "%" + hypervisor + "%");
3066+
sc.setParameters("hypervisorType", "%" + hypervisor + "%");
30573067
}
30583068

30593069
if (hypervisorVersion != null) {
3060-
sc.addAnd("hypervisorVersion", SearchCriteria.Op.LIKE, "%" + hypervisorVersion + "%");
3070+
sc.setParameters("hypervisorVersion", "%" + hypervisorVersion + "%");
30613071
}
30623072

3073+
// Exclude the mappings for guest OS marked as display = false
3074+
sc.setJoinParameters("guestOSSearch", "display", true);
3075+
30633076
if (osDisplayName != null) {
30643077
List<GuestOSVO> guestOSVOS = _guestOSDao.listLikeDisplayName(osDisplayName);
30653078
if (CollectionUtils.isNotEmpty(guestOSVOS)) {

server/src/main/java/org/apache/cloudstack/vm/UnmanagedVMsManagerImpl.java

Lines changed: 1 addition & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,6 @@
155155
import org.apache.cloudstack.api.command.admin.vm.ListVmsForImportCmd;
156156
import org.apache.cloudstack.api.command.admin.vm.UnmanageVMInstanceCmd;
157157
import org.apache.cloudstack.api.response.ListResponse;
158-
import org.apache.cloudstack.api.response.NicResponse;
159-
import org.apache.cloudstack.api.response.UnmanagedInstanceDiskResponse;
160158
import org.apache.cloudstack.api.response.UnmanagedInstanceResponse;
161159
import org.apache.cloudstack.api.response.UserVmResponse;
162160
import org.apache.cloudstack.context.CallContext;
@@ -341,83 +339,6 @@ private VMTemplateVO createDefaultDummyVmImportTemplate(boolean isKVM) {
341339
return template;
342340
}
343341

344-
private UnmanagedInstanceResponse createUnmanagedInstanceResponse(UnmanagedInstanceTO instance, Cluster cluster, Host host) {
345-
UnmanagedInstanceResponse response = new UnmanagedInstanceResponse();
346-
response.setName(instance.getName());
347-
348-
if (cluster != null) {
349-
response.setClusterId(cluster.getUuid());
350-
}
351-
if (host != null) {
352-
response.setHostId(host.getUuid());
353-
response.setHostName(host.getName());
354-
if (host.getHypervisorType() != null) {
355-
response.setHypervisor(host.getHypervisorType().toString());
356-
}
357-
response.setHypervisorVersion(host.getHypervisorVersion());
358-
} else {
359-
// In case the unmanaged instance is on an external host
360-
if (instance.getHostName() != null) {
361-
response.setHostName(instance.getHostName());
362-
}
363-
if (instance.getHostHypervisorVersion() != null) {
364-
response.setHypervisorVersion(instance.getHostHypervisorVersion());
365-
}
366-
if (instance.getHypervisorType() != null) {
367-
response.setHypervisor(instance.getHypervisorType());
368-
}
369-
}
370-
response.setPowerState(instance.getPowerState().toString());
371-
response.setCpuCores(instance.getCpuCores());
372-
response.setCpuSpeed(instance.getCpuSpeed());
373-
response.setCpuCoresPerSocket(instance.getCpuCoresPerSocket());
374-
response.setMemory(instance.getMemory());
375-
response.setOperatingSystemId(instance.getOperatingSystemId());
376-
response.setOperatingSystem(instance.getOperatingSystem());
377-
response.setObjectName("unmanagedinstance");
378-
379-
if (instance.getDisks() != null) {
380-
for (UnmanagedInstanceTO.Disk disk : instance.getDisks()) {
381-
UnmanagedInstanceDiskResponse diskResponse = new UnmanagedInstanceDiskResponse();
382-
diskResponse.setDiskId(disk.getDiskId());
383-
if (StringUtils.isNotEmpty(disk.getLabel())) {
384-
diskResponse.setLabel(disk.getLabel());
385-
}
386-
diskResponse.setCapacity(disk.getCapacity());
387-
diskResponse.setController(disk.getController());
388-
diskResponse.setControllerUnit(disk.getControllerUnit());
389-
diskResponse.setPosition(disk.getPosition());
390-
diskResponse.setImagePath(disk.getImagePath());
391-
diskResponse.setDatastoreName(disk.getDatastoreName());
392-
diskResponse.setDatastoreHost(disk.getDatastoreHost());
393-
diskResponse.setDatastorePath(disk.getDatastorePath());
394-
diskResponse.setDatastoreType(disk.getDatastoreType());
395-
response.addDisk(diskResponse);
396-
}
397-
}
398-
399-
if (instance.getNics() != null) {
400-
for (UnmanagedInstanceTO.Nic nic : instance.getNics()) {
401-
NicResponse nicResponse = new NicResponse();
402-
nicResponse.setId(nic.getNicId());
403-
nicResponse.setNetworkName(nic.getNetwork());
404-
nicResponse.setMacAddress(nic.getMacAddress());
405-
if (StringUtils.isNotEmpty(nic.getAdapterType())) {
406-
nicResponse.setAdapterType(nic.getAdapterType());
407-
}
408-
if (!CollectionUtils.isEmpty(nic.getIpAddress())) {
409-
nicResponse.setIpAddresses(nic.getIpAddress());
410-
}
411-
nicResponse.setVlanId(nic.getVlan());
412-
nicResponse.setIsolatedPvlanId(nic.getPvlan());
413-
nicResponse.setIsolatedPvlanType(nic.getPvlanType());
414-
response.addNic(nicResponse);
415-
}
416-
}
417-
418-
return response;
419-
}
420-
421342
private List<String> getAdditionalNameFilters(Cluster cluster) {
422343
List<String> additionalNameFilter = new ArrayList<>();
423344
if (cluster == null) {
@@ -3016,9 +2937,8 @@ public ListResponse<UnmanagedInstanceResponse> listVmsForImport(ListVmsForImport
30162937
!instance.getName().toLowerCase().contains(keyword)) {
30172938
continue;
30182939
}
3019-
responses.add(createUnmanagedInstanceResponse(instance, null, null));
2940+
responses.add(responseGenerator.createUnmanagedInstanceResponse(instance, null, null));
30202941
}
3021-
30222942
ListResponse<UnmanagedInstanceResponse> listResponses = new ListResponse<>();
30232943
listResponses.setResponses(responses, responses.size());
30242944
return listResponses;

0 commit comments

Comments
 (0)