From 4a5f6faca2b888b4b4499ffdab8ede82ece67d89 Mon Sep 17 00:00:00 2001 From: Nitin Date: Tue, 20 Sep 2011 14:33:46 +0530 Subject: [PATCH] bug 11051: ListClusters - Introduce a flag 'showCapacitites'. When its true it will start displaying all the capacitites (as objects in the response) for the respective cluster. --- api/src/com/cloud/api/ApiConstants.java | 1 + api/src/com/cloud/api/ResponseGenerator.java | 2 +- .../com/cloud/api/commands/AddClusterCmd.java | 2 +- .../cloud/api/commands/ListClustersCmd.java | 12 +++++++++--- .../cloud/api/commands/UpdateClusterCmd.java | 2 +- .../cloud/api/response/ClusterResponse.java | 15 ++++++++++++++- server/src/com/cloud/api/ApiDBUtils.java | 13 +++++++++++++ .../src/com/cloud/api/ApiResponseHelper.java | 14 +++++++++++++- .../com/cloud/capacity/dao/CapacityDao.java | 1 + .../cloud/capacity/dao/CapacityDaoImpl.java | 18 ++++++++++++++++++ 10 files changed, 72 insertions(+), 8 deletions(-) mode change 100644 => 100755 api/src/com/cloud/api/commands/AddClusterCmd.java mode change 100644 => 100755 api/src/com/cloud/api/commands/ListClustersCmd.java mode change 100644 => 100755 api/src/com/cloud/api/commands/UpdateClusterCmd.java mode change 100644 => 100755 api/src/com/cloud/api/response/ClusterResponse.java diff --git a/api/src/com/cloud/api/ApiConstants.java b/api/src/com/cloud/api/ApiConstants.java index f8705536314..33aa1e8d370 100755 --- a/api/src/com/cloud/api/ApiConstants.java +++ b/api/src/com/cloud/api/ApiConstants.java @@ -157,6 +157,7 @@ public class ApiConstants { public static final String SENT = "sent"; public static final String SENT_BYTES = "sentbytes"; public static final String SERVICE_OFFERING_ID = "serviceofferingid"; + public static final String SHOW_CAPACITIES = "showcapacities"; public static final String SIZE = "size"; public static final String SNAPSHOT_ID = "snapshotid"; public static final String SNAPSHOT_POLICY_ID = "snapshotpolicyid"; diff --git a/api/src/com/cloud/api/ResponseGenerator.java b/api/src/com/cloud/api/ResponseGenerator.java index 5879fcc727b..ac0c254b765 100755 --- a/api/src/com/cloud/api/ResponseGenerator.java +++ b/api/src/com/cloud/api/ResponseGenerator.java @@ -149,7 +149,7 @@ public interface ResponseGenerator { StoragePoolResponse createStoragePoolResponse(StoragePool pool); - ClusterResponse createClusterResponse(Cluster cluster); + ClusterResponse createClusterResponse(Cluster cluster, Boolean showCapacities); FirewallRuleResponse createPortForwardingRuleResponse(PortForwardingRule fwRule); diff --git a/api/src/com/cloud/api/commands/AddClusterCmd.java b/api/src/com/cloud/api/commands/AddClusterCmd.java old mode 100644 new mode 100755 index a5cac7c2654..454035fd96d --- a/api/src/com/cloud/api/commands/AddClusterCmd.java +++ b/api/src/com/cloud/api/commands/AddClusterCmd.java @@ -129,7 +129,7 @@ public class AddClusterCmd extends BaseCmd { List clusterResponses = new ArrayList(); if (result != null) { for (Cluster cluster : result) { - ClusterResponse clusterResponse = _responseGenerator.createClusterResponse(cluster); + ClusterResponse clusterResponse = _responseGenerator.createClusterResponse(cluster, false); clusterResponses.add(clusterResponse); } } else { diff --git a/api/src/com/cloud/api/commands/ListClustersCmd.java b/api/src/com/cloud/api/commands/ListClustersCmd.java old mode 100644 new mode 100755 index d602f0a49b1..fb6515c198d --- a/api/src/com/cloud/api/commands/ListClustersCmd.java +++ b/api/src/com/cloud/api/commands/ListClustersCmd.java @@ -66,6 +66,8 @@ public class ListClustersCmd extends BaseListCmd { @Parameter(name=ApiConstants.MANAGED_STATE, type=CommandType.STRING, description="whether this cluster is managed by cloudstack") private String managedState; + @Parameter(name=ApiConstants.SHOW_CAPACITIES, type=CommandType.BOOLEAN, description="flag to display the capacity of the clusters") + private Boolean showCapacities; ///////////////////////////////////////////////////// /////////////////// Accessors /////////////////////// @@ -108,12 +110,16 @@ public class ListClustersCmd extends BaseListCmd { this.managedState = managedstate; } - + + public Boolean getShowCapacities() { + return showCapacities; + } + ///////////////////////////////////////////////////// /////////////// API Implementation/////////////////// ///////////////////////////////////////////////////// - @Override + @Override public String getCommandName() { return s_name; } @@ -124,7 +130,7 @@ public class ListClustersCmd extends BaseListCmd { ListResponse response = new ListResponse(); List clusterResponses = new ArrayList(); for (Cluster cluster : result) { - ClusterResponse clusterResponse = _responseGenerator.createClusterResponse(cluster); + ClusterResponse clusterResponse = _responseGenerator.createClusterResponse(cluster,showCapacities); clusterResponse.setObjectName("cluster"); clusterResponses.add(clusterResponse); } diff --git a/api/src/com/cloud/api/commands/UpdateClusterCmd.java b/api/src/com/cloud/api/commands/UpdateClusterCmd.java old mode 100644 new mode 100755 index d054e50e72d..5995da7f10d --- a/api/src/com/cloud/api/commands/UpdateClusterCmd.java +++ b/api/src/com/cloud/api/commands/UpdateClusterCmd.java @@ -109,7 +109,7 @@ public class UpdateClusterCmd extends BaseCmd { Cluster result = _resourceService.updateCluster(cluster, getClusterType(), getHypervisor(), getAllocationState(), getManagedstate()); if (result != null) { - ClusterResponse clusterResponse = _responseGenerator.createClusterResponse(cluster); + ClusterResponse clusterResponse = _responseGenerator.createClusterResponse(cluster, false); clusterResponse.setResponseName(getCommandName()); this.setResponseObject(clusterResponse); } else { diff --git a/api/src/com/cloud/api/response/ClusterResponse.java b/api/src/com/cloud/api/response/ClusterResponse.java old mode 100644 new mode 100755 index 2c49e5d646e..aea3b729256 --- a/api/src/com/cloud/api/response/ClusterResponse.java +++ b/api/src/com/cloud/api/response/ClusterResponse.java @@ -17,6 +17,9 @@ */ package com.cloud.api.response; +import java.util.ArrayList; +import java.util.List; + import com.cloud.api.ApiConstants; import com.cloud.api.Parameter; import com.cloud.api.BaseCmd.CommandType; @@ -54,6 +57,8 @@ public class ClusterResponse extends BaseResponse { @SerializedName("managedstate") @Param(description="whether this cluster is managed by cloudstack") private String managedState; + @SerializedName("capacity") @Param(description="", responseObject = CapacityResponse.class) + private List capacitites; public Long getId() { return id; @@ -133,5 +138,13 @@ public class ClusterResponse extends BaseResponse { public void setManagedState(String managedState) { this.managedState = managedState; - } + } + + public List getCapacitites() { + return capacitites; + } + + public void setCapacitites(ArrayList arrayList) { + this.capacitites = arrayList; + } } diff --git a/server/src/com/cloud/api/ApiDBUtils.java b/server/src/com/cloud/api/ApiDBUtils.java index 5c23260f1a4..f8aba311ba9 100755 --- a/server/src/com/cloud/api/ApiDBUtils.java +++ b/server/src/com/cloud/api/ApiDBUtils.java @@ -26,6 +26,8 @@ import com.cloud.agent.AgentManager; import com.cloud.api.response.UserVmResponse; import com.cloud.async.AsyncJobManager; import com.cloud.async.AsyncJobVO; +import com.cloud.capacity.CapacityVO; +import com.cloud.capacity.dao.CapacityDao; import com.cloud.configuration.Config; import com.cloud.configuration.ConfigurationService; import com.cloud.configuration.ResourceCount.ResourceType; @@ -136,6 +138,7 @@ public class ApiDBUtils { private static AccountDao _accountDao; private static AccountVlanMapDao _accountVlanMapDao; private static ClusterDao _clusterDao; + private static CapacityDao _capacityDao; private static DiskOfferingDao _diskOfferingDao; private static DomainDao _domainDao; private static DomainRouterDao _domainRouterDao; @@ -182,6 +185,7 @@ public class ApiDBUtils { _accountDao = locator.getDao(AccountDao.class); _accountVlanMapDao = locator.getDao(AccountVlanMapDao.class); _clusterDao = locator.getDao(ClusterDao.class); + _capacityDao = locator.getDao(CapacityDao.class); _diskOfferingDao = locator.getDao(DiskOfferingDao.class); _domainDao = locator.getDao(DomainDao.class); _domainRouterDao = locator.getDao(DomainRouterDao.class); @@ -239,6 +243,15 @@ public class ApiDBUtils { return _ms.getMemoryOrCpuCapacityByHost(poolId, capacityType); } + public static List getCapacityByClusterPodZone(Long zoneId, Long podId, Long clusterId){ + return _capacityDao.findByClusterPodZone(null,null,clusterId); + } + + public static List getCapacityByPod(){ + return null; + + } + public static Long getPodIdForVlan(long vlanDbId) { return _ms.getPodIdForVlan(vlanDbId); } diff --git a/server/src/com/cloud/api/ApiResponseHelper.java b/server/src/com/cloud/api/ApiResponseHelper.java index 11cc5fb9fb7..f5713c54009 100755 --- a/server/src/com/cloud/api/ApiResponseHelper.java +++ b/server/src/com/cloud/api/ApiResponseHelper.java @@ -964,7 +964,7 @@ public class ApiResponseHelper implements ResponseGenerator { } @Override - public ClusterResponse createClusterResponse(Cluster cluster) { + public ClusterResponse createClusterResponse(Cluster cluster, Boolean showCapacities) { ClusterResponse clusterResponse = new ClusterResponse(); clusterResponse.setId(cluster.getId()); clusterResponse.setName(cluster.getName()); @@ -980,6 +980,18 @@ public class ApiResponseHelper implements ResponseGenerator { } DataCenterVO zone = ApiDBUtils.findZoneById(cluster.getDataCenterId()); clusterResponse.setZoneName(zone.getName()); + if (showCapacities != null && showCapacities){ + List capacities = ApiDBUtils.getCapacityByClusterPodZone(null,null,cluster.getId()); + Set capacityResponses = new HashSet(); + for (CapacityVO capacity : capacities){ + CapacityResponse capacityResponse = new CapacityResponse(); + capacityResponse.setCapacityType(capacity.getCapacityType()); + capacityResponse.setCapacityUsed(capacity.getUsedCapacity()); + capacityResponse.setCapacityTotal(capacity.getTotalCapacity()); + capacityResponses.add(capacityResponse); + } + clusterResponse.setCapacitites(new ArrayList(capacityResponses)); + } clusterResponse.setObjectName("cluster"); return clusterResponse; } diff --git a/server/src/com/cloud/capacity/dao/CapacityDao.java b/server/src/com/cloud/capacity/dao/CapacityDao.java index 1b53f6c2acd..25faa92ee3b 100755 --- a/server/src/com/cloud/capacity/dao/CapacityDao.java +++ b/server/src/com/cloud/capacity/dao/CapacityDao.java @@ -31,4 +31,5 @@ public interface CapacityDao extends GenericDao { List findCapacityByType(short capacityType, Long zoneId, Long podId, Long clusterId, Long startIndex, Long pageSize); boolean removeBy(Short capacityType, Long zoneId, Long podId, Long clusterId); + List findByClusterPodZone(Long zoneId, Long podId, Long clusterId); } diff --git a/server/src/com/cloud/capacity/dao/CapacityDaoImpl.java b/server/src/com/cloud/capacity/dao/CapacityDaoImpl.java index 0ed0897d281..e0d098cfce7 100755 --- a/server/src/com/cloud/capacity/dao/CapacityDaoImpl.java +++ b/server/src/com/cloud/capacity/dao/CapacityDaoImpl.java @@ -244,6 +244,24 @@ public class CapacityDaoImpl extends GenericDaoBase implements public SummedCapacity() { } } + public List findByClusterPodZone(Long zoneId, Long podId, Long clusterId){ + + SearchCriteria sc = _allFieldsSearch.create(); + if (zoneId != null) { + sc.setParameters("zoneId", zoneId); + } + + if (podId != null) { + sc.setParameters("podId", podId); + } + + if (clusterId != null) { + sc.setParameters("clusterId", clusterId); + } + + return listBy(sc); + } + @Override public boolean removeBy(Short capacityType, Long zoneId, Long podId, Long clusterId) { SearchCriteria sc = _allFieldsSearch.create();