From 2cd9a4426770dce0e9d607d9e57877f54da49482 Mon Sep 17 00:00:00 2001 From: Kris McQueen Date: Thu, 2 Sep 2010 15:03:06 -0700 Subject: [PATCH] Refactoring listPortForwardingServices to new API framework. Also forgot to return a response from listPortForwardingServicesByVM... --- .../ListPortForwardingServicesByVmCmd.java | 5 + .../ListPortForwardingServicesCmd.java | 147 ++++-------------- .../com/cloud/server/ManagementServer.java | 5 +- .../cloud/server/ManagementServerImpl.java | 40 ++++- 4 files changed, 73 insertions(+), 124 deletions(-) diff --git a/server/src/com/cloud/api/commands/ListPortForwardingServicesByVmCmd.java b/server/src/com/cloud/api/commands/ListPortForwardingServicesByVmCmd.java index 4287f3a3e15..33b3744327d 100644 --- a/server/src/com/cloud/api/commands/ListPortForwardingServicesByVmCmd.java +++ b/server/src/com/cloud/api/commands/ListPortForwardingServicesByVmCmd.java @@ -29,6 +29,7 @@ import com.cloud.api.Implementation; import com.cloud.api.Parameter; import com.cloud.api.response.SecurityGroupResponse; import com.cloud.network.SecurityGroupVO; +import com.cloud.serializer.SerializerHelper; import com.cloud.user.Account; @Implementation(method="searchForSecurityGroupsByVM") @@ -103,7 +104,11 @@ public class ListPortForwardingServicesByVmCmd extends BaseListCmd { pfsData.setDomainId(accountTemp.getDomainId()); pfsData.setDomainName(getManagementServer().findDomainIdById(accountTemp.getDomainId()).getName()); } + + response.add(pfsData); } } + + return SerializerHelper.toSerializedString(response); } } diff --git a/server/src/com/cloud/api/commands/ListPortForwardingServicesCmd.java b/server/src/com/cloud/api/commands/ListPortForwardingServicesCmd.java index 8a209061093..b727d6b4e5c 100644 --- a/server/src/com/cloud/api/commands/ListPortForwardingServicesCmd.java +++ b/server/src/com/cloud/api/commands/ListPortForwardingServicesCmd.java @@ -20,37 +20,22 @@ 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.BaseListCmd; +import com.cloud.api.Implementation; import com.cloud.api.Parameter; -import com.cloud.api.ServerApiException; -import com.cloud.domain.DomainVO; +import com.cloud.api.response.SecurityGroupResponse; import com.cloud.network.SecurityGroupVO; -import com.cloud.server.Criteria; +import com.cloud.serializer.SerializerHelper; import com.cloud.user.Account; -import com.cloud.utils.Pair; - -public class ListPortForwardingServicesCmd extends BaseCmd { + +@Implementation(method="searchForSecurityGroups") +public class ListPortForwardingServicesCmd extends BaseListCmd { public static final Logger s_logger = Logger.getLogger(ListPortForwardingServicesCmd.class.getName()); private static final String s_name = "listportforwardingservicesresponse"; - private static final List> s_properties = new ArrayList>(); - - static { - 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.DOMAIN_ID, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.ID, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.NAME, Boolean.FALSE)); - - s_properties.add(new Pair(BaseCmd.Properties.KEYWORD, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.PAGE, Boolean.FALSE)); - s_properties.add(new Pair(BaseCmd.Properties.PAGESIZE, Boolean.FALSE)); - } ///////////////////////////////////////////////////// //////////////// API parameters ///////////////////// @@ -93,101 +78,33 @@ public class ListPortForwardingServicesCmd extends BaseCmd { /////////////// API Implementation/////////////////// ///////////////////////////////////////////////////// + @Override 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 id = (Long)params.get(BaseCmd.Properties.ID.getName()); - Long domainId = (Long)params.get(BaseCmd.Properties.DOMAIN_ID.getName()); - String accountName = (String)params.get(BaseCmd.Properties.ACCOUNT.getName()); - String name = (String)params.get(BaseCmd.Properties.NAME.getName()); - String keyword = (String)params.get(BaseCmd.Properties.KEYWORD.getName()); - Integer page = (Integer)params.get(BaseCmd.Properties.PAGE.getName()); - Integer pageSize = (Integer)params.get(BaseCmd.Properties.PAGESIZE.getName()); - boolean isAdmin = false; - Long accountId = null; - - // if an admin account was passed in, or no account was passed in, make sure we honor the accountName/domainId parameters - if ((account == null) || isAdmin(account.getType())) { - isAdmin = true; - // validate domainId before proceeding - if (domainId != null) { - if ((account != null) && !getManagementServer().isChildDomain(account.getDomainId(), domainId)) { - throw new ServerApiException(BaseCmd.PARAM_ERROR, "Invalid domain id (" + domainId + ") given, unable to list port forwarding services."); - } - if (accountName != null) { - Account userAccount = getManagementServer().findAccountByName(accountName, domainId); - if (userAccount != null) { - accountId = userAccount.getId(); - } else { - throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "Unable to find account " + accountName + " in domain " + domainId); - } - } - } else { - domainId = ((account == null) ? DomainVO.ROOT_DOMAIN : account.getDomainId()); - } - } else { - accountId = account.getId(); - } - - Long startIndex = Long.valueOf(0); - int pageSizeNum = 50; - if (pageSize != null) { - pageSizeNum = pageSize.intValue(); - } - if (page != null) { - int pageNum = page.intValue(); - if (pageNum > 0) { - startIndex = Long.valueOf(pageSizeNum * (pageNum-1)); - } - } - - Criteria c = new Criteria("id", Boolean.TRUE, startIndex, Long.valueOf(pageSizeNum)); - - c.addCriteria(Criteria.ACCOUNTID, accountId); - if (keyword != null) { - c.addCriteria(Criteria.KEYWORD, keyword); - } else { - if (isAdmin) { - c.addCriteria(Criteria.DOMAINID, domainId); - } - c.addCriteria(Criteria.ID, id); - c.addCriteria(Criteria.NAME, name); - } - - List groups = getManagementServer().searchForSecurityGroups(c); - - if (groups == null) { - throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Internal error searching for port forwarding services"); - } - - List> groupsTags = new ArrayList>(); - Object[] groupTag = new Object[groups.size()]; - int i = 0; - for (SecurityGroupVO group : groups) { - List> groupData = new ArrayList>(); - groupData.add(new Pair(BaseCmd.Properties.ID.getName(), Long.valueOf(group.getId()).toString())); - groupData.add(new Pair(BaseCmd.Properties.NAME.getName(), group.getName())); - groupData.add(new Pair(BaseCmd.Properties.DESCRIPTION.getName(), group.getDescription())); - - Account accountTemp = getManagementServer().findAccountById(group.getAccountId()); - if (accountTemp != null) { - groupData.add(new Pair(BaseCmd.Properties.ACCOUNT.getName(), accountTemp.getAccountName())); - groupData.add(new Pair(BaseCmd.Properties.DOMAIN_ID.getName(), accountTemp.getDomainId())); - groupData.add(new Pair(BaseCmd.Properties.DOMAIN.getName(), getManagementServer().findDomainIdById(accountTemp.getDomainId()).getName())); - } - - groupTag[i++] = groupData; - } - Pair eventTag = new Pair("portforwardingservice", groupTag); - groupsTags.add(eventTag); - return groupsTags; + } + + @Override @SuppressWarnings("unchecked") + public String getResponse() { + List groups = (List)getResponseObject(); + + List response = new ArrayList(); + for (SecurityGroupVO group : groups) { + SecurityGroupResponse pfsData = new SecurityGroupResponse(); + pfsData.setId(group.getId()); + pfsData.setName(group.getName()); + pfsData.setDescription(group.getDescription()); + + Account accountTemp = getManagementServer().findAccountById(group.getAccountId()); + if (accountTemp != null) { + pfsData.setAccountName(accountTemp.getAccountName()); + pfsData.setDomainId(accountTemp.getDomainId()); + pfsData.setDomainName(getManagementServer().findDomainIdById(accountTemp.getDomainId()).getName()); + } + + response.add(pfsData); + } + + return SerializerHelper.toSerializedString(response); } } diff --git a/server/src/com/cloud/server/ManagementServer.java b/server/src/com/cloud/server/ManagementServer.java index 49a31c6e9d1..6d86c021b7e 100644 --- a/server/src/com/cloud/server/ManagementServer.java +++ b/server/src/com/cloud/server/ManagementServer.java @@ -49,6 +49,7 @@ import com.cloud.api.commands.ListLoadBalancerRulesCmd; import com.cloud.api.commands.ListPodsByCmd; import com.cloud.api.commands.ListPortForwardingServiceRulesCmd; import com.cloud.api.commands.ListPortForwardingServicesByVmCmd; +import com.cloud.api.commands.ListPortForwardingServicesCmd; import com.cloud.api.commands.ListTemplatesCmd; import com.cloud.api.commands.LockAccountCmd; import com.cloud.api.commands.LockUserCmd; @@ -1705,10 +1706,10 @@ public interface ManagementServer { /** * returns a list of security groups - * @param c + * @param cmd * @return a list of security groups */ - List searchForSecurityGroups(Criteria c); + List searchForSecurityGroups(ListPortForwardingServicesCmd cmd) throws InvalidParameterValueException, PermissionDeniedException; /** * returns a list of security groups from a given ip and vm id diff --git a/server/src/com/cloud/server/ManagementServerImpl.java b/server/src/com/cloud/server/ManagementServerImpl.java index d9cb574a00a..6705d537b40 100755 --- a/server/src/com/cloud/server/ManagementServerImpl.java +++ b/server/src/com/cloud/server/ManagementServerImpl.java @@ -87,6 +87,7 @@ import com.cloud.api.commands.ListLoadBalancerRulesCmd; import com.cloud.api.commands.ListPodsByCmd; import com.cloud.api.commands.ListPortForwardingServiceRulesCmd; import com.cloud.api.commands.ListPortForwardingServicesByVmCmd; +import com.cloud.api.commands.ListPortForwardingServicesCmd; import com.cloud.api.commands.ListTemplatesCmd; import com.cloud.api.commands.LockAccountCmd; import com.cloud.api.commands.LockUserCmd; @@ -7033,14 +7034,39 @@ public class ManagementServerImpl implements ManagementServer { } @Override - public List searchForSecurityGroups(Criteria c) { - Filter searchFilter = new Filter(SecurityGroupVO.class, c.getOrderBy(), c.getAscending(), c.getOffset(), c.getLimit()); + public List searchForSecurityGroups(ListPortForwardingServicesCmd cmd) throws InvalidParameterValueException, PermissionDeniedException { + // if an admin account was passed in, or no account was passed in, make sure we honor the accountName/domainId parameters + Account account = (Account)UserContext.current().getAccountObject(); + Long accountId = null; + Long domainId = cmd.getDomainId(); + String accountName = cmd.getAccountName(); - Object domainId = c.getCriteria(Criteria.DOMAINID); - Object accountId = c.getCriteria(Criteria.ACCOUNTID); - Object name = c.getCriteria(Criteria.NAME); - Object id = c.getCriteria(Criteria.ID); - Object keyword = c.getCriteria(Criteria.KEYWORD); + if ((account == null) || isAdmin(account.getType())) { + // validate domainId before proceeding + if (domainId != null) { + if ((account != null) && !_domainDao.isChildDomain(account.getDomainId(), domainId)) { + throw new PermissionDeniedException("Invalid domain id (" + domainId + ") given, unable to list port forwarding services."); + } + if (accountName != 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); + } + } + } else { + domainId = ((account == null) ? DomainVO.ROOT_DOMAIN : account.getDomainId()); + } + } else { + accountId = account.getId(); + } + + Filter searchFilter = new Filter(SecurityGroupVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal()); + + Object name = cmd.getPortForwardingServiceName(); + Object id = cmd.getId(); + Object keyword = cmd.getKeyword(); SearchBuilder sb = _securityGroupDao.createSearchBuilder(); sb.and("name", sb.entity().getName(), SearchCriteria.Op.LIKE);