From 8c2756b681838737e9b8b6d9dc02178084d7fdcb Mon Sep 17 00:00:00 2001 From: Kris McQueen Date: Wed, 8 Sep 2010 18:55:38 -0700 Subject: [PATCH] Refactor listZones to new API framework. --- .../cloud/api/commands/ListZonesByCmd.java | 115 ++++++------------ .../com/cloud/api/response/ZoneResponse.java | 11 ++ .../com/cloud/server/ManagementServer.java | 26 +--- .../cloud/server/ManagementServerImpl.java | 40 +++--- 4 files changed, 74 insertions(+), 118 deletions(-) diff --git a/server/src/com/cloud/api/commands/ListZonesByCmd.java b/server/src/com/cloud/api/commands/ListZonesByCmd.java index bf6b2868153..bb5e5c78fce 100644 --- a/server/src/com/cloud/api/commands/ListZonesByCmd.java +++ b/server/src/com/cloud/api/commands/ListZonesByCmd.java @@ -20,28 +20,23 @@ package com.cloud.api.commands; import java.util.ArrayList; import java.util.List; -import java.util.Map; import org.apache.log4j.Logger; -import com.cloud.api.BaseCmd; +import com.cloud.api.BaseListCmd; +import com.cloud.api.Implementation; import com.cloud.api.Parameter; -import com.cloud.api.ServerApiException; +import com.cloud.api.response.ZoneResponse; import com.cloud.dc.DataCenterVO; +import com.cloud.serializer.SerializerHelper; import com.cloud.user.Account; -import com.cloud.utils.Pair; - -//FIXME: consolidate this class and ListDataCentersByCmd -public class ListZonesByCmd extends BaseCmd { +import com.cloud.user.UserContext; + +@Implementation(method="listDataCenters") +public class ListZonesByCmd extends BaseListCmd { public static final Logger s_logger = Logger.getLogger(ListZonesByCmd.class.getName()); private static final String s_name = "listzonesresponse"; - private static final List> s_properties = new ArrayList>(); - - static { - s_properties.add(new Pair(BaseCmd.Properties.AVAILABLE, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.ACCOUNT_OBJ, Boolean.FALSE)); - } ///////////////////////////////////////////////////// //////////////// API parameters ///////////////////// @@ -65,71 +60,35 @@ public class ListZonesByCmd extends BaseCmd { @Override public String getName() { return s_name; - } - @Override - public List> getProperties() { - return s_properties; - } + } - @Override - public List> execute(Map params) { - Account account = (Account)params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName()); - Boolean available = (Boolean)params.get(BaseCmd.Properties.AVAILABLE.getName()); - - List dataCenters = null; - if (account != null) { - if (available != null && available) { - dataCenters = getManagementServer().listDataCenters(); - } else { - dataCenters = getManagementServer().listDataCentersBy(account.getId().longValue()); - } - } else { - // available is kinda useless in this case because we can't exactly list by - // accountId if we don't have one. In this case, we just assume the user - // wants all the zones. - dataCenters = getManagementServer().listDataCenters(); - } - - if (dataCenters == null) { - throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "unable to find zones for account " + account.getAccountName()); - } - List> dcTags = new ArrayList>(); - Object[] dcInstTag = new Object[dataCenters.size()]; - int i = 0; - for (DataCenterVO dataCenter : dataCenters) { - List> dcData = new ArrayList>(); - if (dataCenter.getId() != null) { - dcData.add(new Pair(BaseCmd.Properties.ID.getName(), dataCenter.getId().toString())); - } - dcData.add(new Pair(BaseCmd.Properties.NAME.getName(), dataCenter.getName())); - if ((dataCenter.getDescription() != null) && !dataCenter.getDescription().equalsIgnoreCase("null")) { - dcData.add(new Pair(BaseCmd.Properties.DESCRIPTION.getName(), dataCenter.getDescription())); - } - if ((account == null) || (account.getType() == Account.ACCOUNT_TYPE_ADMIN)) { - if (dataCenter.getDns1() != null) { - dcData.add(new Pair(BaseCmd.Properties.DNS1.getName(), dataCenter.getDns1())); - } - if (dataCenter.getDns2() != null) { - dcData.add(new Pair(BaseCmd.Properties.DNS2.getName(), dataCenter.getDns2())); - } - if (dataCenter.getInternalDns1() != null) { - dcData.add(new Pair(BaseCmd.Properties.INTERNAL_DNS1.getName(), dataCenter.getInternalDns1())); - } - if (dataCenter.getInternalDns2() != null) { - dcData.add(new Pair(BaseCmd.Properties.INTERNAL_DNS2.getName(), dataCenter.getInternalDns2())); - } - if (dataCenter.getVnet() != null) { - dcData.add(new Pair("vlan", dataCenter.getVnet())); - } - if (dataCenter.getGuestNetworkCidr() != null) { - dcData.add(new Pair(BaseCmd.Properties.GUEST_CIDR_ADDRESS.getName(), dataCenter.getGuestNetworkCidr())); - } - } - - dcInstTag[i++] = dcData; - } - Pair dcTag = new Pair("zone", dcInstTag); - dcTags.add(dcTag); - return dcTags; + @Override @SuppressWarnings("unchecked") + public String getResponse() { + List dataCenters = (List)getResponseObject(); + Account account = (Account)UserContext.current().getAccountObject(); + + List response = new ArrayList(); + for (DataCenterVO dataCenter : dataCenters) { + ZoneResponse zoneResponse = new ZoneResponse(); + zoneResponse.setId(dataCenter.getId()); + zoneResponse.setName(dataCenter.getName()); + + if ((dataCenter.getDescription() != null) && !dataCenter.getDescription().equalsIgnoreCase("null")) { + zoneResponse.setDescription(dataCenter.getDescription()); + } + + if ((account == null) || (account.getType() == Account.ACCOUNT_TYPE_ADMIN)) { + zoneResponse.setDns1(dataCenter.getDns1()); + zoneResponse.setDns2(dataCenter.getDns2()); + zoneResponse.setInternalDns1(dataCenter.getInternalDns1()); + zoneResponse.setInternalDns2(dataCenter.getInternalDns2()); + zoneResponse.setVlan(dataCenter.getVnet()); + zoneResponse.setGuestCidrAddress(dataCenter.getGuestNetworkCidr()); + } + + response.add(zoneResponse); + } + + return SerializerHelper.toSerializedString(response); } } diff --git a/server/src/com/cloud/api/response/ZoneResponse.java b/server/src/com/cloud/api/response/ZoneResponse.java index 9f6a1a280dd..c41e185ca08 100644 --- a/server/src/com/cloud/api/response/ZoneResponse.java +++ b/server/src/com/cloud/api/response/ZoneResponse.java @@ -27,6 +27,9 @@ public class ZoneResponse implements ResponseObject { @Param(name="name") private String name; + @Param(name="description") + private String description; + @Param(name="dns1") private String dns1; @@ -61,6 +64,14 @@ public class ZoneResponse implements ResponseObject { this.name = name; } + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + public String getDns1() { return dns1; } diff --git a/server/src/com/cloud/server/ManagementServer.java b/server/src/com/cloud/server/ManagementServer.java index d6dc1162299..40d69e3a83d 100644 --- a/server/src/com/cloud/server/ManagementServer.java +++ b/server/src/com/cloud/server/ManagementServer.java @@ -63,6 +63,7 @@ import com.cloud.api.commands.ListUsersCmd; import com.cloud.api.commands.ListVMsCmd; import com.cloud.api.commands.ListVlanIpRangesCmd; import com.cloud.api.commands.ListVolumesCmd; +import com.cloud.api.commands.ListZonesByCmd; import com.cloud.api.commands.LockAccountCmd; import com.cloud.api.commands.LockUserCmd; import com.cloud.api.commands.RebootSystemVmCmd; @@ -781,19 +782,13 @@ public interface ManagementServer { HostPodVO getPodBy(long podId); /** - * Retrieves the list of all data centers + * Retrieves the list of data centers with search criteria. + * Currently the only search criteria is "available" zones for the account that invokes the API. By specifying + * available=true all zones which the account can access. By specifying available=false the zones where the + * account has virtual machine instances will be returned. * @return a list of DataCenters */ - List listDataCenters(); - - /** - * Retrieves a list of data centers that contain domain routers - * that the specified user owns. - * - * @param userId - * @return a list of DataCenters - */ - List listDataCentersBy(long userId); + List listDataCenters(ListZonesByCmd cmd); /** * Retrieves a host by id @@ -802,15 +797,6 @@ public interface ManagementServer { */ HostVO getHostBy(long hostId); - -// /** -// * Deletes a host -// * -// * @param hostId -// * @param true if deleted, false otherwise -// */ -// boolean deleteHost(long hostId); - /** * Retrieves all Events between the start and end date specified * diff --git a/server/src/com/cloud/server/ManagementServerImpl.java b/server/src/com/cloud/server/ManagementServerImpl.java index 458f38c7b1e..fe6334ee3a7 100755 --- a/server/src/com/cloud/server/ManagementServerImpl.java +++ b/server/src/com/cloud/server/ManagementServerImpl.java @@ -99,6 +99,7 @@ import com.cloud.api.commands.ListUsersCmd; import com.cloud.api.commands.ListVMsCmd; import com.cloud.api.commands.ListVlanIpRangesCmd; import com.cloud.api.commands.ListVolumesCmd; +import com.cloud.api.commands.ListZonesByCmd; import com.cloud.api.commands.LockAccountCmd; import com.cloud.api.commands.LockUserCmd; import com.cloud.api.commands.PrepareForMaintenanceCmd; @@ -2822,26 +2823,29 @@ public class ManagementServerImpl implements ManagementServer { @Override - public List listDataCenters() { - return _dcDao.listAllActive(); - } - - @Override - public List listDataCentersBy(long accountId) { + public List listDataCenters(ListZonesByCmd cmd) { List dcs = _dcDao.listAllActive(); - List routers = _routerDao.listBy(accountId); - for (Iterator iter = dcs.iterator(); iter.hasNext();) { - DataCenterVO dc = iter.next(); - boolean found = false; - for (DomainRouterVO router : routers) { - if (dc.getId() == router.getDataCenterId()) { - found = true; - break; + + Account account = (Account)UserContext.current().getAccountObject(); + Boolean available = cmd.isAvailable(); + if (account != null) { + if ((available != null) && Boolean.FALSE.equals(available)) { + List routers = _routerDao.listBy(account.getId()); + for (Iterator iter = dcs.iterator(); iter.hasNext();) { + DataCenterVO dc = iter.next(); + boolean found = false; + for (DomainRouterVO router : routers) { + if (dc.getId() == router.getDataCenterId()) { + found = true; + break; + } + } + if (!found) + iter.remove(); } } - if (!found) - iter.remove(); } + return dcs; } @@ -2850,10 +2854,6 @@ public class ManagementServerImpl implements ManagementServer { return _hostDao.findById(hostId); } -// public boolean deleteHost(long hostId) { -// return _agentMgr.deleteHost(hostId); -// } - @Override public long getId() { return MacAddress.getMacAddress().toLong();