Skip to content

Commit 39dacc3

Browse files
committed
filter backup offerings by domain id
1 parent 30ce121 commit 39dacc3

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

api/src/main/java/org/apache/cloudstack/api/BaseBackupListCmd.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import org.apache.cloudstack.backup.BackupOffering;
2626
import org.apache.cloudstack.context.CallContext;
2727

28-
public abstract class BaseBackupListCmd extends BaseListCmd {
28+
public abstract class BaseBackupListCmd extends BaseListAccountResourcesCmd {
2929

3030
protected void setupResponseBackupOfferingsList(final List<BackupOffering> offerings, final Integer count) {
3131
final ListResponse<BackupOfferingResponse> response = new ListResponse<>();

server/src/main/java/org/apache/cloudstack/backup/BackupManagerImpl.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ public Pair<List<BackupOffering>, Integer> listBackupOfferings(final ListBackupO
357357
final Long offeringId = cmd.getOfferingId();
358358
final Long zoneId = cmd.getZoneId();
359359
final String keyword = cmd.getKeyword();
360+
Long domainId = cmd.getDomainId();
360361

361362
if (offeringId != null) {
362363
BackupOfferingVO offering = backupOfferingDao.findById(offeringId);
@@ -370,8 +371,13 @@ public Pair<List<BackupOffering>, Integer> listBackupOfferings(final ListBackupO
370371
SearchBuilder<BackupOfferingVO> sb = backupOfferingDao.createSearchBuilder();
371372
sb.and("zone_id", sb.entity().getZoneId(), SearchCriteria.Op.EQ);
372373
sb.and("name", sb.entity().getName(), SearchCriteria.Op.EQ);
374+
373375
CallContext ctx = CallContext.current();
374376
final Account caller = ctx.getCallingAccount();
377+
if (Account.Type.ADMIN != caller.getType() && domainId == null) {
378+
domainId = caller.getDomainId();
379+
}
380+
375381
if (Account.Type.NORMAL == caller.getType()) {
376382
sb.and("user_backups_allowed", sb.entity().isUserDrivenBackupAllowed(), SearchCriteria.Op.EQ);
377383
}
@@ -384,13 +390,36 @@ public Pair<List<BackupOffering>, Integer> listBackupOfferings(final ListBackupO
384390
if (keyword != null) {
385391
sc.setParameters("name", "%" + keyword + "%");
386392
}
393+
387394
if (Account.Type.NORMAL == caller.getType()) {
388395
sc.setParameters("user_backups_allowed", true);
389396
}
397+
390398
Pair<List<BackupOfferingVO>, Integer> result = backupOfferingDao.searchAndCount(sc, searchFilter);
399+
400+
if (domainId != null) {
401+
List<BackupOfferingVO> filteredOfferings = new ArrayList<>();
402+
for (BackupOfferingVO offering : result.first()) {
403+
List<Long> offeringDomains = backupOfferingDetailsDao.findDomainIds(offering.getId());
404+
if (offeringDomains.isEmpty() || offeringDomains.contains(domainId) || containsParentDomain(offeringDomains, domainId)) {
405+
filteredOfferings.add(offering);
406+
}
407+
}
408+
return new Pair<>(new ArrayList<>(filteredOfferings), filteredOfferings.size());
409+
}
410+
391411
return new Pair<>(new ArrayList<>(result.first()), result.second());
392412
}
393413

414+
private boolean containsParentDomain(List<Long> offeringDomains, Long domainId) {
415+
for (Long offeringDomainId : offeringDomains) {
416+
if (domainDao.isChildDomain(offeringDomainId, domainId)) {
417+
return true;
418+
}
419+
}
420+
return false;
421+
}
422+
394423
@Override
395424
public boolean deleteBackupOffering(final Long offeringId) {
396425
final BackupOfferingVO offering = backupOfferingDao.findById(offeringId);

0 commit comments

Comments
 (0)