@@ -624,20 +624,20 @@ protected boolean cleanupDomain(Long domainId, Long ownerId) throws ConcurrentOp
624624 DomainVO domainHandle = _domainDao .findById (domainId );
625625 logger .debug ("Cleaning up domain {}" , domainHandle );
626626 {
627- domainHandle .setState (Domain .State .Inactive );
628- _domainDao .update (domainId , domainHandle );
629-
630627 SearchCriteria <DomainVO > sc = _domainDao .createSearchCriteria ();
631628 sc .addAnd ("parent" , SearchCriteria .Op .EQ , domainId );
632629 List <DomainVO > domains = _domainDao .search (sc , null );
633630
634- List <Long > domainIds = domains .stream ().map (Domain ::getId ).collect (Collectors .toList ());
635- validateNoDeleteProtectedVmsForDomainIds (domainHandle , domainIds );
636-
637631 SearchCriteria <DomainVO > sc1 = _domainDao .createSearchCriteria ();
638632 sc1 .addAnd ("path" , SearchCriteria .Op .LIKE , "%" + "replace(" + domainHandle .getPath () + ", '%', '[%]')" + "%" );
639633 List <DomainVO > domainsToBeInactivated = _domainDao .search (sc1 , null );
640634
635+ // Validate that no Instance in this domain or its subdomains has delete protection
636+ validateNoDeleteProtectedVmsForDomain (domainHandle , domainsToBeInactivated );
637+
638+ domainHandle .setState (Domain .State .Inactive );
639+ _domainDao .update (domainId , domainHandle );
640+
641641 // update all subdomains to inactive so no accounts/users can be created
642642 for (DomainVO domain : domainsToBeInactivated ) {
643643 domain .setState (Domain .State .Inactive );
@@ -729,8 +729,11 @@ protected boolean cleanupDomain(Long domainId, Long ownerId) throws ConcurrentOp
729729 return success && deleteDomainSuccess ;
730730 }
731731
732- private void validateNoDeleteProtectedVmsForDomainIds (Domain domainHandle , List <Long > domainIds ) {
733- List <VMInstanceVO > deleteProtectedVms = vmInstanceDao .listDeleteProtectedVmsByDomainIds (domainIds );
732+ private void validateNoDeleteProtectedVmsForDomain (Domain domainHandle , List <DomainVO > subDomains ) {
733+ List <Long > allDomainIds = subDomains .stream ().map (Domain ::getId ).collect (Collectors .toList ());
734+ allDomainIds .add (domainHandle .getId ());
735+
736+ List <VMInstanceVO > deleteProtectedVms = vmInstanceDao .listDeleteProtectedVmsByDomainIds (allDomainIds );
734737 if (deleteProtectedVms .isEmpty ()) {
735738 return ;
736739 }
0 commit comments