erikbocks commented on code in PR #13236:
URL: https://github.com/apache/cloudstack/pull/13236#discussion_r3395483446
##########
plugins/database/quota/src/main/java/org/apache/cloudstack/api/response/QuotaResponseBuilderImpl.java:
##########
@@ -1158,6 +1135,185 @@ public QuotaValidateActivationRuleResponse
validateActivationRule(QuotaValidateA
return createValidateActivationRuleResponse(activationRule, quotaName,
false, message);
}
+ @Override
+ public QuotaResourceStatementResponse
createQuotaResourceStatement(QuotaResourceStatementCmd cmd) {
+ String resourceUuid = cmd.getId();
+ Integer usageType = cmd.getUsageType();
+ Date startDate = cmd.getStartDate();
+ Date endDate = cmd.getEndDate();
+
+ if (startDate.after(endDate)) {
+ throw new InvalidParameterValueException(String.format("The start
date [%s] must be before the end date [%s].", startDate, endDate));
+ }
+
+ InternalIdentity resource = retrieveResource(resourceUuid, usageType);
+ if (resource == null) {
+ logger.error("Could not find resource [{}] of type [{}]. Returning
an empty list.", resourceUuid, usageType);
+ return createQuotaResourceStatementResponse(resourceUuid,
usageType, new ArrayList<>(), new BigDecimal(0));
+ }
+
+ long id = resource.getId();
+
+ Long currentOwnerId = null;
+ if (resource instanceof ControlledEntity) {
+ currentOwnerId = ((ControlledEntity) resource).getAccountId();
+ }
+ Long accountId = getAccountIdForQuotaStatement(cmd.getEntityOwnerId(),
currentOwnerId);
+ Pair<Long, List<Long>> baseDomainAndFilteredDomains =
getDomainIdsForQuotaStatement(accountId, cmd.getDomainId(), cmd.isRecursive());
+
+ Long resourceId = null;
+ Long networkId = null;
+ Long offeringId = null;
+
+ switch (usageType) {
+ case UsageTypes.NETWORK_OFFERING:
+ case UsageTypes.BACKUP:
+ offeringId = id;
+ break;
+ case UsageTypes.NETWORK_BYTES_RECEIVED:
+ case UsageTypes.NETWORK_BYTES_SENT:
+ networkId = id;
+ break;
+ default:
+ resourceId = id;
+ }
+
+ logger.debug("Attempting to find quota usages with parameters usage
type [{}], usage id [{}], network id [{}], offering id [{}], and between [{}]
and [{}].",
+ usageType, resourceId, networkId, offeringId, startDate,
endDate);
+
+ List<QuotaUsageJoinVO> quotaUsageJoinList =
quotaUsageJoinDao.findQuotaUsage(accountId,
baseDomainAndFilteredDomains.second(), usageType, resourceId, networkId,
offeringId, startDate, endDate, null);
+
+ logger.debug("Found [{}] quota usages using as parameter usage type
[{}], usage id [{}], network id [{}], offering id [{}], and between [{}] and
[{}].",
+ quotaUsageJoinList.size(), usageType, resourceId, networkId,
offeringId, startDate, endDate);
+
+ List<QuotaResourceStatementItemResponse>
quotaResourceStatementItemResponseList = new ArrayList<>();
+ BigDecimal totalQuotaUsed = new BigDecimal(0);
+ List<QuotaTariffVO> quotaTariffs =
_quotaTariffDao.listQuotaTariffs(null, null, usageType, null, null, true, null,
null).first();
+
+ for (QuotaUsageJoinVO quotaUsageJoin : quotaUsageJoinList) {
+ Account account =
_accountMgr.getAccount(quotaUsageJoin.getAccountId());
+ String accountUuid = account.getUuid();
+
+ List<QuotaTariffUsageVO> quotaTariffUsageList =
quotaTariffUsageDao.listQuotaTariffUsages(quotaUsageJoin.getId());
+ logger.debug("Found [{}] quota tariff usages associated to the
quota usage [{}] of resource [{}] and type [{}] between [{}] and [{}].",
+ quotaTariffUsageList.size(), quotaUsageJoin, resourceUuid,
usageType, startDate, endDate);
+ for (QuotaTariffUsageVO quotaTariffUsage: quotaTariffUsageList) {
+
quotaResourceStatementItemResponseList.add(createQuotaResourceStatementItemResponse(quotaTariffUsage,
quotaTariffs, quotaUsageJoin.getStartDate(),
+ quotaUsageJoin.getEndDate(), accountUuid));
+ totalQuotaUsed =
totalQuotaUsed.add(quotaTariffUsage.getQuotaUsed());
+ }
+ }
+ logger.debug("The total quota used of type [{}] between [{}] and [{}]
for the resource [{}] was [{}].", usageType, startDate, endDate, resourceUuid,
totalQuotaUsed);
+
+ return createQuotaResourceStatementResponse(resourceUuid, usageType,
quotaResourceStatementItemResponseList, totalQuotaUsed);
+ }
+
+ protected Long getAccountIdForQuotaStatement(long providedAccountId, Long
fallbackAccountId) {
+ Account caller = CallContext.current().getCallingAccount();
+
+ if (providedAccountId != -1L) {
+ Account account =
_accountDao.findByIdIncludingRemoved(providedAccountId);
+ _accountMgr.checkAccess(caller, null, false, account);
+ logger.debug("Limiting the Quota resource statement for the
provided Account [{}].", providedAccountId);
+ return providedAccountId;
+ }
+
+ Account.Type callerType = caller.getType();
+ if (Account.Type.ADMIN.equals(callerType) ||
Account.Type.DOMAIN_ADMIN.equals(callerType)) {
+ logger.debug("Not limiting the Quota resource statement for a
specific Account, as no specific Account was provided and the caller is either
an admin or domain admin.");
+ return null;
+ }
+
+ if (fallbackAccountId != null) {
+ Account fallbackAccount =
_accountDao.findByIdIncludingRemoved(fallbackAccountId);
+ _accountMgr.checkAccess(caller, null, false, fallbackAccount);
+ logger.debug("Limiting the Quota statement for the fallback
Account [{}], as no specific Account was provided.", fallbackAccountId);
+ return fallbackAccountId;
+ }
+
+ logger.debug("Limiting the Quota resource statement for the calling
account, as no specific Account was provided and no fallback account was
provided.");
+ return caller.getAccountId();
+ }
+
+ protected Pair<Long, List<Long>> getDomainIdsForQuotaStatement(Long
finalAccountId, Long providedDomainId, boolean isRecursive) {
+ if (finalAccountId != null) {
+ // Access to the provided account has already been validated
+ logger.debug("Not limiting the Quota statement for a specific
Domain, as we are already limiting by Account.");
+ return new Pair<>(null, null);
+ }
+
+ // User accounts will have already been limited to themselves
+ Account caller = CallContext.current().getCallingAccount();
+ Long domainId = providedDomainId;
+
+ if (domainId != null) {
+ Domain domain = domainDao.findByIdIncludingRemoved(domainId);
+ _accountMgr.checkAccess(caller, domain);
+ logger.debug("Limiting the Quota statement for the provided Domain
[{}].", domainId);
+ }
+ if (domainId == null) {
Review Comment:
```suggestion
}
if (domainId == null) {
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]