From 21ab91dc2c17ecf03ee5963556365728ebf2523c Mon Sep 17 00:00:00 2001 From: Kris McQueen Date: Tue, 17 Aug 2010 15:21:13 -0700 Subject: [PATCH] refactoring CreateNetworkGroup API command to new API framework --- server/src/com/cloud/api/ApiDispatcher.java | 8 +- server/src/com/cloud/api/BaseCmd.java | 2 +- .../api/commands/CreateNetworkGroupCmd.java | 104 +++--------------- .../api/response/NetworkGroupResponse.java | 72 ++++++++++++ .../network/security/NetworkGroupManager.java | 9 ++ .../security/NetworkGroupManagerImpl.java | 71 +++++++++++- .../com/cloud/server/ManagementServer.java | 10 -- 7 files changed, 172 insertions(+), 104 deletions(-) create mode 100644 server/src/com/cloud/api/response/NetworkGroupResponse.java diff --git a/server/src/com/cloud/api/ApiDispatcher.java b/server/src/com/cloud/api/ApiDispatcher.java index f785fb544f2..ec0ae60e1d6 100644 --- a/server/src/com/cloud/api/ApiDispatcher.java +++ b/server/src/com/cloud/api/ApiDispatcher.java @@ -15,6 +15,7 @@ import org.apache.log4j.Logger; import com.cloud.api.BaseCmd.CommandType; import com.cloud.configuration.ConfigurationManager; import com.cloud.network.NetworkManager; +import com.cloud.network.security.NetworkGroupManager; import com.cloud.server.ManagementServer; import com.cloud.storage.StorageManager; import com.cloud.utils.DateUtil; @@ -30,6 +31,7 @@ public class ApiDispatcher { private ConfigurationManager _configMgr; private ManagementServer _mgmtServer; + private NetworkGroupManager _networkGroupMgr; private NetworkManager _networkMgr; private StorageManager _storageMgr; private UserVmManager _userVmMgr; @@ -38,6 +40,7 @@ public class ApiDispatcher { ComponentLocator locator = ComponentLocator.getLocator(ManagementServer.Name); _mgmtServer = (ManagementServer)ComponentLocator.getComponent(ManagementServer.Name); _configMgr = locator.getManager(ConfigurationManager.class); + _networkGroupMgr = locator.getManager(NetworkGroupManager.class); _networkMgr = locator.getManager(NetworkManager.class); _storageMgr = locator.getManager(StorageManager.class); _userVmMgr = locator.getManager(UserVmManager.class); @@ -86,6 +89,9 @@ public class ApiDispatcher { case ConfigManager: mgr = _configMgr; break; + case NetworkGroupManager: + mgr = _networkGroupMgr; + break; case NetworkManager: mgr = _networkMgr; break; @@ -115,7 +121,7 @@ public class ApiDispatcher { } } - @SuppressWarnings("unchecked") + @SuppressWarnings({"unchecked", "rawtypes"}) private void setFieldValue(Field field, BaseCmd cmdObj, Object paramObj, Parameter annotation) throws IllegalArgumentException, ParseException { try { field.setAccessible(true); diff --git a/server/src/com/cloud/api/BaseCmd.java b/server/src/com/cloud/api/BaseCmd.java index e4ee7d28443..7188caedbad 100644 --- a/server/src/com/cloud/api/BaseCmd.java +++ b/server/src/com/cloud/api/BaseCmd.java @@ -48,7 +48,7 @@ public abstract class BaseCmd { } public enum Manager { - ConfigManager, ManagementServer, NetworkManager, StorageManager, UserVmManager + ConfigManager, ManagementServer, NetworkGroupManager, NetworkManager, StorageManager, UserVmManager } // FIXME: Extract these out into a separate file diff --git a/server/src/com/cloud/api/commands/CreateNetworkGroupCmd.java b/server/src/com/cloud/api/commands/CreateNetworkGroupCmd.java index f5e5cb5ca26..a06682fba33 100644 --- a/server/src/com/cloud/api/commands/CreateNetworkGroupCmd.java +++ b/server/src/com/cloud/api/commands/CreateNetworkGroupCmd.java @@ -18,34 +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.NetworkGroupResponse; import com.cloud.network.security.NetworkGroupVO; -import com.cloud.user.Account; -import com.cloud.utils.Pair; +import com.cloud.serializer.SerializerHelper; +@Implementation(method="createLoadBalancerRule", manager=Manager.NetworkGroupManager) public class CreateNetworkGroupCmd extends BaseCmd { public static final Logger s_logger = Logger.getLogger(CreateNetworkGroupCmd.class.getName()); private static final String s_name = "createnetworkgroupresponse"; - private static final List> s_properties = new ArrayList>(); - - static { - //s_properties.add(new Pair(BaseCmd.Properties.USER_ID, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.ACCOUNT_OBJ, Boolean.FALSE)); - - s_properties.add(new Pair(BaseCmd.Properties.ACCOUNT, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.DESCRIPTION, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.DOMAIN_ID, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.NAME, Boolean.TRUE)); - } ///////////////////////////////////////////////////// //////////////// API parameters ///////////////////// @@ -92,79 +79,20 @@ public class CreateNetworkGroupCmd extends BaseCmd { public String getName() { return s_name; } - public List> getProperties() { - return s_properties; - } @Override - public List> execute(Map params) { - Account account = (Account)params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName()); - Long domainId = (Long)params.get(BaseCmd.Properties.DOMAIN_ID.getName()); - //Long userId = (Long)params.get(BaseCmd.Properties.USER_ID.getName()); - String accountName = (String)params.get(BaseCmd.Properties.ACCOUNT.getName()); - String name = (String)params.get(BaseCmd.Properties.NAME.getName()); - String description = (String)params.get(BaseCmd.Properties.DESCRIPTION.getName()); - Long accountId = null; + public String getResponse() { + NetworkGroupVO group = (NetworkGroupVO)getResponseObject(); - if (account != null) { - if (isAdmin(account.getType())) { - if (domainId != null) { - if (!getManagementServer().isChildDomain(account.getDomainId(), domainId)) { - throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "Unable to create network group in domain " + domainId + ", permission denied."); - } - } else { - // the admin must be creating the network group - if (account != null) { - accountId = account.getId(); - domainId = account.getDomainId(); - accountName = account.getAccountName(); - } - } - } else { - accountId = account.getId(); - domainId = account.getDomainId(); - accountName = account.getAccountName(); - } - } + NetworkGroupResponse response = new NetworkGroupResponse(); + response.setAccountName(group.getAccountName()); + response.setDescription(group.getDescription()); + response.setDomainId(group.getDomainId()); + // TODO: implement +// response.setDomainName(group.getDomainName()); + response.setId(group.getId()); + response.setName(group.getName()); - if (accountId == null) { - if ((accountName != null) && (domainId != null)) { - Account userAccount = getManagementServer().findActiveAccount(accountName, domainId); - if (userAccount != null) { - accountId = userAccount.getId(); - accountName = userAccount.getAccountName(); - } else { - throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "could not find account " + accountName + " in domain " + domainId); - } - } - } - - if (accountId == null) { - throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "Unable to create network group, no account specified."); - } - - boolean isNameInUse = getManagementServer().isNetworkSecurityGroupNameInUse(domainId, accountId, name); - - if (isNameInUse) { - throw new ServerApiException(BaseCmd.PARAM_ERROR, "Unable to create network group, a group with name " + name + " already exisits."); - } - - NetworkGroupVO networkGroup = getManagementServer().createNetworkGroup(name, description, domainId, accountId, accountName); - - List> embeddedObject = new ArrayList>(); - - List> returnValues = new ArrayList>(); - returnValues.add(new Pair(BaseCmd.Properties.ID.getName(), networkGroup.getId().toString())); - returnValues.add(new Pair(BaseCmd.Properties.NAME.getName(), networkGroup.getName())); - returnValues.add(new Pair(BaseCmd.Properties.DESCRIPTION.getName(), networkGroup.getDescription())); - - Account accountTemp = getManagementServer().findAccountById(networkGroup.getAccountId()); - if (accountTemp != null) { - returnValues.add(new Pair(BaseCmd.Properties.ACCOUNT.getName(), accountTemp.getAccountName())); - returnValues.add(new Pair(BaseCmd.Properties.DOMAIN_ID.getName(), accountTemp.getDomainId())); - returnValues.add(new Pair(BaseCmd.Properties.DOMAIN.getName(), getManagementServer().findDomainIdById(accountTemp.getDomainId()).getName())); - } - embeddedObject.add(new Pair("networkgroup", new Object[] { returnValues } )); - return embeddedObject; + return SerializerHelper.toSerializedString(response); } } diff --git a/server/src/com/cloud/api/response/NetworkGroupResponse.java b/server/src/com/cloud/api/response/NetworkGroupResponse.java new file mode 100644 index 00000000000..d6bba965e38 --- /dev/null +++ b/server/src/com/cloud/api/response/NetworkGroupResponse.java @@ -0,0 +1,72 @@ +package com.cloud.api.response; + +import com.cloud.api.ResponseObject; +import com.cloud.serializer.Param; + +public class NetworkGroupResponse implements ResponseObject { + @Param(name="id") + private Long id; + + @Param(name="name") + private String name; + + @Param(name="description") + private String description; + + @Param(name="account") + private String accountName; + + @Param(name="domainid") + private Long domainId; + + @Param(name="domain") + private String domainName; + + 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 getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getAccountName() { + return accountName; + } + + public void setAccountName(String accountName) { + this.accountName = accountName; + } + + public Long getDomainId() { + return domainId; + } + + public void setDomainId(Long domainId) { + this.domainId = domainId; + } + + public String getDomainName() { + return domainName; + } + + public void setDomainName(String domainName) { + this.domainName = domainName; + } +} diff --git a/server/src/com/cloud/network/security/NetworkGroupManager.java b/server/src/com/cloud/network/security/NetworkGroupManager.java index 75f02e0b824..9eaf5506153 100644 --- a/server/src/com/cloud/network/security/NetworkGroupManager.java +++ b/server/src/com/cloud/network/security/NetworkGroupManager.java @@ -20,6 +20,8 @@ package com.cloud.network.security; import java.util.HashMap; import java.util.List; +import com.cloud.api.commands.CreateNetworkGroupCmd; +import com.cloud.exception.InvalidParameterValueException; import com.cloud.exception.PermissionDeniedException; import com.cloud.exception.ResourceInUseException; import com.cloud.server.Criteria; @@ -45,6 +47,13 @@ public interface NetworkGroupManager extends Manager { String [] cidrList, List authorizedGroups); public NetworkGroupVO createNetworkGroup(String name, String description, Long domainId, Long accountId, String accountName); + + /** + * Create a network group with the given name and description + * @param command the command specifying the name and description + * @return the created network group if successful, null otherwise + */ + public NetworkGroupVO createNetworkGroup(CreateNetworkGroupCmd command) throws PermissionDeniedException, InvalidParameterValueException; public NetworkGroupVO createDefaultNetworkGroup( Long accountId); diff --git a/server/src/com/cloud/network/security/NetworkGroupManagerImpl.java b/server/src/com/cloud/network/security/NetworkGroupManagerImpl.java index 96ad145c1b6..f8b279f9ff2 100644 --- a/server/src/com/cloud/network/security/NetworkGroupManagerImpl.java +++ b/server/src/com/cloud/network/security/NetworkGroupManagerImpl.java @@ -41,10 +41,12 @@ import com.cloud.agent.AgentManager; import com.cloud.agent.api.Command; import com.cloud.agent.api.NetworkIngressRulesCmd; import com.cloud.agent.api.NetworkIngressRulesCmd.IpPortAndProto; +import com.cloud.api.commands.CreateNetworkGroupCmd; import com.cloud.configuration.dao.ConfigurationDao; import com.cloud.domain.DomainVO; import com.cloud.domain.dao.DomainDao; import com.cloud.exception.AgentUnavailableException; +import com.cloud.exception.InvalidParameterValueException; import com.cloud.exception.PermissionDeniedException; import com.cloud.exception.ResourceInUseException; import com.cloud.network.security.NetworkGroupWorkVO.Step; @@ -58,6 +60,7 @@ import com.cloud.server.Criteria; import com.cloud.server.ManagementServer; import com.cloud.user.Account; import com.cloud.user.AccountVO; +import com.cloud.user.UserContext; import com.cloud.user.dao.AccountDao; import com.cloud.uservm.UserVm; import com.cloud.utils.Pair; @@ -537,7 +540,66 @@ public class NetworkGroupManagerImpl implements NetworkGroupManager { } } - + + @Override + public NetworkGroupVO createNetworkGroup(CreateNetworkGroupCmd cmd) throws PermissionDeniedException, InvalidParameterValueException { + if (!_enabled) { + return null; + } + + String accountName = cmd.getAccountName(); + Long domainId = cmd.getDomainId(); + Long accountId = null; + + Account account = (Account)UserContext.current().getAccountObject(); + if (account != null) { + if ((account.getType() == Account.ACCOUNT_TYPE_ADMIN) || (account.getType() == Account.ACCOUNT_TYPE_DOMAIN_ADMIN)) { + if ((domainId != null) && (accountName != null)) { + if (!_domainDao.isChildDomain(account.getDomainId(), domainId)) { + throw new PermissionDeniedException("Unable to create network group in domain " + domainId + ", permission denied."); + } + + Account userAccount = _accountDao.findActiveAccount(accountName, domainId); + if (userAccount == null) { + throw new InvalidParameterValueException("Unable to find account " + accountName + " in domain " + domainId + ", failed to create network group " + cmd.getNetworkGroupName()); + } + + accountId = userAccount.getId(); + } else { + // the admin must be creating a network group for himself/herself + if (account != null) { + accountId = account.getId(); + domainId = account.getDomainId(); + accountName = account.getAccountName(); + } + } + } else { + accountId = account.getId(); + domainId = account.getDomainId(); + accountName = account.getAccountName(); + } + } + + // if no account exists in the context, it's a system level command, look up the account + if (accountId == null) { + if ((accountName != null) && (domainId != null)) { + Account userAccount = _accountDao.findActiveAccount(accountName, domainId); + if (userAccount != null) { + accountId = userAccount.getId(); + } else { + throw new InvalidParameterValueException("Unable to find account " + accountName + " in domain " + domainId + ", failed to create network group " + cmd.getNetworkGroupName()); + } + } else { + throw new InvalidParameterValueException("Missing account information (account: " + accountName + ", domain: " + domainId + "), failed to create network group " + cmd.getNetworkGroupName()); + } + } + + if (_networkGroupDao.isNameInUse(accountId, domainId, cmd.getNetworkGroupName())) { + throw new InvalidParameterValueException("Unable to create network group, a group with name " + cmd.getNetworkGroupName() + " already exisits."); + } + + return createNetworkGroup(cmd.getNetworkGroupName(), cmd.getDescription(), domainId, accountId, accountName); + } @DB @Override @@ -629,8 +691,9 @@ public class NetworkGroupManagerImpl implements NetworkGroupManager { @DB public void work() { - - s_logger.trace("Checking the database"); + if (s_logger.isTraceEnabled()) { + s_logger.trace("Checking the database"); + } final NetworkGroupWorkVO work = _workDao.take(_serverId); if (work == null) { return; @@ -800,7 +863,7 @@ public class NetworkGroupManagerImpl implements NetworkGroupManager { if (networkGroup != null) { sc.setParameters("name", networkGroup); } else if (keyword != null) { - SearchCriteria ssc = _networkGroupRulesDao.createSearchCriteria(); + SearchCriteria ssc = _networkGroupRulesDao.createSearchCriteria(); ssc.addOr("name", SearchCriteria.Op.LIKE, "%" + keyword + "%"); ssc.addOr("description", SearchCriteria.Op.LIKE, "%" + keyword + "%"); sc.addAnd("name", SearchCriteria.Op.SC, ssc); diff --git a/server/src/com/cloud/server/ManagementServer.java b/server/src/com/cloud/server/ManagementServer.java index c31caf051b3..a261dc76f1f 100644 --- a/server/src/com/cloud/server/ManagementServer.java +++ b/server/src/com/cloud/server/ManagementServer.java @@ -2032,16 +2032,6 @@ public interface ManagementServer { */ ArrayList getCloudIdentifierResponse(GetCloudIdentifierCmd cmd) throws InvalidParameterValueException; - /** - * check if a network security group name in the given account/domain is in use - * - if accountId is specified, look only for the account - * - otherwise look for the name in domain-level security groups (accountId is null) - * @param domainId id of the domain in which to search for security groups - * @param accountId id of the account in which to search for security groups - * @param name name of the security group to look for - * @return true if the security group name is found, false otherwise - */ - boolean isNetworkSecurityGroupNameInUse(Long domainId, Long accountId, String name); NetworkGroupVO findNetworkGroupByName(Long accountId, String groupName); /**