use of org.apache.cloudstack.api.command.admin.systemvm.ListSystemVMsCmd in project cloudstack by apache.
the class ManagementServerImpl method searchForSystemVm.
@Override
public Pair<List<? extends VirtualMachine>, Integer> searchForSystemVm(final ListSystemVMsCmd cmd) {
final String type = cmd.getSystemVmType();
final Long zoneId = _accountMgr.checkAccessAndSpecifyAuthority(CallContext.current().getCallingAccount(), cmd.getZoneId());
final Long id = cmd.getId();
final String name = cmd.getSystemVmName();
final String state = cmd.getState();
final String keyword = cmd.getKeyword();
final Long podId = cmd.getPodId();
final Long hostId = cmd.getHostId();
final Long storageId = cmd.getStorageId();
final Filter searchFilter = new Filter(VMInstanceVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal());
final SearchBuilder<VMInstanceVO> sb = _vmInstanceDao.createSearchBuilder();
sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
sb.and("hostName", sb.entity().getHostName(), SearchCriteria.Op.LIKE);
sb.and("state", sb.entity().getState(), SearchCriteria.Op.EQ);
sb.and("dataCenterId", sb.entity().getDataCenterId(), SearchCriteria.Op.EQ);
sb.and("podId", sb.entity().getPodIdToDeployIn(), SearchCriteria.Op.EQ);
sb.and("hostId", sb.entity().getHostId(), SearchCriteria.Op.EQ);
sb.and("type", sb.entity().getType(), SearchCriteria.Op.EQ);
sb.and("nulltype", sb.entity().getType(), SearchCriteria.Op.IN);
if (storageId != null) {
StoragePoolVO storagePool = _primaryDataStoreDao.findById(storageId);
if (storagePool.getPoolType() == Storage.StoragePoolType.DatastoreCluster) {
final SearchBuilder<VolumeVO> volumeSearch = _volumeDao.createSearchBuilder();
volumeSearch.and("poolId", volumeSearch.entity().getPoolId(), SearchCriteria.Op.IN);
sb.join("volumeSearch", volumeSearch, sb.entity().getId(), volumeSearch.entity().getInstanceId(), JoinBuilder.JoinType.INNER);
} else {
final SearchBuilder<VolumeVO> volumeSearch = _volumeDao.createSearchBuilder();
volumeSearch.and("poolId", volumeSearch.entity().getPoolId(), SearchCriteria.Op.EQ);
sb.join("volumeSearch", volumeSearch, sb.entity().getId(), volumeSearch.entity().getInstanceId(), JoinBuilder.JoinType.INNER);
}
}
final SearchCriteria<VMInstanceVO> sc = sb.create();
if (keyword != null) {
final SearchCriteria<VMInstanceVO> ssc = _vmInstanceDao.createSearchCriteria();
ssc.addOr("hostName", SearchCriteria.Op.LIKE, "%" + keyword + "%");
ssc.addOr("state", SearchCriteria.Op.LIKE, "%" + keyword + "%");
sc.addAnd("hostName", SearchCriteria.Op.SC, ssc);
}
if (id != null) {
sc.setParameters("id", id);
}
if (name != null) {
sc.setParameters("hostName", name);
}
if (state != null) {
sc.setParameters("state", state);
}
if (zoneId != null) {
sc.setParameters("dataCenterId", zoneId);
}
if (podId != null) {
sc.setParameters("podId", podId);
}
if (hostId != null) {
sc.setParameters("hostId", hostId);
}
if (type != null) {
sc.setParameters("type", type);
} else {
sc.setParameters("nulltype", VirtualMachine.Type.SecondaryStorageVm, VirtualMachine.Type.ConsoleProxy);
}
if (storageId != null) {
StoragePoolVO storagePool = _primaryDataStoreDao.findById(storageId);
if (storagePool.getPoolType() == Storage.StoragePoolType.DatastoreCluster) {
List<StoragePoolVO> childDataStores = _primaryDataStoreDao.listChildStoragePoolsInDatastoreCluster(storageId);
List<Long> childDatastoreIds = childDataStores.stream().map(mo -> mo.getId()).collect(Collectors.toList());
sc.setJoinParameters("volumeSearch", "poolId", childDatastoreIds.toArray());
} else {
sc.setJoinParameters("volumeSearch", "poolId", storageId);
}
}
final Pair<List<VMInstanceVO>, Integer> result = _vmInstanceDao.searchAndCount(sc, searchFilter);
return new Pair<List<? extends VirtualMachine>, Integer>(result.first(), result.second());
}
Aggregations