Refactor listZones to new API framework.

This commit is contained in:
Kris McQueen 2010-09-08 18:55:38 -07:00
parent 741bb77b7e
commit 8c2756b681
4 changed files with 74 additions and 118 deletions

View File

@ -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<Pair<Enum, Boolean>> s_properties = new ArrayList<Pair<Enum, Boolean>>();
static {
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.AVAILABLE, Boolean.FALSE));
s_properties.add(new Pair<Enum, Boolean>(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<Pair<Enum, Boolean>> getProperties() {
return s_properties;
}
}
@Override
public List<Pair<String, Object>> execute(Map<String, Object> params) {
Account account = (Account)params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName());
Boolean available = (Boolean)params.get(BaseCmd.Properties.AVAILABLE.getName());
List<DataCenterVO> 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<Pair<String, Object>> dcTags = new ArrayList<Pair<String, Object>>();
Object[] dcInstTag = new Object[dataCenters.size()];
int i = 0;
for (DataCenterVO dataCenter : dataCenters) {
List<Pair<String, Object>> dcData = new ArrayList<Pair<String, Object>>();
if (dataCenter.getId() != null) {
dcData.add(new Pair<String, Object>(BaseCmd.Properties.ID.getName(), dataCenter.getId().toString()));
}
dcData.add(new Pair<String, Object>(BaseCmd.Properties.NAME.getName(), dataCenter.getName()));
if ((dataCenter.getDescription() != null) && !dataCenter.getDescription().equalsIgnoreCase("null")) {
dcData.add(new Pair<String, Object>(BaseCmd.Properties.DESCRIPTION.getName(), dataCenter.getDescription()));
}
if ((account == null) || (account.getType() == Account.ACCOUNT_TYPE_ADMIN)) {
if (dataCenter.getDns1() != null) {
dcData.add(new Pair<String, Object>(BaseCmd.Properties.DNS1.getName(), dataCenter.getDns1()));
}
if (dataCenter.getDns2() != null) {
dcData.add(new Pair<String, Object>(BaseCmd.Properties.DNS2.getName(), dataCenter.getDns2()));
}
if (dataCenter.getInternalDns1() != null) {
dcData.add(new Pair<String, Object>(BaseCmd.Properties.INTERNAL_DNS1.getName(), dataCenter.getInternalDns1()));
}
if (dataCenter.getInternalDns2() != null) {
dcData.add(new Pair<String, Object>(BaseCmd.Properties.INTERNAL_DNS2.getName(), dataCenter.getInternalDns2()));
}
if (dataCenter.getVnet() != null) {
dcData.add(new Pair<String, Object>("vlan", dataCenter.getVnet()));
}
if (dataCenter.getGuestNetworkCidr() != null) {
dcData.add(new Pair<String, Object>(BaseCmd.Properties.GUEST_CIDR_ADDRESS.getName(), dataCenter.getGuestNetworkCidr()));
}
}
dcInstTag[i++] = dcData;
}
Pair<String, Object> dcTag = new Pair<String, Object>("zone", dcInstTag);
dcTags.add(dcTag);
return dcTags;
@Override @SuppressWarnings("unchecked")
public String getResponse() {
List<DataCenterVO> dataCenters = (List<DataCenterVO>)getResponseObject();
Account account = (Account)UserContext.current().getAccountObject();
List<ZoneResponse> response = new ArrayList<ZoneResponse>();
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);
}
}

View File

@ -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;
}

View File

@ -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<DataCenterVO> listDataCenters();
/**
* Retrieves a list of data centers that contain domain routers
* that the specified user owns.
*
* @param userId
* @return a list of DataCenters
*/
List<DataCenterVO> listDataCentersBy(long userId);
List<DataCenterVO> 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
*

View File

@ -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<DataCenterVO> listDataCenters() {
return _dcDao.listAllActive();
}
@Override
public List<DataCenterVO> listDataCentersBy(long accountId) {
public List<DataCenterVO> listDataCenters(ListZonesByCmd cmd) {
List<DataCenterVO> dcs = _dcDao.listAllActive();
List<DomainRouterVO> routers = _routerDao.listBy(accountId);
for (Iterator<DataCenterVO> 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<DomainRouterVO> routers = _routerDao.listBy(account.getId());
for (Iterator<DataCenterVO> 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();