refactor createPod command to new API framework

This commit is contained in:
Kris McQueen 2010-08-17 19:06:42 -07:00
parent 21ab91dc2c
commit 261c96f99c
6 changed files with 153 additions and 116 deletions

View File

@ -18,36 +18,21 @@
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.BaseCmd.Manager;
import com.cloud.api.Implementation;
import com.cloud.api.Parameter;
import com.cloud.api.ServerApiException;
import com.cloud.dc.DataCenterVO;
import com.cloud.api.response.PodResponse;
import com.cloud.dc.HostPodVO;
import com.cloud.user.User;
import com.cloud.utils.Pair;
import com.cloud.serializer.SerializerHelper;
@Implementation(method="createPod", manager=Manager.ConfigManager)
public class CreatePodCmd extends BaseCmd {
public static final Logger s_logger = Logger.getLogger(CreatePodCmd.class.getName());
private static final String s_name = "createpodresponse";
private static final List<Pair<Enum, Boolean>> s_properties = new ArrayList<Pair<Enum, Boolean>>();
static {
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.CIDR, Boolean.TRUE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.END_IP, Boolean.FALSE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.GATEWAY, Boolean.TRUE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.NAME, Boolean.TRUE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.START_IP, Boolean.TRUE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.ZONE_ID, Boolean.TRUE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.USER_ID, Boolean.FALSE));
}
/////////////////////////////////////////////////////
//////////////// API parameters /////////////////////
@ -108,57 +93,22 @@ public class CreatePodCmd extends BaseCmd {
public String getName() {
return s_name;
}
public List<Pair<Enum, Boolean>> getProperties() {
return s_properties;
}
@Override
public List<Pair<String, Object>> execute(Map<String, Object> params) {
String podName = (String) params.get(BaseCmd.Properties.NAME.getName());
Long zoneId = (Long) params.get(BaseCmd.Properties.ZONE_ID.getName());
String gateway = (String) params.get(BaseCmd.Properties.GATEWAY.getName());
String cidr = (String) params.get(BaseCmd.Properties.CIDR.getName());
String startIp = (String) params.get(BaseCmd.Properties.START_IP.getName());
String endIp = (String) params.get(BaseCmd.Properties.END_IP.getName());
Long userId = (Long)params.get(BaseCmd.Properties.USER_ID.getName());
public String getResponse() {
HostPodVO pod = (HostPodVO)getResponseObject();
if (userId == null) {
userId = Long.valueOf(User.UID_SYSTEM);
}
PodResponse response = new PodResponse();
response.setId(pod.getId());
response.setCidr(pod.getCidrAddress() + "/" + pod.getCidrSize());
// TODO: implement
// response.setEndIp(pod.getEndIp());
// response.setStartIp(pod.getStartIp());
// response.setZoneName(pod.getZoneName());
response.setGateway(pod.getGateway());
response.setName(pod.getName());
response.setZoneId(pod.getDataCenterId());
//verify input parameters
DataCenterVO zone = getManagementServer().findDataCenterById(zoneId);
if (zone == null) {
throw new ServerApiException(BaseCmd.PARAM_ERROR, "unable to find zone by id " + zoneId);
}
if (endIp != null && startIp == null) {
throw new ServerApiException(BaseCmd.PARAM_ERROR, "If an end IP is specified, a start IP must be specified.");
}
HostPodVO pod = null;
try {
pod = getManagementServer().createPod(userId, podName, zoneId, gateway, cidr, startIp, endIp);
} catch (Exception ex) {
s_logger.error("Exception creating pod", ex);
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, ex.getMessage());
}
List<Pair<String, Object>> returnValues = new ArrayList<Pair<String, Object>>();
if (pod == null) {
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to create pod; internal error.");
} else {
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.ID.getName(), pod.getId()));
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.NAME.getName(), podName));
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.ZONE_ID.getName(), zoneId));
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.ZONE_NAME.getName(), zone.getName()));
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.GATEWAY.getName(), pod.getGateway()));
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.CIDR.getName(), cidr));
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.START_IP.getName(), startIp));
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.END_IP.getName(), endIp != null ? endIp : ""));
}
return returnValues;
return SerializerHelper.toSerializedString(response);
}
}

View File

@ -0,0 +1,94 @@
package com.cloud.api.response;
import com.cloud.api.ResponseObject;
import com.cloud.serializer.Param;
public class PodResponse implements ResponseObject {
@Param(name="id")
private Long id;
@Param(name="name")
private String name;
@Param(name="zoneid")
private Long zoneId;
@Param(name="zonename")
private String zoneName;
@Param(name="gateway")
private String gateway;
@Param(name="cidr")
private String cidr;
@Param(name="startip")
private String startIp;
@Param(name="endIp")
private String endIp;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getZoneId() {
return zoneId;
}
public void setZoneId(Long zoneId) {
this.zoneId = zoneId;
}
public String getZoneName() {
return zoneName;
}
public void setZoneName(String zoneName) {
this.zoneName = zoneName;
}
public String getGateway() {
return gateway;
}
public void setGateway(String gateway) {
this.gateway = gateway;
}
public String getCidr() {
return cidr;
}
public void setCidr(String cidr) {
this.cidr = cidr;
}
public String getStartIp() {
return startIp;
}
public void setStartIp(String startIp) {
this.startIp = startIp;
}
public String getEndIp() {
return endIp;
}
public void setEndIp(String endIp) {
this.endIp = endIp;
}
}

View File

@ -21,6 +21,7 @@ import java.util.List;
import com.cloud.api.commands.AddConfigCmd;
import com.cloud.api.commands.CreateDiskOfferingCmd;
import com.cloud.api.commands.CreatePodCmd;
import com.cloud.api.commands.DeleteDiskOfferingCmd;
import com.cloud.api.commands.DeletePodCmd;
import com.cloud.api.commands.UpdateCfgCmd;
@ -141,6 +142,15 @@ public interface ConfigurationManager extends Manager {
*/
HostPodVO createPod(long userId, String podName, long zoneId, String gateway, String cidr, String startIp, String endIp) throws InvalidParameterValueException, InternalErrorException;
/**
* Creates a new pod based on the parameters specified in the command object
* @param cmd the command object that specifies the name, zone, gateway, cidr, and ip range for the pod
* @return the new pod if successful, null otherwise
* @throws InvalidParameterValueException
* @throws InternalErrorException
*/
HostPodVO createPod(CreatePodCmd cmd) throws InvalidParameterValueException, InternalErrorException;
/**
* Edits a pod in the database. Will not allow you to edit pods that are being used anywhere in the system.
* @param userId

View File

@ -35,6 +35,7 @@ import com.cloud.api.BaseCmd;
import com.cloud.api.ServerApiException;
import com.cloud.api.commands.AddConfigCmd;
import com.cloud.api.commands.CreateDiskOfferingCmd;
import com.cloud.api.commands.CreatePodCmd;
import com.cloud.api.commands.DeleteDiskOfferingCmd;
import com.cloud.api.commands.DeletePodCmd;
import com.cloud.api.commands.UpdateCfgCmd;
@ -509,7 +510,34 @@ public class ConfigurationManagerImpl implements ConfigurationManager {
return pod;
}
@DB
@Override
public HostPodVO createPod(CreatePodCmd cmd) throws InvalidParameterValueException, InternalErrorException {
String cidr = cmd.getCidr();
String endIp = cmd.getEndIp();
String gateway = cmd.getGateway();
String name = cmd.getPodName();
String startIp = cmd.getStartIp();
Long zoneId = cmd.getZoneId();
//verify input parameters
DataCenterVO zone = _zoneDao.findById(zoneId);
if (zone == null) {
throw new InvalidParameterValueException("Failed to create pod " + name + " -- unable to find zone " + zoneId);
}
if (endIp != null && startIp == null) {
throw new InvalidParameterValueException("Failed to create pod " + name + " -- if an end IP is specified, a start IP must be specified.");
}
Long userId = UserContext.current().getUserId();
if (userId == null) {
userId = Long.valueOf(User.UID_SYSTEM);
}
return createPod(userId.longValue(), name, zoneId, gateway, cidr, startIp, endIp);
}
@Override @DB
public HostPodVO createPod(long userId, String podName, long zoneId, String gateway, String cidr, String startIp, String endIp) throws InvalidParameterValueException, InternalErrorException {
checkPodAttributes(-1, podName, zoneId, gateway, cidr, startIp, endIp, true);
@ -786,7 +814,6 @@ public class ConfigurationManagerImpl implements ConfigurationManager {
}
//5. Reached here, hence editable
DataCenterVO zoneHandle = _zoneDao.findById(zoneId);
String oldZoneName = zone.getName();
if (zoneName == null) {
@ -1067,8 +1094,6 @@ public class ConfigurationManagerImpl implements ConfigurationManager {
}
}
public String changePrivateIPRange(boolean add, long podId, String startIP, String endIP) throws InvalidParameterValueException {
checkPrivateIpRangeErrors(podId, startIP, endIP);

View File

@ -993,19 +993,6 @@ public interface ManagementServer {
*/
// ServiceOfferingVO updateServiceOffering(long userId, long serviceOfferingId, String name, String displayText, Boolean offerHA, Boolean useVirtualNetwork, String tags);
/**
* Adds a new pod to the database
* @param userId
* @param podName
* @param zoneId
* @param gateway
* @param cidr
* @param startIp
* @param endIp
* @return Pod
*/
HostPodVO createPod(long userId, String podName, Long zoneId, String gateway, String cidr, String startIp, String endIp) throws InvalidParameterValueException, InternalErrorException;
/**
* Edits a pod in the database
* @param userId

View File

@ -38,7 +38,6 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TimeZone;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
@ -89,7 +88,6 @@ import com.cloud.api.commands.UpdateTemplateCmd;
import com.cloud.api.commands.UpdateTemplateOrIsoPermissionsCmd;
import com.cloud.api.commands.UpdateTemplatePermissionsCmd;
import com.cloud.api.commands.UpdateUserCmd;
import com.cloud.api.commands.UpgradeVMCmd;
import com.cloud.async.AsyncInstanceCreateStatus;
import com.cloud.async.AsyncJobExecutor;
import com.cloud.async.AsyncJobManager;
@ -282,7 +280,6 @@ import com.cloud.vm.dao.SecondaryStorageVmDao;
import com.cloud.vm.dao.UserVmDao;
import com.cloud.vm.dao.VMInstanceDao;
import com.google.gson.Gson;
import com.sun.org.apache.bcel.internal.generic.INSTANCEOF;
public class ManagementServerImpl implements ManagementServer {
public static final Logger s_logger = Logger.getLogger(ManagementServerImpl.class.getName());
@ -4316,37 +4313,11 @@ public class ManagementServerImpl implements ManagementServer {
return _configMgr.createServiceOffering(userId, name, cpu, ramSize, speed, displayText, localStorageRequired, offerHA, useVirtualNetwork, tags);
}
// @Override
// public ServiceOfferingVO updateServiceOffering(long userId, long serviceOfferingId, String name, String displayText, Boolean offerHA, Boolean useVirtualNetwork, String tags) {
// return _configMgr.updateServiceOffering(userId, serviceOfferingId, name, displayText, offerHA, useVirtualNetwork, tags);
// }
@Override
public HostPodVO createPod(long userId, String podName, Long zoneId, String gateway, String cidr, String startIp, String endIp) throws InvalidParameterValueException, InternalErrorException {
return _configMgr.createPod(userId, podName, zoneId, gateway, cidr, startIp, endIp);
}
// @Override
// public HostPodVO editPod(long userId, long podId, String newPodName, String gateway, String cidr, String startIp, String endIp) throws InvalidParameterValueException, InternalErrorException {
// return _configMgr.editPod(userId, podId, newPodName, gateway, cidr, startIp, endIp);
// }
// @Override
// public void deletePod(long userId, long podId) throws InvalidParameterValueException, InternalErrorException {
// _configMgr.deletePod(userId, podId);
// }
@Override
public DataCenterVO createZone(long userId, String zoneName, String dns1, String dns2, String internalDns1, String internalDns2, String vnetRange,String guestCidr) throws InvalidParameterValueException, InternalErrorException {
return _configMgr.createZone(userId, zoneName, dns1, dns2, internalDns1, internalDns2, vnetRange, guestCidr);
}
// @Override
// public DataCenterVO editZone(long userId, Long zoneId, String newZoneName, String dns1, String dns2, String dns3, String dns4, String vnetRange, String guestCidr) throws InvalidParameterValueException, InternalErrorException {
// return _configMgr.editZone(userId, zoneId, newZoneName, dns1, dns2, dns3, dns4, vnetRange, guestCidr);
// }
@Override
public void deleteZone(long userId, Long zoneId) throws InvalidParameterValueException, InternalErrorException {
_configMgr.deleteZone(userId, zoneId);