From 1252a2b8a78c821483f9de340940aed2f3c8e7eb Mon Sep 17 00:00:00 2001 From: abhishek Date: Tue, 17 Aug 2010 17:21:04 -0700 Subject: [PATCH] Refactoring update service offering cmd --- .../commands/UpdateServiceOfferingCmd.java | 125 ++++++++---------- .../configuration/ConfigurationManager.java | 4 +- .../ConfigurationManagerImpl.java | 42 ++++-- .../cloud/server/ManagementServerImpl.java | 8 +- 4 files changed, 93 insertions(+), 86 deletions(-) diff --git a/server/src/com/cloud/api/commands/UpdateServiceOfferingCmd.java b/server/src/com/cloud/api/commands/UpdateServiceOfferingCmd.java index 875c66f0d7f..c64907b613c 100644 --- a/server/src/com/cloud/api/commands/UpdateServiceOfferingCmd.java +++ b/server/src/com/cloud/api/commands/UpdateServiceOfferingCmd.java @@ -18,21 +18,12 @@ 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.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) 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()); private static final String s_name = "updateserviceofferingresponse"; - static { - s_properties.add(new Pair(BaseCmd.Properties.DISPLAY_TEXT, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.ID, Boolean.TRUE)); - s_properties.add(new Pair(BaseCmd.Properties.NAME, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.OFFER_HA, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.TAGS, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.USE_VIRTUAL_NETWORK, Boolean.FALSE)); - - s_properties.add(new Pair(BaseCmd.Properties.USER_ID, Boolean.FALSE)); - } - ///////////////////////////////////////////////////// //////////////// API parameters ///////////////////// ///////////////////////////////////////////////////// @@ -108,57 +88,60 @@ public class UpdateServiceOfferingCmd extends BaseCmd @Override public String getName() { return s_name; - } - @Override - public List> getProperties() { - return s_properties; - } - - @Override - public List> execute(Map 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); - } + } - - 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."); - } + @Override + public String getResponse() { + // TODO Auto-generated method stub + return null; + } - List> returnValues = new ArrayList>(); - if (offering != null) { - returnValues.add(new Pair(BaseCmd.Properties.ID.getName(), offeringId.toString())); - returnValues.add(new Pair(BaseCmd.Properties.NAME.getName(), offering.getName())); - returnValues.add(new Pair(BaseCmd.Properties.DISPLAY_TEXT.getName(), offering.getDisplayText())); - returnValues.add(new Pair(BaseCmd.Properties.CPU_NUMBER.getName(), Integer.valueOf(offering.getCpu()).toString())); - returnValues.add(new Pair(BaseCmd.Properties.CPU_SPEED.getName(), Integer.valueOf(offering.getSpeed()).toString())); - returnValues.add(new Pair(BaseCmd.Properties.MEMORY.getName(), Integer.valueOf(offering.getRamSize()).toString())); - returnValues.add(new Pair(BaseCmd.Properties.CREATED.getName(), getDateString(offering.getCreated()))); - String storageType = offering.getUseLocalStorage() ? "local" : "shared"; - returnValues.add(new Pair(BaseCmd.Properties.STORAGE_TYPE.getName(), storageType)); - returnValues.add(new Pair(BaseCmd.Properties.OFFER_HA.getName(), offering.getOfferHA())); - returnValues.add(new Pair(BaseCmd.Properties.USE_VIRTUAL_NETWORK.getName(), (offering.getGuestIpType().equals(GuestIpType.Virtualized)))); - returnValues.add(new Pair(BaseCmd.Properties.TAGS.getName(), offering.getTags())); - } else { - throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to update service offering " + offeringId); - } - return returnValues; - } + +// @Override +// public List> execute(Map 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); +// } +// +// +// 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> returnValues = new ArrayList>(); +// if (offering != null) { +// returnValues.add(new Pair(BaseCmd.Properties.ID.getName(), offeringId.toString())); +// returnValues.add(new Pair(BaseCmd.Properties.NAME.getName(), offering.getName())); +// returnValues.add(new Pair(BaseCmd.Properties.DISPLAY_TEXT.getName(), offering.getDisplayText())); +// returnValues.add(new Pair(BaseCmd.Properties.CPU_NUMBER.getName(), Integer.valueOf(offering.getCpu()).toString())); +// returnValues.add(new Pair(BaseCmd.Properties.CPU_SPEED.getName(), Integer.valueOf(offering.getSpeed()).toString())); +// returnValues.add(new Pair(BaseCmd.Properties.MEMORY.getName(), Integer.valueOf(offering.getRamSize()).toString())); +// returnValues.add(new Pair(BaseCmd.Properties.CREATED.getName(), getDateString(offering.getCreated()))); +// String storageType = offering.getUseLocalStorage() ? "local" : "shared"; +// returnValues.add(new Pair(BaseCmd.Properties.STORAGE_TYPE.getName(), storageType)); +// returnValues.add(new Pair(BaseCmd.Properties.OFFER_HA.getName(), offering.getOfferHA())); +// returnValues.add(new Pair(BaseCmd.Properties.USE_VIRTUAL_NETWORK.getName(), (offering.getGuestIpType().equals(GuestIpType.Virtualized)))); +// returnValues.add(new Pair(BaseCmd.Properties.TAGS.getName(), offering.getTags())); +// } else { +// throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to update service offering " + offeringId); +// } +// return returnValues; +// } } diff --git a/server/src/com/cloud/configuration/ConfigurationManager.java b/server/src/com/cloud/configuration/ConfigurationManager.java index 632a47ea6d0..9b4b287d20f 100644 --- a/server/src/com/cloud/configuration/ConfigurationManager.java +++ b/server/src/com/cloud/configuration/ConfigurationManager.java @@ -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.UpdateServiceOfferingCmd; import com.cloud.api.commands.UpdateZoneCmd; import com.cloud.dc.DataCenterVO; import com.cloud.dc.HostPodVO; @@ -86,7 +87,8 @@ public interface ConfigurationManager extends Manager { * @param tags * @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 diff --git a/server/src/com/cloud/configuration/ConfigurationManagerImpl.java b/server/src/com/cloud/configuration/ConfigurationManagerImpl.java index 92fa5f10281..9a412d6ec16 100644 --- a/server/src/com/cloud/configuration/ConfigurationManagerImpl.java +++ b/server/src/com/cloud/configuration/ConfigurationManagerImpl.java @@ -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.UpdateServiceOfferingCmd; import com.cloud.api.commands.UpdateZoneCmd; import com.cloud.configuration.dao.ConfigurationDao; import com.cloud.dc.AccountVlanMapVO; @@ -66,6 +67,7 @@ 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; @@ -73,6 +75,7 @@ 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; @@ -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) { - boolean updateNeeded = (name != null || displayText != null || offerHA != null || useVirtualNetwork != null || tags != null); - if (!updateNeeded) { - return _serviceOfferingDao.findById(serviceOfferingId); + public ServiceOfferingVO updateServiceOffering(UpdateServiceOfferingCmd cmd) { + + String displayText = cmd.getDisplayText(); + 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) { offering.setName(name); @@ -885,8 +907,8 @@ public class ConfigurationManagerImpl implements ConfigurationManager { offering.setDisplayText(displayText); } - if (offerHA != null) { - offering.setOfferHA(offerHA); + if (ha != null) { + offering.setOfferHA(ha); } if (useVirtualNetwork != null) { @@ -902,8 +924,8 @@ public class ConfigurationManagerImpl implements ConfigurationManager { } } - if (_serviceOfferingDao.update(serviceOfferingId, offering)) { - offering = _serviceOfferingDao.findById(serviceOfferingId); + if (_serviceOfferingDao.update(id, offering)) { + 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(), "displayText=" + offering.getDisplayText(), "offerHA=" + offering.getOfferHA(), "useVirtualNetwork=" + (offering.getGuestIpType() == GuestIpType.Virtualized), "tags=" + offering.getTags()); return offering; @@ -1777,5 +1799,5 @@ public class ConfigurationManagerImpl implements ConfigurationManager { s_logger.error("Unable to add the new config entry:",ex); return false; } - } + } } diff --git a/server/src/com/cloud/server/ManagementServerImpl.java b/server/src/com/cloud/server/ManagementServerImpl.java index fe7219b5a5f..05da8fec024 100755 --- a/server/src/com/cloud/server/ManagementServerImpl.java +++ b/server/src/com/cloud/server/ManagementServerImpl.java @@ -4317,10 +4317,10 @@ public class ManagementServerImpl implements ManagementServer { return _configMgr.createServiceOffering(userId, name, cpu, ramSize, speed, displayText, localStorageRequired, offerHA, useVirtualNetwork, tags); } - @Override - 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); - } +// @Override +// 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); +// } @Override