Refactoring createServiceOffering to the new API framework, just a simple database create that has been moved from a management server proxy method that calls configuration manager directly into the configuration manager.

This commit is contained in:
Kris McQueen 2010-08-18 17:25:56 -07:00
parent 88395cfcd8
commit 96f999a375
6 changed files with 223 additions and 195 deletions

View File

@ -18,39 +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.offering.ServiceOffering.GuestIpType;
import com.cloud.api.response.ServiceOfferingResponse;
import com.cloud.serializer.SerializerHelper;
import com.cloud.service.ServiceOfferingVO;
import com.cloud.user.User;
import com.cloud.utils.Pair;
public class CreateServiceOfferingCmd extends BaseCmd{
@Implementation(method="createServiceOffering", manager=Manager.ConfigManager)
public class CreateServiceOfferingCmd extends BaseCmd {
public static final Logger s_logger = Logger.getLogger(CreateServiceOfferingCmd.class.getName());
private static final String _name = "createserviceofferingresponse";
private static final List<Pair<Enum, Boolean>> s_properties = new ArrayList<Pair<Enum, Boolean>>();
static {
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.CPU_NUMBER, Boolean.TRUE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.CPU_SPEED, Boolean.TRUE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.DISPLAY_TEXT, Boolean.TRUE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.MEMORY, Boolean.TRUE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.NAME, Boolean.TRUE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.OFFER_HA, Boolean.FALSE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.STORAGE_TYPE, Boolean.FALSE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.TAGS, Boolean.FALSE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.USE_VIRTUAL_NETWORK, Boolean.FALSE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.USER_ID, Boolean.FALSE));
}
/////////////////////////////////////////////////////
//////////////// API parameters /////////////////////
/////////////////////////////////////////////////////
@ -82,7 +64,6 @@ public class CreateServiceOfferingCmd extends BaseCmd{
@Parameter(name="usevirtualnetwork", type=CommandType.BOOLEAN)
private Boolean useVirtualNetwork;
/////////////////////////////////////////////////////
/////////////////// Accessors ///////////////////////
/////////////////////////////////////////////////////
@ -123,7 +104,6 @@ public class CreateServiceOfferingCmd extends BaseCmd{
return useVirtualNetwork;
}
/////////////////////////////////////////////////////
/////////////// API Implementation///////////////////
/////////////////////////////////////////////////////
@ -132,98 +112,23 @@ public class CreateServiceOfferingCmd extends BaseCmd{
public String getName() {
return _name;
}
@Override
public List<Pair<Enum, Boolean>> getProperties (){
return s_properties;
}
@Override
public List<Pair<String, Object>> execute(Map<String, Object> params) {
// FIXME: add domain-private service offerings
// Account account = (Account)params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName());
// Long userId = (Long)params.get(BaseCmd.Properties.USER_ID.getName());
// Long domainId = (Long)params.get(BaseCmd.Properties.DOMAIN_ID.getName());
String name = (String)params.get(BaseCmd.Properties.NAME.getName());
String displayText = (String)params.get(BaseCmd.Properties.DISPLAY_TEXT.getName());
Long cpuNumber = (Long)params.get(BaseCmd.Properties.CPU_NUMBER.getName());
Long cpuSpeed = (Long)params.get(BaseCmd.Properties.CPU_SPEED.getName());
Long memory = (Long)params.get(BaseCmd.Properties.MEMORY.getName());
String storageType = (String) params.get(BaseCmd.Properties.STORAGE_TYPE.getName());
Boolean offerHA = (Boolean) params.get(BaseCmd.Properties.OFFER_HA.getName());
Boolean useVirtualNetwork = (Boolean) params.get(BaseCmd.Properties.USE_VIRTUAL_NETWORK.getName());
Long userId = (Long)params.get(BaseCmd.Properties.USER_ID.getName());
String tags = (String)params.get(BaseCmd.Properties.TAGS.getName());
public String getResponse() {
ServiceOfferingVO offering = (ServiceOfferingVO)getResponseObject();
if (userId == null) {
userId = Long.valueOf(User.UID_SYSTEM);
}
if (name.length() == 0) {
throw new ServerApiException(BaseCmd.PARAM_ERROR, "Failed to create service offering: specify the name that has non-zero length");
}
if (displayText.length() == 0) {
throw new ServerApiException(BaseCmd.PARAM_ERROR, "Failed to create service offering: specify the display text that has non-zero length");
}
if ((cpuNumber.intValue() <= 0) || (cpuNumber.intValue() > 2147483647)) {
throw new ServerApiException(BaseCmd.PARAM_ERROR, "Failed to create service offering: specify the cpu number value between 1 and 2147483647");
}
if ((cpuSpeed.intValue() <= 0) || (cpuSpeed.intValue() > 2147483647)) {
throw new ServerApiException(BaseCmd.PARAM_ERROR, "Failed to create service offering: specify the cpu speed value between 1 and 2147483647");
}
if ((memory.intValue() <= 0) || (memory.intValue() > 2147483647)) {
throw new ServerApiException(BaseCmd.PARAM_ERROR, "Failed to create service offering: specify the memory value between 1 and 2147483647");
}
boolean localStorageRequired;
if (storageType == null) {
localStorageRequired = false;
} else if (storageType.equals("local")) {
localStorageRequired = true;
} else if (storageType.equals("shared")) {
localStorageRequired = false;
} else {
throw new ServerApiException(BaseCmd.PARAM_ERROR, "Valid pool types are: 'local' and 'shared'");
}
ServiceOfferingResponse response = new ServiceOfferingResponse();
response.setId(offering.getId());
response.setName(offering.getName());
response.setDisplayText(offering.getDisplayText());
response.setCpuNumber(offering.getCpu());
response.setCpuSpeed(offering.getSpeed());
response.setCreated(offering.getCreated());
response.setMemory(offering.getRamSize());
response.setOfferHa(offering.getOfferHA());
response.setStorageType(offering.getUseLocalStorage() ? "local" : "shared");
response.setTags(offering.getTags());
if (offerHA == null) {
offerHA = false;
}
if (useVirtualNetwork == null) {
useVirtualNetwork = Boolean.TRUE;
}
ServiceOfferingVO offering = null;
try {
offering = getManagementServer().createServiceOffering(userId, name, cpuNumber.intValue(), memory.intValue(), cpuSpeed.intValue(), displayText, localStorageRequired, offerHA, useVirtualNetwork, tags);
} catch (Exception ex) {
s_logger.error("Exception creating service offering", ex);
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to create service offering " + name + ": internal error.");
}
List<Pair<String, Object>> returnValues = new ArrayList<Pair<String, Object>>();
if (offering == null) {
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to create service offering " + name);
} else {
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.ID.getName(), offering.getId()));
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.NAME.getName(), offering.getName()));
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.DISPLAY_TEXT.getName(), offering.getDisplayText()));
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.CPU_NUMBER.getName(), Integer.valueOf(offering.getCpu()).toString()));
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.CPU_SPEED.getName(), Integer.valueOf(offering.getSpeed()).toString()));
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.MEMORY.getName(), Integer.valueOf(offering.getRamSize()).toString()));
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.CREATED.getName(), getDateString(offering.getCreated())));
storageType = offering.getUseLocalStorage() ? "local" : "shared";
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.STORAGE_TYPE.getName(), storageType));
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.OFFER_HA.getName(), offering.getOfferHA()));
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.USE_VIRTUAL_NETWORK.getName(), (offering.getGuestIpType().equals(GuestIpType.Virtualized))));
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.TAGS.getName(), offering.getTags()));
}
return returnValues;
return SerializerHelper.toSerializedString(response);
}
}

View File

@ -0,0 +1,129 @@
package com.cloud.api.response;
import java.util.Date;
import com.cloud.api.ResponseObject;
import com.cloud.serializer.Param;
public class ServiceOfferingResponse implements ResponseObject {
@Param(name="id")
private Long id;
@Param(name="name")
private String name;
@Param(name="displaytext")
private String displayText;
@Param(name="cpunumber")
private int cpuNumber;
@Param(name="cpuspeed")
private int cpuSpeed;
@Param(name="memory")
private int memory;
@Param(name="created")
private Date created;
@Param(name="storagetype")
private String storageType;
@Param(name="offerha")
private Boolean offerHa;
@Param(name="usevirtualnetwork")
private Boolean useVirtualNetwork;
@Param(name="tags")
private String tags;
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 String getDisplayText() {
return displayText;
}
public void setDisplayText(String displayText) {
this.displayText = displayText;
}
public int getCpuNumber() {
return cpuNumber;
}
public void setCpuNumber(int cpuNumber) {
this.cpuNumber = cpuNumber;
}
public int getCpuSpeed() {
return cpuSpeed;
}
public void setCpuSpeed(int cpuSpeed) {
this.cpuSpeed = cpuSpeed;
}
public int getMemory() {
return memory;
}
public void setMemory(int memory) {
this.memory = memory;
}
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
this.created = created;
}
public String getStorageType() {
return storageType;
}
public void setStorageType(String storageType) {
this.storageType = storageType;
}
public Boolean getOfferHa() {
return offerHa;
}
public void setOfferHa(Boolean offerHa) {
this.offerHa = offerHa;
}
public Boolean getUseVirtualNetwork() {
return useVirtualNetwork;
}
public void setUseVirtualNetwork(Boolean useVirtualNetwork) {
this.useVirtualNetwork = useVirtualNetwork;
}
public String getTags() {
return tags;
}
public void setTags(String tags) {
this.tags = tags;
}
}

View File

@ -22,6 +22,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.CreateServiceOfferingCmd;
import com.cloud.api.commands.DeleteDiskOfferingCmd;
import com.cloud.api.commands.DeletePodCmd;
import com.cloud.api.commands.UpdateCfgCmd;
@ -61,8 +62,7 @@ public interface ConfigurationManager extends Manager {
* @param value
*/
void updateConfiguration(UpdateCfgCmd cmd) throws InvalidParameterValueException, InternalErrorException;
/**
* Creates a new service offering
* @param id
@ -77,7 +77,14 @@ public interface ConfigurationManager extends Manager {
* @return ID
*/
ServiceOfferingVO createServiceOffering(long userId, String name, int cpu, int ramSize, int speed, String displayText, boolean localStorageRequired, boolean offerHA, boolean useVirtualNetwork, String tags);
/**
* Create a service offering through the API
* @param cmd the command object that specifies the name, number of cpu cores, amount of RAM, etc. for the service offering
* @return the newly created service offering if successful, null otherwise
*/
ServiceOfferingVO createServiceOffering(CreateServiceOfferingCmd cmd) throws InvalidParameterValueException;
/**
* Updates a service offering
* @param serviceOfferingId

View File

@ -36,6 +36,7 @@ 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.CreateServiceOfferingCmd;
import com.cloud.api.commands.DeleteDiskOfferingCmd;
import com.cloud.api.commands.DeletePodCmd;
import com.cloud.api.commands.UpdateCfgCmd;
@ -69,7 +70,6 @@ import com.cloud.service.ServiceOfferingVO;
import com.cloud.service.dao.ServiceOfferingDao;
import com.cloud.storage.DiskOfferingVO;
import com.cloud.storage.dao.DiskOfferingDao;
import com.cloud.user.Account;
import com.cloud.user.AccountVO;
import com.cloud.user.User;
import com.cloud.user.UserContext;
@ -77,7 +77,6 @@ import com.cloud.user.UserVO;
import com.cloud.user.dao.AccountDao;
import com.cloud.user.dao.UserDao;
import com.cloud.utils.NumbersUtil;
import com.cloud.utils.Pair;
import com.cloud.utils.component.Inject;
import com.cloud.utils.db.DB;
import com.cloud.utils.db.Transaction;
@ -911,7 +910,66 @@ public class ConfigurationManagerImpl implements ConfigurationManager {
return zone;
}
@Override
public ServiceOfferingVO createServiceOffering(CreateServiceOfferingCmd cmd) throws InvalidParameterValueException {
Long userId = UserContext.current().getUserId();
if (userId == null) {
userId = User.UID_SYSTEM;
}
String name = cmd.getServiceOfferingName();
if ((name == null) || (name.length() == 0)) {
throw new InvalidParameterValueException("Failed to create service offering: specify the name that has non-zero length");
}
String displayText = cmd.getDisplayText();
if ((displayText == null) || (displayText.length() == 0)) {
throw new InvalidParameterValueException("Failed to create service offering " + name + ": specify the display text that has non-zero length");
}
Long cpuNumber = cmd.getCpuNumber();
if ((cpuNumber == null) || (cpuNumber.intValue() <= 0) || (cpuNumber.intValue() > 2147483647)) {
throw new InvalidParameterValueException("Failed to create service offering " + name + ": specify the cpu number value between 1 and 2147483647");
}
Long cpuSpeed = cmd.getCpuSpeed();
if ((cpuSpeed == null) || (cpuSpeed.intValue() <= 0) || (cpuSpeed.intValue() > 2147483647)) {
throw new ServerApiException(BaseCmd.PARAM_ERROR, "Failed to create service offering " + name + ": specify the cpu speed value between 1 and 2147483647");
}
Long memory = cmd.getMemory();
if ((memory == null) || (memory.intValue() <= 0) || (memory.intValue() > 2147483647)) {
throw new ServerApiException(BaseCmd.PARAM_ERROR, "Failed to create service offering " + name + ": specify the memory value between 1 and 2147483647");
}
boolean localStorageRequired = false;
String storageType = cmd.getStorageType();
if (storageType == null) {
localStorageRequired = false;
} else if (storageType.equals("local")) {
localStorageRequired = true;
} else if (storageType.equals("shared")) {
localStorageRequired = false;
} else {
throw new InvalidParameterValueException("Invalid storage type " + storageType + " specified, valid types are: 'local' and 'shared'");
}
Boolean offerHA = cmd.getOfferHa();
if (offerHA == null) {
offerHA = false;
}
Boolean useVirtualNetwork = cmd.getUseVirtualNetwork();
if (useVirtualNetwork == null) {
useVirtualNetwork = Boolean.TRUE;
}
return createServiceOffering(userId, cmd.getServiceOfferingName(), cpuSpeed.intValue(), memory.intValue(), cpuSpeed.intValue(), cmd.getDisplayText(),
localStorageRequired, offerHA, useVirtualNetwork, cmd.getTags());
}
@Override
public ServiceOfferingVO createServiceOffering(long userId, String name, int cpu, int ramSize, int speed, String displayText, boolean localStorageRequired, boolean offerHA, boolean useVirtualNetwork, String tags) {
String networkRateStr = _configDao.getValue("network.throttling.rate");
String multicastRateStr = _configDao.getValue("multicast.throttling.rate");

View File

@ -967,56 +967,7 @@ public interface ManagementServer {
* @return List of Pods
*/
List<HostPodVO> listPods(long dataCenterId);
/**
* Creates a new service offering
* @param userId
* @param name
* @param cpu
* @param ramSize
* @param speed
* @param diskSpace
* @param displayText
* @param localStorageRequired
* @param offerHA
* @param useVirtualNetwork
* @return the new ServiceOfferingVO
*/
ServiceOfferingVO createServiceOffering(long userId, String name, int cpu, int ramSize, int speed, String displayText, boolean localStorageRequired, boolean offerHA, boolean useVirtualNetwork, String tags);
/**
* Updates a service offering
* @param userId
* @param serviceOfferingId
* @param name
* @param displayText
* @param offerHA
* @param useVirtualNetwork
* @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);
/**
* Edits a pod in the database
* @param userId
* @param podId
* @param newPodName
* @param gateway
* @param cidr
* @param startIp
* @param endIp
* @return Pod
*/
// 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
// * @param userId
// * @param podId
// */
// void deletePod(long userId, long podId) throws InvalidParameterValueException, InternalErrorException;
/**
* Adds a new zone to the database
* @param userId
@ -1031,21 +982,6 @@ public interface ManagementServer {
*/
DataCenterVO createZone(long userId, String zoneName, String dns1, String dns2, String dns3, String dns4, String vnetRange, String guestCidr) throws InvalidParameterValueException, InternalErrorException;
// /**
// * Edits a zone in the database
// * @param userId
// * @param zoneId
// * @param newZoneName
// * @param dns1
// * @param dns2
// * @param dns3
// * @param dns4
// * @param vnetRange range of the vnet to add to the zone.
// * @param guestNetworkCidr
// * @return Zone
// */
// DataCenterVO editZone(long userId, Long zoneId, String newZoneName, String dns1, String dns2, String dns3, String dns4, String vnetRange, String guestCidr) throws InvalidParameterValueException, InternalErrorException;
/**
* Deletes a zone from the database
* @param userId
@ -1064,8 +1000,6 @@ public interface ManagementServer {
*/
String changePrivateIPRange(boolean add, Long podId, String startIP, String endIP) throws InvalidParameterValueException;
// List<UserVO> searchUsers(String name);
/**
* Finds users with usernames similar to the parameter
* @param username

View File

@ -4238,11 +4238,6 @@ public class ManagementServerImpl implements ManagementServer {
return _hostPodDao.listByDataCenterId(dataCenterId);
}
@Override
public ServiceOfferingVO createServiceOffering(long userId, String name, int cpu, int ramSize, int speed, String displayText, boolean localStorageRequired, boolean offerHA, boolean useVirtualNetwork, String tags) {
return _configMgr.createServiceOffering(userId, name, cpu, ramSize, speed, displayText, localStorageRequired, offerHA, useVirtualNetwork, tags);
}
@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);