Skip to content

Commit d84d62a

Browse files
committed
Separate Admin and User command and response for ListVMsCmd.
1 parent bcd4cdd commit d84d62a

14 files changed

Lines changed: 174 additions & 64 deletions

File tree

‎api/src/org/apache/cloudstack/api/APICommand.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import java.lang.annotation.RetentionPolicy;
2323
import java.lang.annotation.Target;
2424

25+
import org.apache.cloudstack.api.ResponseObject.ResponseView;
26+
2527
@Retention(RetentionPolicy.RUNTIME)
2628
@Target({ TYPE })
2729
public @interface APICommand {
@@ -36,4 +38,6 @@
3638
boolean includeInApiDoc() default true;
3739

3840
String since() default "";
41+
42+
ResponseView responseView() default ResponseView.Admin;
3943
}

‎api/src/org/apache/cloudstack/api/ResponseGenerator.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.apache.cloudstack.affinity.AffinityGroupResponse;
2929
import org.apache.cloudstack.api.ApiConstants.HostDetails;
3030
import org.apache.cloudstack.api.ApiConstants.VMDetails;
31+
import org.apache.cloudstack.api.ResponseObject.ResponseView;
3132
import org.apache.cloudstack.api.command.user.job.QueryAsyncJobResultCmd;
3233
import org.apache.cloudstack.api.response.AccountResponse;
3334
import org.apache.cloudstack.api.response.AclGroupResponse;
@@ -219,6 +220,10 @@ public interface ResponseGenerator {
219220

220221
List<UserVmResponse> createUserVmResponse(String objectName, EnumSet<VMDetails> details, UserVm... userVms);
221222

223+
List<UserVmResponse> createUserVmResponse(ResponseView view, String objectName, UserVm... userVms);
224+
225+
List<UserVmResponse> createUserVmResponse(ResponseView view, String objectName, EnumSet<VMDetails> details, UserVm... userVms);
226+
222227
SystemVmResponse createSystemVmResponse(VirtualMachine systemVM);
223228

224229
DomainRouterResponse createDomainRouterResponse(VirtualRouter router);

‎api/src/org/apache/cloudstack/api/ResponseObject.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,9 @@ public interface ResponseObject {
7676
* @param jobStatus
7777
*/
7878
void setJobStatus(Integer jobStatus);
79+
80+
public enum ResponseView {
81+
Admin,
82+
User
83+
}
7984
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package org.apache.cloudstack.api.command.admin.vm;
18+
19+
import org.apache.log4j.Logger;
20+
21+
import org.apache.cloudstack.api.APICommand;
22+
import org.apache.cloudstack.api.ApiConstants;
23+
import org.apache.cloudstack.api.Parameter;
24+
import org.apache.cloudstack.api.ResponseObject.ResponseView;
25+
import org.apache.cloudstack.api.command.user.vm.ListVMsCmd;
26+
import org.apache.cloudstack.api.response.HostResponse;
27+
import org.apache.cloudstack.api.response.PodResponse;
28+
import org.apache.cloudstack.api.response.StoragePoolResponse;
29+
import org.apache.cloudstack.api.response.UserVmResponse;
30+
31+
32+
@APICommand(name = "listVirtualMachines", description = "List the virtual machines owned by the account.", responseObject = UserVmResponse.class, responseView = ResponseView.Admin)
33+
public class ListVMsCmdByAdmin extends ListVMsCmd {
34+
public static final Logger s_logger = Logger.getLogger(ListVMsCmdByAdmin.class.getName());
35+
36+
/////////////////////////////////////////////////////
37+
//////////////// API parameters /////////////////////
38+
/////////////////////////////////////////////////////
39+
40+
@Parameter(name=ApiConstants.HOST_ID, type=CommandType.UUID, entityType=HostResponse.class,
41+
description="the host ID")
42+
private Long hostId;
43+
44+
@Parameter(name=ApiConstants.POD_ID, type=CommandType.UUID, entityType=PodResponse.class,
45+
description="the pod ID")
46+
private Long podId;
47+
48+
@Parameter(name=ApiConstants.STORAGE_ID, type=CommandType.UUID, entityType=StoragePoolResponse.class,
49+
description="the storage ID where vm's volumes belong to")
50+
private Long storageId;
51+
52+
53+
/////////////////////////////////////////////////////
54+
/////////////////// Accessors ///////////////////////
55+
/////////////////////////////////////////////////////
56+
57+
public Long getHostId() {
58+
return hostId;
59+
}
60+
61+
public Long getPodId() {
62+
return podId;
63+
}
64+
65+
public Long getStorageId() {
66+
return storageId;
67+
}
68+
69+
}

‎api/src/org/apache/cloudstack/api/command/user/vm/ListVMsCmd.java‎

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,29 @@
2020
import java.util.EnumSet;
2121
import java.util.List;
2222

23+
import org.apache.log4j.Logger;
24+
2325
import org.apache.cloudstack.affinity.AffinityGroupResponse;
2426
import org.apache.cloudstack.api.APICommand;
2527
import org.apache.cloudstack.api.ApiCommandJobType;
2628
import org.apache.cloudstack.api.ApiConstants;
2729
import org.apache.cloudstack.api.ApiConstants.VMDetails;
2830
import org.apache.cloudstack.api.BaseListTaggedResourcesCmd;
2931
import org.apache.cloudstack.api.Parameter;
30-
import org.apache.cloudstack.api.response.HostResponse;
32+
import org.apache.cloudstack.api.ResponseObject.ResponseView;
3133
import org.apache.cloudstack.api.response.InstanceGroupResponse;
3234
import org.apache.cloudstack.api.response.IsoVmResponse;
3335
import org.apache.cloudstack.api.response.ListResponse;
3436
import org.apache.cloudstack.api.response.NetworkResponse;
35-
import org.apache.cloudstack.api.response.PodResponse;
36-
import org.apache.cloudstack.api.response.StoragePoolResponse;
3737
import org.apache.cloudstack.api.response.TemplateResponse;
3838
import org.apache.cloudstack.api.response.UserVmResponse;
3939
import org.apache.cloudstack.api.response.VpcResponse;
4040
import org.apache.cloudstack.api.response.ZoneResponse;
41-
import org.apache.log4j.Logger;
4241

4342
import com.cloud.exception.InvalidParameterValueException;
4443

4544

46-
@APICommand(name = "listVirtualMachines", description="List the virtual machines owned by the account.", responseObject=UserVmResponse.class)
45+
@APICommand(name = "listVirtualMachines", description = "List the virtual machines owned by the account.", responseObject = UserVmResponse.class, responseView = ResponseView.User)
4746
public class ListVMsCmd extends BaseListTaggedResourcesCmd {
4847
public static final Logger s_logger = Logger.getLogger(ListVMsCmd.class.getName());
4948

@@ -57,21 +56,13 @@ public class ListVMsCmd extends BaseListTaggedResourcesCmd {
5756
description="the group ID")
5857
private Long groupId;
5958

60-
@Parameter(name=ApiConstants.HOST_ID, type=CommandType.UUID, entityType=HostResponse.class,
61-
description="the host ID")
62-
private Long hostId;
63-
6459
@Parameter(name=ApiConstants.ID, type=CommandType.UUID, entityType=UserVmResponse.class,
6560
description="the ID of the virtual machine")
6661
private Long id;
6762

6863
@Parameter(name=ApiConstants.NAME, type=CommandType.STRING, description="name of the virtual machine")
6964
private String name;
7065

71-
@Parameter(name=ApiConstants.POD_ID, type=CommandType.UUID, entityType=PodResponse.class,
72-
description="the pod ID")
73-
private Long podId;
74-
7566
@Parameter(name=ApiConstants.STATE, type=CommandType.STRING, description="state of the virtual machine")
7667
private String state;
7768

@@ -89,10 +80,6 @@ public class ListVMsCmd extends BaseListTaggedResourcesCmd {
8980
@Parameter(name=ApiConstants.HYPERVISOR, type=CommandType.STRING, description="the target hypervisor for the template")
9081
private String hypervisor;
9182

92-
@Parameter(name=ApiConstants.STORAGE_ID, type=CommandType.UUID, entityType=StoragePoolResponse.class,
93-
description="the storage ID where vm's volumes belong to")
94-
private Long storageId;
95-
9683
@Parameter(name=ApiConstants.DETAILS, type=CommandType.LIST, collectionType=CommandType.STRING,
9784
description="comma separated list of host details requested, " +
9885
"value can be a list of [all, group, nics, stats, secgrp, tmpl, servoff, iso, volume, min, affgrp]." +
@@ -122,10 +109,6 @@ public Long getGroupId() {
122109
return groupId;
123110
}
124111

125-
public Long getHostId() {
126-
return hostId;
127-
}
128-
129112
public Long getId() {
130113
return id;
131114
}
@@ -134,10 +117,6 @@ public String getName() {
134117
return name;
135118
}
136119

137-
public Long getPodId() {
138-
return podId;
139-
}
140-
141120
public String getState() {
142121
return state;
143122
}
@@ -162,9 +141,6 @@ public String getHypervisor() {
162141
return hypervisor;
163142
}
164143

165-
public Long getStorageId() {
166-
return storageId;
167-
}
168144

169145
public Long getTemplateId() {
170146
return templateId;
@@ -219,6 +195,6 @@ public ApiCommandJobType getInstanceType() {
219195
public void execute(){
220196
ListResponse<UserVmResponse> response = _queryService.searchForUserVMs(this);
221197
response.setResponseName(getCommandName());
222-
this.setResponseObject(response);
198+
setResponseObject(response);
223199
}
224200
}

‎server/src/com/cloud/api/ApiDBUtils.java‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.apache.cloudstack.api.ApiCommandJobType;
3434
import org.apache.cloudstack.api.ApiConstants.HostDetails;
3535
import org.apache.cloudstack.api.ApiConstants.VMDetails;
36+
import org.apache.cloudstack.api.ResponseObject.ResponseView;
3637
import org.apache.cloudstack.api.response.AccountResponse;
3738
import org.apache.cloudstack.api.response.AclGroupResponse;
3839
import org.apache.cloudstack.api.response.AclRoleResponse;
@@ -1423,12 +1424,12 @@ public static List<DomainRouterJoinVO> newDomainRouterView(VirtualRouter vr){
14231424
return _domainRouterJoinDao.newDomainRouterView(vr);
14241425
}
14251426

1426-
public static UserVmResponse newUserVmResponse(String objectName, UserVmJoinVO userVm, EnumSet<VMDetails> details, Account caller) {
1427-
return _userVmJoinDao.newUserVmResponse(objectName, userVm, details, caller);
1427+
public static UserVmResponse newUserVmResponse(ResponseView view, String objectName, UserVmJoinVO userVm, EnumSet<VMDetails> details, Account caller) {
1428+
return _userVmJoinDao.newUserVmResponse(view, objectName, userVm, details, caller);
14281429
}
14291430

1430-
public static UserVmResponse fillVmDetails(UserVmResponse vmData, UserVmJoinVO vm){
1431-
return _userVmJoinDao.setUserVmResponse(vmData, vm);
1431+
public static UserVmResponse fillVmDetails(ResponseView view, UserVmResponse vmData, UserVmJoinVO vm) {
1432+
return _userVmJoinDao.setUserVmResponse(view, vmData, vm);
14321433
}
14331434

14341435
public static List<UserVmJoinVO> newUserVmView(UserVm... userVms){

‎server/src/com/cloud/api/ApiResponseHelper.java‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.apache.cloudstack.api.ApiConstants.HostDetails;
4343
import org.apache.cloudstack.api.ApiConstants.VMDetails;
4444
import org.apache.cloudstack.api.ResponseGenerator;
45+
import org.apache.cloudstack.api.ResponseObject.ResponseView;
4546
import org.apache.cloudstack.api.command.user.job.QueryAsyncJobResultCmd;
4647
import org.apache.cloudstack.api.response.AccountResponse;
4748
import org.apache.cloudstack.api.response.AclGroupResponse;
@@ -1084,17 +1085,27 @@ public IpForwardingRuleResponse createIpForwardingRuleResponse(StaticNatRule fwR
10841085
return response;
10851086
}
10861087

1088+
@Override
1089+
public List<UserVmResponse> createUserVmResponse(String objectName, UserVm... userVms) {
1090+
return createUserVmResponse(null, objectName, userVms);
1091+
}
1092+
10871093
@Override
10881094
public List<UserVmResponse> createUserVmResponse(String objectName, EnumSet<VMDetails> details, UserVm... userVms) {
1095+
return createUserVmResponse(null, objectName, userVms);
1096+
}
1097+
1098+
@Override
1099+
public List<UserVmResponse> createUserVmResponse(ResponseView view, String objectName, EnumSet<VMDetails> details, UserVm... userVms) {
10891100
List<UserVmJoinVO> viewVms = ApiDBUtils.newUserVmView(userVms);
1090-
return ViewResponseHelper.createUserVmResponse(objectName, details, viewVms.toArray(new UserVmJoinVO[viewVms.size()]));
1101+
return ViewResponseHelper.createUserVmResponse(view, objectName, details, viewVms.toArray(new UserVmJoinVO[viewVms.size()]));
10911102

10921103
}
10931104

10941105
@Override
1095-
public List<UserVmResponse> createUserVmResponse(String objectName, UserVm... userVms) {
1106+
public List<UserVmResponse> createUserVmResponse(ResponseView view, String objectName, UserVm... userVms) {
10961107
List<UserVmJoinVO> viewVms = ApiDBUtils.newUserVmView(userVms);
1097-
return ViewResponseHelper.createUserVmResponse(objectName, viewVms.toArray(new UserVmJoinVO[viewVms.size()]));
1108+
return ViewResponseHelper.createUserVmResponse(view, objectName, viewVms.toArray(new UserVmJoinVO[viewVms.size()]));
10981109
}
10991110

11001111
@Override

‎server/src/com/cloud/api/ApiServer.java‎

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
import org.apache.cloudstack.api.BaseCmd;
9292
import org.apache.cloudstack.api.BaseListCmd;
9393
import org.apache.cloudstack.api.ResponseObject;
94+
import org.apache.cloudstack.api.ResponseObject.ResponseView;
9495
import org.apache.cloudstack.api.ServerApiException;
9596
import org.apache.cloudstack.api.command.admin.host.ListHostsCmd;
9697
import org.apache.cloudstack.api.command.admin.router.ListRoutersCmd;
@@ -176,7 +177,7 @@ public class ApiServer extends ManagerBase implements HttpRequestHandler, ApiSer
176177
protected ApiAsyncJobDispatcher _asyncDispatcher;
177178
private static int _workerCount = 0;
178179
private static final DateFormat _dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
179-
private static Map<String, Class<?>> _apiNameCmdClassMap = new HashMap<String, Class<?>>();
180+
private static Map<String, List<Class<?>>> _apiNameCmdClassMap = new HashMap<String, List<Class<?>>>();
180181

181182
private static ExecutorService _executor = new ThreadPoolExecutor(10, 150, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), new NamedThreadFactory("ApiServer"));
182183

@@ -232,11 +233,12 @@ public void init() {
232233
throw new CloudRuntimeException(String.format("%s is claimed as a API command, but it doesn't have @APICommand annotation", cmdClass.getName()));
233234
}
234235
String apiName = at.name();
235-
if (_apiNameCmdClassMap.containsKey(apiName)) {
236-
s_logger.error("API Cmd class " + cmdClass.getName() + " has non-unique apiname" + apiName);
237-
continue;
236+
List<Class<?>> apiCmdList = _apiNameCmdClassMap.get(apiName);
237+
if (apiCmdList == null) {
238+
apiCmdList = new ArrayList<Class<?>>();
239+
_apiNameCmdClassMap.put(apiName, apiCmdList);
238240
}
239-
_apiNameCmdClassMap.put(apiName, cmdClass);
241+
apiCmdList.add(cmdClass);
240242
}
241243

242244
encodeApiResponse = Boolean.valueOf(_configDao.getValue(Config.EncodeApiResponse.key()));
@@ -882,7 +884,31 @@ private void checkCommandAvailable(User user, String commandName) throws Permiss
882884
}
883885

884886
private Class<?> getCmdClass(String cmdName) {
885-
return _apiNameCmdClassMap.get(cmdName);
887+
List<Class<?>> cmdList = _apiNameCmdClassMap.get(cmdName);
888+
if (cmdList == null || cmdList.size() == 0)
889+
return null;
890+
else if (cmdList.size() == 1)
891+
return cmdList.get(0);
892+
else {
893+
// determine the cmd class based on calling context
894+
ResponseView view = ResponseView.User;
895+
if (_accountMgr.isRootAdmin(CallContext.current().getCallingAccount().getId())) {
896+
view = ResponseView.Admin;
897+
}
898+
for (Class<?> cmdClass : cmdList) {
899+
APICommand at = cmdClass.getAnnotation(APICommand.class);
900+
if (at == null) {
901+
throw new CloudRuntimeException(String.format("%s is claimed as a API command, but it doesn't have @APICommand annotation", cmdClass.getName()));
902+
}
903+
if (at.responseView() == null) {
904+
throw new CloudRuntimeException(String.format(
905+
"%s @APICommand annotation should specify responseView attribute to distinguish multiple command classes for a single api name", cmdClass.getName()));
906+
} else if (at.responseView() == view) {
907+
return cmdClass;
908+
}
909+
}
910+
return null;
911+
}
886912
}
887913

888914
// FIXME: rather than isError, we might was to pass in the status code to give more flexibility

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,15 @@
4141
import org.apache.cloudstack.affinity.dao.AffinityGroupDomainMapDao;
4242
import org.apache.cloudstack.affinity.dao.AffinityGroupVMMapDao;
4343
import org.apache.cloudstack.api.BaseListProjectAndAccountResourcesCmd;
44+
import org.apache.cloudstack.api.ResponseObject.ResponseView;
4445
import org.apache.cloudstack.api.command.admin.host.ListHostsCmd;
4546
import org.apache.cloudstack.api.command.admin.internallb.ListInternalLBVMsCmd;
4647
import org.apache.cloudstack.api.command.admin.router.ListRoutersCmd;
4748
import org.apache.cloudstack.api.command.admin.storage.ListImageStoresCmd;
4849
import org.apache.cloudstack.api.command.admin.storage.ListSecondaryStagingStoresCmd;
4950
import org.apache.cloudstack.api.command.admin.storage.ListStoragePoolsCmd;
5051
import org.apache.cloudstack.api.command.admin.user.ListUsersCmd;
52+
import org.apache.cloudstack.api.command.admin.vm.ListVMsCmdByAdmin;
5153
import org.apache.cloudstack.api.command.user.account.ListAccountsCmd;
5254
import org.apache.cloudstack.api.command.user.account.ListProjectAccountsCmd;
5355
import org.apache.cloudstack.api.command.user.event.ListEventsCmd;
@@ -715,7 +717,11 @@ private Pair<List<InstanceGroupJoinVO>, Integer> searchForVmGroupsInternal(ListV
715717
public ListResponse<UserVmResponse> searchForUserVMs(ListVMsCmd cmd) {
716718
Pair<List<UserVmJoinVO>, Integer> result = searchForUserVMsInternal(cmd);
717719
ListResponse<UserVmResponse> response = new ListResponse<UserVmResponse>();
718-
List<UserVmResponse> vmResponses = ViewResponseHelper.createUserVmResponse("virtualmachine", cmd.getDetails(),
720+
ResponseView respView = ResponseView.User;
721+
if (cmd instanceof ListVMsCmdByAdmin) {
722+
respView = ResponseView.Admin;
723+
}
724+
List<UserVmResponse> vmResponses = ViewResponseHelper.createUserVmResponse(respView, "virtualmachine", cmd.getDetails(),
719725
result.first().toArray(new UserVmJoinVO[result.first().size()]));
720726
response.setResponses(vmResponses, result.second());
721727
return response;
@@ -764,10 +770,11 @@ private Pair<List<UserVmJoinVO>, Integer> searchForUserVMsInternal(ListVMsCmd cm
764770
}
765771

766772
// ignore these search requests if it's not an admin
767-
if (_accountMgr.isAdmin(caller.getType())) {
768-
c.addCriteria(Criteria.PODID, cmd.getPodId());
769-
c.addCriteria(Criteria.HOSTID, cmd.getHostId());
770-
c.addCriteria(Criteria.STORAGE_ID, cmd.getStorageId());
773+
if (cmd instanceof ListVMsCmdByAdmin) {
774+
ListVMsCmdByAdmin adCmd = (ListVMsCmdByAdmin)cmd;
775+
c.addCriteria(Criteria.PODID, adCmd.getPodId());
776+
c.addCriteria(Criteria.HOSTID, adCmd.getHostId());
777+
c.addCriteria(Criteria.STORAGE_ID, adCmd.getStorageId());
771778
}
772779

773780
if (!permittedAccounts.isEmpty()) {

0 commit comments

Comments
 (0)