mirror of https://github.com/apache/cloudstack.git
Refactoring listLoadBalancerRules to new API framework.
This commit is contained in:
parent
583f29242d
commit
ec9ad29c9a
|
|
@ -20,40 +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.network.IPAddressVO;
|
||||
import com.cloud.api.response.LoadBalancerResponse;
|
||||
import com.cloud.network.LoadBalancerVO;
|
||||
import com.cloud.server.Criteria;
|
||||
import com.cloud.serializer.SerializerHelper;
|
||||
import com.cloud.user.Account;
|
||||
import com.cloud.utils.Pair;
|
||||
|
||||
public class ListLoadBalancerRulesCmd extends BaseCmd {
|
||||
|
||||
@Implementation(method="searchForLoadBalancers")
|
||||
public class ListLoadBalancerRulesCmd extends BaseListCmd {
|
||||
public static final Logger s_logger = Logger.getLogger (ListLoadBalancerRulesCmd.class.getName());
|
||||
|
||||
private static final String s_name = "listloadbalancerrulesresponse";
|
||||
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.NAME, Boolean.FALSE));
|
||||
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.PUBLIC_IP, Boolean.FALSE));
|
||||
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.VIRTUAL_MACHINE_ID, Boolean.FALSE));
|
||||
|
||||
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.KEYWORD, Boolean.FALSE));
|
||||
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.PAGE, Boolean.FALSE));
|
||||
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.PAGESIZE, Boolean.FALSE));
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////
|
||||
//////////////// API parameters /////////////////////
|
||||
|
|
@ -110,122 +92,37 @@ public class ListLoadBalancerRulesCmd 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 id = (Long)params.get(BaseCmd.Properties.ID.getName());
|
||||
Long domainId = (Long)params.get(BaseCmd.Properties.DOMAIN_ID.getName());
|
||||
String publicIp = (String)params.get(BaseCmd.Properties.PUBLIC_IP.getName());
|
||||
String name = (String)params.get(BaseCmd.Properties.NAME.getName());
|
||||
String keyword = (String)params.get(BaseCmd.Properties.KEYWORD.getName());
|
||||
Long vmId = (Long)params.get(BaseCmd.Properties.VIRTUAL_MACHINE_ID.getName());
|
||||
Integer page = (Integer)params.get(BaseCmd.Properties.PAGE.getName());
|
||||
Integer pageSize = (Integer)params.get(BaseCmd.Properties.PAGESIZE.getName());
|
||||
Long accountId = null;
|
||||
boolean isAdmin = false;
|
||||
Account ipAddressOwner = null;
|
||||
}
|
||||
|
||||
if (publicIp != null) {
|
||||
IPAddressVO ipAddressVO = getManagementServer().findIPAddressById(publicIp);
|
||||
if (ipAddressVO == null) {
|
||||
throw new ServerApiException(BaseCmd.PARAM_ERROR, "Unable to find IP address " + publicIp);
|
||||
} else {
|
||||
ipAddressOwner = getManagementServer().findAccountById(ipAddressVO.getAccountId());
|
||||
@Override @SuppressWarnings("unchecked")
|
||||
public String getResponse() {
|
||||
List<LoadBalancerVO> loadBalancers = (List<LoadBalancerVO>)getResponseObject();
|
||||
|
||||
List<LoadBalancerResponse> response = new ArrayList<LoadBalancerResponse>();
|
||||
for (LoadBalancerVO loadBalancer : loadBalancers) {
|
||||
LoadBalancerResponse lbResponse = new LoadBalancerResponse();
|
||||
lbResponse.setId(loadBalancer.getId());
|
||||
lbResponse.setName(loadBalancer.getName());
|
||||
lbResponse.setDescription(loadBalancer.getDescription());
|
||||
lbResponse.setPublicIp(loadBalancer.getIpAddress());
|
||||
lbResponse.setPublicPort(loadBalancer.getPublicPort());
|
||||
lbResponse.setPrivatePort(loadBalancer.getPrivatePort());
|
||||
lbResponse.setAlgorithm(loadBalancer.getAlgorithm());
|
||||
|
||||
// TODO: implement
|
||||
Account accountTemp = getManagementServer().findAccountById(loadBalancer.getAccountId());
|
||||
if (accountTemp != null) {
|
||||
lbResponse.setAccountName(accountTemp.getAccountName());
|
||||
lbResponse.setDomainId(accountTemp.getDomainId());
|
||||
lbResponse.setDomainName(getManagementServer().findDomainIdById(accountTemp.getDomainId()).getName());
|
||||
}
|
||||
|
||||
response.add(lbResponse);
|
||||
}
|
||||
|
||||
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 load balancer rules.");
|
||||
}
|
||||
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 if (ipAddressOwner != null) {
|
||||
if ((account != null) && !getManagementServer().isChildDomain(account.getDomainId(), ipAddressOwner.getDomainId())) {
|
||||
throw new ServerApiException(BaseCmd.PARAM_ERROR, "Unable to list load balancer rules for IP address " + publicIp + ", permission denied.");
|
||||
}
|
||||
} 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("ipAddress", Boolean.TRUE, startIndex, Long.valueOf(pageSizeNum));
|
||||
c.addCriteria(Criteria.ACCOUNTID, accountId);
|
||||
if (keyword == null) {
|
||||
c.addCriteria(Criteria.ID, id);
|
||||
c.addCriteria(Criteria.NAME, name);
|
||||
c.addCriteria(Criteria.INSTANCEID, vmId);
|
||||
c.addCriteria(Criteria.IPADDRESS, publicIp);
|
||||
if (isAdmin) {
|
||||
c.addCriteria(Criteria.DOMAINID, domainId);
|
||||
}
|
||||
} else {
|
||||
c.addCriteria(Criteria.KEYWORD, keyword);
|
||||
}
|
||||
|
||||
// FIXME: this should be constrained by domain to search for all load balancers in a domain if an admin is searching
|
||||
List<LoadBalancerVO> loadBalancers = getManagementServer().searchForLoadBalancers(c);
|
||||
|
||||
if (loadBalancers == null) {
|
||||
throw new ServerApiException(BaseCmd.NET_LIST_ERROR, "unable to find load balancing rules");
|
||||
}
|
||||
|
||||
List<Pair<String, Object>> lbTags = new ArrayList<Pair<String, Object>>();
|
||||
Object[] lbTag = new Object[loadBalancers.size()];
|
||||
int i = 0;
|
||||
for (LoadBalancerVO loadBalancer : loadBalancers) {
|
||||
List<Pair<String, Object>> lbData = new ArrayList<Pair<String, Object>>();
|
||||
|
||||
lbData.add(new Pair<String, Object>(BaseCmd.Properties.ID.getName(), Long.valueOf(loadBalancer.getId()).toString()));
|
||||
lbData.add(new Pair<String, Object>(BaseCmd.Properties.NAME.getName(), loadBalancer.getName()));
|
||||
lbData.add(new Pair<String, Object>(BaseCmd.Properties.DESCRIPTION.getName(), loadBalancer.getDescription()));
|
||||
lbData.add(new Pair<String, Object>(BaseCmd.Properties.PUBLIC_IP.getName(), loadBalancer.getIpAddress()));
|
||||
lbData.add(new Pair<String, Object>(BaseCmd.Properties.PUBLIC_PORT.getName(), loadBalancer.getPublicPort()));
|
||||
lbData.add(new Pair<String, Object>(BaseCmd.Properties.PRIVATE_PORT.getName(), loadBalancer.getPrivatePort()));
|
||||
lbData.add(new Pair<String, Object>(BaseCmd.Properties.ALGORITHM.getName(), loadBalancer.getAlgorithm()));
|
||||
|
||||
Account accountTemp = getManagementServer().findAccountById(loadBalancer.getAccountId());
|
||||
if (accountTemp != null) {
|
||||
lbData.add(new Pair<String, Object>(BaseCmd.Properties.ACCOUNT.getName(), accountTemp.getAccountName()));
|
||||
lbData.add(new Pair<String, Object>(BaseCmd.Properties.DOMAIN_ID.getName(), accountTemp.getDomainId()));
|
||||
lbData.add(new Pair<String, Object>(BaseCmd.Properties.DOMAIN.getName(), getManagementServer().findDomainIdById(accountTemp.getDomainId()).getName()));
|
||||
}
|
||||
|
||||
lbTag[i++] = lbData;
|
||||
}
|
||||
Pair<String, Object> ruleTag = new Pair<String, Object>("loadbalancerrule", lbTag);
|
||||
lbTags.add(ruleTag);
|
||||
return lbTags;
|
||||
|
||||
return SerializerHelper.toSerializedString(response);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -45,6 +45,7 @@ import com.cloud.api.commands.ListGuestOsCmd;
|
|||
import com.cloud.api.commands.ListHostsCmd;
|
||||
import com.cloud.api.commands.ListIsosCmd;
|
||||
import com.cloud.api.commands.ListLoadBalancerRuleInstancesCmd;
|
||||
import com.cloud.api.commands.ListLoadBalancerRulesCmd;
|
||||
import com.cloud.api.commands.ListTemplatesCmd;
|
||||
import com.cloud.api.commands.LockAccountCmd;
|
||||
import com.cloud.api.commands.LockUserCmd;
|
||||
|
|
@ -1767,7 +1768,14 @@ public interface ManagementServer {
|
|||
*/
|
||||
List<UserVmVO> listLoadBalancerInstances(ListLoadBalancerRuleInstancesCmd cmd) throws PermissionDeniedException;
|
||||
|
||||
List<LoadBalancerVO> searchForLoadBalancers(Criteria c);
|
||||
/**
|
||||
* List load balancer rules based on the given criteria
|
||||
* @param cmd the command that specifies the criteria to use for listing load balancers. Load balancers can be listed
|
||||
* by id, name, public ip, and vm instance id
|
||||
* @return list of load balancers that match the criteria
|
||||
*/
|
||||
List<LoadBalancerVO> searchForLoadBalancers(ListLoadBalancerRulesCmd cmd) throws InvalidParameterValueException, PermissionDeniedException;
|
||||
|
||||
boolean deleteLoadBalancer(long userId, long loadBalancerId);
|
||||
long deleteLoadBalancerAsync(long userId, long loadBalancerId);
|
||||
|
||||
|
|
|
|||
|
|
@ -83,6 +83,7 @@ import com.cloud.api.commands.ListGuestOsCmd;
|
|||
import com.cloud.api.commands.ListHostsCmd;
|
||||
import com.cloud.api.commands.ListIsosCmd;
|
||||
import com.cloud.api.commands.ListLoadBalancerRuleInstancesCmd;
|
||||
import com.cloud.api.commands.ListLoadBalancerRulesCmd;
|
||||
import com.cloud.api.commands.ListTemplatesCmd;
|
||||
import com.cloud.api.commands.LockAccountCmd;
|
||||
import com.cloud.api.commands.LockUserCmd;
|
||||
|
|
@ -7490,16 +7491,59 @@ public class ManagementServerImpl implements ManagementServer {
|
|||
}
|
||||
|
||||
@Override
|
||||
public List<LoadBalancerVO> searchForLoadBalancers(Criteria c) {
|
||||
Filter searchFilter = new Filter(LoadBalancerVO.class, c.getOrderBy(), c.getAscending(), c.getOffset(), c.getLimit());
|
||||
public List<LoadBalancerVO> searchForLoadBalancers(ListLoadBalancerRulesCmd cmd) throws InvalidParameterValueException, PermissionDeniedException {
|
||||
// do some parameter validation
|
||||
Account account = (Account)UserContext.current().getAccountObject();
|
||||
String accountName = cmd.getAccountName();
|
||||
Long domainId = cmd.getDomainId();
|
||||
Long accountId = null;
|
||||
Account ipAddressOwner = null;
|
||||
String ipAddress = cmd.getPublicIp();
|
||||
|
||||
Object accountId = c.getCriteria(Criteria.ACCOUNTID);
|
||||
Object id = c.getCriteria(Criteria.ID);
|
||||
Object name = c.getCriteria(Criteria.NAME);
|
||||
Object domainId = c.getCriteria(Criteria.DOMAINID);
|
||||
Object keyword = c.getCriteria(Criteria.KEYWORD);
|
||||
Object ipAddress = c.getCriteria(Criteria.IPADDRESS);
|
||||
Object instanceId = c.getCriteria(Criteria.INSTANCEID);
|
||||
if (ipAddress != null) {
|
||||
IPAddressVO ipAddressVO = _publicIpAddressDao.findById(ipAddress);
|
||||
if (ipAddressVO == null) {
|
||||
throw new InvalidParameterValueException("Unable to list load balancers, IP address " + ipAddress + " not found.");
|
||||
} else {
|
||||
Long ipAddrAcctId = ipAddressVO.getAccountId();
|
||||
if (ipAddrAcctId == null) {
|
||||
throw new InvalidParameterValueException("Unable to list load balancers, IP address " + ipAddress + " is not associated with an account.");
|
||||
}
|
||||
ipAddressOwner = _accountDao.findById(ipAddrAcctId);
|
||||
}
|
||||
}
|
||||
|
||||
if ((account == null) || isAdmin(account.getType())) {
|
||||
// validate domainId before proceeding
|
||||
if (domainId != null) {
|
||||
if ((account != null) && !_domainDao.isChildDomain(account.getDomainId(), domainId)) {
|
||||
throw new PermissionDeniedException("Unable to list load balancers for domain id " + domainId + ", permission denied.");
|
||||
}
|
||||
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 if (ipAddressOwner != null) {
|
||||
if ((account != null) && !_domainDao.isChildDomain(account.getDomainId(), ipAddressOwner.getDomainId())) {
|
||||
throw new PermissionDeniedException("Unable to list load balancer rules for IP address " + ipAddress + ", permission denied.");
|
||||
}
|
||||
} else {
|
||||
domainId = ((account == null) ? DomainVO.ROOT_DOMAIN : account.getDomainId());
|
||||
}
|
||||
} else {
|
||||
accountId = account.getId();
|
||||
}
|
||||
|
||||
Filter searchFilter = new Filter(LoadBalancerVO.class, "ipAddress", true, cmd.getStartIndex(), cmd.getPageSizeVal());
|
||||
|
||||
Object id = cmd.getId();
|
||||
Object name = cmd.getLoadBalancerRuleName();
|
||||
Object keyword = cmd.getKeyword();
|
||||
Object instanceId = cmd.getVirtualMachineId();
|
||||
|
||||
SearchBuilder<LoadBalancerVO> sb = _loadBalancerDao.createSearchBuilder();
|
||||
sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
|
||||
|
|
|
|||
Loading…
Reference in New Issue