Skip to content

Commit baa8610

Browse files
committed
CLOUDSTACK-9351: Add ids parameter to resource listing API calls
1 parent 6d0c92b commit baa8610

10 files changed

Lines changed: 225 additions & 9 deletions

File tree

api/src/org/apache/cloudstack/api/ApiConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,7 @@ public class ApiConstants {
551551
public static final String VM_SNAPSHOT_DESCRIPTION = "description";
552552
public static final String VM_SNAPSHOT_DISPLAYNAME = "name";
553553
public static final String VM_SNAPSHOT_ID = "vmsnapshotid";
554+
public static final String VM_SNAPSHOT_IDS = "vmsnapshotids";
554555
public static final String VM_SNAPSHOT_DISK_IDS = "vmsnapshotdiskids";
555556
public static final String VM_SNAPSHOT_MEMORY = "snapshotmemory";
556557
public static final String VM_SNAPSHOT_QUIESCEVM = "quiescevm";

api/src/org/apache/cloudstack/api/command/user/snapshot/ListSnapshotsCmd.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ public class ListSnapshotsCmd extends BaseListTaggedResourcesCmd {
4848
@Parameter(name = ApiConstants.ID, type = CommandType.UUID, entityType = SnapshotResponse.class, description = "lists snapshot by snapshot ID")
4949
private Long id;
5050

51+
@Parameter(name=ApiConstants.IDS, type=CommandType.LIST, collectionType=CommandType.UUID, entityType=SnapshotResponse.class, description="the IDs of the snapshots, mutually exclusive with id", since = "4.9")
52+
private List<Long> ids;
53+
5154
@Parameter(name = ApiConstants.INTERVAL_TYPE, type = CommandType.STRING, description = "valid values are HOURLY, DAILY, WEEKLY, and MONTHLY.")
5255
private String intervalType;
5356

@@ -120,4 +123,8 @@ public void execute() {
120123

121124
setResponseObject(response);
122125
}
126+
127+
public List<Long> getIds() {
128+
return ids;
129+
}
123130
}

api/src/org/apache/cloudstack/api/command/user/template/ListTemplatesCmd.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import org.apache.log4j.Logger;
2020

21+
import java.util.List;
2122
import org.apache.cloudstack.api.APICommand;
2223
import org.apache.cloudstack.api.ApiCommandJobType;
2324
import org.apache.cloudstack.api.ApiConstants;
@@ -50,6 +51,9 @@ public class ListTemplatesCmd extends BaseListTaggedResourcesCmd {
5051
@Parameter(name = ApiConstants.ID, type = CommandType.UUID, entityType = TemplateResponse.class, description = "the template ID")
5152
private Long id;
5253

54+
@Parameter(name=ApiConstants.IDS, type=CommandType.LIST, collectionType=CommandType.UUID, entityType=TemplateResponse.class, description="the IDs of the templates, mutually exclusive with id", since = "4.9")
55+
private List<Long> ids;
56+
5357
@Parameter(name = ApiConstants.NAME, type = CommandType.STRING, description = "the template name")
5458
private String templateName;
5559

@@ -132,4 +136,8 @@ public void execute() {
132136
response.setResponseName(getCommandName());
133137
setResponseObject(response);
134138
}
139+
140+
public List<Long> getIds() {
141+
return ids;
142+
}
135143
}

api/src/org/apache/cloudstack/api/command/user/vmsnapshot/ListVMSnapshotCmd.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ public class ListVMSnapshotCmd extends BaseListTaggedResourcesCmd {
3939
@Parameter(name = ApiConstants.VM_SNAPSHOT_ID, type = CommandType.UUID, entityType = VMSnapshotResponse.class, description = "The ID of the VM snapshot")
4040
private Long id;
4141

42+
@Parameter(name=ApiConstants.VM_SNAPSHOT_IDS, type=CommandType.LIST, collectionType=CommandType.UUID, entityType=VMSnapshotResponse.class, description="the IDs of the vm snapshots, mutually exclusive with vmsnapshotid", since = "4.9")
43+
private List<Long> ids;
44+
4245
@Parameter(name = ApiConstants.STATE, type = CommandType.STRING, description = "state of the virtual machine snapshot")
4346
private String state;
4447

@@ -84,4 +87,8 @@ public String getCommandName() {
8487
return s_name;
8588
}
8689

90+
public List<Long> getIds() {
91+
return ids;
92+
}
93+
8794
}

api/src/org/apache/cloudstack/api/command/user/volume/ListVolumesCmd.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import org.apache.log4j.Logger;
2020

21+
import java.util.List;
2122
import org.apache.cloudstack.acl.RoleType;
2223
import org.apache.cloudstack.api.APICommand;
2324
import org.apache.cloudstack.api.ApiCommandJobType;
@@ -53,6 +54,9 @@ public class ListVolumesCmd extends BaseListTaggedResourcesCmd {
5354
@Parameter(name = ApiConstants.ID, type = CommandType.UUID, entityType = VolumeResponse.class, description = "the ID of the disk volume")
5455
private Long id;
5556

57+
@Parameter(name=ApiConstants.IDS, type=CommandType.LIST, collectionType=CommandType.UUID, entityType=VolumeResponse.class, description="the IDs of the volumes, mutually exclusive with id", since = "4.9")
58+
private List<Long> ids;
59+
5660
@Parameter(name = ApiConstants.NAME, type = CommandType.STRING, description = "the name of the disk volume")
5761
private String volumeName;
5862

@@ -153,4 +157,8 @@ public void execute() {
153157
response.setResponseName(getCommandName());
154158
setResponseObject(response);
155159
}
160+
161+
public List<Long> getIds() {
162+
return ids;
163+
}
156164
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//
2+
// Licensed to the Apache Software Foundation (ASF) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The ASF licenses this file
6+
// to you under the Apache License, Version 2.0 (the
7+
// "License"); you may not use this file except in compliance
8+
// with the License. You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing,
13+
// software distributed under the License is distributed on an
14+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
// KIND, either express or implied. See the License for the
16+
// specific language governing permissions and limitations
17+
// under the License.
18+
//
19+
package com.cloud.api.query;
20+
21+
import java.util.ArrayList;
22+
import java.util.List;
23+
24+
import com.cloud.exception.InvalidParameterValueException;
25+
import com.cloud.utils.component.ManagerBase;
26+
import com.cloud.utils.db.SearchCriteria;
27+
28+
public class MutualExclusiveIdsManagerBase extends ManagerBase {
29+
30+
/***
31+
* Include ids list in query criteria if ids is not null
32+
* @param sc search criteria, class type SearchCriteria<Z>
33+
* @param ids ids list, class type List<T>
34+
*/
35+
protected <Z,T> void setIdsListToSearchCriteria(SearchCriteria<Z> sc, List<T> ids){
36+
if (ids != null && !ids.isEmpty()) {
37+
sc.setParameters("idIN", ids.toArray());
38+
}
39+
}
40+
41+
/***
42+
* Mutually exclusive parameters id and ids for API calls.<br/>
43+
* Retrieve a list of ids or a list containing id depending on which of them is not null, or null if both are null
44+
* @param id entity id, class type T
45+
* @param ids entities ids, class type List<T>
46+
* @return if id is not null return a list containing id else return ids, if both parameters are null -> return null
47+
* @throws InvalidParameterValueException - if id and ids are both not null
48+
*/
49+
protected <T> List<T> getIdsListFromCmd(T id, List<T> ids){
50+
List<T> idsList = null;
51+
if (id != null) {
52+
if (ids != null && !ids.isEmpty()) {
53+
throw new InvalidParameterValueException("Specify either id or ids but not both parameters");
54+
}
55+
idsList = new ArrayList<T>();
56+
idsList.add(id);
57+
} else {
58+
idsList = ids;
59+
}
60+
return idsList;
61+
}
62+
}

server/src/com/cloud/api/query/QueryManagerImpl.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,6 @@
205205
import com.cloud.utils.Pair;
206206
import com.cloud.utils.StringUtils;
207207
import com.cloud.utils.Ternary;
208-
import com.cloud.utils.component.ManagerBase;
209208
import com.cloud.utils.db.Filter;
210209
import com.cloud.utils.db.JoinBuilder;
211210
import com.cloud.utils.db.SearchBuilder;
@@ -222,7 +221,7 @@
222221
import com.cloud.vm.dao.VMInstanceDao;
223222

224223
@Component
225-
public class QueryManagerImpl extends ManagerBase implements QueryService, Configurable {
224+
public class QueryManagerImpl extends MutualExclusiveIdsManagerBase implements QueryService, Configurable {
226225

227226
public static final Logger s_logger = Logger.getLogger(QueryManagerImpl.class);
228227

@@ -1731,6 +1730,8 @@ private Pair<List<VolumeJoinVO>, Integer> searchForVolumesInternal(ListVolumesCm
17311730
Long zoneId = cmd.getZoneId();
17321731
Long podId = cmd.getPodId();
17331732

1733+
List<Long> ids = getIdsListFromCmd(cmd.getId(), cmd.getIds());
1734+
17341735
Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<Long, Boolean, ListProjectResourcesCriteria>(
17351736
cmd.getDomainId(), cmd.isRecursive(), null);
17361737
_accountMgr.buildACLSearchParameters(caller, id, cmd.getAccountName(), cmd.getProjectId(), permittedAccounts,
@@ -1754,6 +1755,7 @@ private Pair<List<VolumeJoinVO>, Integer> searchForVolumesInternal(ListVolumesCm
17541755

17551756
sb.and("name", sb.entity().getName(), SearchCriteria.Op.EQ);
17561757
sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
1758+
sb.and("idIN", sb.entity().getId(), SearchCriteria.Op.IN);
17571759
sb.and("volumeType", sb.entity().getVolumeType(), SearchCriteria.Op.LIKE);
17581760
sb.and("instanceId", sb.entity().getVmId(), SearchCriteria.Op.EQ);
17591761
sb.and("dataCenterId", sb.entity().getDataCenterId(), SearchCriteria.Op.EQ);
@@ -1790,6 +1792,8 @@ private Pair<List<VolumeJoinVO>, Integer> searchForVolumesInternal(ListVolumesCm
17901792
sc.setParameters("display", display);
17911793
}
17921794

1795+
setIdsListToSearchCriteria(sc, ids);
1796+
17931797
sc.setParameters("systemUse", 1);
17941798

17951799
if (tags != null && !tags.isEmpty()) {
@@ -3077,14 +3081,15 @@ private Pair<List<TemplateJoinVO>, Integer> searchForTemplatesInternal(ListTempl
30773081

30783082
return searchForTemplatesInternal(id, cmd.getTemplateName(), cmd.getKeyword(), templateFilter, false, null,
30793083
cmd.getPageSizeVal(), cmd.getStartIndex(), cmd.getZoneId(), hypervisorType, showDomr,
3080-
cmd.listInReadyState(), permittedAccounts, caller, listProjectResourcesCriteria, tags, showRemovedTmpl);
3084+
cmd.listInReadyState(), permittedAccounts, caller, listProjectResourcesCriteria, tags, showRemovedTmpl,
3085+
cmd.getIds());
30813086
}
30823087

30833088
private Pair<List<TemplateJoinVO>, Integer> searchForTemplatesInternal(Long templateId, String name,
30843089
String keyword, TemplateFilter templateFilter, boolean isIso, Boolean bootable, Long pageSize,
30853090
Long startIndex, Long zoneId, HypervisorType hyperType, boolean showDomr, boolean onlyReady,
30863091
List<Account> permittedAccounts, Account caller, ListProjectResourcesCriteria listProjectResourcesCriteria,
3087-
Map<String, String> tags, boolean showRemovedTmpl) {
3092+
Map<String, String> tags, boolean showRemovedTmpl, List<Long> ids) {
30883093

30893094
// check if zone is configured, if not, just return empty list
30903095
List<HypervisorType> hypers = null;
@@ -3104,6 +3109,9 @@ private Pair<List<TemplateJoinVO>, Integer> searchForTemplatesInternal(Long temp
31043109

31053110
SearchBuilder<TemplateJoinVO> sb = _templateJoinDao.createSearchBuilder();
31063111
sb.select(null, Func.DISTINCT, sb.entity().getTempZonePair()); // select distinct (templateId, zoneId) pair
3112+
if (ids != null && !ids.isEmpty()){
3113+
sb.and("idIN", sb.entity().getId(), SearchCriteria.Op.IN);
3114+
}
31073115
SearchCriteria<TemplateJoinVO> sc = sb.create();
31083116

31093117
// verify templateId parameter and specially handle it
@@ -3149,6 +3157,8 @@ private Pair<List<TemplateJoinVO>, Integer> searchForTemplatesInternal(Long temp
31493157
// hypers = _resourceMgr.listAvailHypervisorInZone(null, null);
31503158
// }
31513159

3160+
setIdsListToSearchCriteria(sc, ids);
3161+
31523162
// add criteria for project or not
31533163
if (listProjectResourcesCriteria == ListProjectResourcesCriteria.SkipProjectResources) {
31543164
sc.addAnd("accountType", SearchCriteria.Op.NEQ, Account.ACCOUNT_TYPE_PROJECT);
@@ -3386,7 +3396,8 @@ private Pair<List<TemplateJoinVO>, Integer> searchForIsosInternal(ListIsosCmd cm
33863396

33873397
return searchForTemplatesInternal(cmd.getId(), cmd.getIsoName(), cmd.getKeyword(), isoFilter, true,
33883398
cmd.isBootable(), cmd.getPageSizeVal(), cmd.getStartIndex(), cmd.getZoneId(), hypervisorType, true,
3389-
cmd.listInReadyState(), permittedAccounts, caller, listProjectResourcesCriteria, tags, showRemovedISO);
3399+
cmd.listInReadyState(), permittedAccounts, caller, listProjectResourcesCriteria, tags, showRemovedISO,
3400+
null);
33903401
}
33913402

33923403
@Override

server/src/com/cloud/storage/snapshot/SnapshotManagerImpl.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import com.cloud.agent.api.DeleteSnapshotsDirCommand;
5959
import com.cloud.alert.AlertManager;
6060
import com.cloud.api.commands.ListRecurringSnapshotScheduleCmd;
61+
import com.cloud.api.query.MutualExclusiveIdsManagerBase;
6162
import com.cloud.configuration.Config;
6263
import com.cloud.configuration.Resource.ResourceType;
6364
import com.cloud.dc.ClusterVO;
@@ -112,7 +113,6 @@
112113
import com.cloud.utils.NumbersUtil;
113114
import com.cloud.utils.Pair;
114115
import com.cloud.utils.Ternary;
115-
import com.cloud.utils.component.ManagerBase;
116116
import com.cloud.utils.db.DB;
117117
import com.cloud.utils.db.Filter;
118118
import com.cloud.utils.db.JoinBuilder;
@@ -129,7 +129,7 @@
129129
import com.cloud.vm.snapshot.dao.VMSnapshotDao;
130130

131131
@Component
132-
public class SnapshotManagerImpl extends ManagerBase implements SnapshotManager, SnapshotApiService {
132+
public class SnapshotManagerImpl extends MutualExclusiveIdsManagerBase implements SnapshotManager, SnapshotApiService {
133133
private static final Logger s_logger = Logger.getLogger(SnapshotManagerImpl.class);
134134
@Inject
135135
VMTemplateDao _templateDao;
@@ -512,6 +512,8 @@ public Pair<List<? extends Snapshot>, Integer> listSnapshots(ListSnapshotsCmd cm
512512
}
513513
}
514514

515+
List<Long> ids = getIdsListFromCmd(cmd.getId(), cmd.getIds());
516+
515517
Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<Long, Boolean, ListProjectResourcesCriteria>(cmd.getDomainId(), cmd.isRecursive(), null);
516518
_accountMgr.buildACLSearchParameters(caller, id, cmd.getAccountName(), cmd.getProjectId(), permittedAccounts, domainIdRecursiveListProject, cmd.listAll(), false);
517519
Long domainId = domainIdRecursiveListProject.first();
@@ -526,6 +528,7 @@ public Pair<List<? extends Snapshot>, Integer> listSnapshots(ListSnapshotsCmd cm
526528
sb.and("volumeId", sb.entity().getVolumeId(), SearchCriteria.Op.EQ);
527529
sb.and("name", sb.entity().getName(), SearchCriteria.Op.LIKE);
528530
sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
531+
sb.and("idIN", sb.entity().getId(), SearchCriteria.Op.IN);
529532
sb.and("snapshotTypeEQ", sb.entity().getsnapshotType(), SearchCriteria.Op.IN);
530533
sb.and("snapshotTypeNEQ", sb.entity().getsnapshotType(), SearchCriteria.Op.NEQ);
531534
sb.and("dataCenterId", sb.entity().getDataCenterId(), SearchCriteria.Op.EQ);
@@ -565,6 +568,8 @@ public Pair<List<? extends Snapshot>, Integer> listSnapshots(ListSnapshotsCmd cm
565568
sc.setParameters("dataCenterId", zoneId);
566569
}
567570

571+
setIdsListToSearchCriteria(sc, ids);
572+
568573
if (name != null) {
569574
sc.setParameters("name", "%" + name + "%");
570575
}

server/src/com/cloud/vm/snapshot/VMSnapshotManagerImpl.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import org.apache.cloudstack.jobs.JobInfo;
4747
import org.apache.cloudstack.utils.identity.ManagementServerNode;
4848

49+
import com.cloud.api.query.MutualExclusiveIdsManagerBase;
4950
import com.cloud.event.ActionEvent;
5051
import com.cloud.event.EventTypes;
5152
import com.cloud.exception.ConcurrentOperationException;
@@ -77,7 +78,6 @@
7778
import com.cloud.utils.Predicate;
7879
import com.cloud.utils.ReflectionUse;
7980
import com.cloud.utils.Ternary;
80-
import com.cloud.utils.component.ManagerBase;
8181
import com.cloud.utils.db.EntityManager;
8282
import com.cloud.utils.db.Filter;
8383
import com.cloud.utils.db.SearchBuilder;
@@ -99,7 +99,7 @@
9999
import com.cloud.vm.snapshot.dao.VMSnapshotDao;
100100

101101
@Component
102-
public class VMSnapshotManagerImpl extends ManagerBase implements VMSnapshotManager, VMSnapshotService, VmWorkJobHandler {
102+
public class VMSnapshotManagerImpl extends MutualExclusiveIdsManagerBase implements VMSnapshotManager, VMSnapshotService, VmWorkJobHandler {
103103
private static final Logger s_logger = Logger.getLogger(VMSnapshotManagerImpl.class);
104104

105105
public static final String VM_WORK_JOB_HANDLER = VMSnapshotManagerImpl.class.getSimpleName();
@@ -176,6 +176,8 @@ public List<VMSnapshotVO> listVMSnapshots(ListVMSnapshotCmd cmd) {
176176
String name = cmd.getVmSnapshotName();
177177
String accountName = cmd.getAccountName();
178178

179+
List<Long> ids = getIdsListFromCmd(cmd.getId(), cmd.getIds());
180+
179181
Ternary<Long, Boolean, ListProjectResourcesCriteria> domainIdRecursiveListProject = new Ternary<Long, Boolean, ListProjectResourcesCriteria>(
180182
cmd.getDomainId(), cmd.isRecursive(), null);
181183
_accountMgr.buildACLSearchParameters(caller, id, cmd.getAccountName(), cmd.getProjectId(), permittedAccounts, domainIdRecursiveListProject, listAll,
@@ -193,6 +195,7 @@ public List<VMSnapshotVO> listVMSnapshots(ListVMSnapshotCmd cmd) {
193195
sb.and("status", sb.entity().getState(), SearchCriteria.Op.IN);
194196
sb.and("state", sb.entity().getState(), SearchCriteria.Op.EQ);
195197
sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
198+
sb.and("idIN", sb.entity().getId(), SearchCriteria.Op.IN);
196199
sb.and("display_name", sb.entity().getDisplayName(), SearchCriteria.Op.EQ);
197200
sb.and("account_id", sb.entity().getAccountId(), SearchCriteria.Op.EQ);
198201
sb.done();
@@ -209,6 +212,8 @@ public List<VMSnapshotVO> listVMSnapshots(ListVMSnapshotCmd cmd) {
209212
sc.setParameters("vm_id", vmId);
210213
}
211214

215+
setIdsListToSearchCriteria(sc, ids);
216+
212217
if (domainId != null) {
213218
sc.setParameters("domain_id", domainId);
214219
}

0 commit comments

Comments
 (0)