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; package com.cloud.api.commands;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import com.cloud.api.BaseCmd; import com.cloud.api.BaseCmd;
import com.cloud.api.Implementation;
import com.cloud.api.Parameter; import com.cloud.api.Parameter;
import com.cloud.api.ServerApiException; import com.cloud.api.BaseCmd.Manager;
import com.cloud.dc.DataCenterVO;
import com.cloud.dc.HostPodVO;
import com.cloud.user.User;
import com.cloud.utils.Pair;
@Implementation(method="editPod", manager=Manager.ConfigManager)
public class UpdatePodCmd extends BaseCmd { public class UpdatePodCmd extends BaseCmd {
public static final Logger s_logger = Logger.getLogger(UpdatePodCmd.class.getName()); public static final Logger s_logger = Logger.getLogger(UpdatePodCmd.class.getName());
private static final String s_name = "updatepodresponse"; 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 ///////////////////// //////////////// API parameters /////////////////////
@ -106,57 +88,60 @@ public class UpdatePodCmd extends BaseCmd {
public String getName() { public String getName() {
return s_name; return s_name;
} }
public List<Pair<Enum, Boolean>> getProperties() {
return s_properties;
}
@Override @Override
public List<Pair<String, Object>> execute(Map<String, Object> params) { public String getResponse() {
Long podId = (Long) params.get(BaseCmd.Properties.ID.getName()); // TODO Auto-generated method stub
String podName = (String) params.get(BaseCmd.Properties.NAME.getName()); return null;
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>>(); // @Override
// public List<Pair<String, Object>> execute(Map<String, Object> params) {
if (updatedPod == null) { // Long podId = (Long) params.get(BaseCmd.Properties.ID.getName());
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to update pod; internal error."); // String podName = (String) params.get(BaseCmd.Properties.NAME.getName());
} else { // String gateway = (String) params.get(BaseCmd.Properties.GATEWAY.getName());
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.SUCCESS.getName(), "true")); // String cidr = (String) params.get(BaseCmd.Properties.CIDR.getName());
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.DISPLAY_TEXT.getName(), "Successfully updated pod.")); // 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());
return returnValues; //
} // 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.DeletePodCmd;
import com.cloud.api.commands.UpdateCfgCmd; import com.cloud.api.commands.UpdateCfgCmd;
import com.cloud.api.commands.UpdateDiskOfferingCmd; import com.cloud.api.commands.UpdateDiskOfferingCmd;
import com.cloud.api.commands.UpdatePodCmd;
import com.cloud.api.commands.UpdateServiceOfferingCmd; import com.cloud.api.commands.UpdateServiceOfferingCmd;
import com.cloud.api.commands.UpdateZoneCmd; import com.cloud.api.commands.UpdateZoneCmd;
import com.cloud.dc.DataCenterVO; import com.cloud.dc.DataCenterVO;
@ -149,9 +150,11 @@ public interface ConfigurationManager extends Manager {
* @param startIp * @param startIp
* @param endIp * @param endIp
* @return Pod * @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. * Deletes a pod from the database. Will not allow you to delete pods that are being used anywhere in the system.
* @param userId * @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.DeletePodCmd;
import com.cloud.api.commands.UpdateCfgCmd; import com.cloud.api.commands.UpdateCfgCmd;
import com.cloud.api.commands.UpdateDiskOfferingCmd; import com.cloud.api.commands.UpdateDiskOfferingCmd;
import com.cloud.api.commands.UpdatePodCmd;
import com.cloud.api.commands.UpdateServiceOfferingCmd; import com.cloud.api.commands.UpdateServiceOfferingCmd;
import com.cloud.api.commands.UpdateZoneCmd; import com.cloud.api.commands.UpdateZoneCmd;
import com.cloud.configuration.dao.ConfigurationDao; import com.cloud.configuration.dao.ConfigurationDao;
@ -105,6 +106,8 @@ public class ConfigurationManagerImpl implements ConfigurationManager {
@Inject AccountDao _accountDao; @Inject AccountDao _accountDao;
@Inject EventDao _eventDao; @Inject EventDao _eventDao;
@Inject UserDao _userDao; @Inject UserDao _userDao;
@Inject DataCenterDao _dcDao;
@Inject HostPodDao _hostPodDao;
public boolean _premium; public boolean _premium;
private int _maxVolumeSizeInGb; private int _maxVolumeSizeInGb;
@ -395,25 +398,54 @@ public class ConfigurationManagerImpl implements ConfigurationManager {
} }
@DB @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 // Make sure the pod exists
if (!validPod(podId)) { if (!validPod(id)) {
throw new InvalidParameterValueException("A pod with ID: " + podId + " does not exist."); 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 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 (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."); 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(); String oldPodName = pod.getName();
long zoneId = pod.getDataCenterId();
if (newPodName == null) { if (name == null) {
newPodName = oldPodName; name = oldPodName;
} }
if (gateway == null) { if (gateway == null) {
@ -424,8 +456,8 @@ public class ConfigurationManagerImpl implements ConfigurationManager {
cidr = pod.getCidrAddress() + "/" + pod.getCidrSize(); cidr = pod.getCidrAddress() + "/" + pod.getCidrSize();
} }
boolean checkForDuplicates = !oldPodName.equals(newPodName); boolean checkForDuplicates = !oldPodName.equals(name);
checkPodAttributes(podId, newPodName, pod.getDataCenterId(), gateway, cidr, startIp, endIp, checkForDuplicates); checkPodAttributes(id, name, pod.getDataCenterId(), gateway, cidr, startIp, endIp, checkForDuplicates);
String cidrAddress = getCidrAddress(cidr); String cidrAddress = getCidrAddress(cidr);
long cidrSize = getCidrSize(cidr); long cidrSize = getCidrSize(cidr);
@ -454,14 +486,14 @@ public class ConfigurationManagerImpl implements ConfigurationManager {
ipRange = pod.getDescription(); ipRange = pod.getDescription();
} }
pod.setName(newPodName); pod.setName(name);
pod.setDataCenterId(zoneId); pod.setDataCenterId(zoneId);
pod.setGateway(gateway); pod.setGateway(gateway);
pod.setCidrAddress(cidrAddress); pod.setCidrAddress(cidrAddress);
pod.setCidrSize(cidrSize); pod.setCidrSize(cidrSize);
pod.setDescription(ipRange); pod.setDescription(ipRange);
if (!_podDao.update(podId, pod)) { if (!_podDao.update(id, pod)) {
throw new InternalErrorException("Failed to edit pod. Please contact Cloud Support."); 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."); 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: " + name + " 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: " + newPodName + " and new zone name is: " + zone.getName() + ".", "podId=" + pod.getId(), "dcId=" + zone.getId(), "gateway=" + gateway, "cidr=" + cidr, "startIp=" + startIp, "endIp=" + endIp);
return pod; 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. * @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 * @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 * Adds a new pod to the database
@ -1017,7 +1017,7 @@ public interface ManagementServer {
* @param endIp * @param endIp
* @return Pod * @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 // * 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); return _configMgr.createPod(userId, podName, zoneId, gateway, cidr, startIp, endIp);
} }
@Override // @Override
public HostPodVO editPod(long userId, long podId, String newPodName, String gateway, String cidr, String startIp, String endIp) throws InvalidParameterValueException, InternalErrorException { // 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); // return _configMgr.editPod(userId, podId, newPodName, gateway, cidr, startIp, endIp);
} // }
// @Override // @Override
// public void deletePod(long userId, long podId) throws InvalidParameterValueException, InternalErrorException { // public void deletePod(long userId, long podId) throws InvalidParameterValueException, InternalErrorException {