Skip to content

Commit 1265b62

Browse files
committed
fix domain delection check, add limit of 10 to the return instance list
1 parent 12d61e3 commit 1265b62

File tree

5 files changed

+21
-19
lines changed

5 files changed

+21
-19
lines changed

engine/schema/src/main/java/com/cloud/vm/dao/VMInstanceDao.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.HashMap;
2222
import java.util.List;
2323
import java.util.Map;
24+
import java.util.Set;
2425

2526
import com.cloud.hypervisor.Hypervisor;
2627
import com.cloud.utils.Pair;
@@ -195,5 +196,5 @@ List<VMInstanceVO> searchRemovedByRemoveDate(final Date startDate, final Date en
195196

196197
List<VMInstanceVO> listDeleteProtectedVmsByAccountId(long accountId);
197198

198-
List<VMInstanceVO> listDeleteProtectedVmsByDomainIds(List<Long> domainIds);
199+
List<VMInstanceVO> listDeleteProtectedVmsByDomainIds(Set<Long> domainIds);
199200
}

engine/schema/src/main/java/com/cloud/vm/dao/VMInstanceDaoImpl.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.HashMap;
2626
import java.util.List;
2727
import java.util.Map;
28+
import java.util.Set;
2829
import java.util.stream.Collectors;
2930

3031
import javax.annotation.PostConstruct;
@@ -1318,14 +1319,16 @@ public List<VMInstanceVO> listDeleteProtectedVmsByAccountId(long accountId) {
13181319
SearchCriteria<VMInstanceVO> sc = DeleteProtectedVmSearchByAccount.create();
13191320
sc.setParameters(ApiConstants.ACCOUNT_ID, accountId);
13201321
sc.setParameters(ApiConstants.DELETE_PROTECTION, true);
1321-
return listBy(sc);
1322+
Filter filter = new Filter(VMInstanceVO.class, null, false, 0L, 10L);
1323+
return listBy(sc, filter);
13221324
}
13231325

13241326
@Override
1325-
public List<VMInstanceVO> listDeleteProtectedVmsByDomainIds(List<Long> domainIds) {
1327+
public List<VMInstanceVO> listDeleteProtectedVmsByDomainIds(Set<Long> domainIds) {
13261328
SearchCriteria<VMInstanceVO> sc = DeleteProtectedVmSearchByDomainIds.create();
13271329
sc.setParameters(ApiConstants.DOMAIN_IDS, domainIds.toArray());
13281330
sc.setParameters(ApiConstants.DELETE_PROTECTION, true);
1329-
return listBy(sc);
1331+
Filter filter = new Filter(VMInstanceVO.class, null, false, 0L, 10L);
1332+
return listBy(sc, filter);
13301333
}
13311334
}

server/src/main/java/com/cloud/user/AccountManagerImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2142,7 +2142,7 @@ protected boolean isDeleteNeeded(AccountVO account, long accountId, Account call
21422142
private void validateNoDeleteProtectedVmsForAccount(Account account) {
21432143
long accountId = account.getId();
21442144
List<VMInstanceVO> deleteProtectedVms = _vmDao.listDeleteProtectedVmsByAccountId(accountId);
2145-
if (deleteProtectedVms.isEmpty()) {
2145+
if (CollectionUtils.isEmpty(deleteProtectedVms)) {
21462146
return;
21472147
}
21482148

server/src/main/java/com/cloud/user/DomainManagerImpl.java

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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

server/src/test/java/com/cloud/user/DomainManagerImplTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ public void testDeleteDomainRootDomain() {
216216
@Test
217217
public void testDeleteDomainNoCleanup() {
218218
Mockito.when(_configMgr.releaseDomainSpecificVirtualRanges(Mockito.any())).thenReturn(true);
219+
Mockito.doReturn(Collections.emptySet()).when(domainManager).getDomainChildrenIds(Mockito.any());
219220
domainManager.deleteDomain(DOMAIN_ID, testDomainCleanup);
220221
Mockito.verify(domainManager).deleteDomain(domain, testDomainCleanup);
221222
Mockito.verify(domainManager).removeDomainWithNoAccountsForCleanupNetworksOrDedicatedResources(domain);
@@ -281,6 +282,7 @@ public void deleteDomain() {
281282
Mockito.when(_dedicatedDao.listByDomainId(Mockito.anyLong())).thenReturn(new ArrayList<DedicatedResourceVO>());
282283
Mockito.when(domainDaoMock.remove(Mockito.anyLong())).thenReturn(true);
283284
Mockito.when(_configMgr.releaseDomainSpecificVirtualRanges(Mockito.any())).thenReturn(true);
285+
Mockito.doReturn(Collections.emptySet()).when(domainManager).getDomainChildrenIds(Mockito.any());
284286

285287
try {
286288
Assert.assertTrue(domainManager.deleteDomain(20l, false));

0 commit comments

Comments
 (0)