Skip to content

Commit c56594b

Browse files
committed
Extract creation of search parameters & fix project resources being ignored when passing multiple IDs
1 parent c7989c0 commit c56594b

File tree

2 files changed

+35
-22
lines changed

2 files changed

+35
-22
lines changed

plugins/metrics/src/main/java/org/apache/cloudstack/api/ListVolumesUsageHistoryCmd.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class ListVolumesUsageHistoryCmd extends BaseResourceUsageHistoryCmd {
3737
@Parameter(name = ApiConstants.ID, type = CommandType.UUID, entityType = VolumeResponse.class, description = "the ID of the volume.")
3838
private Long id;
3939

40-
@Parameter(name=ApiConstants.IDS, type=CommandType.LIST, collectionType=CommandType.UUID, entityType= SystemVmResponse.class, description="the IDs of the volumes, mutually exclusive with id.")
40+
@Parameter(name = ApiConstants.IDS, type = CommandType.LIST, collectionType = CommandType.UUID, entityType = VolumeResponse.class, description = "the IDs of the volumes, mutually exclusive with id.")
4141
private List<Long> ids;
4242

4343
@Parameter(name = ApiConstants.NAME, type = CommandType.STRING, description = "name of the volume (a substring match is made against the parameter value returning the data for all matching Volumes).")

plugins/metrics/src/main/java/org/apache/cloudstack/metrics/MetricsServiceImpl.java

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
import org.apache.cloudstack.utils.bytescale.ByteScaleUtils;
8383
import org.apache.commons.beanutils.BeanUtils;
8484
import org.apache.commons.collections.CollectionUtils;
85+
import org.apache.commons.lang3.ObjectUtils;
8586
import org.apache.commons.lang3.StringUtils;
8687
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
8788
import org.apache.commons.lang3.builder.ToStringStyle;
@@ -245,30 +246,48 @@ public ListResponse<VolumeMetricsStatsResponse> searchForVolumeMetricsStats(List
245246
}
246247

247248
/**
248-
* Searches VMs based on {@code ListVMsUsageHistoryCmd} parameters.
249-
*
250-
* @param cmd the {@link ListVMsUsageHistoryCmd} specifying the parameters.
251-
* @return the list of VMs.
249+
* Outputs the parameters that should be used for access control in the query of a resource to
250+
* {@code permittedAccounts} and {@code domainIdRecursiveListProject}.
251+
* @param isIdProvided indicates whether any ID was provided to the command
252252
*/
253-
protected Pair<List<UserVmVO>, Integer> searchForUserVmsInternal(ListVMsUsageHistoryCmd cmd) {
254-
final Long id = cmd.getId();
253+
private void buildBaseACLSearchParametersForMetrics(boolean isIdProvided, List<Long> permittedAccounts, Ternary<Long, Boolean,
254+
Project.ListProjectResourcesCriteria> domainIdRecursiveListProject) {
255255
Account caller = CallContext.current().getCallingAccount();
256-
List<Long> permittedAccounts = new ArrayList<>();
257256
Account.Type callerType = caller.getType();
257+
258258
boolean recursive = AccountTypesWithRecursiveUsageAccess.contains(callerType);
259-
Ternary<Long, Boolean, Project.ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<>(null, recursive, null);
259+
domainIdRecursiveListProject.second(recursive);
260+
261+
// If no ID was provided, then the listing will skip project resources (null); otherwise, project resources should
262+
// be listed as well (any long allows this)
263+
Long id = isIdProvided ? 1L : null;
260264

261265
// Allow users to also list metrics of resources owned by projects they belong to (-1L), and admins to list all
262266
// metrics belonging to their domains recursively (null)
263-
Long projectId = callerType == Account.Type.NORMAL ? -1L : null;
267+
Long projectId = isIdProvided && callerType == Account.Type.NORMAL ? -1L : null;
264268

265269
accountMgr.buildACLSearchParameters(caller, id, null, projectId, permittedAccounts, domainIdRecursiveListProject, true, false);
270+
}
271+
272+
/**
273+
* Searches VMs based on {@code ListVMsUsageHistoryCmd} parameters.
274+
*
275+
* @param cmd the {@link ListVMsUsageHistoryCmd} specifying the parameters.
276+
* @return the list of VMs.
277+
*/
278+
protected Pair<List<UserVmVO>, Integer> searchForUserVmsInternal(ListVMsUsageHistoryCmd cmd) {
279+
List<Long> ids = getIdsListFromCmd(cmd.getId(), cmd.getIds());
280+
281+
boolean isIdProvided = CollectionUtils.isNotEmpty(ids);
282+
List<Long> permittedAccounts = new ArrayList<>();
283+
Ternary<Long, Boolean, Project.ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<>(null, null, null);
284+
buildBaseACLSearchParametersForMetrics(isIdProvided, permittedAccounts, domainIdRecursiveListProject);
285+
266286
Long domainId = domainIdRecursiveListProject.first();
267287
Boolean isRecursive = domainIdRecursiveListProject.second();
268288
Project.ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third();
269289

270290
Filter searchFilter = new Filter(UserVmVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal());
271-
List<Long> ids = getIdsListFromCmd(cmd.getId(), cmd.getIds());
272291
String name = cmd.getName();
273292
String keyword = cmd.getKeyword();
274293

@@ -363,24 +382,18 @@ protected Map<Long,List<VmStatsVO>> searchForVmMetricsStatsInternal(Date startDa
363382
* @return the list of VMs.
364383
*/
365384
protected Pair<List<VolumeVO>, Integer> searchForVolumesInternal(ListVolumesUsageHistoryCmd cmd) {
366-
final Long id = cmd.getId();
367-
Account caller = CallContext.current().getCallingAccount();
368-
List<Long> permittedAccounts = new ArrayList<>();
369-
Account.Type callerType = caller.getType();
370-
boolean recursive = AccountTypesWithRecursiveUsageAccess.contains(callerType);
371-
Ternary<Long, Boolean, Project.ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<>(null, recursive, null);
385+
List<Long> ids = getIdsListFromCmd(cmd.getId(), cmd.getIds());
372386

373-
// Allow users to also list metrics of resources owned by projects they belong to (-1L), and admins to list all
374-
// metrics belonging to their domains recursively (null)
375-
Long projectId = callerType == Account.Type.NORMAL ? -1L : null;
387+
boolean isIdProvided = CollectionUtils.isNotEmpty(ids);
388+
List<Long> permittedAccounts = new ArrayList<>();
389+
Ternary<Long, Boolean, Project.ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<>(null, null, null);
390+
buildBaseACLSearchParametersForMetrics(isIdProvided, permittedAccounts, domainIdRecursiveListProject);
376391

377-
accountMgr.buildACLSearchParameters(caller, id, null, projectId, permittedAccounts, domainIdRecursiveListProject, true, false);
378392
Long domainId = domainIdRecursiveListProject.first();
379393
Boolean isRecursive = domainIdRecursiveListProject.second();
380394
Project.ListProjectResourcesCriteria listProjectResourcesCriteria = domainIdRecursiveListProject.third();
381395

382396
Filter searchFilter = new Filter(VolumeVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal());
383-
List<Long> ids = getIdsListFromCmd(cmd.getId(), cmd.getIds());
384397
String name = cmd.getName();
385398
String keyword = cmd.getKeyword();
386399

0 commit comments

Comments
 (0)