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


Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/a2fea4d4
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/a2fea4d4
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/a2fea4d4

Branch: refs/heads/ui-vpc-redesign
Commit: a2fea4d4499a963a7bb5aed73b4ee03016684529
Parents: e7332b7
Author: Sanjay Tripathi <sanjay.tripa...@citrix.com>
Authored: Mon May 13 10:54:52 2013 +0530
Committer: Devdeep Singh <devd...@gmail.com>
Committed: Mon May 20 14:08:31 2013 +0530

----------------------------------------------------------------------
 .../cloud/configuration/dao/ResourceCountDao.java  |    2 +
 .../configuration/dao/ResourceCountDaoImpl.java    |   16 ++++++++++++++
 .../cloud/configuration/dao/ResourceLimitDao.java  |    2 +
 .../configuration/dao/ResourceLimitDaoImpl.java    |   14 ++++++++++++
 server/src/com/cloud/user/AccountManagerImpl.java  |   17 +++++++++++++++
 server/src/com/cloud/user/DomainManagerImpl.java   |   15 ++++++++++--
 6 files changed, 63 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a2fea4d4/engine/schema/src/com/cloud/configuration/dao/ResourceCountDao.java
----------------------------------------------------------------------
diff --git 
a/engine/schema/src/com/cloud/configuration/dao/ResourceCountDao.java 
b/engine/schema/src/com/cloud/configuration/dao/ResourceCountDao.java
index 111bcb1..dc5a65c 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<ResourceCountVO, Long> {
     Set<Long> listAllRowsToUpdate(long ownerId, ResourceOwnerType ownerType, 
ResourceType type);
 
     Set<Long> listRowsToUpdateForDomain(long domainId, ResourceType type);
+
+    long removeEntriesByOwner(long ownerId, ResourceOwnerType ownerType);
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a2fea4d4/engine/schema/src/com/cloud/configuration/dao/ResourceCountDaoImpl.java
----------------------------------------------------------------------
diff --git 
a/engine/schema/src/com/cloud/configuration/dao/ResourceCountDaoImpl.java 
b/engine/schema/src/com/cloud/configuration/dao/ResourceCountDaoImpl.java
index 52bc746..cfd2137 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<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;
+    }
+
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a2fea4d4/engine/schema/src/com/cloud/configuration/dao/ResourceLimitDao.java
----------------------------------------------------------------------
diff --git 
a/engine/schema/src/com/cloud/configuration/dao/ResourceLimitDao.java 
b/engine/schema/src/com/cloud/configuration/dao/ResourceLimitDao.java
index 5fd79b3..e47b383 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<ResourceLimitVO, Long> {
     ResourceCount.ResourceType getLimitType(String type);
 
     ResourceLimitVO findByOwnerIdAndType(long ownerId, ResourceOwnerType 
ownerType, ResourceCount.ResourceType type);
+
+    long removeEntriesByOwner(Long ownerId, ResourceOwnerType ownerType);
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a2fea4d4/engine/schema/src/com/cloud/configuration/dao/ResourceLimitDaoImpl.java
----------------------------------------------------------------------
diff --git 
a/engine/schema/src/com/cloud/configuration/dao/ResourceLimitDaoImpl.java 
b/engine/schema/src/com/cloud/configuration/dao/ResourceLimitDaoImpl.java
index d337070..bb67f6b 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<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;
+    }
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a2fea4d4/server/src/com/cloud/user/AccountManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/user/AccountManagerImpl.java 
b/server/src/com/cloud/user/AccountManagerImpl.java
index 4088f64..aac8d19 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<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);

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a2fea4d4/server/src/com/cloud/user/DomainManagerImpl.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/user/DomainManagerImpl.java 
b/server/src/com/cloud/user/DomainManagerImpl.java
index dbcbe4e..c451041 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<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");
         }

Reply via email to