diff --git a/api/src/com/cloud/server/ManagementService.java b/api/src/com/cloud/server/ManagementService.java index fb8af1a5b4b..5302daa493b 100755 --- a/api/src/com/cloud/server/ManagementService.java +++ b/api/src/com/cloud/server/ManagementService.java @@ -25,6 +25,7 @@ import java.util.Set; import com.cloud.alert.Alert; import org.apache.cloudstack.api.ServerApiException; import org.apache.cloudstack.api.command.admin.cluster.ListClustersCmd; +import org.apache.cloudstack.api.command.admin.host.ListHostsCmd; import org.apache.cloudstack.api.command.admin.host.UpdateHostPasswordCmd; import org.apache.cloudstack.api.command.admin.pod.ListPodsByCmd; import org.apache.cloudstack.api.command.admin.resource.ListAlertsCmd; @@ -140,6 +141,14 @@ public interface ManagementService { */ Pair, Integer> searchForPods(ListPodsByCmd cmd); + /** + * Searches for servers by the specified search criteria Can search by: "name", "type", "state", "dataCenterId", + * "podId" + * + * @param cmd + * @return List of Hosts + */ + Pair, Integer> searchForServers(ListHostsCmd cmd); /** * Creates a new template @@ -384,7 +393,7 @@ public interface ManagementService { * @return Pair, List> List of all Hosts in VM's cluster and list of Hosts with * enough capacity */ - Pair, List> listHostsForMigrationOfVM(Long vmId, Long startIndex, Long pageSize); + Pair, Integer>, List> listHostsForMigrationOfVM(Long vmId, Long startIndex, Long pageSize); String[] listEventTypes(); diff --git a/api/src/org/apache/cloudstack/api/command/admin/host/ListHostsCmd.java b/api/src/org/apache/cloudstack/api/command/admin/host/ListHostsCmd.java index 82f329f4b7e..876da9a6bda 100644 --- a/api/src/org/apache/cloudstack/api/command/admin/host/ListHostsCmd.java +++ b/api/src/org/apache/cloudstack/api/command/admin/host/ListHostsCmd.java @@ -168,17 +168,16 @@ public class ListHostsCmd extends BaseListCmd { if (getVirtualMachineId() == null) { response = _queryService.searchForServers(this); } else { - List result = new ArrayList(); + Pair,Integer> result; List hostsWithCapacity = new ArrayList(); - Pair, List> hostsForMigration = _mgr.listHostsForMigrationOfVM(getVirtualMachineId(), - this.getStartIndex(), this.getPageSizeVal()); + Pair,Integer>, List> hostsForMigration = _mgr.listHostsForMigrationOfVM(getVirtualMachineId(), this.getStartIndex(), this.getPageSizeVal()); result = hostsForMigration.first(); hostsWithCapacity = hostsForMigration.second(); response = new ListResponse(); List hostResponses = new ArrayList(); - for (Host host : result) { + for (Host host : result.first()) { HostResponse hostResponse = _responseGenerator.createHostResponse(host, getDetails()); Boolean suitableForMigration = false; if (hostsWithCapacity.contains(host)) { @@ -189,7 +188,7 @@ public class ListHostsCmd extends BaseListCmd { hostResponses.add(hostResponse); } - response.setResponses(hostResponses); + response.setResponses(hostResponses, result.second()); } response.setResponseName(getCommandName()); this.setResponseObject(response); diff --git a/server/src/com/cloud/server/ManagementServerImpl.java b/server/src/com/cloud/server/ManagementServerImpl.java index 4efae630f36..bad834dde5a 100755 --- a/server/src/com/cloud/server/ManagementServerImpl.java +++ b/server/src/com/cloud/server/ManagementServerImpl.java @@ -89,6 +89,7 @@ import org.apache.cloudstack.api.command.admin.vlan.ListVlanIpRangesCmd; import org.apache.cloudstack.api.command.admin.systemvm.RebootSystemVmCmd; import org.apache.cloudstack.api.command.admin.systemvm.StopSystemVmCmd; import org.apache.cloudstack.api.command.admin.domain.UpdateDomainCmd; +import org.apache.cloudstack.api.command.admin.host.ListHostsCmd; import org.apache.cloudstack.api.command.admin.host.UpdateHostPasswordCmd; import com.cloud.api.query.dao.DomainRouterJoinDao; import com.cloud.api.query.dao.InstanceGroupJoinDao; @@ -219,6 +220,7 @@ import com.cloud.user.UserVO; import com.cloud.user.dao.AccountDao; import com.cloud.user.dao.SSHKeyPairDao; import com.cloud.user.dao.UserDao; +import com.cloud.uservm.UserVm; import com.cloud.utils.EnumUtils; import com.cloud.utils.NumbersUtil; import com.cloud.utils.Pair; @@ -915,9 +917,26 @@ public class ManagementServerImpl implements ManagementServer { return new Pair, Integer>(result.first(), result.second()); } + @Override + public Pair, Integer> searchForServers(ListHostsCmd cmd) { + + Long zoneId = _accountMgr.checkAccessAndSpecifyAuthority(UserContext.current().getCaller(), cmd.getZoneId()); + Object name = cmd.getHostName(); + Object type = cmd.getType(); + Object state = cmd.getState(); + Object pod = cmd.getPodId(); + Object cluster = cmd.getClusterId(); + Object id = cmd.getId(); + Object keyword = cmd.getKeyword(); + Object resourceState = cmd.getResourceState(); + Object haHosts = cmd.getHaHost(); + + Pair, Integer> result = searchForServers(cmd.getStartIndex(), cmd.getPageSizeVal(), name, type, state, zoneId, pod, cluster, id, keyword, resourceState, haHosts); + return new Pair, Integer>(result.first(), result.second()); + } @Override - public Pair, List> listHostsForMigrationOfVM(Long vmId, Long startIndex, Long pageSize) { + public Pair, Integer>, List> listHostsForMigrationOfVM(Long vmId, Long startIndex, Long pageSize) { // access check - only root admin can migrate VM Account caller = UserContext.current().getCaller(); if (caller.getType() != Account.ACCOUNT_TYPE_ADMIN) { @@ -976,10 +995,12 @@ public class ManagementServerImpl implements ManagementServer { s_logger.debug("Searching for all hosts in cluster: " + cluster + " for migrating VM " + vm); } - List allHostsInCluster = searchForServers(startIndex, pageSize, null, hostType, null, null, null, cluster, null, null, null, - null); - // filter out the current host + Pair, Integer> allHostsInClusterPair = searchForServers(startIndex, pageSize, null, hostType, null, null, null, cluster, null, null, null, null); + + // filter out the current host + List allHostsInCluster = allHostsInClusterPair.first(); allHostsInCluster.remove(srcHost); + Pair, Integer> otherHostsInCluster = new Pair, Integer>(allHostsInCluster, new Integer(allHostsInClusterPair.second().intValue()-1)); if (s_logger.isDebugEnabled()) { s_logger.debug("Other Hosts in this cluster: " + allHostsInCluster); @@ -1013,11 +1034,11 @@ public class ManagementServerImpl implements ManagementServer { } } - return new Pair, List>(allHostsInCluster, suitableHosts); + return new Pair, Integer>, List>(otherHostsInCluster, suitableHosts); } - private List searchForServers(Long startIndex, Long pageSize, Object name, Object type, Object state, Object zone, Object pod, - Object cluster, Object id, Object keyword, Object resourceState, Object haHosts) { + private Pair, Integer> searchForServers(Long startIndex, Long pageSize, Object name, Object type, Object state, Object zone, Object pod, Object cluster, Object id, Object keyword, + Object resourceState, Object haHosts) { Filter searchFilter = new Filter(HostVO.class, "id", Boolean.TRUE, startIndex, pageSize); SearchBuilder sb = _hostDao.createSearchBuilder(); @@ -1087,7 +1108,7 @@ public class ManagementServerImpl implements ManagementServer { sc.setJoinParameters("hostTagSearch", "tag", haTag); } - return _hostDao.search(sc, searchFilter); + return _hostDao.searchAndCount(sc, searchFilter); } @Override