@@ -366,6 +366,8 @@ public boolean deleteDomain(long domainId, Boolean cleanup) {
366366 }
367367
368368 _accountMgr .checkAccess (caller , domain );
369+ // Check across the domain hierarchy (current + children) for any delete-protected instances
370+ validateNoDeleteProtectedVmsForDomain (domain );
369371
370372 return deleteDomain (domain , cleanup );
371373 }
@@ -624,6 +626,9 @@ protected boolean cleanupDomain(Long domainId, Long ownerId) throws ConcurrentOp
624626 DomainVO domainHandle = _domainDao .findById (domainId );
625627 logger .debug ("Cleaning up domain {}" , domainHandle );
626628 {
629+ domainHandle .setState (Domain .State .Inactive );
630+ _domainDao .update (domainId , domainHandle );
631+
627632 SearchCriteria <DomainVO > sc = _domainDao .createSearchCriteria ();
628633 sc .addAnd ("parent" , SearchCriteria .Op .EQ , domainId );
629634 List <DomainVO > domains = _domainDao .search (sc , null );
@@ -632,12 +637,6 @@ protected boolean cleanupDomain(Long domainId, Long ownerId) throws ConcurrentOp
632637 sc1 .addAnd ("path" , SearchCriteria .Op .LIKE , "%" + "replace(" + domainHandle .getPath () + ", '%', '[%]')" + "%" );
633638 List <DomainVO > domainsToBeInactivated = _domainDao .search (sc1 , null );
634639
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-
641640 // update all subdomains to inactive so no accounts/users can be created
642641 for (DomainVO domain : domainsToBeInactivated ) {
643642 domain .setState (Domain .State .Inactive );
@@ -729,23 +728,20 @@ protected boolean cleanupDomain(Long domainId, Long ownerId) throws ConcurrentOp
729728 return success && deleteDomainSuccess ;
730729 }
731730
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-
731+ private void validateNoDeleteProtectedVmsForDomain (Domain parentDomain ) {
732+ Set <Long > allDomainIds = getDomainChildrenIds (parentDomain .getPath ());
736733 List <VMInstanceVO > deleteProtectedVms = vmInstanceDao .listDeleteProtectedVmsByDomainIds (allDomainIds );
737- if (deleteProtectedVms .isEmpty ()) {
734+ if (CollectionUtils .isEmpty (deleteProtectedVms )) {
738735 return ;
739736 }
740-
741737 if (logger .isDebugEnabled ()) {
742738 List <String > vmUuids = deleteProtectedVms .stream ().map (VMInstanceVO ::getUuid ).collect (Collectors .toList ());
743- logger .debug ("Cannot delete Domain {}, it has delete protection enabled for Instances: {}" , domainHandle , vmUuids );
739+ logger .debug ("Cannot delete Domain {}, it has delete protection enabled for Instances: {}" , parentDomain , vmUuids );
744740 }
745741
746742 throw new InvalidParameterValueException (
747743 String .format ("Cannot delete Domain '%s'. One or more Instances have delete protection enabled." ,
748- domainHandle .getName ()));
744+ parentDomain .getName ()));
749745 }
750746
751747 @ Override
0 commit comments