filter backup offerings by domain id

This commit is contained in:
Pearl Dsilva 2025-12-08 18:37:06 -05:00
parent 30ce1217cc
commit 39dacc300e
2 changed files with 30 additions and 1 deletions

View File

@ -25,7 +25,7 @@ import org.apache.cloudstack.api.response.ListResponse;
import org.apache.cloudstack.backup.BackupOffering;
import org.apache.cloudstack.context.CallContext;
public abstract class BaseBackupListCmd extends BaseListCmd {
public abstract class BaseBackupListCmd extends BaseListAccountResourcesCmd {
protected void setupResponseBackupOfferingsList(final List<BackupOffering> offerings, final Integer count) {
final ListResponse<BackupOfferingResponse> response = new ListResponse<>();

View File

@ -357,6 +357,7 @@ public class BackupManagerImpl extends ManagerBase implements BackupManager {
final Long offeringId = cmd.getOfferingId();
final Long zoneId = cmd.getZoneId();
final String keyword = cmd.getKeyword();
Long domainId = cmd.getDomainId();
if (offeringId != null) {
BackupOfferingVO offering = backupOfferingDao.findById(offeringId);
@ -370,8 +371,13 @@ public class BackupManagerImpl extends ManagerBase implements BackupManager {
SearchBuilder<BackupOfferingVO> sb = backupOfferingDao.createSearchBuilder();
sb.and("zone_id", sb.entity().getZoneId(), SearchCriteria.Op.EQ);
sb.and("name", sb.entity().getName(), SearchCriteria.Op.EQ);
CallContext ctx = CallContext.current();
final Account caller = ctx.getCallingAccount();
if (Account.Type.ADMIN != caller.getType() && domainId == null) {
domainId = caller.getDomainId();
}
if (Account.Type.NORMAL == caller.getType()) {
sb.and("user_backups_allowed", sb.entity().isUserDrivenBackupAllowed(), SearchCriteria.Op.EQ);
}
@ -384,13 +390,36 @@ public class BackupManagerImpl extends ManagerBase implements BackupManager {
if (keyword != null) {
sc.setParameters("name", "%" + keyword + "%");
}
if (Account.Type.NORMAL == caller.getType()) {
sc.setParameters("user_backups_allowed", true);
}
Pair<List<BackupOfferingVO>, Integer> result = backupOfferingDao.searchAndCount(sc, searchFilter);
if (domainId != null) {
List<BackupOfferingVO> filteredOfferings = new ArrayList<>();
for (BackupOfferingVO offering : result.first()) {
List<Long> offeringDomains = backupOfferingDetailsDao.findDomainIds(offering.getId());
if (offeringDomains.isEmpty() || offeringDomains.contains(domainId) || containsParentDomain(offeringDomains, domainId)) {
filteredOfferings.add(offering);
}
}
return new Pair<>(new ArrayList<>(filteredOfferings), filteredOfferings.size());
}
return new Pair<>(new ArrayList<>(result.first()), result.second());
}
private boolean containsParentDomain(List<Long> offeringDomains, Long domainId) {
for (Long offeringDomainId : offeringDomains) {
if (domainDao.isChildDomain(offeringDomainId, domainId)) {
return true;
}
}
return false;
}
@Override
public boolean deleteBackupOffering(final Long offeringId) {
final BackupOfferingVO offering = backupOfferingDao.findById(offeringId);