CLOUDSTACK-2297 : Delete Account/Domain is not updating the resources usage of the parent domain

This commit is contained in:
Sanjay Tripathi 2013-05-13 10:54:52 +05:30 committed by Devdeep Singh
parent e7332b7c04
commit a2fea4d449
6 changed files with 63 additions and 3 deletions

View File

@ -55,4 +55,6 @@ public interface ResourceCountDao extends GenericDao<ResourceCountVO, Long> {
Set<Long> listAllRowsToUpdate(long ownerId, ResourceOwnerType ownerType, ResourceType type);
Set<Long> listRowsToUpdateForDomain(long domainId, ResourceType type);
long removeEntriesByOwner(long ownerId, ResourceOwnerType ownerType);
}

View File

@ -219,4 +219,20 @@ public class ResourceCountDaoImpl extends GenericDaoBase<ResourceCountVO, Long>
return super.persist(resourceCountVO);
}
@Override
public long removeEntriesByOwner(long ownerId, ResourceOwnerType ownerType) {
SearchCriteria<ResourceCountVO> sc = TypeSearch.create();
if (ownerType == ResourceOwnerType.Account) {
sc.setParameters("accountId", ownerId);
return remove(sc);
} else if (ownerType == ResourceOwnerType.Domain) {
sc.setParameters("domainId", ownerId);
return remove(sc);
}
return 0;
}
}

View File

@ -32,4 +32,6 @@ public interface ResourceLimitDao extends GenericDao<ResourceLimitVO, Long> {
ResourceCount.ResourceType getLimitType(String type);
ResourceLimitVO findByOwnerIdAndType(long ownerId, ResourceOwnerType ownerType, ResourceCount.ResourceType type);
long removeEntriesByOwner(Long ownerId, ResourceOwnerType ownerType);
}

View File

@ -97,4 +97,18 @@ public class ResourceLimitDaoImpl extends GenericDaoBase<ResourceLimitVO, Long>
return null;
}
}
@Override
public long removeEntriesByOwner(Long ownerId, ResourceOwnerType ownerType) {
SearchCriteria<ResourceLimitVO> sc = IdTypeSearch.create();
if (ownerType == ResourceOwnerType.Account) {
sc.setParameters("accountId", ownerId);
return remove(sc);
} else if (ownerType == ResourceOwnerType.Domain) {
sc.setParameters("domainId", ownerId);
return remove(sc);
}
return 0;
}
}

View File

@ -54,9 +54,12 @@ import com.cloud.api.query.dao.UserAccountJoinDao;
import com.cloud.api.query.vo.ControlledViewEntity;
import com.cloud.configuration.Config;
import com.cloud.configuration.ConfigurationManager;
import com.cloud.configuration.ResourceCountVO;
import com.cloud.configuration.ResourceLimit;
import com.cloud.configuration.Resource.ResourceOwnerType;
import com.cloud.configuration.dao.ConfigurationDao;
import com.cloud.configuration.dao.ResourceCountDao;
import com.cloud.configuration.dao.ResourceLimitDao;
import com.cloud.dc.DataCenterVO;
import com.cloud.dc.dao.DataCenterDao;
import com.cloud.dc.dao.DataCenterVnetDao;
@ -229,6 +232,10 @@ public class AccountManagerImpl extends ManagerBase implements AccountManager, M
private AccountGuestVlanMapDao _accountGuestVlanMapDao;
@Inject
private DataCenterVnetDao _dataCenterVnetDao;
@Inject
private ResourceLimitService _resourceLimitMgr;
@Inject
private ResourceLimitDao _resourceLimitDao;
private List<UserAuthenticator> _userAuthenticators;
List<UserAuthenticator> _userPasswordEncoders;
@ -714,6 +721,16 @@ public class AccountManagerImpl extends ManagerBase implements AccountManager, M
int vlansReleased = _accountGuestVlanMapDao.removeByAccountId(accountId);
s_logger.info("deleteAccount: Released " + vlansReleased + " dedicated guest vlan ranges from account " + accountId);
// Update resource count for this account and for parent domains.
List<ResourceCountVO> resourceCounts = _resourceCountDao.listByOwnerId(accountId, ResourceOwnerType.Account);
for (ResourceCountVO resourceCount : resourceCounts) {
_resourceLimitMgr.decrementResourceCount(accountId, resourceCount.getType(), resourceCount.getCount());
}
// Delete resource count and resource limits entries set for this account (if there are any).
_resourceCountDao.removeEntriesByOwner(accountId, ResourceOwnerType.Account);
_resourceLimitDao.removeEntriesByOwner(accountId, ResourceOwnerType.Account);
return true;
} catch (Exception ex) {
s_logger.warn("Failed to cleanup account " + account + " due to ", ex);

View File

@ -16,11 +16,13 @@
// under the License.
package com.cloud.user;
import java.util.*;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import javax.ejb.Local;
import javax.inject.Inject;
import javax.naming.ConfigurationException;
import org.apache.cloudstack.api.command.admin.domain.ListDomainChildrenCmd;
import org.apache.cloudstack.api.command.admin.domain.ListDomainsCmd;
@ -29,8 +31,10 @@ import org.apache.cloudstack.region.RegionManager;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import com.cloud.configuration.Resource.ResourceOwnerType;
import com.cloud.configuration.ResourceLimit;
import com.cloud.configuration.dao.ResourceCountDao;
import com.cloud.configuration.dao.ResourceLimitDao;
import com.cloud.domain.Domain;
import com.cloud.domain.DomainVO;
import com.cloud.domain.dao.DomainDao;
@ -49,7 +53,6 @@ import com.cloud.storage.DiskOfferingVO;
import com.cloud.storage.dao.DiskOfferingDao;
import com.cloud.user.dao.AccountDao;
import com.cloud.utils.Pair;
import com.cloud.utils.component.Manager;
import com.cloud.utils.component.ManagerBase;
import com.cloud.utils.db.DB;
import com.cloud.utils.db.Filter;
@ -82,6 +85,8 @@ public class DomainManagerImpl extends ManagerBase implements DomainManager, Dom
private ProjectManager _projectMgr;
@Inject
private RegionManager _regionMgr;
@Inject
private ResourceLimitDao _resourceLimitDao;
@Override
public Domain getDomain(long domainId) {
@ -329,6 +334,10 @@ public class DomainManagerImpl extends ManagerBase implements DomainManager, Dom
List<AccountVO> accountsForCleanup = _accountDao.findCleanupsForRemovedAccounts(domainId);
if (accountsForCleanup.isEmpty()) {
deleteDomainSuccess = _domainDao.remove(domainId);
// Delete resource count and resource limits entries set for this domain (if there are any).
_resourceCountDao.removeEntriesByOwner(domainId, ResourceOwnerType.Domain);
_resourceLimitDao.removeEntriesByOwner(domainId, ResourceOwnerType.Domain);
} else {
s_logger.debug("Can't delete the domain yet because it has " + accountsForCleanup.size() + "accounts that need a cleanup");
}