From 167433e56272c3c8abbdcf255b2d4f8edcd82b8c Mon Sep 17 00:00:00 2001 From: alena Date: Thu, 12 Aug 2010 16:32:23 -0700 Subject: [PATCH] Api refactor: * Moved ManagementServer interface from "core" to "server" package * Changed AddConfig, UpdateCfg, UpdateAccount commands --- .../com/cloud/api/commands/AddConfigCmd.java | 5 +- .../cloud/api/commands/UpdateAccountCmd.java | 107 ++++++++---------- .../com/cloud/api/commands/UpdateCfgCmd.java | 58 ++++------ .../configuration/ConfigurationManager.java | 16 ++- .../ConfigurationManagerImpl.java | 29 ++++- .../com/cloud/server/ManagementServer.java | 36 +++--- .../cloud/server/ManagementServerImpl.java | 58 ++++++---- 7 files changed, 173 insertions(+), 136 deletions(-) rename {core => server}/src/com/cloud/server/ManagementServer.java (98%) diff --git a/server/src/com/cloud/api/commands/AddConfigCmd.java b/server/src/com/cloud/api/commands/AddConfigCmd.java index ce423a2917e..be279e03025 100644 --- a/server/src/com/cloud/api/commands/AddConfigCmd.java +++ b/server/src/com/cloud/api/commands/AddConfigCmd.java @@ -24,8 +24,10 @@ import com.cloud.api.BaseCmd; import com.cloud.api.Implementation; import com.cloud.api.Parameter; import com.cloud.api.BaseCmd.Manager; +import com.cloud.configuration.ConfigurationVO; +import com.cloud.storage.DiskOfferingVO; -@Implementation(method="addConfig", manager=Manager.ManagementServer) +@Implementation(method="addConfig", manager=Manager.ConfigManager) public class AddConfigCmd extends BaseCmd { public static final Logger s_logger = Logger.getLogger(AddConfigCmd.class.getName()); @@ -87,6 +89,7 @@ public class AddConfigCmd extends BaseCmd { /////////////// API Implementation/////////////////// ///////////////////////////////////////////////////// + @Override public String getName() { return s_name; diff --git a/server/src/com/cloud/api/commands/UpdateAccountCmd.java b/server/src/com/cloud/api/commands/UpdateAccountCmd.java index 0a39a5db108..3b2eaa3b438 100644 --- a/server/src/com/cloud/api/commands/UpdateAccountCmd.java +++ b/server/src/com/cloud/api/commands/UpdateAccountCmd.java @@ -25,24 +25,19 @@ 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.user.Account; import com.cloud.utils.Pair; - + +@Implementation(method="updateAccount", manager=Manager.ManagementServer) public class UpdateAccountCmd extends BaseCmd{ public static final Logger s_logger = Logger.getLogger(UpdateAccountCmd.class.getName()); private static final String s_name = "updateaccountresponse"; private static final List> s_properties = new ArrayList>(); - static { - s_properties.add(new Pair(BaseCmd.Properties.NEW_NAME, Boolean.TRUE)); - s_properties.add(new Pair(BaseCmd.Properties.ACCOUNT, Boolean.TRUE)); - s_properties.add(new Pair(BaseCmd.Properties.DOMAIN_ID, Boolean.TRUE)); - - s_properties.add(new Pair(BaseCmd.Properties.ACCOUNT_OBJ, Boolean.FALSE)); - } - ///////////////////////////////////////////////////// //////////////// API parameters ///////////////////// ///////////////////////////////////////////////////// @@ -79,53 +74,49 @@ public class UpdateAccountCmd extends BaseCmd{ @Override public String getName() { return s_name; - } - @Override - public List> getProperties() { - return s_properties; - } - - @Override - public List> execute(Map params) { - Long domainId = (Long)params.get(BaseCmd.Properties.DOMAIN_ID.getName()); - String accountName = (String)params.get(BaseCmd.Properties.ACCOUNT.getName()); - String newAccountName = (String)params.get(BaseCmd.Properties.NEW_NAME.getName()); - Account adminAccount = (Account)params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName());; - Boolean updateAccountResult = false; - Account account = null; - - // check if account exists in the system - account = getManagementServer().findAccountByName(accountName, domainId); - if (account == null) { - throw new ServerApiException(BaseCmd.PARAM_ERROR, "unable to find account " + accountName + " in domain " + domainId); - } - - if ((adminAccount != null) && !getManagementServer().isChildDomain(adminAccount.getDomainId(), account.getDomainId())) { - throw new ServerApiException(BaseCmd.PARAM_ERROR, "Invalid account " + accountName + " in domain " + domainId + " given, unable to update account."); - } - - // don't allow modify system account - if (account.getId().longValue() == Account.ACCOUNT_ID_SYSTEM) { - throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "can not modify system account"); - } - - try { - getManagementServer().updateAccount(account.getId(), newAccountName); - account = getManagementServer().findAccountById(account.getId()); - if (account.getAccountName().equals(newAccountName)) { - updateAccountResult = true; - } - } catch (Exception ex) { - s_logger.error("Exception updating account", ex); - throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to update account " + accountName + " in domain " + domainId + ": internal error."); - } - - List> returnValues = new ArrayList>(); - if (updateAccountResult == true) { - returnValues.add(new Pair(BaseCmd.Properties.SUCCESS.getName(), new Boolean(true))); - } else { - throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to update account " + accountName + " in domain " + domainId); - } - return returnValues; - } + } + +// @Override +// public List> execute(Map params) { +// Long domainId = (Long)params.get(BaseCmd.Properties.DOMAIN_ID.getName()); +// String accountName = (String)params.get(BaseCmd.Properties.ACCOUNT.getName()); +// String newAccountName = (String)params.get(BaseCmd.Properties.NEW_NAME.getName()); +// Account adminAccount = (Account)params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName());; +// Boolean updateAccountResult = false; +// Account account = null; +// +// // check if account exists in the system +// account = getManagementServer().findAccountByName(accountName, domainId); +// if (account == null) { +// throw new ServerApiException(BaseCmd.PARAM_ERROR, "unable to find account " + accountName + " in domain " + domainId); +// } +// +// if ((adminAccount != null) && !getManagementServer().isChildDomain(adminAccount.getDomainId(), account.getDomainId())) { +// throw new ServerApiException(BaseCmd.PARAM_ERROR, "Invalid account " + accountName + " in domain " + domainId + " given, unable to update account."); +// } +// +// // don't allow modify system account +// if (account.getId().longValue() == Account.ACCOUNT_ID_SYSTEM) { +// throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "can not modify system account"); +// } +// +// try { +// getManagementServer().updateAccount(account.getId(), newAccountName); +// account = getManagementServer().findAccountById(account.getId()); +// if (account.getAccountName().equals(newAccountName)) { +// updateAccountResult = true; +// } +// } catch (Exception ex) { +// s_logger.error("Exception updating account", ex); +// throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to update account " + accountName + " in domain " + domainId + ": internal error."); +// } +// +// List> returnValues = new ArrayList>(); +// if (updateAccountResult == true) { +// returnValues.add(new Pair(BaseCmd.Properties.SUCCESS.getName(), new Boolean(true))); +// } else { +// throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to update account " + accountName + " in domain " + domainId); +// } +// return returnValues; +// } } diff --git a/server/src/com/cloud/api/commands/UpdateCfgCmd.java b/server/src/com/cloud/api/commands/UpdateCfgCmd.java index 84f9afb0344..e32561dfc34 100644 --- a/server/src/com/cloud/api/commands/UpdateCfgCmd.java +++ b/server/src/com/cloud/api/commands/UpdateCfgCmd.java @@ -25,23 +25,18 @@ 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.user.User; import com.cloud.utils.Pair; +@Implementation(method="updateConfiguration", manager=Manager.ConfigManager) public class UpdateCfgCmd extends BaseCmd { public static final Logger s_logger = Logger.getLogger(UpdateCfgCmd.class.getName()); private static final String s_name = "updateconfigurationresponse"; - private static final List> s_properties = new ArrayList>(); - - static { - s_properties.add(new Pair(BaseCmd.Properties.NAME, Boolean.TRUE)); - s_properties.add(new Pair(BaseCmd.Properties.VALUE, Boolean.FALSE)); - - s_properties.add(new Pair(BaseCmd.Properties.USER_ID, Boolean.FALSE)); - } ///////////////////////////////////////////////////// //////////////// API parameters ///////////////////// @@ -72,30 +67,27 @@ public class UpdateCfgCmd extends BaseCmd { public String getName() { return s_name; } - public List> getProperties() { - return s_properties; - } - @Override - public List> execute(Map params) { - String name = (String) params.get(BaseCmd.Properties.NAME.getName()); - String value = (String) params.get(BaseCmd.Properties.VALUE.getName()); - Long userId = (Long)params.get(BaseCmd.Properties.USER_ID.getName()); - - if (userId == null) { - userId = Long.valueOf(User.UID_SYSTEM); - } - - try { - getManagementServer().updateConfiguration(userId, name, value); - } catch (Exception ex) { - throw new ServerApiException(BaseCmd.INTERNAL_ERROR, ex.getMessage()); - } - - List> returnValues = new ArrayList>(); - returnValues.add(new Pair(BaseCmd.Properties.SUCCESS.getName(), "true")); - returnValues.add(new Pair(BaseCmd.Properties.DISPLAY_TEXT.getName(), "Successfully updated configuration value.")); - - return returnValues; - } +// @Override +// public List> execute(Map params) { +// String name = (String) params.get(BaseCmd.Properties.NAME.getName()); +// String value = (String) params.get(BaseCmd.Properties.VALUE.getName()); +// Long userId = (Long)params.get(BaseCmd.Properties.USER_ID.getName()); +// +// if (userId == null) { +// userId = Long.valueOf(User.UID_SYSTEM); +// } +// +// try { +// getManagementServer().updateConfiguration(userId, name, value); +// } catch (Exception ex) { +// throw new ServerApiException(BaseCmd.INTERNAL_ERROR, ex.getMessage()); +// } +// +// List> returnValues = new ArrayList>(); +// returnValues.add(new Pair(BaseCmd.Properties.SUCCESS.getName(), "true")); +// returnValues.add(new Pair(BaseCmd.Properties.DISPLAY_TEXT.getName(), "Successfully updated configuration value.")); +// +// return returnValues; +// } } diff --git a/server/src/com/cloud/configuration/ConfigurationManager.java b/server/src/com/cloud/configuration/ConfigurationManager.java index 607ef6095c6..156f38c8996 100644 --- a/server/src/com/cloud/configuration/ConfigurationManager.java +++ b/server/src/com/cloud/configuration/ConfigurationManager.java @@ -19,8 +19,10 @@ package com.cloud.configuration; import java.util.List; +import com.cloud.api.commands.AddConfigCmd; import com.cloud.api.commands.CreateDiskOfferingCmd; import com.cloud.api.commands.DeleteDiskOfferingCmd; +import com.cloud.api.commands.UpdateCfgCmd; import com.cloud.api.commands.UpdateDiskOfferingCmd; import com.cloud.api.commands.UpdateZoneCmd; import com.cloud.dc.DataCenterVO; @@ -47,6 +49,16 @@ public interface ConfigurationManager extends Manager { */ void updateConfiguration(long userId, String name, String value) throws InvalidParameterValueException, InternalErrorException; + + /** + * Updates a configuration entry with a new value + * @param userId + * @param name + * @param value + */ + void updateConfiguration(UpdateCfgCmd cmd) throws InvalidParameterValueException, InternalErrorException; + + /** * Creates a new service offering * @param id @@ -84,7 +96,7 @@ public interface ConfigurationManager extends Manager { * @param tags * @return updated disk offering */ - DiskOfferingVO updateDiskOffering(UpdateDiskOfferingCmd cmd); + DiskOfferingVO updateDiskOffering(UpdateDiskOfferingCmd cmd) throws InvalidParameterValueException; /** * Deletes a disk offering @@ -238,5 +250,5 @@ public interface ConfigurationManager extends Manager { * @param description * @return */ - public boolean addConfig(String instance, String component,String category, String name, String value, String description); + public boolean addConfig(AddConfigCmd cmd); } diff --git a/server/src/com/cloud/configuration/ConfigurationManagerImpl.java b/server/src/com/cloud/configuration/ConfigurationManagerImpl.java index e6b2fcc6e18..28b5901301f 100644 --- a/server/src/com/cloud/configuration/ConfigurationManagerImpl.java +++ b/server/src/com/cloud/configuration/ConfigurationManagerImpl.java @@ -33,8 +33,10 @@ import org.apache.log4j.Logger; import com.cloud.api.BaseCmd; import com.cloud.api.ServerApiException; +import com.cloud.api.commands.AddConfigCmd; import com.cloud.api.commands.CreateDiskOfferingCmd; import com.cloud.api.commands.DeleteDiskOfferingCmd; +import com.cloud.api.commands.UpdateCfgCmd; import com.cloud.api.commands.UpdateDiskOfferingCmd; import com.cloud.api.commands.UpdateZoneCmd; import com.cloud.configuration.dao.ConfigurationDao; @@ -158,6 +160,13 @@ public class ConfigurationManagerImpl implements ConfigurationManager { saveConfigurationEvent(userId, null, EventTypes.EVENT_CONFIGURATION_VALUE_EDIT, "Successfully edited configuration value.", "name=" + name, "value=" + value); } + public void updateConfiguration(UpdateCfgCmd cmd) throws InvalidParameterValueException, InternalErrorException{ + long userId = UserContext.current().getUserId(); + String name = cmd.getName(); + String value = cmd.getValue(); + updateConfiguration (userId, name, value); + } + private String validateConfigurationValue(String name, String value) throws InvalidParameterValueException { if (value == null) { @@ -921,18 +930,25 @@ public class ConfigurationManagerImpl implements ConfigurationManager { return _diskOfferingDao.persist(newDiskOffering); } - public DiskOfferingVO updateDiskOffering(UpdateDiskOfferingCmd cmd) { + public DiskOfferingVO updateDiskOffering(UpdateDiskOfferingCmd cmd) throws InvalidParameterValueException{ Long diskOfferingId = cmd.getId(); String name = cmd.getName(); String displayText = cmd.getDisplayText(); String tags = cmd.getTags(); + //Check if diskOffering exists + DiskOfferingVO diskOffering = _diskOfferingDao.findById(diskOfferingId); + + if (diskOffering == null) { + throw new InvalidParameterValueException("Unable to find disk offering by id " + diskOfferingId); + } + boolean updateNeeded = (name != null || displayText != null || tags != null); if (!updateNeeded) { return _diskOfferingDao.findById(diskOfferingId); } - DiskOfferingVO diskOffering = _diskOfferingDao.createForUpdate(diskOfferingId); + diskOffering = _diskOfferingDao.createForUpdate(diskOfferingId); if (name != null) { diskOffering.setName(name); @@ -1735,8 +1751,13 @@ public class ConfigurationManagerImpl implements ConfigurationManager { } @Override - public boolean addConfig(String instance, String component,String category, String name, String value, String description) - { + public boolean addConfig(AddConfigCmd cmd){ + String category = cmd.getCategory(); + String instance = cmd.getInstance(); + String component = cmd.getComponent(); + String name = cmd.getName(); + String value = cmd.getValue(); + String description = cmd.getDescription(); try { ConfigurationVO entity = new ConfigurationVO(category, instance, component, name, value, description); diff --git a/core/src/com/cloud/server/ManagementServer.java b/server/src/com/cloud/server/ManagementServer.java similarity index 98% rename from core/src/com/cloud/server/ManagementServer.java rename to server/src/com/cloud/server/ManagementServer.java index 4bb7758b6e6..7006d6d6778 100644 --- a/core/src/com/cloud/server/ManagementServer.java +++ b/server/src/com/cloud/server/ManagementServer.java @@ -25,6 +25,7 @@ import java.util.List; import java.util.Map; import com.cloud.alert.AlertVO; +import com.cloud.api.commands.UpdateAccountCmd; import com.cloud.async.AsyncJobResult; import com.cloud.async.AsyncJobVO; import com.cloud.capacity.CapacityVO; @@ -98,6 +99,7 @@ import com.cloud.vm.VMInstanceVO; import com.cloud.vm.VirtualMachine; import com.cloud.vm.VmStats; + /** * ManagementServer is the interface to talk to the Managment Server. * This will be the line drawn between the UI and MS. If we need to build @@ -223,7 +225,7 @@ public interface ManagementServer { * @return true if update was successful, false otherwise */ - boolean updateAccount(long accountId, String accountName); + boolean updateAccount(UpdateAccountCmd cmd) throws InvalidParameterValueException; /** * Enables a user by userId @@ -1030,20 +1032,20 @@ 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; +// /** +// * 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 @@ -1436,7 +1438,7 @@ public interface ManagementServer { * @param value * @return */ - void updateConfiguration(long userId, String name, String value) throws InvalidParameterValueException, InternalErrorException; +// void updateConfiguration(long userId, String name, String value) throws InvalidParameterValueException, InternalErrorException; /** * Creates or updates an IP forwarding or load balancer rule. @@ -2132,7 +2134,7 @@ public interface ManagementServer { boolean checkLocalStorageConfigVal(); - boolean addConfig(String instance, String component, String category, String name, String value, String description); +// boolean addConfig(String instance, String component, String category, String name, String value, String description); boolean validateCustomVolumeSizeRange(long size) throws InvalidParameterValueException; } diff --git a/server/src/com/cloud/server/ManagementServerImpl.java b/server/src/com/cloud/server/ManagementServerImpl.java index 674814ea72d..dc8da78174c 100755 --- a/server/src/com/cloud/server/ManagementServerImpl.java +++ b/server/src/com/cloud/server/ManagementServerImpl.java @@ -76,6 +76,7 @@ import com.cloud.api.commands.ReconnectHostCmd; import com.cloud.api.commands.StartRouterCmd; import com.cloud.api.commands.StartSystemVMCmd; import com.cloud.api.commands.StartVMCmd; +import com.cloud.api.commands.UpdateAccountCmd; import com.cloud.api.commands.UpgradeVMCmd; import com.cloud.async.AsyncInstanceCreateStatus; import com.cloud.async.AsyncJobExecutor; @@ -1137,15 +1138,41 @@ public class ManagementServerImpl implements ManagementServer { } @Override - public boolean updateAccount(long accountId, String accountName) { + public boolean updateAccount(UpdateAccountCmd cmd) throws InvalidParameterValueException{ + Long domainId = cmd.getDomainId(); + String accountName = cmd.getAccountName(); + String newAccountName = cmd.getNewName(); + + if (newAccountName == null) { + newAccountName = accountName; + } + boolean success = false; - AccountVO account = _accountDao.findById(accountId); - if ((account == null) || (account.getAccountName().equals(accountName))) { + Account account = _accountDao.findAccount(accountName, domainId); + + //Check if account exists + if (account == null) { + s_logger.error("Unable to find account " + accountName + " in domain " + domainId); + throw new InvalidParameterValueException("Unable to find account " + accountName + " in domain " + domainId); + } + + //Don't allow to modify system account + if (account.getId().longValue() == Account.ACCOUNT_ID_SYSTEM) { + throw new InvalidParameterValueException ("Can not modify system account"); + } + + //Check if user performing the action is allowed to modify this account + Account adminAccount = (Account)UserContext.current().getAccountObject(); + if ((adminAccount != null) && isChildDomain(adminAccount.getDomainId(), account.getDomainId())) { + throw new InvalidParameterValueException("Invalid account " + accountName + " in domain " + domainId + " given, unable to update account."); + } + + if (account.getAccountName().equals(accountName)) { success = true; } else { AccountVO acctForUpdate = _accountDao.createForUpdate(); - acctForUpdate.setAccountName(accountName); - success = _accountDao.update(Long.valueOf(accountId), acctForUpdate); + acctForUpdate.setAccountName(newAccountName); + success = _accountDao.update(Long.valueOf(account.getId()), acctForUpdate); } return success; } @@ -4203,12 +4230,7 @@ public class ManagementServerImpl implements ManagementServer { public List listPods(long dataCenterId) { return _hostPodDao.listByDataCenterId(dataCenterId); } - - @Override - public void updateConfiguration(long userId, String name, String value) throws InvalidParameterValueException, InternalErrorException { - _configMgr.updateConfiguration(userId, name, value); - } - + @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); @@ -4240,10 +4262,10 @@ public class ManagementServerImpl implements ManagementServer { return _configMgr.createZone(userId, zoneName, dns1, dns2, internalDns1, internalDns2, vnetRange, guestCidr); } - @Override - public DataCenterVO editZone(long userId, Long zoneId, String newZoneName, String dns1, String dns2, String dns3, String dns4, String vnetRange, String guestCidr) throws InvalidParameterValueException, InternalErrorException { - return _configMgr.editZone(userId, zoneId, newZoneName, dns1, dns2, dns3, dns4, vnetRange, guestCidr); - } +// @Override +// public DataCenterVO editZone(long userId, Long zoneId, String newZoneName, String dns1, String dns2, String dns3, String dns4, String vnetRange, String guestCidr) throws InvalidParameterValueException, InternalErrorException { +// return _configMgr.editZone(userId, zoneId, newZoneName, dns1, dns2, dns3, dns4, vnetRange, guestCidr); +// } @Override public void deleteZone(long userId, Long zoneId) throws InvalidParameterValueException, InternalErrorException { @@ -8185,12 +8207,6 @@ public class ManagementServerImpl implements ManagementServer { return false; } - @Override - public boolean addConfig(String instance, String component,String category, String name, String value, String description) - { - return _configMgr.addConfig(category, instance, component, name, value, description); - } - public boolean preparePrimaryStorageForMaintenance(long primaryStorageId, long userId) { return _storageMgr.preparePrimaryStorageForMaintenance(primaryStorageId, userId);