From a2fea4d4499a963a7bb5aed73b4ee03016684529 Mon Sep 17 00:00:00 2001 From: Sanjay Tripathi Date: Mon, 13 May 2013 10:54:52 +0530 Subject: [PATCH] CLOUDSTACK-2297 : Delete Account/Domain is not updating the resources usage of the parent domain --- .../configuration/dao/ResourceCountDao.java | 2 ++ .../configuration/dao/ResourceCountDaoImpl.java | 16 ++++++++++++++++ .../configuration/dao/ResourceLimitDao.java | 2 ++ .../configuration/dao/ResourceLimitDaoImpl.java | 14 ++++++++++++++ .../src/com/cloud/user/AccountManagerImpl.java | 17 +++++++++++++++++ .../src/com/cloud/user/DomainManagerImpl.java | 15 ++++++++++++--- 6 files changed, 63 insertions(+), 3 deletions(-) diff --git a/engine/schema/src/com/cloud/configuration/dao/ResourceCountDao.java b/engine/schema/src/com/cloud/configuration/dao/ResourceCountDao.java index 111bcb122d6..dc5a65c8ef7 100644 --- a/engine/schema/src/com/cloud/configuration/dao/ResourceCountDao.java +++ b/engine/schema/src/com/cloud/configuration/dao/ResourceCountDao.java @@ -55,4 +55,6 @@ public interface ResourceCountDao extends GenericDao { Set listAllRowsToUpdate(long ownerId, ResourceOwnerType ownerType, ResourceType type); Set listRowsToUpdateForDomain(long domainId, ResourceType type); + + long removeEntriesByOwner(long ownerId, ResourceOwnerType ownerType); } diff --git a/engine/schema/src/com/cloud/configuration/dao/ResourceCountDaoImpl.java b/engine/schema/src/com/cloud/configuration/dao/ResourceCountDaoImpl.java index 52bc746fd8e..cfd2137f479 100644 --- a/engine/schema/src/com/cloud/configuration/dao/ResourceCountDaoImpl.java +++ b/engine/schema/src/com/cloud/configuration/dao/ResourceCountDaoImpl.java @@ -219,4 +219,20 @@ public class ResourceCountDaoImpl extends GenericDaoBase return super.persist(resourceCountVO); } + + + @Override + public long removeEntriesByOwner(long ownerId, ResourceOwnerType ownerType) { + SearchCriteria 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; + } + } \ No newline at end of file diff --git a/engine/schema/src/com/cloud/configuration/dao/ResourceLimitDao.java b/engine/schema/src/com/cloud/configuration/dao/ResourceLimitDao.java index 5fd79b37bc4..e47b38340c2 100644 --- a/engine/schema/src/com/cloud/configuration/dao/ResourceLimitDao.java +++ b/engine/schema/src/com/cloud/configuration/dao/ResourceLimitDao.java @@ -32,4 +32,6 @@ public interface ResourceLimitDao extends GenericDao { ResourceCount.ResourceType getLimitType(String type); ResourceLimitVO findByOwnerIdAndType(long ownerId, ResourceOwnerType ownerType, ResourceCount.ResourceType type); + + long removeEntriesByOwner(Long ownerId, ResourceOwnerType ownerType); } diff --git a/engine/schema/src/com/cloud/configuration/dao/ResourceLimitDaoImpl.java b/engine/schema/src/com/cloud/configuration/dao/ResourceLimitDaoImpl.java index d337070e921..bb67f6bbe21 100644 --- a/engine/schema/src/com/cloud/configuration/dao/ResourceLimitDaoImpl.java +++ b/engine/schema/src/com/cloud/configuration/dao/ResourceLimitDaoImpl.java @@ -97,4 +97,18 @@ public class ResourceLimitDaoImpl extends GenericDaoBase return null; } } + + @Override + public long removeEntriesByOwner(Long ownerId, ResourceOwnerType ownerType) { + SearchCriteria 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; + } } diff --git a/server/src/com/cloud/user/AccountManagerImpl.java b/server/src/com/cloud/user/AccountManagerImpl.java index 4088f64f58b..aac8d19eb0e 100755 --- a/server/src/com/cloud/user/AccountManagerImpl.java +++ b/server/src/com/cloud/user/AccountManagerImpl.java @@ -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 _userAuthenticators; List _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 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); diff --git a/server/src/com/cloud/user/DomainManagerImpl.java b/server/src/com/cloud/user/DomainManagerImpl.java index dbcbe4ee431..c451041d951 100644 --- a/server/src/com/cloud/user/DomainManagerImpl.java +++ b/server/src/com/cloud/user/DomainManagerImpl.java @@ -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 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"); }