use of org.apache.cloudstack.api.command.admin.vm.ListVMsCmdByAdmin in project cloudstack by apache.
the class QueryManagerImpl method searchForUserVMsInternal.
private Pair<List<UserVmJoinVO>, Integer> searchForUserVMsInternal(ListVMsCmd cmd) {
Account caller = CallContext.current().getCallingAccount();
List<Long> permittedAccounts = new ArrayList<Long>();
boolean listAll = cmd.listAll();
Long id = cmd.getId();
Long userId = cmd.getUserId();
Map<String, String> tags = cmd.getTags();
Boolean display = cmd.getDisplay();
Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<Long, Boolean, ListProjectResourcesCriteria>(cmd.getDomainId(), cmd.isRecursive(), null);
_accountMgr.buildACLSearchParameters(caller, id, cmd.getAccountName(), cmd.getProjectId(), permittedAccounts, domainIdRecursiveListProject, listAll, false);
Long domainId = domainIdRecursiveListProject.first();
Boolean isRecursive = domainIdRecursiveListProject.second();
ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third();
Filter searchFilter = new Filter(UserVmJoinVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal());
List<Long> ids = null;
if (cmd.getId() != null) {
if (cmd.getIds() != null && !cmd.getIds().isEmpty()) {
throw new InvalidParameterValueException("Specify either id or ids but not both parameters");
}
ids = new ArrayList<Long>();
ids.add(cmd.getId());
} else {
ids = cmd.getIds();
}
// first search distinct vm id by using query criteria and pagination
SearchBuilder<UserVmJoinVO> sb = _userVmJoinDao.createSearchBuilder();
// select distinct ids
sb.select(null, Func.DISTINCT, sb.entity().getId());
_accountMgr.buildACLViewSearchBuilder(sb, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
String hypervisor = cmd.getHypervisor();
Object name = cmd.getName();
String state = cmd.getState();
Object zoneId = cmd.getZoneId();
Object keyword = cmd.getKeyword();
boolean isAdmin = false;
boolean isRootAdmin = false;
if (_accountMgr.isAdmin(caller.getId())) {
isAdmin = true;
}
if (_accountMgr.isRootAdmin(caller.getId())) {
isRootAdmin = true;
}
Object groupId = cmd.getGroupId();
Object networkId = cmd.getNetworkId();
if (HypervisorType.getType(hypervisor) == HypervisorType.None && hypervisor != null) {
// invalid hypervisor type input
throw new InvalidParameterValueException("Invalid HypervisorType " + hypervisor);
}
Object templateId = cmd.getTemplateId();
Object isoId = cmd.getIsoId();
Object vpcId = cmd.getVpcId();
Object affinityGroupId = cmd.getAffinityGroupId();
Object keyPairName = cmd.getKeyPairName();
Object serviceOffId = cmd.getServiceOfferingId();
Object securityGroupId = cmd.getSecurityGroupId();
Object backupOfferingId = cmd.getBackupOfferingId();
Object isHaEnabled = cmd.getHaEnabled();
Object pod = null;
Long clusterId = null;
Object hostId = null;
Object storageId = null;
if (_accountMgr.isRootAdmin(caller.getId())) {
pod = cmd.getPodId();
if (cmd instanceof ListVMsCmdByAdmin) {
clusterId = ((ListVMsCmdByAdmin) cmd).getClusterId();
}
hostId = cmd.getHostId();
storageId = cmd.getStorageId();
}
sb.and("displayName", sb.entity().getDisplayName(), SearchCriteria.Op.LIKE);
sb.and("idIN", sb.entity().getId(), SearchCriteria.Op.IN);
sb.and("name", sb.entity().getName(), SearchCriteria.Op.LIKE);
sb.and("stateEQ", sb.entity().getState(), SearchCriteria.Op.EQ);
sb.and("stateNEQ", sb.entity().getState(), SearchCriteria.Op.NEQ);
sb.and("stateNIN", sb.entity().getState(), SearchCriteria.Op.NIN);
sb.and("dataCenterId", sb.entity().getDataCenterId(), SearchCriteria.Op.EQ);
sb.and("podId", sb.entity().getPodId(), SearchCriteria.Op.EQ);
sb.and("clusterId", sb.entity().getClusterId(), SearchCriteria.Op.EQ);
sb.and("hypervisorType", sb.entity().getHypervisorType(), SearchCriteria.Op.EQ);
sb.and("hostIdEQ", sb.entity().getHostId(), SearchCriteria.Op.EQ);
sb.and("templateId", sb.entity().getTemplateId(), SearchCriteria.Op.EQ);
sb.and("isoId", sb.entity().getIsoId(), SearchCriteria.Op.EQ);
sb.and("instanceGroupId", sb.entity().getInstanceGroupId(), SearchCriteria.Op.EQ);
if (serviceOffId != null) {
sb.and("serviceOfferingId", sb.entity().getServiceOfferingId(), SearchCriteria.Op.EQ);
}
if (backupOfferingId != null) {
sb.and("backupOfferingId", sb.entity().getBackupOfferingId(), SearchCriteria.Op.EQ);
}
if (display != null) {
sb.and("display", sb.entity().isDisplayVm(), SearchCriteria.Op.EQ);
}
if (isHaEnabled != null) {
sb.and("haEnabled", sb.entity().isHaEnabled(), SearchCriteria.Op.EQ);
}
if (groupId != null && (Long) groupId != -1) {
sb.and("instanceGroupId", sb.entity().getInstanceGroupId(), SearchCriteria.Op.EQ);
}
if (userId != null) {
sb.and("userId", sb.entity().getUserId(), SearchCriteria.Op.EQ);
}
if (networkId != null) {
sb.and("networkId", sb.entity().getNetworkId(), SearchCriteria.Op.EQ);
}
if (vpcId != null && networkId == null) {
sb.and("vpcId", sb.entity().getVpcId(), SearchCriteria.Op.EQ);
}
if (storageId != null) {
StoragePoolVO poolVO = _storagePoolDao.findById((Long) storageId);
if (poolVO.getPoolType() == Storage.StoragePoolType.DatastoreCluster) {
sb.and("poolId", sb.entity().getPoolId(), SearchCriteria.Op.IN);
} else {
sb.and("poolId", sb.entity().getPoolId(), SearchCriteria.Op.EQ);
}
}
if (affinityGroupId != null) {
sb.and("affinityGroupId", sb.entity().getAffinityGroupId(), SearchCriteria.Op.EQ);
}
if (keyPairName != null) {
sb.and("keyPairName", sb.entity().getKeypairName(), SearchCriteria.Op.EQ);
}
if (!isRootAdmin) {
sb.and("displayVm", sb.entity().isDisplayVm(), SearchCriteria.Op.EQ);
}
if (securityGroupId != null) {
sb.and("securityGroupId", sb.entity().getSecurityGroupId(), SearchCriteria.Op.EQ);
}
// populate the search criteria with the values passed in
SearchCriteria<UserVmJoinVO> sc = sb.create();
// building ACL condition
_accountMgr.buildACLViewSearchCriteria(sc, domainId, isRecursive, permittedAccounts, listProjectResourcesCriteria);
if (tags != null && !tags.isEmpty()) {
SearchCriteria<UserVmJoinVO> tagSc = _userVmJoinDao.createSearchCriteria();
for (Map.Entry<String, String> entry : tags.entrySet()) {
SearchCriteria<UserVmJoinVO> tsc = _userVmJoinDao.createSearchCriteria();
tsc.addAnd("tagKey", SearchCriteria.Op.EQ, entry.getKey());
tsc.addAnd("tagValue", SearchCriteria.Op.EQ, entry.getValue());
tagSc.addOr("tagKey", SearchCriteria.Op.SC, tsc);
}
sc.addAnd("tagKey", SearchCriteria.Op.SC, tagSc);
}
if (groupId != null && (Long) groupId != -1) {
sc.setParameters("instanceGroupId", groupId);
}
if (keyword != null) {
SearchCriteria<UserVmJoinVO> ssc = _userVmJoinDao.createSearchCriteria();
ssc.addOr("displayName", SearchCriteria.Op.LIKE, "%" + keyword + "%");
ssc.addOr("name", SearchCriteria.Op.LIKE, "%" + keyword + "%");
if (isRootAdmin) {
ssc.addOr("instanceName", SearchCriteria.Op.LIKE, "%" + keyword + "%");
}
ssc.addOr("state", SearchCriteria.Op.EQ, keyword);
sc.addAnd("displayName", SearchCriteria.Op.SC, ssc);
}
if (serviceOffId != null) {
sc.setParameters("serviceOfferingId", serviceOffId);
}
if (backupOfferingId != null) {
sc.setParameters("backupOfferingId", backupOfferingId);
}
if (securityGroupId != null) {
sc.setParameters("securityGroupId", securityGroupId);
}
if (display != null) {
sc.setParameters("display", display);
}
if (isHaEnabled != null) {
sc.setParameters("haEnabled", isHaEnabled);
}
if (ids != null && !ids.isEmpty()) {
sc.setParameters("idIN", ids.toArray());
}
if (templateId != null) {
sc.setParameters("templateId", templateId);
}
if (isoId != null) {
sc.setParameters("isoId", isoId);
}
if (userId != null) {
sc.setParameters("userId", userId);
}
if (networkId != null) {
sc.setParameters("networkId", networkId);
}
if (vpcId != null && networkId == null) {
sc.setParameters("vpcId", vpcId);
}
if (name != null) {
sc.setParameters("name", "%" + name + "%");
}
if (state != null) {
if (state.equalsIgnoreCase("present")) {
sc.setParameters("stateNIN", "Destroyed", "Expunging");
} else {
sc.setParameters("stateEQ", state);
}
}
if (hypervisor != null) {
sc.setParameters("hypervisorType", hypervisor);
}
// Don't show Destroyed and Expunging vms to the end user if the AllowUserViewDestroyedVM flag is not set.
if (!isAdmin && !AllowUserViewDestroyedVM.valueIn(caller.getAccountId())) {
sc.setParameters("stateNIN", "Destroyed", "Expunging");
}
if (zoneId != null) {
sc.setParameters("dataCenterId", zoneId);
}
if (affinityGroupId != null) {
sc.setParameters("affinityGroupId", affinityGroupId);
}
if (keyPairName != null) {
sc.setParameters("keyPairName", keyPairName);
}
if (_accountMgr.isRootAdmin(caller.getId())) {
if (cmd.getPodId() != null) {
sc.setParameters("podId", pod);
if (state == null) {
sc.setParameters("stateNEQ", "Destroyed");
}
}
if (clusterId != null) {
sc.setParameters("clusterId", clusterId);
}
if (hostId != null) {
sc.setParameters("hostIdEQ", hostId);
}
if (storageId != null) {
StoragePoolVO poolVO = _storagePoolDao.findById((Long) storageId);
if (poolVO.getPoolType() == Storage.StoragePoolType.DatastoreCluster) {
List<StoragePoolVO> childDatastores = _storagePoolDao.listChildStoragePoolsInDatastoreCluster((Long) storageId);
List<Long> childDatastoreIds = childDatastores.stream().map(mo -> mo.getId()).collect(Collectors.toList());
sc.setParameters("poolId", childDatastoreIds.toArray());
} else {
sc.setParameters("poolId", storageId);
}
}
}
if (!isRootAdmin) {
sc.setParameters("displayVm", 1);
}
// search vm details by ids
Pair<List<UserVmJoinVO>, Integer> uniqueVmPair = _userVmJoinDao.searchAndDistinctCount(sc, searchFilter);
Integer count = uniqueVmPair.second();
if (count.intValue() == 0) {
// handle empty result cases
return uniqueVmPair;
}
List<UserVmJoinVO> uniqueVms = uniqueVmPair.first();
Long[] vmIds = new Long[uniqueVms.size()];
int i = 0;
for (UserVmJoinVO v : uniqueVms) {
vmIds[i++] = v.getId();
}
List<UserVmJoinVO> vms = _userVmJoinDao.searchByIds(vmIds);
return new Pair<List<UserVmJoinVO>, Integer>(vms, count);
}
Aggregations