Refactoring update service offering cmd

This commit is contained in:
abhishek 2010-08-17 17:21:04 -07:00
parent 1174486e27
commit 1252a2b8a7
4 changed files with 93 additions and 86 deletions

View File

@ -18,21 +18,12 @@
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.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.api.BaseCmd.Manager;
import com.cloud.offering.ServiceOffering.GuestIpType;
import com.cloud.service.ServiceOfferingVO;
import com.cloud.user.User;
import com.cloud.utils.Pair;
@Implementation(method="updateServiceOffering", manager=Manager.ConfigManager) @Implementation(method="updateServiceOffering", manager=Manager.ConfigManager)
public class UpdateServiceOfferingCmd extends BaseCmd public class UpdateServiceOfferingCmd extends BaseCmd
@ -40,17 +31,6 @@ public class UpdateServiceOfferingCmd extends BaseCmd
public static final Logger s_logger = Logger.getLogger(UpdateServiceOfferingCmd.class.getName()); public static final Logger s_logger = Logger.getLogger(UpdateServiceOfferingCmd.class.getName());
private static final String s_name = "updateserviceofferingresponse"; private static final String s_name = "updateserviceofferingresponse";
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.OFFER_HA, 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 ///////////////////// //////////////// API parameters /////////////////////
///////////////////////////////////////////////////// /////////////////////////////////////////////////////
@ -108,57 +88,60 @@ public class UpdateServiceOfferingCmd extends BaseCmd
@Override @Override
public String getName() { public String getName() {
return s_name; 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 offeringId = (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());
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());
if (userId == null) {
userId = Long.valueOf(User.UID_SYSTEM);
}
// Verify input parameters
ServiceOfferingVO offering = getManagementServer().findServiceOfferingById(offeringId);
if (offering == null) {
throw new ServerApiException(BaseCmd.PARAM_ERROR, "unable to find service offering " + offeringId);
}
@Override
try { public String getResponse() {
offering = getManagementServer().updateServiceOffering(userId, offeringId, name, displayText, offerHA, useVirtualNetwork, tags); // TODO Auto-generated method stub
} catch (Exception ex) { return null;
s_logger.error("Exception updating service offering", ex); }
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to update service offering " + offeringId + ": internal error.");
}
List<Pair<String, Object>> returnValues = new ArrayList<Pair<String, Object>>();
if (offering != null) { // @Override
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.ID.getName(), offeringId.toString())); // public List<Pair<String, Object>> execute(Map<String, Object> params) {
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.NAME.getName(), offering.getName())); // Long offeringId = (Long)params.get(BaseCmd.Properties.ID.getName());
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.DISPLAY_TEXT.getName(), offering.getDisplayText())); // String name = (String)params.get(BaseCmd.Properties.NAME.getName());
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.CPU_NUMBER.getName(), Integer.valueOf(offering.getCpu()).toString())); // String displayText = (String)params.get(BaseCmd.Properties.DISPLAY_TEXT.getName());
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.CPU_SPEED.getName(), Integer.valueOf(offering.getSpeed()).toString())); // Boolean offerHA = (Boolean) params.get(BaseCmd.Properties.OFFER_HA.getName());
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.MEMORY.getName(), Integer.valueOf(offering.getRamSize()).toString())); // Boolean useVirtualNetwork = (Boolean) params.get(BaseCmd.Properties.USE_VIRTUAL_NETWORK.getName());
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.CREATED.getName(), getDateString(offering.getCreated()))); // Long userId = (Long)params.get(BaseCmd.Properties.USER_ID.getName());
String storageType = offering.getUseLocalStorage() ? "local" : "shared"; // String tags = (String)params.get(BaseCmd.Properties.TAGS.getName());
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())); // if (userId == null) {
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.USE_VIRTUAL_NETWORK.getName(), (offering.getGuestIpType().equals(GuestIpType.Virtualized)))); // userId = Long.valueOf(User.UID_SYSTEM);
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.TAGS.getName(), offering.getTags())); // }
} else { //
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to update service offering " + offeringId); // // Verify input parameters
} // ServiceOfferingVO offering = getManagementServer().findServiceOfferingById(offeringId);
return returnValues; // if (offering == null) {
} // throw new ServerApiException(BaseCmd.PARAM_ERROR, "unable to find service offering " + offeringId);
// }
//
//
// try {
// offering = getManagementServer().updateServiceOffering(userId, offeringId, name, displayText, offerHA, useVirtualNetwork, tags);
// } catch (Exception ex) {
// s_logger.error("Exception updating service offering", ex);
// throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to update service offering " + offeringId + ": internal error.");
// }
//
// List<Pair<String, Object>> returnValues = new ArrayList<Pair<String, Object>>();
// if (offering != null) {
// returnValues.add(new Pair<String, Object>(BaseCmd.Properties.ID.getName(), offeringId.toString()));
// 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())));
// String 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()));
// } else {
// throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to update service offering " + offeringId);
// }
// 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.UpdateServiceOfferingCmd;
import com.cloud.api.commands.UpdateZoneCmd; import com.cloud.api.commands.UpdateZoneCmd;
import com.cloud.dc.DataCenterVO; import com.cloud.dc.DataCenterVO;
import com.cloud.dc.HostPodVO; import com.cloud.dc.HostPodVO;
@ -86,7 +87,8 @@ public interface ConfigurationManager extends Manager {
* @param tags * @param tags
* @return updated service offering * @return updated service offering
*/ */
ServiceOfferingVO updateServiceOffering(long serviceOfferingId, long userId, String name, String displayText, Boolean offerHA, Boolean useVirtualNetwork, String tags); // ServiceOfferingVO updateServiceOffering(long serviceOfferingId, long userId, String name, String displayText, Boolean offerHA, Boolean useVirtualNetwork, String tags);
ServiceOfferingVO updateServiceOffering(UpdateServiceOfferingCmd cmd);
/** /**
* Updates a disk offering * Updates a disk offering

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.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;
import com.cloud.dc.AccountVlanMapVO; import com.cloud.dc.AccountVlanMapVO;
@ -66,6 +67,7 @@ import com.cloud.service.ServiceOfferingVO;
import com.cloud.service.dao.ServiceOfferingDao; import com.cloud.service.dao.ServiceOfferingDao;
import com.cloud.storage.DiskOfferingVO; import com.cloud.storage.DiskOfferingVO;
import com.cloud.storage.dao.DiskOfferingDao; import com.cloud.storage.dao.DiskOfferingDao;
import com.cloud.user.Account;
import com.cloud.user.AccountVO; import com.cloud.user.AccountVO;
import com.cloud.user.User; import com.cloud.user.User;
import com.cloud.user.UserContext; import com.cloud.user.UserContext;
@ -73,6 +75,7 @@ import com.cloud.user.UserVO;
import com.cloud.user.dao.AccountDao; import com.cloud.user.dao.AccountDao;
import com.cloud.user.dao.UserDao; import com.cloud.user.dao.UserDao;
import com.cloud.utils.NumbersUtil; import com.cloud.utils.NumbersUtil;
import com.cloud.utils.Pair;
import com.cloud.utils.component.Inject; import com.cloud.utils.component.Inject;
import com.cloud.utils.db.DB; import com.cloud.utils.db.DB;
import com.cloud.utils.db.Transaction; import com.cloud.utils.db.Transaction;
@ -869,13 +872,32 @@ public class ConfigurationManagerImpl implements ConfigurationManager {
} }
} }
public ServiceOfferingVO updateServiceOffering(long userId, long serviceOfferingId, String name, String displayText, Boolean offerHA, Boolean useVirtualNetwork, String tags) { public ServiceOfferingVO updateServiceOffering(UpdateServiceOfferingCmd cmd) {
boolean updateNeeded = (name != null || displayText != null || offerHA != null || useVirtualNetwork != null || tags != null);
if (!updateNeeded) { String displayText = cmd.getDisplayText();
return _serviceOfferingDao.findById(serviceOfferingId); Long id = cmd.getId();
String name = cmd.getName();
Boolean ha = cmd.getOfferHa();
String tags = cmd.getTags();
Boolean useVirtualNetwork = cmd.getUseVirtualNetwork();
Long userId = UserContext.current().getUserId();
if (userId == null) {
userId = Long.valueOf(User.UID_SYSTEM);
}
// Verify input parameters
ServiceOfferingVO offeringHandle = _serviceOfferingDao.findById(id);;
if (offeringHandle == null) {
throw new ServerApiException(BaseCmd.PARAM_ERROR, "unable to find service offering " + id);
} }
ServiceOfferingVO offering = _serviceOfferingDao.createForUpdate(serviceOfferingId); boolean updateNeeded = (name != null || displayText != null || ha != null || useVirtualNetwork != null || tags != null);
if (!updateNeeded) {
return _serviceOfferingDao.findById(id);
}
ServiceOfferingVO offering = _serviceOfferingDao.createForUpdate(id);
if (name != null) { if (name != null) {
offering.setName(name); offering.setName(name);
@ -885,8 +907,8 @@ public class ConfigurationManagerImpl implements ConfigurationManager {
offering.setDisplayText(displayText); offering.setDisplayText(displayText);
} }
if (offerHA != null) { if (ha != null) {
offering.setOfferHA(offerHA); offering.setOfferHA(ha);
} }
if (useVirtualNetwork != null) { if (useVirtualNetwork != null) {
@ -902,8 +924,8 @@ public class ConfigurationManagerImpl implements ConfigurationManager {
} }
} }
if (_serviceOfferingDao.update(serviceOfferingId, offering)) { if (_serviceOfferingDao.update(id, offering)) {
offering = _serviceOfferingDao.findById(serviceOfferingId); offering = _serviceOfferingDao.findById(id);
saveConfigurationEvent(userId, null, EventTypes.EVENT_SERVICE_OFFERING_EDIT, "Successfully updated service offering with name: " + offering.getName() + ".", "soId=" + offering.getId(), "name=" + offering.getName(), saveConfigurationEvent(userId, null, EventTypes.EVENT_SERVICE_OFFERING_EDIT, "Successfully updated service offering with name: " + offering.getName() + ".", "soId=" + offering.getId(), "name=" + offering.getName(),
"displayText=" + offering.getDisplayText(), "offerHA=" + offering.getOfferHA(), "useVirtualNetwork=" + (offering.getGuestIpType() == GuestIpType.Virtualized), "tags=" + offering.getTags()); "displayText=" + offering.getDisplayText(), "offerHA=" + offering.getOfferHA(), "useVirtualNetwork=" + (offering.getGuestIpType() == GuestIpType.Virtualized), "tags=" + offering.getTags());
return offering; return offering;
@ -1777,5 +1799,5 @@ public class ConfigurationManagerImpl implements ConfigurationManager {
s_logger.error("Unable to add the new config entry:",ex); s_logger.error("Unable to add the new config entry:",ex);
return false; return false;
} }
} }
} }

View File

@ -4317,10 +4317,10 @@ public class ManagementServerImpl implements ManagementServer {
return _configMgr.createServiceOffering(userId, name, cpu, ramSize, speed, displayText, localStorageRequired, offerHA, useVirtualNetwork, tags); return _configMgr.createServiceOffering(userId, name, cpu, ramSize, speed, displayText, localStorageRequired, offerHA, useVirtualNetwork, tags);
} }
@Override // @Override
public ServiceOfferingVO updateServiceOffering(long userId, long serviceOfferingId, String name, String displayText, Boolean offerHA, Boolean useVirtualNetwork, String tags) { // 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); // return _configMgr.updateServiceOffering(userId, serviceOfferingId, name, displayText, offerHA, useVirtualNetwork, tags);
} // }
@Override @Override