Refactored updateDiskOffering and deleteDiskOffering commands

This commit is contained in:
alena 2010-08-12 11:27:37 -07:00
parent 96f76062f9
commit a9d48a1b8a
6 changed files with 206 additions and 151 deletions

View File

@ -983,14 +983,6 @@ public interface ManagementServer {
*/
ServiceOfferingVO updateServiceOffering(long userId, long serviceOfferingId, String name, String displayText, Boolean offerHA, Boolean useVirtualNetwork, String tags);
/**
* Deletes a service offering
* @param userId
* @param serviceOfferingId
* @return success/failure
*/
boolean deleteServiceOffering(long userId, long serviceOfferingId);
/**
* Adds a new pod to the database
* @param userId
@ -1825,24 +1817,7 @@ public interface ManagementServer {
* @return a list of disk offerings that match the given criteria
*/
List<DiskOfferingVO> searchForDiskOfferings(Criteria c);
/**
* Delete a disk offering
* @param id id of the disk offering to delete
* @return true if deleted, false otherwise
*/
boolean deleteDiskOffering(long id);
/**
* Update a disk offering
* @param userId
* @param disk offering id
* @param name the name of the disk offering to be updated
* @param description a string description of the disk offering to be updated
* @param tags for the disk offering. if null, no change will be made. if empty string, all tags will be removed.
* @return updated disk offering
*/
DiskOfferingVO updateDiskOffering(long userId, long diskOfferingId, String name, String description, String tags);
/**
*

View File

@ -16,29 +16,20 @@
*
*/
package com.cloud.api.commands;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
package com.cloud.api.commands;
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.storage.DiskOfferingVO;
import com.cloud.utils.Pair;
import com.cloud.api.BaseCmd.Manager;
@Implementation(method="deleteDiskOffering", manager=Manager.ConfigManager)
public class DeleteDiskOfferingCmd extends BaseCmd {
public static final Logger s_logger = Logger.getLogger(DeleteDiskOfferingCmd.class.getName());
private static final String s_name = "deletediskofferingresponse";
private static final List<Pair<Enum, Boolean>> s_properties = new ArrayList<Pair<Enum, Boolean>>();
static {
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.ID, Boolean.TRUE));
}
/////////////////////////////////////////////////////
//////////////// API parameters /////////////////////
@ -65,24 +56,11 @@ public class DeleteDiskOfferingCmd 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 id = (Long)params.get(BaseCmd.Properties.ID.getName());
//verify input parameters
DiskOfferingVO disk = getManagementServer().findDiskOfferingById(id);
if (disk == null) {
throw new ServerApiException(BaseCmd.PARAM_ERROR, "unable to find a disk offering with id " + id);
}
boolean result = getManagementServer().deleteDiskOffering(id);
List<Pair<String, Object>> returnValues = new ArrayList<Pair<String, Object>>();
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.SUCCESS.getName(), Boolean.valueOf(result).toString()));
return returnValues;
@Override
public String getResponse() {
// There's no specific response for this command, if the command failed an exception would have been thrown. If we are here, then it succeeded.
// Seems like we should return success/true as the response though, so this will probably have to change.
return null;
}
}

View File

@ -16,38 +16,30 @@
*
*/
package com.cloud.api.commands;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
package com.cloud.api.commands;
import org.apache.log4j.Logger;
import java.util.Date;
import com.cloud.api.BaseCmd;
import com.cloud.api.Implementation;
import com.cloud.api.Parameter;
import com.cloud.api.ServerApiException;
import com.cloud.api.BaseCmd.Manager;
import com.cloud.serializer.Param;
import com.cloud.serializer.SerializerHelper;
import com.cloud.storage.DiskOfferingVO;
import com.cloud.user.User;
import com.cloud.utils.Pair;
@Implementation(method="updateDiskOffering", manager=Manager.ConfigManager)
public class UpdateDiskOfferingCmd extends BaseCmd{
public static final Logger s_logger = Logger.getLogger(UpdateDiskOfferingCmd.class.getName());
private static final String s_name = "updatediskofferingresponse";
private static final List<Pair<Enum, Boolean>> s_properties = new ArrayList<Pair<Enum, Boolean>>();
static {
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.DISPLAY_TEXT, 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.TAGS, Boolean.FALSE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.USER_ID, Boolean.FALSE));
}
/////////////////////////////////////////////////////
//////////////// API parameters /////////////////////
/////////////////////////////////////////////////////
@Parameter(name="displaytext", type=CommandType.STRING)
private String displayText;
@ -84,43 +76,125 @@ public class UpdateDiskOfferingCmd extends BaseCmd{
/////////////////////////////////////////////////////
/////////////// API Implementation///////////////////
/////////////////////////////////////////////////////
private DiskOfferingVO responseObject = null;
@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) {
Long id = (Long)params.get(BaseCmd.Properties.ID.getName());
String name = (String)params.get(BaseCmd.Properties.NAME.getName());
String displayText = (String)params.get(BaseCmd.Properties.DISPLAY_TEXT.getName());
Long userId = (Long)params.get(BaseCmd.Properties.USER_ID.getName());
String tags = (String)params.get(BaseCmd.Properties.TAGS.getName());
if (userId == null) {
userId = Long.valueOf(User.UID_SYSTEM);
}
public String getResponse() {
DiskOfferingResponse response = new DiskOfferingResponse();
if (responseObject != null) {
response.setId(responseObject.getId());
response.setCreated(responseObject.getCreated());
response.setDiskSize(responseObject.getDiskSize());
response.setDisplayText(responseObject.getDisplayText());
response.setDomainId(responseObject.getDomainId());
// FIXME: domain name in the response
// response.setDomain(responseObject.getDomain());
response.setName(responseObject.getName());
response.setTags(responseObject.getTags());
} else {
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to update disk offering");
}
return SerializerHelper.toSerializedString(responseObject);
}
public void setResponseObject(DiskOfferingVO diskOffering) {
responseObject = diskOffering;
}
// helper class for the response object
private class DiskOfferingResponse {
@Param(name="id")
private Long id;
@Param(name="domainid")
private Long domainId;
@Param(name="domain")
private String domain;
@Param(name="name")
private String name;
@Param(name="displaytext")
private String displayText;
@Param(name="disksize")
private Long diskSize;
@Param(name="created")
private Date created;
@Param(name="tags")
private String tags;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Long getDomainId() {
return domainId;
}
public void setDomainId(Long domainId) {
this.domainId = domainId;
}
public String getDomain() {
return domain;
}
public void setDomain(String domain) {
this.domain = domain;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDisplayText() {
return displayText;
}
public void setDisplayText(String displayText) {
this.displayText = displayText;
}
public Long getDiskSize() {
return diskSize;
}
public void setDiskSize(Long diskSize) {
this.diskSize = diskSize;
}
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
this.created = created;
}
public String getTags() {
return tags;
}
public void setTags(String tags) {
this.tags = tags;
}
//Verify input parameters
DiskOfferingVO offering = getManagementServer().findDiskOfferingById(id);
if (offering == null) {
throw new ServerApiException(BaseCmd.PARAM_ERROR, "unable to find disk offering " + id);
}
try {
getManagementServer().updateDiskOffering(userId, id, name, displayText, tags);
} catch (Exception ex) {
s_logger.error("Exception updating disk offering", ex);
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to update disk offering " + id + ": internal error.");
}
List<Pair<String, Object>> returnValues = new ArrayList<Pair<String, Object>>();
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.SUCCESS.getName(), Boolean.TRUE));
return returnValues;
}
}

View File

@ -20,6 +20,8 @@ package com.cloud.configuration;
import java.util.List;
import com.cloud.api.commands.CreateDiskOfferingCmd;
import com.cloud.api.commands.DeleteDiskOfferingCmd;
import com.cloud.api.commands.UpdateDiskOfferingCmd;
import com.cloud.dc.DataCenterVO;
import com.cloud.dc.HostPodVO;
import com.cloud.dc.Vlan.VlanType;
@ -81,14 +83,21 @@ public interface ConfigurationManager extends Manager {
* @param tags
* @return updated disk offering
*/
DiskOfferingVO updateDiskOffering(long userId, long diskOfferingId, String name, String description, String tags);
DiskOfferingVO updateDiskOffering(UpdateDiskOfferingCmd cmd);
/**
* Deletes a disk offering
* @param userId
* @param diskOfferingId
*/
boolean deleteDiskOffering(DeleteDiskOfferingCmd cmd) throws InvalidParameterValueException;
/**
* Deletes a service offering
* @param userId
* @param serviceOfferingId
*/
boolean deleteServiceOffering(long userId, long serviceOfferingId);
boolean deleteServiceOffering(long userId, long serviceOfferingId) throws InvalidParameterValueException;
/**
* Creates a new disk offering

View File

@ -32,6 +32,8 @@ import javax.naming.ConfigurationException;
import org.apache.log4j.Logger;
import com.cloud.api.commands.CreateDiskOfferingCmd;
import com.cloud.api.commands.DeleteDiskOfferingCmd;
import com.cloud.api.commands.UpdateDiskOfferingCmd;
import com.cloud.configuration.dao.ConfigurationDao;
import com.cloud.dc.AccountVlanMapVO;
import com.cloud.dc.DataCenterVO;
@ -151,6 +153,7 @@ public class ConfigurationManagerImpl implements ConfigurationManager {
saveConfigurationEvent(userId, null, EventTypes.EVENT_CONFIGURATION_VALUE_EDIT, "Successfully edited configuration value.", "name=" + name, "value=" + value);
}
private String validateConfigurationValue(String name, String value) throws InvalidParameterValueException {
if (value == null) {
return null;
@ -864,7 +867,37 @@ public class ConfigurationManagerImpl implements ConfigurationManager {
}
}
public DiskOfferingVO updateDiskOffering(long userId, long diskOfferingId, String name, String displayText, String tags) {
@Override
public DiskOfferingVO createDiskOffering(CreateDiskOfferingCmd cmd) throws InvalidParameterValueException {
Long domainId = cmd.getDomainId();
String name = cmd.getOfferingName();
String description = cmd.getDisplayText();
int numGibibytes = cmd.getDiskSize().intValue();
String tags = cmd.getTags();
if (domainId == null) {
domainId = Long.valueOf(DomainVO.ROOT_DOMAIN);
}
if ((numGibibytes != 0) && (numGibibytes < 1)) {
throw new InvalidParameterValueException("Please specify a disk size of at least 1 Gb.");
} else if (numGibibytes > _maxVolumeSizeInGb) {
throw new InvalidParameterValueException("The maximum size for a disk is " + _maxVolumeSizeInGb + " Gb.");
}
long diskSize = numGibibytes * 1024;
tags = cleanupTags(tags);
DiskOfferingVO newDiskOffering = new DiskOfferingVO(domainId, name, description, diskSize,tags);
return _diskOfferingDao.persist(newDiskOffering);
}
public DiskOfferingVO updateDiskOffering(UpdateDiskOfferingCmd cmd) {
Long diskOfferingId = cmd.getId();
String name = cmd.getName();
String displayText = cmd.getDisplayText();
String tags = cmd.getTags();
boolean updateNeeded = (name != null || displayText != null || tags != null);
if (!updateNeeded) {
return _diskOfferingDao.findById(diskOfferingId);
@ -895,9 +928,31 @@ public class ConfigurationManagerImpl implements ConfigurationManager {
}
}
public boolean deleteServiceOffering(long userId, long serviceOfferingId) {
public boolean deleteDiskOffering(DeleteDiskOfferingCmd cmd) throws InvalidParameterValueException{
Long diskOfferingId = cmd.getId();
DiskOfferingVO offering = _diskOfferingDao.findById(diskOfferingId);
if (offering == null) {
throw new InvalidParameterValueException("Unable to find disk offering by id " + diskOfferingId);
}
if (_diskOfferingDao.remove(diskOfferingId)) {
return true;
} else {
return false;
}
}
public boolean deleteServiceOffering(long userId, long serviceOfferingId) throws InvalidParameterValueException{
ServiceOfferingVO offering = _serviceOfferingDao.findById(serviceOfferingId);
if (offering == null) {
throw new InvalidParameterValueException("Unable to find service offering by id " + serviceOfferingId);
}
if (_serviceOfferingDao.remove(serviceOfferingId)) {
saveConfigurationEvent(userId, null, EventTypes.EVENT_SERVICE_OFFERING_EDIT, "Successfully deleted service offering with name: " + offering.getName(), "soId=" + serviceOfferingId, "name=" + offering.getName(),
"displayText=" + offering.getDisplayText(), "offerHA=" + offering.getOfferHA(), "useVirtualNetwork=" + (offering.getGuestIpType() == GuestIpType.Virtualized));
@ -907,29 +962,7 @@ public class ConfigurationManagerImpl implements ConfigurationManager {
}
}
@Override
public DiskOfferingVO createDiskOffering(CreateDiskOfferingCmd cmd) throws InvalidParameterValueException {
Long domainId = cmd.getDomainId();
String name = cmd.getOfferingName();
String description = cmd.getDisplayText();
int numGibibytes = cmd.getDiskSize().intValue();
String tags = cmd.getTags();
if (domainId == null) {
domainId = Long.valueOf(DomainVO.ROOT_DOMAIN);
}
if ((numGibibytes != 0) && (numGibibytes < 1)) {
throw new InvalidParameterValueException("Please specify a disk size of at least 1 Gb.");
} else if (numGibibytes > _maxVolumeSizeInGb) {
throw new InvalidParameterValueException("The maximum size for a disk is " + _maxVolumeSizeInGb + " Gb.");
}
long diskSize = numGibibytes * 1024;
tags = cleanupTags(tags);
DiskOfferingVO newDiskOffering = new DiskOfferingVO(domainId, name, description, diskSize,tags);
return _diskOfferingDao.persist(newDiskOffering);
}
public String changePrivateIPRange(boolean add, long podId, String startIP, String endIP) throws InvalidParameterValueException {
checkPrivateIpRangeErrors(podId, startIP, endIP);

View File

@ -4219,10 +4219,6 @@ public class ManagementServerImpl implements ManagementServer {
return _configMgr.updateServiceOffering(userId, serviceOfferingId, name, displayText, offerHA, useVirtualNetwork, tags);
}
@Override
public boolean deleteServiceOffering(long userId, long serviceOfferingId) {
return _configMgr.deleteServiceOffering(userId, serviceOfferingId);
}
@Override
public HostPodVO createPod(long userId, String podName, Long zoneId, String gateway, String cidr, String startIp, String endIp) throws InvalidParameterValueException, InternalErrorException {
@ -6727,16 +6723,6 @@ public class ManagementServerImpl implements ManagementServer {
return _diskOfferingDao.search(sc, searchFilter);
}
@Override
public DiskOfferingVO updateDiskOffering(long userId, long diskOfferingId, String name, String description, String tags) {
return _configMgr.updateDiskOffering(userId, diskOfferingId, name, description, tags);
}
@Override
public boolean deleteDiskOffering(long id) {
return _diskOfferingDao.remove(Long.valueOf(id));
}
@Override
public AsyncJobResult queryAsyncJobResult(long jobId) throws PermissionDeniedException {
AsyncJobVO job = _asyncMgr.getAsyncJob(jobId);