mirror of
https://github.com/apache/cloudstack.git
synced 2025-11-03 04:12:31 +01:00
Refactor listPortForwardingServiceRules to new API framework.
This commit is contained in:
parent
931706ad87
commit
476151453e
@ -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<Pair<Enum, Boolean>> s_properties = new ArrayList<Pair<Enum, Boolean>>();
|
||||
|
||||
static {
|
||||
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.ACCOUNT_OBJ, Boolean.FALSE));
|
||||
|
||||
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.ACCOUNT, Boolean.FALSE));
|
||||
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.DOMAIN_ID, Boolean.FALSE));
|
||||
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.ID, Boolean.FALSE));
|
||||
s_properties.add(new Pair<Enum, Boolean>(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<Pair<Enum, Boolean>> getProperties() {
|
||||
return s_properties;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Pair<String, Object>> execute(Map<String, Object> 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<NetworkRuleConfigVO> portForwardingServiceRules = (List<NetworkRuleConfigVO>)getResponseObject();
|
||||
|
||||
List<PortForwardingServiceRuleResponse> response = new ArrayList<PortForwardingServiceRuleResponse>();
|
||||
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<NetworkRuleConfigVO> netRules = getManagementServer().searchForNetworkRules(c);
|
||||
|
||||
if (netRules == null) {
|
||||
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Internal error searching for network rules for security group " + groupId);
|
||||
}
|
||||
|
||||
List<Pair<String, Object>> netRulesTags = new ArrayList<Pair<String, Object>>();
|
||||
Object[] netRuleTag = new Object[netRules.size()];
|
||||
int i = 0;
|
||||
for (NetworkRuleConfigVO netRule : netRules) {
|
||||
List<Pair<String, Object>> netRuleData = new ArrayList<Pair<String, Object>>();
|
||||
netRuleData.add(new Pair<String, Object>(BaseCmd.Properties.ID.getName(), netRule.getId().toString()));
|
||||
netRuleData.add(new Pair<String, Object>(BaseCmd.Properties.PORT_FORWARDING_SERVICE_ID.getName(), Long.valueOf(netRule.getSecurityGroupId()).toString()));
|
||||
netRuleData.add(new Pair<String, Object>(BaseCmd.Properties.PUBLIC_PORT.getName(), netRule.getPublicPort()));
|
||||
netRuleData.add(new Pair<String, Object>(BaseCmd.Properties.PRIVATE_PORT.getName(), netRule.getPrivatePort()));
|
||||
netRuleData.add(new Pair<String, Object>(BaseCmd.Properties.PROTOCOL.getName(), netRule.getProtocol()));
|
||||
|
||||
AsyncJobVO asyncJob = getManagementServer().findInstancePendingAsyncJob("network_rule_config", netRule.getId());
|
||||
if(asyncJob != null) {
|
||||
netRuleData.add(new Pair<String, Object>(BaseCmd.Properties.JOB_ID.getName(), asyncJob.getId().toString()));
|
||||
netRuleData.add(new Pair<String, Object>(BaseCmd.Properties.JOB_STATUS.getName(), String.valueOf(asyncJob.getStatus())));
|
||||
}
|
||||
netRuleTag[i++] = netRuleData;
|
||||
}
|
||||
Pair<String, Object> eventTag = new Pair<String, Object>("portforwardingservicerule", netRuleTag);
|
||||
netRulesTags.add(eventTag);
|
||||
return netRulesTags;
|
||||
|
||||
return SerializerHelper.toSerializedString(response);
|
||||
}
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@ -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<NetworkRuleConfigVO> searchForNetworkRules(Criteria c);
|
||||
List<NetworkRuleConfigVO> searchForNetworkRules(ListPortForwardingServiceRulesCmd c) throws InvalidParameterValueException, PermissionDeniedException;
|
||||
|
||||
/**
|
||||
* Saves an event with the specified parameters.
|
||||
|
||||
@ -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<NetworkRuleConfigVO> searchForNetworkRules(Criteria c) {
|
||||
Filter searchFilter = new Filter(NetworkRuleConfigVO.class, c.getOrderBy(), c.getAscending(), c.getOffset(), c.getLimit());
|
||||
public List<NetworkRuleConfigVO> 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<NetworkRuleConfigVO> sb = _networkRuleConfigDao.createSearchBuilder();
|
||||
if (id != null) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user