Refactoring the updateResourceLimits code, moving all logic to agent manager, updating the enum with a new manager type for reflection

This commit is contained in:
abhishek 2010-08-18 10:32:21 -07:00
parent 154c6985a4
commit e553bcde96
6 changed files with 347 additions and 261 deletions

View File

@ -48,7 +48,7 @@ public abstract class BaseCmd {
}
public enum Manager {
ConfigManager, ManagementServer, NetworkManager, StorageManager, UserVmManager
ConfigManager, ManagementServer, NetworkManager, StorageManager, UserVmManager, AccountManager
}
// FIXME: Extract these out into a separate file

View File

@ -25,8 +25,10 @@ import java.util.Map;
import org.apache.log4j.Logger;
import com.cloud.api.BaseCmd;
import com.cloud.api.Implementation;
import com.cloud.api.Parameter;
import com.cloud.api.ServerApiException;
import com.cloud.api.BaseCmd.Manager;
import com.cloud.configuration.ResourceCount.ResourceType;
import com.cloud.configuration.ResourceLimitVO;
import com.cloud.domain.DomainVO;
@ -35,20 +37,12 @@ import com.cloud.server.Criteria;
import com.cloud.user.Account;
import com.cloud.utils.Pair;
@Implementation(method="updateResourceLimit", manager=Manager.AccountManager)
public class UpdateResourceLimitCmd extends BaseCmd {
public static final Logger s_logger = Logger.getLogger(UpdateResourceLimitCmd.class.getName());
private static final String s_name = "updateresourcelimitresponse";
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.MAX, Boolean.FALSE));
s_properties.add(new Pair<Enum, Boolean>(BaseCmd.Properties.RESOURCE_TYPE, Boolean.TRUE));
}
/////////////////////////////////////////////////////
//////////////// API parameters /////////////////////
@ -94,117 +88,119 @@ public class UpdateResourceLimitCmd extends BaseCmd {
public String getName() {
return s_name;
}
@Override
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());
Long domainId = (Long) params.get(BaseCmd.Properties.DOMAIN_ID.getName());
String accountName = (String) params.get(BaseCmd.Properties.ACCOUNT.getName());
Integer type = (Integer) params.get(BaseCmd.Properties.RESOURCE_TYPE.getName());
Long max = (Long) params.get(BaseCmd.Properties.MAX.getName());
Long accountId = null;
@Override
public String getResponse() {
// TODO look at the execute method to construct response
return null;
}
if (max == null) {
max = new Long(-1);
} else if (max < -1) {
throw new ServerApiException(BaseCmd.PARAM_ERROR, "Please specify either '-1' for an infinite limit, or a limit that is at least '0'.");
}
// Map resource type
ResourceType resourceType;
try {
resourceType = ResourceType.values()[type];
} catch (ArrayIndexOutOfBoundsException e) {
throw new ServerApiException(BaseCmd.PARAM_ERROR, "Please specify a valid resource type.");
}
/*
if (accountName==null && domainId != null && !domainId.equals(DomainVO.ROOT_DOMAIN)) {
throw new ServerApiException(BaseCmd.PARAM_ERROR, "Resource limits must be made for an account or the ROOT domain.");
}
*/
if (account != null) {
if (domainId != null) {
if (!getManagementServer().isChildDomain(account.getDomainId(), domainId)) {
throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "Unable to update resource limit for " + ((accountName == null) ? "" : "account " + accountName + " in ") + "domain " + domainId + ", permission denied");
}
} else if (account.getType() == Account.ACCOUNT_TYPE_ADMIN) {
domainId = DomainVO.ROOT_DOMAIN; // for root admin, default to root domain if domain is not specified
}
if (account.getType() == Account.ACCOUNT_TYPE_DOMAIN_ADMIN) {
if ((domainId != null) && (accountName == null) && domainId.equals(account.getDomainId())) {
// if the admin is trying to update their own domain, disallow...
throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "Unable to update resource limit for " + ((accountName == null) ? "" : "account " + accountName + " in ") + "domain " + domainId + ", permission denied");
}
// If there is an existing ROOT domain limit, make sure its max isn't being exceeded
Criteria c = new Criteria();
c.addCriteria(Criteria.DOMAINID, DomainVO.ROOT_DOMAIN);
c.addCriteria(Criteria.TYPE, resourceType);
List<ResourceLimitVO> currentRootDomainLimits = getManagementServer().searchForLimits(c);
ResourceLimitVO currentRootDomainLimit = (currentRootDomainLimits.size() == 0) ? null : currentRootDomainLimits.get(0);
if (currentRootDomainLimit != null) {
long currentRootDomainMax = currentRootDomainLimits.get(0).getMax();
if ((max == -1 && currentRootDomainMax != -1) || max > currentRootDomainMax) {
throw new ServerApiException(BaseCmd.PARAM_ERROR, "The current ROOT domain limit for resource type " + resourceType + " is " + currentRootDomainMax + " and cannot be exceeded.");
}
}
}
} else if (domainId == null) {
domainId = DomainVO.ROOT_DOMAIN; // for system commands, default to root domain if domain is not specified
}
if (domainId == null) {
throw new ServerApiException(BaseCmd.PARAM_ERROR, "Unable to update resource limit, unable to determine domain in which to update limit.");
} else if (accountName != null) {
Account userAccount = getManagementServer().findActiveAccount(accountName, domainId);
if (userAccount == null) {
throw new ServerApiException(BaseCmd.PARAM_ERROR, "unable to find account by name " + accountName + " in domain with id " + domainId);
}
accountId = userAccount.getId();
domainId = userAccount.getDomainId();
}
ResourceLimitVO limit = null;
try {
if (accountId != null) domainId = null;
limit = getManagementServer().updateResourceLimit(domainId, accountId, resourceType, max);
} catch (InvalidParameterValueException paramException) {
throw new ServerApiException(BaseCmd.PARAM_ERROR, paramException.getMessage());
} catch (Exception ex) {
s_logger.error("Exception updating resource limit", ex);
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to update limit due to exception: " + ex.getMessage());
}
List<Pair<String, Object>> embeddedObject = new ArrayList<Pair<String, Object>>();
List<Pair<String, Object>> returnValues = new ArrayList<Pair<String, Object>>();
if (limit == null)
throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to update resource limit. Please contact Cloud Support.");
else {
if (limit.getDomainId() != null) {
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.DOMAIN_ID.getName(), limit.getDomainId()));
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.DOMAIN.getName(), getManagementServer().findDomainIdById(limit.getDomainId()).getName()));
}
if (limit.getAccountId() != null) {
Account accountTemp = getManagementServer().findAccountById(limit.getAccountId());
if (accountTemp != null) {
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.ACCOUNT.getName(), accountTemp.getAccountName()));
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.DOMAIN_ID.getName(), accountTemp.getDomainId()));
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.DOMAIN.getName(), getManagementServer().findDomainIdById(accountTemp.getDomainId()).getName()));
}
}
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.RESOURCE_TYPE.getName(), limit.getType().ordinal()));
returnValues.add(new Pair<String, Object>(BaseCmd.Properties.MAX.getName(), limit.getMax()));
embeddedObject.add(new Pair<String, Object>("resourcelimit", new Object[] { returnValues } ));
}
return embeddedObject;
}
// @Override
// public List<Pair<String, Object>> execute(Map<String, Object> params) {
// Account account = (Account)params.get(BaseCmd.Properties.ACCOUNT_OBJ.getName());
// Long domainId = (Long) params.get(BaseCmd.Properties.DOMAIN_ID.getName());
// String accountName = (String) params.get(BaseCmd.Properties.ACCOUNT.getName());
// Integer type = (Integer) params.get(BaseCmd.Properties.RESOURCE_TYPE.getName());
// Long max = (Long) params.get(BaseCmd.Properties.MAX.getName());
// Long accountId = null;
//
// if (max == null) {
// max = new Long(-1);
// } else if (max < -1) {
// throw new ServerApiException(BaseCmd.PARAM_ERROR, "Please specify either '-1' for an infinite limit, or a limit that is at least '0'.");
// }
//
// // Map resource type
// ResourceType resourceType;
// try {
// resourceType = ResourceType.values()[type];
// } catch (ArrayIndexOutOfBoundsException e) {
// throw new ServerApiException(BaseCmd.PARAM_ERROR, "Please specify a valid resource type.");
// }
//
// /*
// if (accountName==null && domainId != null && !domainId.equals(DomainVO.ROOT_DOMAIN)) {
// throw new ServerApiException(BaseCmd.PARAM_ERROR, "Resource limits must be made for an account or the ROOT domain.");
// }
// */
//
// if (account != null) {
// if (domainId != null) {
// if (!getManagementServer().isChildDomain(account.getDomainId(), domainId)) {
// throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "Unable to update resource limit for " + ((accountName == null) ? "" : "account " + accountName + " in ") + "domain " + domainId + ", permission denied");
// }
// } else if (account.getType() == Account.ACCOUNT_TYPE_ADMIN) {
// domainId = DomainVO.ROOT_DOMAIN; // for root admin, default to root domain if domain is not specified
// }
//
// if (account.getType() == Account.ACCOUNT_TYPE_DOMAIN_ADMIN) {
// if ((domainId != null) && (accountName == null) && domainId.equals(account.getDomainId())) {
// // if the admin is trying to update their own domain, disallow...
// throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "Unable to update resource limit for " + ((accountName == null) ? "" : "account " + accountName + " in ") + "domain " + domainId + ", permission denied");
// }
//
// // If there is an existing ROOT domain limit, make sure its max isn't being exceeded
// Criteria c = new Criteria();
// c.addCriteria(Criteria.DOMAINID, DomainVO.ROOT_DOMAIN);
// c.addCriteria(Criteria.TYPE, resourceType);
// List<ResourceLimitVO> currentRootDomainLimits = getManagementServer().searchForLimits(c);
// ResourceLimitVO currentRootDomainLimit = (currentRootDomainLimits.size() == 0) ? null : currentRootDomainLimits.get(0);
// if (currentRootDomainLimit != null) {
// long currentRootDomainMax = currentRootDomainLimits.get(0).getMax();
// if ((max == -1 && currentRootDomainMax != -1) || max > currentRootDomainMax) {
// throw new ServerApiException(BaseCmd.PARAM_ERROR, "The current ROOT domain limit for resource type " + resourceType + " is " + currentRootDomainMax + " and cannot be exceeded.");
// }
// }
// }
// } else if (domainId == null) {
// domainId = DomainVO.ROOT_DOMAIN; // for system commands, default to root domain if domain is not specified
// }
//
// if (domainId == null) {
// throw new ServerApiException(BaseCmd.PARAM_ERROR, "Unable to update resource limit, unable to determine domain in which to update limit.");
// } else if (accountName != null) {
// Account userAccount = getManagementServer().findActiveAccount(accountName, domainId);
// if (userAccount == null) {
// throw new ServerApiException(BaseCmd.PARAM_ERROR, "unable to find account by name " + accountName + " in domain with id " + domainId);
// }
// accountId = userAccount.getId();
// domainId = userAccount.getDomainId();
// }
//
// ResourceLimitVO limit = null;
// try {
// if (accountId != null) domainId = null;
// limit = getManagementServer().updateResourceLimit(domainId, accountId, resourceType, max);
// } catch (InvalidParameterValueException paramException) {
// throw new ServerApiException(BaseCmd.PARAM_ERROR, paramException.getMessage());
// } catch (Exception ex) {
// s_logger.error("Exception updating resource limit", ex);
// throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to update limit due to exception: " + ex.getMessage());
// }
// List<Pair<String, Object>> embeddedObject = new ArrayList<Pair<String, Object>>();
// List<Pair<String, Object>> returnValues = new ArrayList<Pair<String, Object>>();
//
// if (limit == null)
// throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to update resource limit. Please contact Cloud Support.");
// else {
//
// if (limit.getDomainId() != null) {
// returnValues.add(new Pair<String, Object>(BaseCmd.Properties.DOMAIN_ID.getName(), limit.getDomainId()));
// returnValues.add(new Pair<String, Object>(BaseCmd.Properties.DOMAIN.getName(), getManagementServer().findDomainIdById(limit.getDomainId()).getName()));
// }
//
// if (limit.getAccountId() != null) {
// Account accountTemp = getManagementServer().findAccountById(limit.getAccountId());
// if (accountTemp != null) {
// returnValues.add(new Pair<String, Object>(BaseCmd.Properties.ACCOUNT.getName(), accountTemp.getAccountName()));
// returnValues.add(new Pair<String, Object>(BaseCmd.Properties.DOMAIN_ID.getName(), accountTemp.getDomainId()));
// returnValues.add(new Pair<String, Object>(BaseCmd.Properties.DOMAIN.getName(), getManagementServer().findDomainIdById(accountTemp.getDomainId()).getName()));
// }
// }
// returnValues.add(new Pair<String, Object>(BaseCmd.Properties.RESOURCE_TYPE.getName(), limit.getType().ordinal()));
// returnValues.add(new Pair<String, Object>(BaseCmd.Properties.MAX.getName(), limit.getMax()));
// embeddedObject.add(new Pair<String, Object>("resourcelimit", new Object[] { returnValues } ));
// }
// return embeddedObject;
// }
}

View File

@ -1635,7 +1635,7 @@ public interface ManagementServer {
* @return
* @throws InvalidParameterValueException
*/
ResourceLimitVO updateResourceLimit(Long domainId, Long accountId, ResourceType type, Long max) throws InvalidParameterValueException;
// ResourceLimitVO updateResourceLimit(Long domainId, Long accountId, ResourceType type, Long max) throws InvalidParameterValueException;
/**
* Deletes a Limit
@ -1658,7 +1658,7 @@ public interface ManagementServer {
* @param type
* @return a list of Limits
*/
List<ResourceLimitVO> searchForLimits(Criteria c);
// List<ResourceLimitVO> searchForLimits(Criteria c);
/**
* Finds the correct limit for an account. I.e. if an account's limit is not present, it will check the account's domain, and as a last resort use the global limit.

View File

@ -38,7 +38,6 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TimeZone;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
@ -89,7 +88,6 @@ import com.cloud.api.commands.UpdateTemplateCmd;
import com.cloud.api.commands.UpdateTemplateOrIsoPermissionsCmd;
import com.cloud.api.commands.UpdateTemplatePermissionsCmd;
import com.cloud.api.commands.UpdateUserCmd;
import com.cloud.api.commands.UpgradeVMCmd;
import com.cloud.async.AsyncInstanceCreateStatus;
import com.cloud.async.AsyncJobExecutor;
import com.cloud.async.AsyncJobManager;
@ -112,17 +110,16 @@ import com.cloud.async.executor.NetworkGroupIngressParam;
import com.cloud.async.executor.ResetVMPasswordParam;
import com.cloud.async.executor.SecurityGroupParam;
import com.cloud.async.executor.UpdateLoadBalancerParam;
import com.cloud.async.executor.UpgradeVMParam;
import com.cloud.async.executor.VMOperationParam;
import com.cloud.async.executor.VMOperationParam.VmOp;
import com.cloud.async.executor.VolumeOperationParam;
import com.cloud.async.executor.VMOperationParam.VmOp;
import com.cloud.async.executor.VolumeOperationParam.VolumeOp;
import com.cloud.capacity.CapacityVO;
import com.cloud.capacity.dao.CapacityDao;
import com.cloud.configuration.ConfigurationManager;
import com.cloud.configuration.ConfigurationVO;
import com.cloud.configuration.ResourceCount.ResourceType;
import com.cloud.configuration.ResourceLimitVO;
import com.cloud.configuration.ResourceCount.ResourceType;
import com.cloud.configuration.dao.ConfigurationDao;
import com.cloud.configuration.dao.ResourceLimitDao;
import com.cloud.consoleproxy.ConsoleProxyManager;
@ -132,8 +129,8 @@ import com.cloud.dc.DataCenterIpAddressVO;
import com.cloud.dc.DataCenterVO;
import com.cloud.dc.HostPodVO;
import com.cloud.dc.PodVlanMapVO;
import com.cloud.dc.Vlan.VlanType;
import com.cloud.dc.VlanVO;
import com.cloud.dc.Vlan.VlanType;
import com.cloud.dc.dao.AccountVlanMapDao;
import com.cloud.dc.dao.ClusterDao;
import com.cloud.dc.dao.DataCenterDao;
@ -199,22 +196,22 @@ import com.cloud.storage.GuestOSCategoryVO;
import com.cloud.storage.GuestOSVO;
import com.cloud.storage.LaunchPermissionVO;
import com.cloud.storage.Snapshot;
import com.cloud.storage.Snapshot.SnapshotType;
import com.cloud.storage.SnapshotPolicyVO;
import com.cloud.storage.SnapshotScheduleVO;
import com.cloud.storage.SnapshotVO;
import com.cloud.storage.Storage;
import com.cloud.storage.Storage.FileSystem;
import com.cloud.storage.Storage.ImageFormat;
import com.cloud.storage.StorageManager;
import com.cloud.storage.StoragePoolVO;
import com.cloud.storage.StorageStats;
import com.cloud.storage.VMTemplateHostVO;
import com.cloud.storage.VMTemplateStorageResourceAssoc;
import com.cloud.storage.VMTemplateVO;
import com.cloud.storage.Volume.VolumeType;
import com.cloud.storage.VolumeStats;
import com.cloud.storage.VolumeVO;
import com.cloud.storage.Snapshot.SnapshotType;
import com.cloud.storage.Storage.FileSystem;
import com.cloud.storage.Storage.ImageFormat;
import com.cloud.storage.Volume.VolumeType;
import com.cloud.storage.dao.DiskOfferingDao;
import com.cloud.storage.dao.DiskTemplateDao;
import com.cloud.storage.dao.GuestOSCategoryDao;
@ -224,9 +221,9 @@ import com.cloud.storage.dao.SnapshotDao;
import com.cloud.storage.dao.SnapshotPolicyDao;
import com.cloud.storage.dao.StoragePoolDao;
import com.cloud.storage.dao.VMTemplateDao;
import com.cloud.storage.dao.VMTemplateDao.TemplateFilter;
import com.cloud.storage.dao.VMTemplateHostDao;
import com.cloud.storage.dao.VolumeDao;
import com.cloud.storage.dao.VMTemplateDao.TemplateFilter;
import com.cloud.storage.preallocatedlun.PreallocatedLunVO;
import com.cloud.storage.preallocatedlun.dao.PreallocatedLunDao;
import com.cloud.storage.secondary.SecondaryStorageVmManager;
@ -248,12 +245,12 @@ import com.cloud.user.dao.UserDao;
import com.cloud.user.dao.UserStatisticsDao;
import com.cloud.uservm.UserVm;
import com.cloud.utils.DateUtil;
import com.cloud.utils.DateUtil.IntervalType;
import com.cloud.utils.EnumUtils;
import com.cloud.utils.NumbersUtil;
import com.cloud.utils.Pair;
import com.cloud.utils.PasswordGenerator;
import com.cloud.utils.StringUtils;
import com.cloud.utils.DateUtil.IntervalType;
import com.cloud.utils.component.Adapters;
import com.cloud.utils.component.ComponentLocator;
import com.cloud.utils.concurrency.NamedThreadFactory;
@ -283,7 +280,6 @@ import com.cloud.vm.dao.SecondaryStorageVmDao;
import com.cloud.vm.dao.UserVmDao;
import com.cloud.vm.dao.VMInstanceDao;
import com.google.gson.Gson;
import com.sun.org.apache.bcel.internal.generic.INSTANCEOF;
public class ManagementServerImpl implements ManagementServer {
public static final Logger s_logger = Logger.getLogger(ManagementServerImpl.class.getName());
@ -4474,10 +4470,10 @@ public class ManagementServerImpl implements ManagementServer {
return null;
}
@Override
public ResourceLimitVO updateResourceLimit(Long domainId, Long accountId, ResourceType type, Long max) throws InvalidParameterValueException {
return _accountMgr.updateResourceLimit(domainId, accountId, type, max);
}
// @Override
// public ResourceLimitVO updateResourceLimit(Long domainId, Long accountId, ResourceType type, Long max) throws InvalidParameterValueException {
// return _accountMgr.updateResourceLimit(domainId, accountId, type, max);
// }
@Override
public boolean deleteLimit(Long limitId) {
@ -4493,117 +4489,6 @@ public class ManagementServerImpl implements ManagementServer {
return _resourceLimitDao.findById(limitId);
}
@Override
public List<ResourceLimitVO> searchForLimits(Criteria c) {
Long domainId = (Long) c.getCriteria(Criteria.DOMAINID);
Long accountId = (Long) c.getCriteria(Criteria.ACCOUNTID);
ResourceType type = (ResourceType) c.getCriteria(Criteria.TYPE);
// For 2.0, we are just limiting the scope to having an user retrieve
// limits for himself and if limits don't exist, use the ROOT domain's limits.
// - Will
List<ResourceLimitVO> limits = new ArrayList<ResourceLimitVO>();
if(accountId!=null && domainId!=null)
{
//if domainId==1 and account belongs to admin
//return all records for resource limits (bug 3778)
if(domainId==1)
{
AccountVO account = _accountDao.findById(accountId);
if(account!=null && account.getType()==1)
{
//account belongs to admin
//return all limits
limits = _resourceLimitDao.listAll();
return limits;
}
}
//if account belongs to system, accountid=1,domainid=1
//return all the records for resource limits (bug:3778)
if(accountId==1 && domainId==1)
{
limits = _resourceLimitDao.listAll();
return limits;
}
}
if (accountId != null) {
SearchBuilder<ResourceLimitVO> sb = _resourceLimitDao.createSearchBuilder();
sb.and("accountId", sb.entity().getAccountId(), SearchCriteria.Op.EQ);
sb.and("type", sb.entity().getType(), SearchCriteria.Op.EQ);
SearchCriteria<ResourceLimitVO> sc = sb.create();
if (accountId != null) {
sc.setParameters("accountId", accountId);
}
if (type != null) {
sc.setParameters("type", type);
}
// Listing all limits for an account
if (type == null) {
//List<ResourceLimitVO> userLimits = _resourceLimitDao.search(sc, searchFilter);
List<ResourceLimitVO> userLimits = _resourceLimitDao.listByAccountId(accountId);
List<ResourceLimitVO> rootLimits = _resourceLimitDao.listByDomainId(DomainVO.ROOT_DOMAIN);
ResourceType resourceTypes[] = ResourceType.values();
for (ResourceType resourceType: resourceTypes) {
boolean found = false;
for (ResourceLimitVO userLimit : userLimits) {
if (userLimit.getType() == resourceType) {
limits.add(userLimit);
found = true;
break;
}
}
if (!found) {
// Check the ROOT domain
for (ResourceLimitVO rootLimit : rootLimits) {
if (rootLimit.getType() == resourceType) {
limits.add(rootLimit);
found = true;
break;
}
}
}
if (!found) {
limits.add(new ResourceLimitVO(domainId, accountId, resourceType, -1L));
}
}
} else {
AccountVO account = _accountDao.findById(accountId);
limits.add(new ResourceLimitVO(null, accountId, type, _accountMgr.findCorrectResourceLimit(account, type)));
}
} else if (domainId != null) {
if (type == null) {
ResourceType resourceTypes[] = ResourceType.values();
List<ResourceLimitVO> domainLimits = _resourceLimitDao.listByDomainId(domainId);
for (ResourceType resourceType: resourceTypes) {
boolean found = false;
for (ResourceLimitVO domainLimit : domainLimits) {
if (domainLimit.getType() == resourceType) {
limits.add(domainLimit);
found = true;
break;
}
}
if (!found) {
limits.add(new ResourceLimitVO(domainId, null, resourceType, -1L));
}
}
} else {
limits.add(_resourceLimitDao.findByDomainIdAndType(domainId, type));
}
}
return limits;
}
@Override
public long findCorrectResourceLimit(ResourceType type, long accountId) {

View File

@ -18,11 +18,15 @@
package com.cloud.user;
import java.util.List;
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.server.Criteria;
import com.cloud.utils.component.Manager;
/**
@ -98,6 +102,10 @@ public interface AccountManager extends Manager {
* @return
* @throws InvalidParameterValueException
*/
public ResourceLimitVO updateResourceLimit(Long domainId, Long accountId, ResourceType type, Long max) throws InvalidParameterValueException;
// public ResourceLimitVO updateResourceLimit(Long domainId, Long accountId, ResourceType type, Long max) throws InvalidParameterValueException;
List<ResourceLimitVO> searchForLimits(Criteria c);
ResourceLimitVO updateResourceLimit(UpdateResourceLimitCmd cmd) throws InvalidParameterValueException;
}

View File

@ -18,6 +18,7 @@
package com.cloud.user;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@ -26,6 +27,9 @@ import javax.naming.ConfigurationException;
import org.apache.log4j.Logger;
import com.cloud.api.BaseCmd;
import com.cloud.api.ServerApiException;
import com.cloud.api.commands.UpdateResourceLimitCmd;
import com.cloud.configuration.ResourceLimitVO;
import com.cloud.configuration.ResourceCount.ResourceType;
import com.cloud.configuration.dao.ResourceCountDao;
@ -33,12 +37,15 @@ 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.server.Criteria;
import com.cloud.storage.dao.VMTemplateDao;
import com.cloud.user.dao.AccountDao;
import com.cloud.user.dao.UserDao;
import com.cloud.utils.Pair;
import com.cloud.utils.component.ComponentLocator;
import com.cloud.utils.db.Filter;
import com.cloud.utils.db.GlobalLock;
import com.cloud.utils.db.SearchBuilder;
import com.cloud.utils.db.SearchCriteria;
@Local(value={AccountManager.class})
@ -252,9 +259,200 @@ public class AccountManagerImpl implements AccountManager {
public long getResourceCount(AccountVO account, ResourceType type) {
return _resourceCountDao.getAccountCount(account.getId(), type);
}
@Override
public List<ResourceLimitVO> searchForLimits(Criteria c) {
Long domainId = (Long) c.getCriteria(Criteria.DOMAINID);
Long accountId = (Long) c.getCriteria(Criteria.ACCOUNTID);
ResourceType type = (ResourceType) c.getCriteria(Criteria.TYPE);
// For 2.0, we are just limiting the scope to having an user retrieve
// limits for himself and if limits don't exist, use the ROOT domain's limits.
// - Will
List<ResourceLimitVO> limits = new ArrayList<ResourceLimitVO>();
if(accountId!=null && domainId!=null)
{
//if domainId==1 and account belongs to admin
//return all records for resource limits (bug 3778)
if(domainId==1)
{
AccountVO account = _accountDao.findById(accountId);
if(account!=null && account.getType()==1)
{
//account belongs to admin
//return all limits
limits = _resourceLimitDao.listAll();
return limits;
}
}
//if account belongs to system, accountid=1,domainid=1
//return all the records for resource limits (bug:3778)
if(accountId==1 && domainId==1)
{
limits = _resourceLimitDao.listAll();
return limits;
}
}
if (accountId != null) {
SearchBuilder<ResourceLimitVO> sb = _resourceLimitDao.createSearchBuilder();
sb.and("accountId", sb.entity().getAccountId(), SearchCriteria.Op.EQ);
sb.and("type", sb.entity().getType(), SearchCriteria.Op.EQ);
SearchCriteria<ResourceLimitVO> sc = sb.create();
if (accountId != null) {
sc.setParameters("accountId", accountId);
}
if (type != null) {
sc.setParameters("type", type);
}
// Listing all limits for an account
if (type == null) {
//List<ResourceLimitVO> userLimits = _resourceLimitDao.search(sc, searchFilter);
List<ResourceLimitVO> userLimits = _resourceLimitDao.listByAccountId(accountId);
List<ResourceLimitVO> rootLimits = _resourceLimitDao.listByDomainId(DomainVO.ROOT_DOMAIN);
ResourceType resourceTypes[] = ResourceType.values();
for (ResourceType resourceType: resourceTypes) {
boolean found = false;
for (ResourceLimitVO userLimit : userLimits) {
if (userLimit.getType() == resourceType) {
limits.add(userLimit);
found = true;
break;
}
}
if (!found) {
// Check the ROOT domain
for (ResourceLimitVO rootLimit : rootLimits) {
if (rootLimit.getType() == resourceType) {
limits.add(rootLimit);
found = true;
break;
}
}
}
if (!found) {
limits.add(new ResourceLimitVO(domainId, accountId, resourceType, -1L));
}
}
} else {
AccountVO account = _accountDao.findById(accountId);
limits.add(new ResourceLimitVO(null, accountId, type, findCorrectResourceLimit(account, type)));
}
} else if (domainId != null) {
if (type == null) {
ResourceType resourceTypes[] = ResourceType.values();
List<ResourceLimitVO> domainLimits = _resourceLimitDao.listByDomainId(domainId);
for (ResourceType resourceType: resourceTypes) {
boolean found = false;
for (ResourceLimitVO domainLimit : domainLimits) {
if (domainLimit.getType() == resourceType) {
limits.add(domainLimit);
found = true;
break;
}
}
if (!found) {
limits.add(new ResourceLimitVO(domainId, null, resourceType, -1L));
}
}
} else {
limits.add(_resourceLimitDao.findByDomainIdAndType(domainId, type));
}
}
return limits;
}
@Override
public ResourceLimitVO updateResourceLimit(Long domainId, Long accountId, ResourceType type, Long max) throws InvalidParameterValueException {
public ResourceLimitVO updateResourceLimit(UpdateResourceLimitCmd cmd) throws InvalidParameterValueException {
Account account = (Account)UserContext.current().getAccountObject();
Long userId = UserContext.current().getUserId();
Long domainId = cmd.getDomainId();
Long max = cmd.getMax();
Integer type = cmd.getResourceType();
//Validate input
Long accountId = null;
if (max == null) {
max = new Long(-1);
} else if (max < -1) {
throw new ServerApiException(BaseCmd.PARAM_ERROR, "Please specify either '-1' for an infinite limit, or a limit that is at least '0'.");
}
// Map resource type
ResourceType resourceType;
try {
resourceType = ResourceType.values()[type];
} catch (ArrayIndexOutOfBoundsException e) {
throw new ServerApiException(BaseCmd.PARAM_ERROR, "Please specify a valid resource type.");
}
/*
if (accountName==null && domainId != null && !domainId.equals(DomainVO.ROOT_DOMAIN)) {
throw new ServerApiException(BaseCmd.PARAM_ERROR, "Resource limits must be made for an account or the ROOT domain.");
}
*/
if (account != null) {
if (domainId != null) {
if (!_domainDao.isChildDomain(account.getDomainId(), domainId)) {
throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "Unable to update resource limit for " + ((account.getAccountName() == null) ? "" : "account " + account.getAccountName() + " in ") + "domain " + domainId + ", permission denied");
}
} else if (account.getType() == Account.ACCOUNT_TYPE_ADMIN) {
domainId = DomainVO.ROOT_DOMAIN; // for root admin, default to root domain if domain is not specified
}
if (account.getType() == Account.ACCOUNT_TYPE_DOMAIN_ADMIN) {
if ((domainId != null) && (account.getAccountName() == null) && domainId.equals(account.getDomainId())) {
// if the admin is trying to update their own domain, disallow...
throw new ServerApiException(BaseCmd.ACCOUNT_ERROR, "Unable to update resource limit for " + ((account.getAccountName() == null) ? "" : "account " + account.getAccountName() + " in ") + "domain " + domainId + ", permission denied");
}
// If there is an existing ROOT domain limit, make sure its max isn't being exceeded
Criteria c = new Criteria();
c.addCriteria(Criteria.DOMAINID, DomainVO.ROOT_DOMAIN);
c.addCriteria(Criteria.TYPE, resourceType);
List<ResourceLimitVO> currentRootDomainLimits = searchForLimits(c);
ResourceLimitVO currentRootDomainLimit = (currentRootDomainLimits.size() == 0) ? null : currentRootDomainLimits.get(0);
if (currentRootDomainLimit != null) {
long currentRootDomainMax = currentRootDomainLimits.get(0).getMax();
if ((max == -1 && currentRootDomainMax != -1) || max > currentRootDomainMax) {
throw new ServerApiException(BaseCmd.PARAM_ERROR, "The current ROOT domain limit for resource type " + resourceType + " is " + currentRootDomainMax + " and cannot be exceeded.");
}
}
}
} else if (domainId == null) {
domainId = DomainVO.ROOT_DOMAIN; // for system commands, default to root domain if domain is not specified
}
if (domainId == null) {
throw new ServerApiException(BaseCmd.PARAM_ERROR, "Unable to update resource limit, unable to determine domain in which to update limit.");
} else if (account.getAccountName() != null) {
if (domainId == null) {
domainId = DomainVO.ROOT_DOMAIN;
}
Account userAccount = _accountDao.findActiveAccount(account.getAccountName(), domainId);
if (userAccount == null) {
throw new ServerApiException(BaseCmd.PARAM_ERROR, "unable to find account by name " + account.getAccountName() + " in domain with id " + domainId);
}
accountId = userAccount.getId();
domainId = userAccount.getDomainId();
}
if (accountId != null) domainId = null;
// Either a domainId or an accountId must be passed in, but not both.
if ((domainId == null) && (accountId == null)) {
throw new InvalidParameterValueException("Either a domainId or domainId/accountId must be passed in.");
@ -262,17 +460,17 @@ public class AccountManagerImpl implements AccountManager {
// Check if the domain or account exists and is valid
if (accountId != null) {
AccountVO account = _accountDao.findById(accountId);
if (account == null) {
AccountVO accountHandle = _accountDao.findById(accountId);
if (accountHandle == null) {
throw new InvalidParameterValueException("Please specify a valid account ID.");
} else if (account.getRemoved() != null) {
} else if (accountHandle.getRemoved() != null) {
throw new InvalidParameterValueException("Please specify an active account.");
} else if (account.getType() == Account.ACCOUNT_TYPE_ADMIN || account.getType() == Account.ACCOUNT_ID_SYSTEM) {
} else if (account.getType() == Account.ACCOUNT_TYPE_ADMIN || accountHandle.getType() == Account.ACCOUNT_ID_SYSTEM) {
throw new InvalidParameterValueException("Please specify a non-admin account.");
}
DomainVO domain = _domainDao.findById(account.getDomainId());
long parentMaximum = findCorrectResourceLimit(domain, type);
long parentMaximum = findCorrectResourceLimit(domain, resourceType);
if ((parentMaximum >= 0) && ((max.longValue() == -1) || (max.longValue() > parentMaximum))) {
throw new InvalidParameterValueException("Account " + account.getAccountName() + "(id: " + accountId + ") has maximum allowed resource limit " + parentMaximum +
" for " + type + ", please specify a value less that or equal to " + parentMaximum);
@ -288,7 +486,7 @@ public class AccountManagerImpl implements AccountManager {
Long parentDomainId = domain.getParent();
if (parentDomainId != null) {
DomainVO parentDomain = _domainDao.findById(parentDomainId);
long parentMaximum = findCorrectResourceLimit(parentDomain, type);
long parentMaximum = findCorrectResourceLimit(parentDomain, resourceType);
if ((parentMaximum >= 0) && (max.longValue() > parentMaximum)) {
throw new InvalidParameterValueException("Domain " + domain.getName() + "(id: " + domainId + ") has maximum allowed resource limit " + parentMaximum +
" for " + type + ", please specify a value less that or equal to " + parentMaximum);
@ -325,8 +523,7 @@ public class AccountManagerImpl implements AccountManager {
return _resourceLimitDao.findById(limit.getId());
} else {
// Persist the new Limit
return _resourceLimitDao.persist(new ResourceLimitVO(domainId, accountId, type, max));
return _resourceLimitDao.persist(new ResourceLimitVO(domainId, accountId, resourceType, max));
}
}
}