Refactoring updatePod command

This commit is contained in:
abhishek 2010-08-17 17:55:38 -07:00
parent 1252a2b8a7
commit 154c6985a4
5 changed files with 113 additions and 94 deletions

View File

@ -18,36 +18,18 @@
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.Implementation;
import com.cloud.api.Parameter;
import com.cloud.api.ServerApiException;
import com.cloud.dc.DataCenterVO;
import com.cloud.dc.HostPodVO;
import com.cloud.user.User;
import com.cloud.utils.Pair;
import com.cloud.api.BaseCmd.Manager;
@Implementation(method="editPod", manager=Manager.ConfigManager)
public class UpdatePodCmd extends BaseCmd {
public static final Logger s_logger = Logger.getLogger(UpdatePodCmd.class.getName());
private static final String s_name = "updatepodresponse";
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.FALSE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.END_IP, Boolean.FALSE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.GATEWAY, Boolean.FALSE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.ID, Boolean.TRUE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.NAME, Boolean.FALSE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.START_IP, Boolean.FALSE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.USER_ID, Boolean.FALSE));
}
/////////////////////////////////////////////////////
//////////////// API parameters /////////////////////
@ -106,57 +88,60 @@ public class UpdatePodCmd 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) {
Long podId = (Long) params.get(BaseCmd.Properties.ID.getName());
String podName = (String) params.get(BaseCmd.Properties.NAME.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());
if (userId == null) {
userId = Long.valueOf(User.UID_SYSTEM);
public String getResponse() {
// TODO Auto-generated method stub
return null;
}
//verify parameters
HostPodVO pod = getManagementServer().findHostPodById(podId);
if (pod == null) {
throw new ServerApiException(BaseCmd.PARAM_ERROR, "Unable to find pod by id " + podId);
}
long zoneId = pod.getDataCenterId();
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 updatedPod = null;
try {
updatedPod = getManagementServer().editPod(userId, podId, podName, gateway, cidr, startIp, endIp);
} catch (Exception ex) {
s_logger.error("Exception updating pod", ex);
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, ex.getMessage());
}
List<Pair<String, Object>> returnValues = new ArrayList<Pair<String, Object>>();
if (updatedPod == null) {
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to update pod; internal error.");
} else {
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.SUCCESS.getName(), "true"));
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.DISPLAY_TEXT.getName(), "Successfully updated pod."));
}
return returnValues;
}
// @Override
// public List<Pair<String, Object>> execute(Map<String, Object> params) {
// Long podId = (Long) params.get(BaseCmd.Properties.ID.getName());
// String podName = (String) params.get(BaseCmd.Properties.NAME.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());
//
// if (userId == null) {
// userId = Long.valueOf(User.UID_SYSTEM);
// }
//
// //verify parameters
// HostPodVO pod = getManagementServer().findHostPodById(podId);
// if (pod == null) {
// throw new ServerApiException(BaseCmd.PARAM_ERROR, "Unable to find pod by id " + podId);
// }
//
// long zoneId = pod.getDataCenterId();
// 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 updatedPod = null;
// try {
// updatedPod = getManagementServer().editPod(userId, podId, podName, gateway, cidr, startIp, endIp);
// } catch (Exception ex) {
// s_logger.error("Exception updating pod", ex);
// throw new ServerApiException(BaseCmd.INTERNAL_ERROR, ex.getMessage());
// }
//
// List<Pair<String, Object>> returnValues = new ArrayList<Pair<String, Object>>();
//
// if (updatedPod == null) {
// throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to update pod; internal error.");
// } else {
// returnValues.add(new Pair<String, Object>(BaseCmd.Properties.SUCCESS.getName(), "true"));
// returnValues.add(new Pair<String, Object>(BaseCmd.Properties.DISPLAY_TEXT.getName(), "Successfully updated pod."));
// }
//
// return returnValues;
// }
}

View File

@ -25,6 +25,7 @@ import com.cloud.api.commands.DeleteDiskOfferingCmd;
import com.cloud.api.commands.DeletePodCmd;
import com.cloud.api.commands.UpdateCfgCmd;
import com.cloud.api.commands.UpdateDiskOfferingCmd;
import com.cloud.api.commands.UpdatePodCmd;
import com.cloud.api.commands.UpdateServiceOfferingCmd;
import com.cloud.api.commands.UpdateZoneCmd;
import com.cloud.dc.DataCenterVO;
@ -149,9 +150,11 @@ public interface ConfigurationManager extends Manager {
* @param startIp
* @param endIp
* @return Pod
* @throws InternalErrorException
* @throws InvalidParameterValueException
*/
HostPodVO editPod(long userId, long podId, String newPodName, String gateway, String cidr, String startIp, String endIp) throws InvalidParameterValueException, InternalErrorException;
// HostPodVO editPod(long userId, long podId, String newPodName, String gateway, String cidr, String startIp, String endIp) throws InvalidParameterValueException, InternalErrorException;
HostPodVO editPod(UpdatePodCmd cmd) throws InvalidParameterValueException, InternalErrorException;
/**
* Deletes a pod from the database. Will not allow you to delete pods that are being used anywhere in the system.
* @param userId

View File

@ -39,6 +39,7 @@ import com.cloud.api.commands.DeleteDiskOfferingCmd;
import com.cloud.api.commands.DeletePodCmd;
import com.cloud.api.commands.UpdateCfgCmd;
import com.cloud.api.commands.UpdateDiskOfferingCmd;
import com.cloud.api.commands.UpdatePodCmd;
import com.cloud.api.commands.UpdateServiceOfferingCmd;
import com.cloud.api.commands.UpdateZoneCmd;
import com.cloud.configuration.dao.ConfigurationDao;
@ -105,6 +106,8 @@ public class ConfigurationManagerImpl implements ConfigurationManager {
@Inject AccountDao _accountDao;
@Inject EventDao _eventDao;
@Inject UserDao _userDao;
@Inject DataCenterDao _dcDao;
@Inject HostPodDao _hostPodDao;
public boolean _premium;
private int _maxVolumeSizeInGb;
@ -395,25 +398,54 @@ public class ConfigurationManagerImpl implements ConfigurationManager {
}
@DB
public HostPodVO editPod(long userId, long podId, String newPodName, String gateway, String cidr, String startIp, String endIp) throws InvalidParameterValueException, InternalErrorException {
public HostPodVO editPod(UpdatePodCmd cmd) throws InvalidParameterValueException, InternalErrorException
{
//Input validation
String cidr = cmd.getCidr();
String startIp = cmd.getStartIp();
String endIp = cmd.getEndIp();
String gateway = cmd.getGateway();
Long id = cmd.getId();
String name = cmd.getName();
Long userId = UserContext.current().getUserId();
if (userId == null) {
userId = Long.valueOf(User.UID_SYSTEM);
}
//verify parameters
HostPodVO pod = _hostPodDao.findById(id);;
if (pod == null) {
throw new ServerApiException(BaseCmd.PARAM_ERROR, "Unable to find pod by id " + id);
}
long zoneId = pod.getDataCenterId();
DataCenterVO zone = _dcDao.findById(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.");
}
// Make sure the pod exists
if (!validPod(podId)) {
throw new InvalidParameterValueException("A pod with ID: " + podId + " does not exist.");
if (!validPod(id)) {
throw new InvalidParameterValueException("A pod with ID: " + id + " does not exist.");
}
// If the gateway, CIDR, private IP range is being updated, check if the pod has allocated private IP addresses
if (gateway!= null || cidr != null || startIp != null || endIp != null) {
if (podHasAllocatedPrivateIPs(podId)) {
if (podHasAllocatedPrivateIPs(id)) {
throw new InternalErrorException("The specified pod has allocated private IP addresses, so its CIDR and IP address range cannot be changed.");
}
}
HostPodVO pod = _podDao.findById(podId);
String oldPodName = pod.getName();
long zoneId = pod.getDataCenterId();
if (newPodName == null) {
newPodName = oldPodName;
if (name == null) {
name = oldPodName;
}
if (gateway == null) {
@ -424,8 +456,8 @@ public class ConfigurationManagerImpl implements ConfigurationManager {
cidr = pod.getCidrAddress() + "/" + pod.getCidrSize();
}
boolean checkForDuplicates = !oldPodName.equals(newPodName);
checkPodAttributes(podId, newPodName, pod.getDataCenterId(), gateway, cidr, startIp, endIp, checkForDuplicates);
boolean checkForDuplicates = !oldPodName.equals(name);
checkPodAttributes(id, name, pod.getDataCenterId(), gateway, cidr, startIp, endIp, checkForDuplicates);
String cidrAddress = getCidrAddress(cidr);
long cidrSize = getCidrSize(cidr);
@ -454,14 +486,14 @@ public class ConfigurationManagerImpl implements ConfigurationManager {
ipRange = pod.getDescription();
}
pod.setName(newPodName);
pod.setName(name);
pod.setDataCenterId(zoneId);
pod.setGateway(gateway);
pod.setCidrAddress(cidrAddress);
pod.setCidrSize(cidrSize);
pod.setDescription(ipRange);
if (!_podDao.update(podId, pod)) {
if (!_podDao.update(id, pod)) {
throw new InternalErrorException("Failed to edit pod. Please contact Cloud Support.");
}
@ -472,8 +504,7 @@ public class ConfigurationManagerImpl implements ConfigurationManager {
throw new InternalErrorException("Failed to edit pod. Please contact Cloud Support.");
}
DataCenterVO zone = _zoneDao.findById(zoneId);
saveConfigurationEvent(userId, null, EventTypes.EVENT_POD_EDIT, "Successfully edited pod. New pod name is: " + newPodName + " and new zone name is: " + zone.getName() + ".", "podId=" + pod.getId(), "dcId=" + zone.getId(), "gateway=" + gateway, "cidr=" + cidr, "startIp=" + startIp, "endIp=" + endIp);
saveConfigurationEvent(userId, null, EventTypes.EVENT_POD_EDIT, "Successfully edited pod. New pod name is: " + name + " and new zone name is: " + zone.getName() + ".", "podId=" + pod.getId(), "dcId=" + zone.getId(), "gateway=" + gateway, "cidr=" + cidr, "startIp=" + startIp, "endIp=" + endIp);
return pod;
}

View File

@ -991,7 +991,7 @@ public interface ManagementServer {
* @param tags tags for the service offering. if null, no change will be made. if empty string, all tags will be removed.
* @return the updated ServiceOfferingVO
*/
ServiceOfferingVO updateServiceOffering(long userId, long serviceOfferingId, String name, String displayText, Boolean offerHA, Boolean useVirtualNetwork, String tags);
// ServiceOfferingVO updateServiceOffering(long userId, long serviceOfferingId, String name, String displayText, Boolean offerHA, Boolean useVirtualNetwork, String tags);
/**
* Adds a new pod to the database
@ -1017,7 +1017,7 @@ public interface ManagementServer {
* @param endIp
* @return Pod
*/
HostPodVO editPod(long userId, long podId, String newPodName, String gateway, String cidr, String startIp, String endIp) throws InvalidParameterValueException, InternalErrorException;
// HostPodVO editPod(long userId, long podId, String newPodName, String gateway, String cidr, String startIp, String endIp) throws InvalidParameterValueException, InternalErrorException;
// /**
// * Deletes a pod from the database

View File

@ -4328,10 +4328,10 @@ public class ManagementServerImpl implements ManagementServer {
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 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 {