From 479ff26051044a18a56707727a5c2f42d9bc52d3 Mon Sep 17 00:00:00 2001 From: Kris McQueen Date: Wed, 25 Aug 2010 18:26:11 -0700 Subject: [PATCH] Refactoring createZone to new API framework. The logic was entirely delegated to configuration manager, so pushed the logic there and removed the proxy method from Management server. --- .../cloud/api/commands/CreateVolumeCmd.java | 29 ++---- .../com/cloud/api/commands/CreateZoneCmd.java | 79 ++++------------ .../com/cloud/api/response/ZoneResponse.java | 94 +++++++++++++++++++ .../configuration/ConfigurationManager.java | 16 ++-- .../ConfigurationManagerImpl.java | 37 +++++--- .../com/cloud/server/ManagementServer.java | 21 ----- .../cloud/server/ManagementServerImpl.java | 55 ++++------- 7 files changed, 173 insertions(+), 158 deletions(-) create mode 100644 server/src/com/cloud/api/response/ZoneResponse.java diff --git a/server/src/com/cloud/api/commands/CreateVolumeCmd.java b/server/src/com/cloud/api/commands/CreateVolumeCmd.java index d16f41c5072..2c40ebdeb3d 100644 --- a/server/src/com/cloud/api/commands/CreateVolumeCmd.java +++ b/server/src/com/cloud/api/commands/CreateVolumeCmd.java @@ -176,29 +176,22 @@ public class CreateVolumeCmd extends BaseCmd { } boolean useSnapshot = false; - if (snapshotId == null) - { - if ((zoneId == null)) - { + if (snapshotId == null) { + if ((zoneId == null)) { throw new ServerApiException(BaseCmd.PARAM_ERROR, "Missing parameter,zoneid must be specified."); } - - if(diskOfferingId == null && size == 0) - { + + if (diskOfferingId == null && size == 0) { throw new ServerApiException(BaseCmd.PARAM_ERROR, "Missing parameter(s),either a positive volume size or a valid disk offering id must be specified."); - } - else if(diskOfferingId == null && size != 0) - { + } else if(diskOfferingId == null && size != 0) { //validate the size to ensure between min and max size range - try - { + try { boolean ok = getManagementServer().validateCustomVolumeSizeRange(size); - if(!ok) + if (!ok) { throw new ServerApiException(BaseCmd.PARAM_ERROR, "Invalid size for custom volume creation:"); - - } catch (InvalidParameterValueException e) - { + } + } catch (InvalidParameterValueException e) { s_logger.warn("Invalid size for custom volume creation"); throw new ServerApiException(BaseCmd.PARAM_ERROR, "Invalid size for custom volume creation:"+e.getMessage()); } @@ -207,9 +200,7 @@ public class CreateVolumeCmd extends BaseCmd { List privateTemplateList = getManagementServer().findPrivateDiskOffering(); diskOfferingId = privateTemplateList.get(0).getId(); //we use this id for creating volume } - } - else - { + } else { useSnapshot = true; //Verify parameters Snapshot snapshotCheck = getManagementServer().findSnapshotById(snapshotId); diff --git a/server/src/com/cloud/api/commands/CreateZoneCmd.java b/server/src/com/cloud/api/commands/CreateZoneCmd.java index dc094b56871..a90e0a2f6d2 100644 --- a/server/src/com/cloud/api/commands/CreateZoneCmd.java +++ b/server/src/com/cloud/api/commands/CreateZoneCmd.java @@ -18,36 +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.api.response.ZoneResponse; import com.cloud.dc.DataCenterVO; -import com.cloud.user.User; -import com.cloud.utils.Pair; +import com.cloud.serializer.SerializerHelper; +@Implementation(method="createZone", manager=Manager.ConfigManager) public class CreateZoneCmd extends BaseCmd { public static final Logger s_logger = Logger.getLogger(CreateZoneCmd.class.getName()); private static final String s_name = "createzoneresponse"; - private static final List> s_properties = new ArrayList>(); - - static { - s_properties.add(new Pair(BaseCmd.Properties.DNS1, Boolean.TRUE)); - s_properties.add(new Pair(BaseCmd.Properties.DNS2, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.GUEST_CIDR_ADDRESS, Boolean.TRUE)); - s_properties.add(new Pair(BaseCmd.Properties.INTERNAL_DNS1, Boolean.TRUE)); - s_properties.add(new Pair(BaseCmd.Properties.INTERNAL_DNS2, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.NAME, Boolean.TRUE)); - s_properties.add(new Pair(BaseCmd.Properties.VNET, Boolean.FALSE)); - - s_properties.add(new Pair(BaseCmd.Properties.USER_ID, Boolean.FALSE)); - } ///////////////////////////////////////////////////// //////////////// API parameters ///////////////////// @@ -107,7 +92,6 @@ public class CreateZoneCmd extends BaseCmd { return vlan; } - ///////////////////////////////////////////////////// /////////////// API Implementation/////////////////// ///////////////////////////////////////////////////// @@ -116,50 +100,21 @@ public class CreateZoneCmd extends BaseCmd { public String getName() { return s_name; } - @Override - public List> getProperties() { - return s_properties; - } @Override - public List> execute(Map params) { - String zoneName = (String) params.get(BaseCmd.Properties.NAME.getName()); - String dns1 = (String) params.get(BaseCmd.Properties.DNS1.getName()); - String dns2 = (String) params.get(BaseCmd.Properties.DNS2.getName()); - String dns3 = (String) params.get(BaseCmd.Properties.INTERNAL_DNS1.getName()); - String dns4 = (String) params.get(BaseCmd.Properties.INTERNAL_DNS2.getName()); - String vnet = (String) params.get(BaseCmd.Properties.VNET.getName()); - String guestCidr = (String) params.get(BaseCmd.Properties.GUEST_CIDR_ADDRESS.getName()); - Long userId = (Long)params.get(BaseCmd.Properties.USER_ID.getName()); + public String getResponse() { + DataCenterVO zone = (DataCenterVO)getResponseObject(); - if (userId == null) { - userId = Long.valueOf(User.UID_SYSTEM); - } - - DataCenterVO zone = null; - - try { - zone = getManagementServer().createZone(userId, zoneName, dns1, dns2, dns3, dns4, vnet, guestCidr); - } catch (Exception ex) { - s_logger.error("Exception creating zone", ex); - throw new ServerApiException(BaseCmd.INTERNAL_ERROR, ex.getMessage()); - } + ZoneResponse response = new ZoneResponse(); + response.setId(zone.getId()); + response.setName(zone.getName()); + response.setDns1(zone.getDns1()); + response.setDns2(zone.getDns2()); + response.setInternalDns1(zone.getInternalDns1()); + response.setInternalDns2(zone.getInternalDns2()); + response.setVlan(zone.getVnet()); + response.setGuestCidrAddress(zone.getGuestNetworkCidr()); - List> returnValues = new ArrayList>(); - - if (zone == null) { - throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to create zone " + zoneName + ": internal error."); - } else { - returnValues.add(new Pair(BaseCmd.Properties.ID.getName(), zone.getId())); - returnValues.add(new Pair(BaseCmd.Properties.NAME.getName(), zoneName)); - returnValues.add(new Pair(BaseCmd.Properties.DNS1.getName(), dns1)); - returnValues.add(new Pair(BaseCmd.Properties.DNS2.getName(), dns2)); - returnValues.add(new Pair(BaseCmd.Properties.INTERNAL_DNS1.getName(), dns3)); - returnValues.add(new Pair(BaseCmd.Properties.INTERNAL_DNS2.getName(), dns4)); - returnValues.add(new Pair(BaseCmd.Properties.VNET.getName(), vnet)); - returnValues.add(new Pair(BaseCmd.Properties.GUEST_CIDR_ADDRESS.getName(), guestCidr)); - } - - return returnValues; + return SerializerHelper.toSerializedString(response); } } diff --git a/server/src/com/cloud/api/response/ZoneResponse.java b/server/src/com/cloud/api/response/ZoneResponse.java new file mode 100644 index 00000000000..48d0bb44689 --- /dev/null +++ b/server/src/com/cloud/api/response/ZoneResponse.java @@ -0,0 +1,94 @@ +package com.cloud.api.response; + +import com.cloud.api.ResponseObject; +import com.cloud.serializer.Param; + +public class ZoneResponse implements ResponseObject { + @Param(name="id") + private Long id; + + @Param(name="name") + private String name; + + @Param(name="dns1") + private String dns1; + + @Param(name="dns2") + private String dns2; + + @Param(name="internaldns1") + private String internalDns1; + + @Param(name="internaldns2") + private String internalDns2; + + @Param(name="vlan") + private String vlan; + + @Param(name="guestcidraddress") + private String guestCidrAddress; + + 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 getDns1() { + return dns1; + } + + public void setDns1(String dns1) { + this.dns1 = dns1; + } + + public String getDns2() { + return dns2; + } + + public void setDns2(String dns2) { + this.dns2 = dns2; + } + + public String getInternalDns1() { + return internalDns1; + } + + public void setInternalDns1(String internalDns1) { + this.internalDns1 = internalDns1; + } + + public String getInternalDns2() { + return internalDns2; + } + + public void setInternalDns2(String internalDns2) { + this.internalDns2 = internalDns2; + } + + public String getVlan() { + return vlan; + } + + public void setVlan(String vlan) { + this.vlan = vlan; + } + + public String getGuestCidrAddress() { + return guestCidrAddress; + } + + public void setGuestCidrAddress(String guestCidrAddress) { + this.guestCidrAddress = guestCidrAddress; + } +} diff --git a/server/src/com/cloud/configuration/ConfigurationManager.java b/server/src/com/cloud/configuration/ConfigurationManager.java index 71141f245fa..0d4f8a4b3b5 100644 --- a/server/src/com/cloud/configuration/ConfigurationManager.java +++ b/server/src/com/cloud/configuration/ConfigurationManager.java @@ -24,6 +24,7 @@ import com.cloud.api.commands.CreateDiskOfferingCmd; import com.cloud.api.commands.CreatePodCmd; import com.cloud.api.commands.CreateServiceOfferingCmd; import com.cloud.api.commands.CreateVlanIpRangeCmd; +import com.cloud.api.commands.CreateZoneCmd; import com.cloud.api.commands.DeleteDiskOfferingCmd; import com.cloud.api.commands.DeletePodCmd; import com.cloud.api.commands.DeleteServiceOfferingCmd; @@ -186,17 +187,12 @@ public interface ConfigurationManager extends Manager { /** * Creates a new zone - * @param userId - * @param zoneName - * @param dns1 - * @param dns2 - * @param dns3 - * @param dns4 - * @param vnetRange - * @param guestNetworkCidr - * @return Zone + * @param cmd + * @return the zone if successful, null otherwise + * @throws InvalidParameterValueException + * @throws InternalErrorException */ - DataCenterVO createZone(long userId, String zoneName, String dns1, String dns2, String dns3, String dns4, String vnetRange, String guestCidr) throws InvalidParameterValueException, InternalErrorException; + DataCenterVO createZone(CreateZoneCmd cmd) throws InvalidParameterValueException, InternalErrorException; /** * Edits a zone in the database. Will not allow you to edit DNS values if there are VMs in the specified zone. diff --git a/server/src/com/cloud/configuration/ConfigurationManagerImpl.java b/server/src/com/cloud/configuration/ConfigurationManagerImpl.java index b2f9fd850d5..d36afec6193 100644 --- a/server/src/com/cloud/configuration/ConfigurationManagerImpl.java +++ b/server/src/com/cloud/configuration/ConfigurationManagerImpl.java @@ -38,6 +38,7 @@ import com.cloud.api.commands.CreateDiskOfferingCmd; import com.cloud.api.commands.CreatePodCmd; import com.cloud.api.commands.CreateServiceOfferingCmd; import com.cloud.api.commands.CreateVlanIpRangeCmd; +import com.cloud.api.commands.CreateZoneCmd; import com.cloud.api.commands.DeleteDiskOfferingCmd; import com.cloud.api.commands.DeletePodCmd; import com.cloud.api.commands.DeleteServiceOfferingCmd; @@ -886,8 +887,21 @@ public class ConfigurationManagerImpl implements ConfigurationManager { } @DB - public DataCenterVO createZone(long userId, String zoneName, String dns1, String dns2, String internalDns1, String internalDns2, String vnetRange, String guestCidr) throws InvalidParameterValueException, InternalErrorException { - + public DataCenterVO createZone(CreateZoneCmd cmd) throws InvalidParameterValueException, InternalErrorException { + // grab parameters from the command + Long userId = UserContext.current().getUserId(); + String zoneName = cmd.getZoneName(); + String dns1 = cmd.getDns1(); + String dns2 = cmd.getDns2(); + String internalDns1 = cmd.getInternalDns1(); + String internalDns2 = cmd.getInternalDns2(); + String vnetRange = cmd.getVlan(); + String guestCidr = cmd.getGuestCidrAddress(); + + if (userId == null) { + userId = User.UID_SYSTEM; + } + int vnetStart, vnetEnd; if (vnetRange != null) { String[] tokens = vnetRange.split("-"); @@ -911,23 +925,22 @@ public class ConfigurationManagerImpl implements ConfigurationManager { throw new InvalidParameterValueException("Please specify a vlan range."); } } - + //checking the following params outside checkzoneparams method as we do not use these params for updatezone //hence the method below is generic to check for common params - if(guestCidr!=null && !NetUtils.isValidCIDR(guestCidr)) - { + if ((guestCidr != null) && !NetUtils.isValidCIDR(guestCidr)) { throw new InvalidParameterValueException("Please enter a valid guest cidr"); } - + checkZoneParameters(zoneName, dns1, dns2, internalDns1, internalDns2,true); - + // Create the new zone in the database DataCenterVO zone = new DataCenterVO(null, zoneName, null, dns1, dns2, internalDns1, internalDns2, vnetRange, guestCidr); zone = _zoneDao.persist(zone); - + // Add vnet entries for the new zone _zoneDao.addVnet(zone.getId(), vnetStart, vnetEnd); - + saveConfigurationEvent(userId, null, EventTypes.EVENT_ZONE_CREATE, "Successfully created new zone with name: " + zoneName + ".", "dcId=" + zone.getId(), "dns1=" + dns1, "dns2=" + dns2, "internalDns1=" + internalDns1, "internalDns2=" + internalDns2, "vnetRange=" + vnetRange, "guestCidr=" + guestCidr); return zone; @@ -1441,8 +1454,10 @@ public class ConfigurationManagerImpl implements ConfigurationManager { "vlanNetmask=" + vlanNetmask, "startIP=" + startIP, "endIP=" + endIP); - // if this is an account VLAN, now associate the IP Addresses to the account - associateIpAddressListToAccount(userId, account.getId(), zoneId, vlan.getId()); + if (associateIpRangeToAccount) { + // if this is an account VLAN, now associate the IP Addresses to the account + associateIpAddressListToAccount(userId, account.getId(), zoneId, vlan.getId()); + } return vlan; } diff --git a/server/src/com/cloud/server/ManagementServer.java b/server/src/com/cloud/server/ManagementServer.java index 29cbd025e76..27d2c836493 100644 --- a/server/src/com/cloud/server/ManagementServer.java +++ b/server/src/com/cloud/server/ManagementServer.java @@ -927,27 +927,6 @@ public interface ManagementServer { */ List listPods(long dataCenterId); - /** - * Adds a new zone to the database - * @param userId - * @param zoneName - * @param dns1 - * @param dns2 - * @param dns3 - * @param dns4 - * @param "-" separated range for network virtualization. - * @param guestNetworkCidr - * @return Zone - */ - DataCenterVO createZone(long userId, String zoneName, String dns1, String dns2, String dns3, String dns4, String vnetRange, String guestCidr) throws InvalidParameterValueException, InternalErrorException; - - /** - * Deletes a zone from the database - * @param userId - * @param zoneId - */ -// void deleteZone(long userId, Long zoneId) throws InvalidParameterValueException, InternalErrorException; - /** * Change a pod's private IP range * @param op diff --git a/server/src/com/cloud/server/ManagementServerImpl.java b/server/src/com/cloud/server/ManagementServerImpl.java index f1f3c3b04fc..8d6ed3a232e 100755 --- a/server/src/com/cloud/server/ManagementServerImpl.java +++ b/server/src/com/cloud/server/ManagementServerImpl.java @@ -1113,33 +1113,29 @@ public class ManagementServerImpl implements ManagementServer { } // make sure the account is enabled too - if (user != null) { - // if the user is either locked already or disabled already, don't change state...only lock currently enabled users - if (user.getState().equals(Account.ACCOUNT_STATE_LOCKED)) { - // already locked...no-op - return true; - } else if (user.getState().equals(Account.ACCOUNT_STATE_ENABLED)) { - success = doSetUserStatus(user.getId(), Account.ACCOUNT_STATE_LOCKED); + // if the user is either locked already or disabled already, don't change state...only lock currently enabled users + if (user.getState().equals(Account.ACCOUNT_STATE_LOCKED)) { + // already locked...no-op + return true; + } else if (user.getState().equals(Account.ACCOUNT_STATE_ENABLED)) { + success = doSetUserStatus(user.getId(), Account.ACCOUNT_STATE_LOCKED); - boolean lockAccount = true; - List allUsersByAccount = _userDao.listByAccount(user.getAccountId()); - for (UserVO oneUser : allUsersByAccount) { - if (oneUser.getState().equals(Account.ACCOUNT_STATE_ENABLED)) { - lockAccount = false; - break; - } - } - - if (lockAccount) { - success = (success && lockAccountInternal(user.getAccountId())); - } - } else { - if (s_logger.isInfoEnabled()) { - s_logger.info("Attempting to lock a non-enabled user, current state is " + user.getState() + " (userId: " + user.getId() + "), locking failed."); + boolean lockAccount = true; + List allUsersByAccount = _userDao.listByAccount(user.getAccountId()); + for (UserVO oneUser : allUsersByAccount) { + if (oneUser.getState().equals(Account.ACCOUNT_STATE_ENABLED)) { + lockAccount = false; + break; } } + + if (lockAccount) { + success = (success && lockAccountInternal(user.getAccountId())); + } } else { - s_logger.warn("Unable to find user with id: " + UserContext.current().getUserId()); + if (s_logger.isInfoEnabled()) { + s_logger.info("Attempting to lock a non-enabled user, current state is " + user.getState() + " (userId: " + user.getId() + "), locking failed."); + } } return success; } @@ -1621,7 +1617,6 @@ public class ManagementServerImpl implements ManagementServer { Long userId = UserContext.current().getUserId(); Account account = (Account)UserContext.current().getAccountObject(); String ipAddress = cmd.getIpAddress(); - boolean result = false; // Verify input parameters Account accountByIp = findAccountByIpAddress(ipAddress); @@ -4252,16 +4247,6 @@ public class ManagementServerImpl implements ManagementServer { return _hostPodDao.listByDataCenterId(dataCenterId); } - @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); - } - -// @Override -// public void deleteZone(long userId, Long zoneId) throws InvalidParameterValueException, InternalErrorException { -// _configMgr.deleteZone(userId, zoneId); -// } - @Override public String changePrivateIPRange(boolean add, Long podId, String startIP, String endIP) throws InvalidParameterValueException { return _configMgr.changePrivateIPRange(add, podId, startIP, endIP); @@ -7954,7 +7939,7 @@ public class ManagementServerImpl implements ManagementServer { VMInstanceVO systemVm = _vmInstanceDao.findByIdTypes(cmd.getId(), VirtualMachine.Type.ConsoleProxy, VirtualMachine.Type.SecondaryStorageVm); if (systemVm == null) { - throw new ServerApiException (BaseCmd.PARAM_ERROR, "unable to find a system vm with id " + systemVm.getId()); + throw new ServerApiException (BaseCmd.PARAM_ERROR, "unable to find a system vm with id " + cmd.getId()); } if (systemVm.getType().equals(VirtualMachine.Type.ConsoleProxy)){