Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added recursive fetch of child domains for listUsageRecords API call #4717

Merged
merged 10 commits into from Apr 10, 2021
Expand Up @@ -85,6 +85,10 @@ public class ListUsageRecordsCmd extends BaseListCmd {
@Parameter(name = ApiConstants.OLD_FORMAT, type = CommandType.BOOLEAN, description = "Flag to enable description rendered in old format which uses internal database IDs instead of UUIDs. False by default.")
private Boolean oldFormat;

@Parameter(name = ApiConstants.IS_RECURSIVE, type = CommandType.BOOLEAN,
description = "Specify if usage records should be fetched recursively per domain.")
Spaceman1984 marked this conversation as resolved.
Show resolved Hide resolved
private Boolean recursive = false;

/////////////////////////////////////////////////////
/////////////////// Accessors ///////////////////////
/////////////////////////////////////////////////////
Expand Down Expand Up @@ -153,6 +157,10 @@ public boolean getOldFormat() {
return oldFormat != null && oldFormat;
}

public Boolean isRecursive() {
return recursive;
}

/////////////////////////////////////////////////////
/////////////// API Implementation///////////////////
/////////////////////////////////////////////////////
Expand Down
12 changes: 11 additions & 1 deletion server/src/main/java/com/cloud/usage/UsageServiceImpl.java
Expand Up @@ -248,7 +248,17 @@ public Pair<List<? extends Usage>, Integer> getUsageRecords(ListUsageRecordsCmd
sc.addAnd("domainId", SearchCriteria.Op.IN, domainIds.toArray());
}

if (domainId != null) {
if (cmd.isRecursive() && domainId != null){
Spaceman1984 marked this conversation as resolved.
Show resolved Hide resolved
SearchCriteria<DomainVO> sdc = _domainDao.createSearchCriteria();
sdc.addOr("path", SearchCriteria.Op.LIKE, _domainDao.findById(domainId).getPath() + "%");
List<DomainVO> domains = _domainDao.search(sdc, null);
List<Long> domainIds = new ArrayList<Long>();
for (DomainVO domain : domains)
domainIds.add(domain.getId());
sc.addAnd("domainId", SearchCriteria.Op.IN, domainIds.toArray());
}

if (!cmd.isRecursive() && domainId != null) {
Spaceman1984 marked this conversation as resolved.
Show resolved Hide resolved
sc.addAnd("domainId", SearchCriteria.Op.EQ, domainId);
}

Expand Down