From 476151453ee04ed925a397764dd2ea6f2269dfba Mon Sep 17 00:00:00 2001 From: Kris McQueen Date: Wed, 1 Sep 2010 16:19:11 -0700 Subject: [PATCH] Refactor listPortForwardingServiceRules to new API framework. --- .../ListPortForwardingServiceRulesCmd.java | 122 +++++------------- .../PortForwardingServiceRuleResponse.java | 22 ++++ .../com/cloud/server/ManagementServer.java | 9 +- .../cloud/server/ManagementServerImpl.java | 45 ++++++- 4 files changed, 97 insertions(+), 101 deletions(-) diff --git a/server/src/com/cloud/api/commands/ListPortForwardingServiceRulesCmd.java b/server/src/com/cloud/api/commands/ListPortForwardingServiceRulesCmd.java index 9b855f075df..48089bdd01e 100644 --- a/server/src/com/cloud/api/commands/ListPortForwardingServiceRulesCmd.java +++ b/server/src/com/cloud/api/commands/ListPortForwardingServiceRulesCmd.java @@ -20,34 +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.api.response.PortForwardingServiceRuleResponse; import com.cloud.async.AsyncJobVO; import com.cloud.network.NetworkRuleConfigVO; -import com.cloud.network.SecurityGroupVO; -import com.cloud.server.Criteria; -import com.cloud.user.Account; -import com.cloud.utils.Pair; - -public class ListPortForwardingServiceRulesCmd extends BaseCmd { +import com.cloud.serializer.SerializerHelper; + +@Implementation(method="searchForNetworkRules") +public class ListPortForwardingServiceRulesCmd extends BaseListCmd { public static final Logger s_logger = Logger.getLogger(ListPortForwardingServiceRulesCmd.class.getName()); private static final String s_name = "listportforwardingservicerulesresponse"; - 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.PORT_FORWARDING_SERVICE_ID, Boolean.FALSE)); - } ///////////////////////////////////////////////////// //////////////// API parameters ///////////////////// @@ -90,83 +78,33 @@ public class ListPortForwardingServiceRulesCmd 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()); - String accountName = (String)params.get(BaseCmd.Properties.ACCOUNT.getName()); - Long domainId = (Long)params.get(BaseCmd.Properties.DOMAIN_ID.getName()); - Long id = (Long)params.get(BaseCmd.Properties.ID.getName()); - Long groupId = (Long)params.get(BaseCmd.Properties.PORT_FORWARDING_SERVICE_ID.getName()); - - // FIXME: validate that the domain admin can list network rules for the group in question - Long accountId = null; - if ((account == null) || isAdmin(account.getType())) { - if (domainId != null) { - if ((account != null) && !getManagementServer().isChildDomain(account.getDomainId(), domainId)) { - throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "Invalid domain id (" + domainId + ") given, unable to list port forwarding service rules."); - } - if (accountName != null) { - Account userAcct = getManagementServer().findActiveAccount(accountName, domainId); - if (userAcct != null) { - accountId = userAcct.getId(); - } else { - throw new ServerApiException(BaseCmd.PARAM_ERROR, "Unable to find account " + accountName + " in domain " + domainId); - } - } - } - } else { - accountId = account.getId(); - } + } - if ((groupId != null) && (accountId != null)) { - SecurityGroupVO sg = getManagementServer().findSecurityGroupById(groupId); - if (sg != null) { - if ((sg.getAccountId() != null) && sg.getAccountId().longValue() != accountId.longValue()) { - throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "Unable to list port forwarding service rules, account " + accountId + " does not own port forwarding service " + groupId); - } - } else { - throw new ServerApiException(BaseCmd.PARAM_ERROR, "Unable to find port forwarding service with id " + groupId); + @Override @SuppressWarnings("unchecked") + public String getResponse() { + List portForwardingServiceRules = (List)getResponseObject(); + + List response = new ArrayList(); + for (NetworkRuleConfigVO rule : portForwardingServiceRules) { + PortForwardingServiceRuleResponse ruleResponse = new PortForwardingServiceRuleResponse(); + ruleResponse.setRuleId(rule.getId()); + ruleResponse.setPortForwardingServiceId(rule.getSecurityGroupId()); + ruleResponse.setPublicPort(rule.getPublicPort()); + ruleResponse.setPrivatePort(rule.getPrivatePort()); + ruleResponse.setProtocol(rule.getProtocol()); + + AsyncJobVO asyncJob = getManagementServer().findInstancePendingAsyncJob("network_rule_config", rule.getId()); + if(asyncJob != null) { + ruleResponse.setJobId(asyncJob.getId()); + ruleResponse.setJobStatus(asyncJob.getStatus()); } + + response.add(ruleResponse); } - - Criteria c = new Criteria("id", Boolean.TRUE, null, null); - c.addCriteria(Criteria.ID, id); - c.addCriteria(Criteria.GROUPID, groupId); - c.addCriteria(Criteria.ACCOUNTID, accountId); - - List netRules = getManagementServer().searchForNetworkRules(c); - - if (netRules == null) { - throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Internal error searching for network rules for security group " + groupId); - } - - List> netRulesTags = new ArrayList>(); - Object[] netRuleTag = new Object[netRules.size()]; - int i = 0; - for (NetworkRuleConfigVO netRule : netRules) { - List> netRuleData = new ArrayList>(); - netRuleData.add(new Pair(BaseCmd.Properties.ID.getName(), netRule.getId().toString())); - netRuleData.add(new Pair(BaseCmd.Properties.PORT_FORWARDING_SERVICE_ID.getName(), Long.valueOf(netRule.getSecurityGroupId()).toString())); - netRuleData.add(new Pair(BaseCmd.Properties.PUBLIC_PORT.getName(), netRule.getPublicPort())); - netRuleData.add(new Pair(BaseCmd.Properties.PRIVATE_PORT.getName(), netRule.getPrivatePort())); - netRuleData.add(new Pair(BaseCmd.Properties.PROTOCOL.getName(), netRule.getProtocol())); - - AsyncJobVO asyncJob = getManagementServer().findInstancePendingAsyncJob("network_rule_config", netRule.getId()); - if(asyncJob != null) { - netRuleData.add(new Pair(BaseCmd.Properties.JOB_ID.getName(), asyncJob.getId().toString())); - netRuleData.add(new Pair(BaseCmd.Properties.JOB_STATUS.getName(), String.valueOf(asyncJob.getStatus()))); - } - netRuleTag[i++] = netRuleData; - } - Pair eventTag = new Pair("portforwardingservicerule", netRuleTag); - netRulesTags.add(eventTag); - return netRulesTags; + + return SerializerHelper.toSerializedString(response); } } diff --git a/server/src/com/cloud/api/response/PortForwardingServiceRuleResponse.java b/server/src/com/cloud/api/response/PortForwardingServiceRuleResponse.java index 5337040a3d2..c9bd7b0e16b 100644 --- a/server/src/com/cloud/api/response/PortForwardingServiceRuleResponse.java +++ b/server/src/com/cloud/api/response/PortForwardingServiceRuleResponse.java @@ -19,6 +19,12 @@ public class PortForwardingServiceRuleResponse implements ResponseObject { @Param(name="portforwardingserviceid") private Long portForwardingServiceId; + @Param(name="jobid") + private Long jobId; + + @Param(name="jobstatus") + private Integer jobStatus; + public Long getPortForwardingServiceId() { return portForwardingServiceId; } @@ -58,4 +64,20 @@ public class PortForwardingServiceRuleResponse implements ResponseObject { public void setProtocol(String protocol) { this.protocol = protocol; } + + public Long getJobId() { + return jobId; + } + + public void setJobId(Long jobId) { + this.jobId = jobId; + } + + public Integer getJobStatus() { + return jobStatus; + } + + public void setJobStatus(Integer jobStatus) { + this.jobStatus = jobStatus; + } } diff --git a/server/src/com/cloud/server/ManagementServer.java b/server/src/com/cloud/server/ManagementServer.java index ff7f6f1d3c6..2f7672f4950 100644 --- a/server/src/com/cloud/server/ManagementServer.java +++ b/server/src/com/cloud/server/ManagementServer.java @@ -47,6 +47,7 @@ import com.cloud.api.commands.ListIsosCmd; import com.cloud.api.commands.ListLoadBalancerRuleInstancesCmd; import com.cloud.api.commands.ListLoadBalancerRulesCmd; import com.cloud.api.commands.ListPodsByCmd; +import com.cloud.api.commands.ListPortForwardingServiceRulesCmd; import com.cloud.api.commands.ListTemplatesCmd; import com.cloud.api.commands.LockAccountCmd; import com.cloud.api.commands.LockUserCmd; @@ -1150,11 +1151,11 @@ public interface ManagementServer { IPAddressVO findIPAddressById(String ipAddress); /** - * Search for network rules given the search criteria. For now only group id (security group id) is supported. - * @param c the search criteria including order by and max rows - * @return list of rules for the security group id specified in the search criteria + * Search for network rules given the search criteria. + * @param cmd the command containing the search criteria including port forwarding service id or rule id. + * @return list of rules for the port forwarding service id specified in the search criteria */ - List searchForNetworkRules(Criteria c); + List searchForNetworkRules(ListPortForwardingServiceRulesCmd c) throws InvalidParameterValueException, PermissionDeniedException; /** * Saves an event with the specified parameters. diff --git a/server/src/com/cloud/server/ManagementServerImpl.java b/server/src/com/cloud/server/ManagementServerImpl.java index 9801ce6066f..6b758a265f6 100755 --- a/server/src/com/cloud/server/ManagementServerImpl.java +++ b/server/src/com/cloud/server/ManagementServerImpl.java @@ -85,6 +85,7 @@ import com.cloud.api.commands.ListIsosCmd; import com.cloud.api.commands.ListLoadBalancerRuleInstancesCmd; import com.cloud.api.commands.ListLoadBalancerRulesCmd; import com.cloud.api.commands.ListPodsByCmd; +import com.cloud.api.commands.ListPortForwardingServiceRulesCmd; import com.cloud.api.commands.ListTemplatesCmd; import com.cloud.api.commands.LockAccountCmd; import com.cloud.api.commands.LockUserCmd; @@ -5120,12 +5121,46 @@ public class ManagementServerImpl implements ManagementServer { } @Override - public List searchForNetworkRules(Criteria c) { - Filter searchFilter = new Filter(NetworkRuleConfigVO.class, c.getOrderBy(), c.getAscending(), c.getOffset(), c.getLimit()); + public List searchForNetworkRules(ListPortForwardingServiceRulesCmd cmd) throws InvalidParameterValueException, PermissionDeniedException { + Long accountId = null; + Account account = (Account)UserContext.current().getAccountObject(); + Long domainId = cmd.getDomainId(); + String accountName = cmd.getAccountName(); + Long groupId = cmd.getPortForwardingServiceId(); - Object groupId = c.getCriteria(Criteria.GROUPID); - Object id = c.getCriteria(Criteria.ID); - Object accountId = c.getCriteria(Criteria.ACCOUNTID); + if ((account == null) || isAdmin(account.getType())) { + if (domainId != null) { + if ((account != null) && !_domainDao.isChildDomain(account.getDomainId(), domainId)) { + throw new PermissionDeniedException("Unable to list port forwarding service rules for domain " + domainId + ", permission denied."); + } + if (accountName != null) { + Account userAcct = _accountDao.findActiveAccount(accountName, domainId); + if (userAcct != null) { + accountId = userAcct.getId(); + } else { + throw new InvalidParameterValueException("Unable to find account " + accountName + " in domain " + domainId); + } + } + } + } else { + accountId = account.getId(); + } + + if ((groupId != null) && (accountId != null)) { + SecurityGroupVO sg = _securityGroupDao.findById(groupId); + if (sg != null) { + if ((sg.getAccountId() != null) && sg.getAccountId().longValue() != accountId.longValue()) { + throw new PermissionDeniedException("Unable to list port forwarding service rules, account " + accountId + " does not own port forwarding service " + groupId); + } + } else { + throw new InvalidParameterValueException("Unable to find port forwarding service with id " + groupId); + } + } + + Filter searchFilter = new Filter(NetworkRuleConfigVO.class, "id", true, null, null); + + // search by rule id is also supported + Object id = cmd.getId(); SearchBuilder sb = _networkRuleConfigDao.createSearchBuilder(); if (id != null) {