mirror of https://github.com/apache/cloudstack.git
Finish refactoring listResourceLimits to new API framework.
This commit is contained in:
parent
cee6b37757
commit
4e5aa9cd98
|
|
@ -20,39 +20,23 @@ 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.BaseListCmd;
|
||||
import com.cloud.api.Implementation;
|
||||
import com.cloud.api.Parameter;
|
||||
import com.cloud.api.ServerApiException;
|
||||
import com.cloud.configuration.ResourceCount.ResourceType;
|
||||
import com.cloud.api.response.ResourceLimitResponse;
|
||||
import com.cloud.configuration.ResourceLimitVO;
|
||||
import com.cloud.domain.DomainVO;
|
||||
import com.cloud.server.Criteria;
|
||||
import com.cloud.serializer.SerializerHelper;
|
||||
import com.cloud.user.Account;
|
||||
import com.cloud.utils.Pair;
|
||||
|
||||
@Implementation(method="searchForLimits")
|
||||
@Implementation(method="searchForLimits", manager=Manager.AccountManager)
|
||||
public class ListResourceLimitsCmd extends BaseListCmd {
|
||||
public static final Logger s_logger = Logger.getLogger(ListResourceLimitsCmd.class.getName());
|
||||
|
||||
private static final String s_name = "listresourcelimitsresponse";
|
||||
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, 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.RESOURCE_TYPE, Boolean.FALSE));
|
||||
|
||||
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.ACCOUNT_OBJ, 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 /////////////////////
|
||||
|
|
@ -99,122 +83,33 @@ public class ListResourceLimitsCmd extends BaseListCmd {
|
|||
return s_name;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Override @SuppressWarnings("unchecked")
|
||||
public String getResponse() {
|
||||
}
|
||||
List<ResourceLimitVO> limits = (List<ResourceLimitVO>)getResponseObject();
|
||||
|
||||
@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());
|
||||
Integer type = (Integer) params.get(BaseCmd.Properties.RESOURCE_TYPE.getName());
|
||||
Integer page = (Integer)params.get(BaseCmd.Properties.PAGE.getName());
|
||||
Integer pageSize = (Integer)params.get(BaseCmd.Properties.PAGESIZE.getName());
|
||||
Long accountId = null;
|
||||
|
||||
if (account == null || isAdmin(account.getType())) {
|
||||
if (accountName != null) {
|
||||
// Look up limits for the specified account
|
||||
|
||||
if (domainId == null) {
|
||||
throw new ServerApiException(BaseCmd.PARAM_ERROR, "You must specify domain Id for the account: " + accountName);
|
||||
}
|
||||
|
||||
Account userAccount = getManagementServer().findActiveAccount(accountName, domainId);
|
||||
|
||||
if (userAccount == null) {
|
||||
throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "Unable to find account " + accountName + " in domain " + domainId);
|
||||
} else if (account != null && (account.getType() == Account.ACCOUNT_TYPE_DOMAIN_ADMIN || account.getType() == Account.ACCOUNT_TYPE_READ_ONLY_ADMIN)) {
|
||||
// If this is a non-root admin, make sure that the admin and the user account belong in the same domain or
|
||||
// that the user account's domain is a child domain of the parent
|
||||
if (account.getDomainId() != userAccount.getDomainId() && !getManagementServer().isChildDomain(account.getDomainId(), userAccount.getDomainId())) {
|
||||
throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "You do not have permission to access limits for this account: " + accountName);
|
||||
}
|
||||
}
|
||||
|
||||
accountId = userAccount.getId();
|
||||
domainId = null;
|
||||
} else if (domainId != null) {
|
||||
// Look up limits for the specified domain
|
||||
accountId = null;
|
||||
} else if (account == null) {
|
||||
// Look up limits for the ROOT domain
|
||||
domainId = DomainVO.ROOT_DOMAIN;
|
||||
} else {
|
||||
// Look up limits for the admin's account
|
||||
accountId = account.getId();
|
||||
domainId = null;
|
||||
}
|
||||
} else {
|
||||
// Look up limits for the user's account
|
||||
accountId = account.getId();
|
||||
domainId = null;
|
||||
}
|
||||
|
||||
// Map resource type
|
||||
ResourceType resourceType = null;
|
||||
try {
|
||||
if (type != null) {
|
||||
resourceType = ResourceType.values()[type];
|
||||
}
|
||||
} catch (ArrayIndexOutOfBoundsException e) {
|
||||
throw new ServerApiException(BaseCmd.PARAM_ERROR, "Please specify a valid resource type.");
|
||||
}
|
||||
|
||||
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.FALSE, startIndex, Long.valueOf(pageSizeNum));
|
||||
c.addCriteria(Criteria.ACCOUNTID, accountId);
|
||||
c.addCriteria(Criteria.DOMAINID, domainId);
|
||||
c.addCriteria(Criteria.TYPE, resourceType);
|
||||
|
||||
List<ResourceLimitVO> limits = null;
|
||||
try {
|
||||
limits = getManagementServer().searchForLimits(c);
|
||||
} catch (Exception ex) {
|
||||
s_logger.error("Exception listing limits", ex);
|
||||
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to list resource limits due to exception: " + ex.getMessage());
|
||||
}
|
||||
|
||||
List<Pair<String, Object>> limitTags = new ArrayList<Pair<String, Object>>();
|
||||
Object[] lTag = new Object[limits.size()];
|
||||
int i = 0;
|
||||
List<ResourceLimitResponse> response = new ArrayList<ResourceLimitResponse>();
|
||||
for (ResourceLimitVO limit : limits) {
|
||||
List<Pair<String, Object>> limitData = new ArrayList<Pair<String, Object>>();
|
||||
//limitData.add(new Pair<String, Object>(BaseCmd.Properties.ID.getName(), limit.getId()));
|
||||
if (limit.getDomainId() != null)
|
||||
{
|
||||
limitData.add(new Pair<String, Object>(BaseCmd.Properties.DOMAIN_ID.getName(), limit.getDomainId().toString()));
|
||||
limitData.add(new Pair<String, Object>(BaseCmd.Properties.DOMAIN.getName(), getManagementServer().findDomainIdById(limit.getDomainId()).getName()));
|
||||
ResourceLimitResponse resourceLimitResponse = new ResourceLimitResponse();
|
||||
if (limit.getDomainId() != null) {
|
||||
resourceLimitResponse.setDomainId(limit.getDomainId());
|
||||
resourceLimitResponse.setDomainName(getManagementServer().findDomainIdById(limit.getDomainId()).getName());
|
||||
}
|
||||
|
||||
|
||||
if (limit.getAccountId() != null) {
|
||||
Account accountTemp = getManagementServer().findAccountById(limit.getAccountId());
|
||||
Account accountTemp = getManagementServer().findAccountById(limit.getAccountId());
|
||||
if (accountTemp != null) {
|
||||
limitData.add(new Pair<String, Object>(BaseCmd.Properties.ACCOUNT.getName(), accountTemp.getAccountName()));
|
||||
limitData.add(new Pair<String, Object>(BaseCmd.Properties.DOMAIN_ID.getName(), accountTemp.getDomainId()));
|
||||
limitData.add(new Pair<String, Object>(BaseCmd.Properties.DOMAIN.getName(), getManagementServer().findDomainIdById(accountTemp.getDomainId()).getName()));
|
||||
resourceLimitResponse.setAccountName(accountTemp.getAccountName());
|
||||
resourceLimitResponse.setDomainId(accountTemp.getDomainId());
|
||||
resourceLimitResponse.setDomainName(getManagementServer().findDomainIdById(accountTemp.getDomainId()).getName());
|
||||
}
|
||||
}
|
||||
|
||||
limitData.add(new Pair<String, Object>(BaseCmd.Properties.RESOURCE_TYPE.getName(), limit.getType().ordinal()));
|
||||
limitData.add(new Pair<String, Object>(BaseCmd.Properties.MAX.getName(), limit.getMax()));
|
||||
}
|
||||
|
||||
lTag[i++] = limitData;
|
||||
resourceLimitResponse.setResourceType(limit.getType().ordinal());
|
||||
resourceLimitResponse.setMax(limit.getMax());
|
||||
|
||||
response.add(resourceLimitResponse);
|
||||
}
|
||||
Pair<String, Object> limitTag = new Pair<String, Object>("resourcelimit", lTag);
|
||||
limitTags.add(limitTag);
|
||||
return limitTags;
|
||||
|
||||
return SerializerHelper.toSerializedString(response);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,61 @@
|
|||
package com.cloud.api.response;
|
||||
|
||||
import com.cloud.api.ResponseObject;
|
||||
import com.cloud.serializer.Param;
|
||||
|
||||
public class ResourceLimitResponse implements ResponseObject {
|
||||
@Param(name="account")
|
||||
private String accountName;
|
||||
|
||||
@Param(name="domainid")
|
||||
private Long domainId;
|
||||
|
||||
@Param(name="domain")
|
||||
private String domainName;
|
||||
|
||||
@Param(name="resourcetype")
|
||||
private Integer resourceType;
|
||||
|
||||
@Param(name="max")
|
||||
private Long max;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public Integer getResourceType() {
|
||||
return resourceType;
|
||||
}
|
||||
|
||||
public void setResourceType(Integer resourceType) {
|
||||
this.resourceType = resourceType;
|
||||
}
|
||||
|
||||
public Long getMax() {
|
||||
return max;
|
||||
}
|
||||
|
||||
public void setMax(Long max) {
|
||||
this.max = max;
|
||||
}
|
||||
}
|
||||
|
|
@ -20,12 +20,14 @@ package com.cloud.user;
|
|||
|
||||
import java.util.List;
|
||||
|
||||
import com.cloud.api.commands.ListResourceLimitsCmd;
|
||||
import com.cloud.api.commands.UpdateResourceLimitCmd;
|
||||
import com.cloud.configuration.ResourceCount;
|
||||
import com.cloud.configuration.ResourceCount.ResourceType;
|
||||
import com.cloud.configuration.ResourceLimitVO;
|
||||
import com.cloud.domain.DomainVO;
|
||||
import com.cloud.exception.InvalidParameterValueException;
|
||||
import com.cloud.exception.PermissionDeniedException;
|
||||
import com.cloud.server.Criteria;
|
||||
import com.cloud.utils.component.Manager;
|
||||
|
||||
|
|
@ -93,19 +95,22 @@ public interface AccountManager extends Manager {
|
|||
*/
|
||||
public long getResourceCount(AccountVO account, ResourceType type);
|
||||
|
||||
/**
|
||||
* Updates an existing resource limit with the specified details. If a limit doesn't exist, will create one.
|
||||
* @param domainId
|
||||
* @param accountId
|
||||
* @param type
|
||||
* @param max
|
||||
* @return
|
||||
* @throws InvalidParameterValueException
|
||||
*/
|
||||
// public ResourceLimitVO updateResourceLimit(Long domainId, Long accountId, ResourceType type, Long max) throws InvalidParameterValueException;
|
||||
|
||||
List<ResourceLimitVO> searchForLimits(Criteria c);
|
||||
|
||||
/**
|
||||
* Search for resource limits for the given id and/or account and/or type and/or domain.
|
||||
* @param cmd the command wrapping the id, type, account, and domain
|
||||
* @return a list of limits that match the criteria
|
||||
* @throws InvalidParameterValueException
|
||||
* @throws PermissionDeniedException
|
||||
*/
|
||||
List<ResourceLimitVO> searchForLimits(ListResourceLimitsCmd cmd) throws InvalidParameterValueException, PermissionDeniedException;
|
||||
|
||||
/**
|
||||
* Updates an existing resource limit with the specified details. If a limit doesn't exist, will create one.
|
||||
* @param cmd the command that wraps the domainId, accountId, type, and max parameters
|
||||
* @return the updated/created resource limit
|
||||
* @throws InvalidParameterValueException
|
||||
*/
|
||||
ResourceLimitVO updateResourceLimit(UpdateResourceLimitCmd cmd) throws InvalidParameterValueException;
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -29,6 +29,7 @@ import org.apache.log4j.Logger;
|
|||
|
||||
import com.cloud.api.BaseCmd;
|
||||
import com.cloud.api.ServerApiException;
|
||||
import com.cloud.api.commands.ListResourceLimitsCmd;
|
||||
import com.cloud.api.commands.UpdateResourceLimitCmd;
|
||||
import com.cloud.configuration.ResourceCount.ResourceType;
|
||||
import com.cloud.configuration.ResourceLimitVO;
|
||||
|
|
@ -37,6 +38,7 @@ import com.cloud.configuration.dao.ResourceLimitDao;
|
|||
import com.cloud.domain.DomainVO;
|
||||
import com.cloud.domain.dao.DomainDao;
|
||||
import com.cloud.exception.InvalidParameterValueException;
|
||||
import com.cloud.exception.PermissionDeniedException;
|
||||
import com.cloud.server.Criteria;
|
||||
import com.cloud.storage.dao.VMTemplateDao;
|
||||
import com.cloud.user.dao.AccountDao;
|
||||
|
|
@ -261,6 +263,7 @@ public class AccountManagerImpl implements AccountManager {
|
|||
|
||||
@Override
|
||||
public List<ResourceLimitVO> searchForLimits(Criteria c) {
|
||||
Long id = (Long) c.getCriteria(Criteria.ID);
|
||||
Long domainId = (Long) c.getCriteria(Criteria.DOMAINID);
|
||||
Long accountId = (Long) c.getCriteria(Criteria.ACCOUNTID);
|
||||
ResourceType type = (ResourceType) c.getCriteria(Criteria.TYPE);
|
||||
|
|
@ -289,6 +292,7 @@ public class AccountManagerImpl implements AccountManager {
|
|||
SearchBuilder<ResourceLimitVO> sb = _resourceLimitDao.createSearchBuilder();
|
||||
sb.and("accountId", sb.entity().getAccountId(), SearchCriteria.Op.EQ);
|
||||
sb.and("type", sb.entity().getType(), SearchCriteria.Op.EQ);
|
||||
sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
|
||||
|
||||
SearchCriteria<ResourceLimitVO> sc = sb.create();
|
||||
|
||||
|
|
@ -299,7 +303,11 @@ public class AccountManagerImpl implements AccountManager {
|
|||
if (type != null) {
|
||||
sc.setParameters("type", type);
|
||||
}
|
||||
|
||||
|
||||
if (id != null) {
|
||||
sc.setParameters("id", id);
|
||||
}
|
||||
|
||||
// Listing all limits for an account
|
||||
if (type == null) {
|
||||
//List<ResourceLimitVO> userLimits = _resourceLimitDao.search(sc, searchFilter);
|
||||
|
|
@ -358,6 +366,74 @@ public class AccountManagerImpl implements AccountManager {
|
|||
return limits;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ResourceLimitVO> searchForLimits(ListResourceLimitsCmd cmd) throws InvalidParameterValueException, PermissionDeniedException {
|
||||
String accountName = cmd.getAccountName();
|
||||
Long domainId = cmd.getDomainId();
|
||||
Long accountId = null;
|
||||
Account account = (Account)UserContext.current().getAccountObject();
|
||||
|
||||
if ((account == null) ||
|
||||
(account.getType() == Account.ACCOUNT_TYPE_ADMIN) ||
|
||||
(account.getType() == Account.ACCOUNT_TYPE_DOMAIN_ADMIN) ||
|
||||
(account.getType() == Account.ACCOUNT_TYPE_READ_ONLY_ADMIN)) {
|
||||
|
||||
if (accountName != null) {
|
||||
// Look up limits for the specified account
|
||||
|
||||
if (domainId == null) {
|
||||
throw new InvalidParameterValueException("Failed to list limits for account " + accountName + " no domain id specified.");
|
||||
}
|
||||
|
||||
Account userAccount = _accountDao.findActiveAccount(accountName, domainId);
|
||||
|
||||
if (userAccount == null) {
|
||||
throw new InvalidParameterValueException("Unable to find account " + accountName + " in domain " + domainId);
|
||||
} else if (account != null && (account.getType() == Account.ACCOUNT_TYPE_DOMAIN_ADMIN || account.getType() == Account.ACCOUNT_TYPE_READ_ONLY_ADMIN)) {
|
||||
// If this is a non-root admin, make sure that the admin and the user account belong in the same domain or
|
||||
// that the user account's domain is a child domain of the parent
|
||||
if (account.getDomainId() != userAccount.getDomainId() && !_domainDao.isChildDomain(account.getDomainId(), userAccount.getDomainId())) {
|
||||
throw new PermissionDeniedException("You do not have permission to access limits for this account: " + accountName);
|
||||
}
|
||||
}
|
||||
|
||||
accountId = userAccount.getId();
|
||||
domainId = null;
|
||||
} else if (domainId != null) {
|
||||
// Look up limits for the specified domain
|
||||
accountId = null;
|
||||
} else if (account == null) {
|
||||
// Look up limits for the ROOT domain
|
||||
domainId = DomainVO.ROOT_DOMAIN;
|
||||
} else {
|
||||
// Look up limits for the admin's account
|
||||
accountId = account.getId();
|
||||
domainId = null;
|
||||
}
|
||||
} else {
|
||||
// Look up limits for the user's account
|
||||
accountId = account.getId();
|
||||
domainId = null;
|
||||
}
|
||||
|
||||
// Map resource type
|
||||
ResourceType resourceType = null;
|
||||
Integer type = cmd.getResourceType();
|
||||
try {
|
||||
if (type != null) {
|
||||
resourceType = ResourceType.values()[type];
|
||||
}
|
||||
} catch (ArrayIndexOutOfBoundsException e) {
|
||||
throw new InvalidParameterValueException("Invalid resource type " + type + " given. Please specify a valid resource type.");
|
||||
}
|
||||
|
||||
Criteria c = new Criteria("id", Boolean.FALSE, cmd.getStartIndex(), cmd.getPageSizeVal());
|
||||
c.addCriteria(Criteria.ID, cmd.getId());
|
||||
c.addCriteria(Criteria.DOMAINID, domainId);
|
||||
c.addCriteria(Criteria.ACCOUNTID, accountId);
|
||||
c.addCriteria(Criteria.TYPE, resourceType);
|
||||
return searchForLimits(c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResourceLimitVO updateResourceLimit(UpdateResourceLimitCmd cmd) throws InvalidParameterValueException {
|
||||
|
|
|
|||
Loading…
Reference in New Issue