Skip to content

Commit 0096e21

Browse files
committed
early check delete protected vm count and error out for account deletion
1 parent 6569709 commit 0096e21

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,4 +192,6 @@ List<VMInstanceVO> searchRemovedByRemoveDate(final Date startDate, final Date en
192192
int getVmCountByOfferingNotInDomain(Long serviceOfferingId, List<Long> domainIds);
193193

194194
List<VMInstanceVO> listByIdsIncludingRemoved(List<Long> ids);
195+
196+
List<VMInstanceVO> listDeleteProtectedVmsByAccountId(long accountId);
195197
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import javax.annotation.PostConstruct;
3131
import javax.inject.Inject;
3232

33+
import org.apache.cloudstack.api.ApiConstants;
3334
import org.apache.commons.collections.CollectionUtils;
3435
import org.springframework.stereotype.Component;
3536

@@ -106,6 +107,7 @@ public class VMInstanceDaoImpl extends GenericDaoBase<VMInstanceVO, Long> implem
106107
protected SearchBuilder<VMInstanceVO> IdsPowerStateSelectSearch;
107108
GenericSearchBuilder<VMInstanceVO, Integer> CountByOfferingId;
108109
GenericSearchBuilder<VMInstanceVO, Integer> CountUserVmNotInDomain;
110+
SearchBuilder<VMInstanceVO> DeleteProtectedVmSearch;
109111

110112
@Inject
111113
ResourceTagDao tagsDao;
@@ -368,6 +370,12 @@ protected void init() {
368370
CountUserVmNotInDomain.and("domainIdsNotIn", CountUserVmNotInDomain.entity().getDomainId(), Op.NIN);
369371
CountUserVmNotInDomain.done();
370372

373+
DeleteProtectedVmSearch = createSearchBuilder();
374+
DeleteProtectedVmSearch.selectFields(DeleteProtectedVmSearch.entity().getUuid());
375+
DeleteProtectedVmSearch.and(ApiConstants.ACCOUNT_ID, DeleteProtectedVmSearch.entity().getAccountId(), Op.EQ);
376+
DeleteProtectedVmSearch.and(ApiConstants.DELETE_PROTECTION, DeleteProtectedVmSearch.entity().isDeleteProtection(), Op.EQ);
377+
DeleteProtectedVmSearch.and(ApiConstants.REMOVED, DeleteProtectedVmSearch.entity().getRemoved(), Op.NULL);
378+
DeleteProtectedVmSearch.done();
371379
}
372380

373381
@Override
@@ -1296,4 +1304,12 @@ public List<VMInstanceVO> listByIdsIncludingRemoved(List<Long> ids) {
12961304
sc.setParameters("ids", ids.toArray());
12971305
return listIncludingRemovedBy(sc);
12981306
}
1307+
1308+
@Override
1309+
public List<VMInstanceVO> listDeleteProtectedVmsByAccountId(long accountId) {
1310+
SearchCriteria<VMInstanceVO> sc = DeleteProtectedVmSearch.create();
1311+
sc.setParameters(ApiConstants.ACCOUNT_ID, accountId);
1312+
sc.setParameters(ApiConstants.DELETE_PROTECTION, true);
1313+
return listBy(sc);
1314+
}
12991315
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2103,6 +2103,7 @@ public boolean deleteUserAccount(long accountId) {
21032103
return true;
21042104
}
21052105

2106+
validateNoDeleteProtectedVms(account);
21062107
checkIfAccountManagesProjects(accountId);
21072108
verifyCallerPrivilegeForUserOrAccountOperations(account);
21082109

@@ -2144,6 +2145,24 @@ protected boolean isDeleteNeeded(AccountVO account, long accountId, Account call
21442145
return true;
21452146
}
21462147

2148+
private void validateNoDeleteProtectedVms(Account account) {
2149+
long accountId = account.getId();
2150+
List<VMInstanceVO> deleteProtectedVms = _vmDao.listDeleteProtectedVmsByAccountId(accountId);
2151+
if (deleteProtectedVms.isEmpty()) {
2152+
return;
2153+
}
2154+
2155+
if (logger.isDebugEnabled()) {
2156+
List<String> vmUuids = deleteProtectedVms.stream().map(VMInstanceVO::getUuid).collect(Collectors.toList());
2157+
logger.debug("Cannot delete Account {} (id={}), delete protection enabled for Instances: {}",
2158+
account.getAccountName(), accountId, vmUuids);
2159+
}
2160+
2161+
throw new InvalidParameterValueException(
2162+
String.format("Cannot delete Account '%s'. One or more Instances have delete protection enabled.",
2163+
account.getAccountName()));
2164+
}
2165+
21472166
@Override
21482167
@ActionEvent(eventType = EventTypes.EVENT_ACCOUNT_ENABLE, eventDescription = "enabling account", async = true)
21492168
public AccountVO enableAccount(String accountName, Long domainId, Long accountId) {

0 commit comments

Comments
 (0)