Skip to content

Commit 4de66f9

Browse files
shwstppryadvrDaanHoogland
authored
server: fix listing vm metrics for infra resources (#6851)
Fixes #6786 listVirtualMachinesMetrics does not support some of the params that are supported by admin API call for listVirtualMachines. These parameters are used in UI. Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com> Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com> Co-authored-by: Rohit Yadav <rohityadav89@gmail.com> Co-authored-by: Daan Hoogland <daan@onecht.net>
1 parent 335e26b commit 4de66f9

File tree

4 files changed

+117
-29
lines changed

4 files changed

+117
-29
lines changed

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,18 @@
1717

1818
package org.apache.cloudstack.api;
1919

20+
import java.util.List;
21+
22+
import javax.inject.Inject;
23+
2024
import org.apache.cloudstack.acl.RoleType;
25+
import org.apache.cloudstack.api.command.user.UserCmd;
2126
import org.apache.cloudstack.api.command.user.vm.ListVMsCmd;
2227
import org.apache.cloudstack.api.response.ListResponse;
2328
import org.apache.cloudstack.api.response.UserVmResponse;
2429
import org.apache.cloudstack.metrics.MetricsService;
2530
import org.apache.cloudstack.response.VmMetricsResponse;
2631

27-
import javax.inject.Inject;
28-
import java.util.List;
29-
3032
/**
3133
* API supported for backward compatibility. Use the {@link ListVMsUsageHistoryCmd} API instead. <br>
3234
* The reasons for this are: <br>
@@ -42,9 +44,8 @@
4244
*/
4345
@APICommand(name = ListVMsMetricsCmd.APINAME, description = "Lists VM metrics", responseObject = VmMetricsResponse.class,
4446
requestHasSensitiveInfo = false, responseHasSensitiveInfo = false, responseView = ResponseObject.ResponseView.Full,
45-
since = "4.9.3", authorized = {RoleType.Admin, RoleType.ResourceAdmin, RoleType.DomainAdmin, RoleType.User})
46-
@Deprecated(since = "4.17.0")
47-
public class ListVMsMetricsCmd extends ListVMsCmd {
47+
since = "4.9.3", authorized = {RoleType.ResourceAdmin, RoleType.DomainAdmin, RoleType.User})
48+
public class ListVMsMetricsCmd extends ListVMsCmd implements UserCmd {
4849
public static final String APINAME = "listVirtualMachinesMetrics";
4950

5051
@Inject
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
18+
package org.apache.cloudstack.api;
19+
20+
import org.apache.cloudstack.acl.RoleType;
21+
import org.apache.cloudstack.api.command.admin.AdminCmd;
22+
import org.apache.cloudstack.api.response.ClusterResponse;
23+
import org.apache.cloudstack.api.response.HostResponse;
24+
import org.apache.cloudstack.api.response.PodResponse;
25+
import org.apache.cloudstack.api.response.StoragePoolResponse;
26+
import org.apache.cloudstack.response.VmMetricsResponse;
27+
28+
@APICommand(name = ListVMsMetricsCmd.APINAME, description = "Lists VM metrics", responseObject = VmMetricsResponse.class,
29+
requestHasSensitiveInfo = false, responseHasSensitiveInfo = false, responseView = ResponseObject.ResponseView.Full,
30+
authorized = {RoleType.Admin})
31+
public class ListVMsMetricsCmdByAdmin extends ListVMsMetricsCmd implements AdminCmd {
32+
33+
/////////////////////////////////////////////////////
34+
//////////////// API parameters /////////////////////
35+
/////////////////////////////////////////////////////
36+
37+
@Parameter(name=ApiConstants.HOST_ID, type= BaseCmd.CommandType.UUID, entityType= HostResponse.class,
38+
description="the host ID")
39+
private Long hostId;
40+
41+
@Parameter(name=ApiConstants.POD_ID, type= BaseCmd.CommandType.UUID, entityType= PodResponse.class,
42+
description="the pod ID")
43+
private Long podId;
44+
45+
@Parameter(name=ApiConstants.STORAGE_ID, type= BaseCmd.CommandType.UUID, entityType= StoragePoolResponse.class,
46+
description="the storage ID where vm's volumes belong to")
47+
private Long storageId;
48+
49+
@Parameter(name = ApiConstants.CLUSTER_ID, type = BaseCmd.CommandType.UUID, entityType = ClusterResponse.class,
50+
description = "the cluster ID")
51+
private Long clusterId;
52+
53+
54+
/////////////////////////////////////////////////////
55+
/////////////////// Accessors ///////////////////////
56+
/////////////////////////////////////////////////////
57+
58+
public Long getHostId() {
59+
return hostId;
60+
}
61+
62+
public Long getPodId() {
63+
return podId;
64+
}
65+
66+
public Long getStorageId() {
67+
return storageId;
68+
}
69+
70+
public Long getClusterId() {
71+
return clusterId;
72+
}
73+
}

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

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
package org.apache.cloudstack.metrics;
1919

20+
import static com.cloud.utils.NumbersUtil.toReadableSize;
21+
2022
import java.lang.reflect.InvocationTargetException;
2123
import java.text.DecimalFormat;
2224
import java.util.ArrayList;
@@ -28,14 +30,6 @@
2830

2931
import javax.inject.Inject;
3032

31-
import com.cloud.server.DbStatsCollection;
32-
import com.cloud.server.StatsCollector;
33-
import com.cloud.usage.UsageJobVO;
34-
import com.cloud.usage.dao.UsageJobDao;
35-
import com.cloud.utils.db.DbProperties;
36-
import com.cloud.utils.db.DbUtil;
37-
import com.cloud.utils.db.TransactionLegacy;
38-
import com.cloud.utils.script.Script;
3933
import org.apache.cloudstack.api.ApiErrorCode;
4034
import org.apache.cloudstack.api.ListClustersMetricsCmd;
4135
import org.apache.cloudstack.api.ListDbMetricsCmd;
@@ -45,15 +39,16 @@
4539
import org.apache.cloudstack.api.ListStoragePoolsMetricsCmd;
4640
import org.apache.cloudstack.api.ListUsageServerMetricsCmd;
4741
import org.apache.cloudstack.api.ListVMsMetricsCmd;
42+
import org.apache.cloudstack.api.ListVMsMetricsCmdByAdmin;
4843
import org.apache.cloudstack.api.ListVMsUsageHistoryCmd;
4944
import org.apache.cloudstack.api.ListVolumesMetricsCmd;
5045
import org.apache.cloudstack.api.ListZonesMetricsCmd;
5146
import org.apache.cloudstack.api.ServerApiException;
5247
import org.apache.cloudstack.api.response.ClusterResponse;
5348
import org.apache.cloudstack.api.response.HostResponse;
5449
import org.apache.cloudstack.api.response.ListResponse;
55-
import org.apache.cloudstack.api.response.StatsResponse;
5650
import org.apache.cloudstack.api.response.ManagementServerResponse;
51+
import org.apache.cloudstack.api.response.StatsResponse;
5752
import org.apache.cloudstack.api.response.StoragePoolResponse;
5853
import org.apache.cloudstack.api.response.UserVmResponse;
5954
import org.apache.cloudstack.api.response.VolumeResponse;
@@ -76,6 +71,10 @@
7671
import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao;
7772
import org.apache.commons.beanutils.BeanUtils;
7873
import org.apache.commons.collections.CollectionUtils;
74+
import org.apache.commons.lang3.StringUtils;
75+
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
76+
import org.apache.commons.lang3.builder.ToStringStyle;
77+
import org.apache.log4j.Logger;
7978

8079
import com.cloud.agent.api.VmStatsEntryBase;
8180
import com.cloud.alert.AlertManager;
@@ -103,13 +102,21 @@
103102
import com.cloud.org.Cluster;
104103
import com.cloud.org.Grouping;
105104
import com.cloud.org.Managed;
105+
import com.cloud.server.DbStatsCollection;
106106
import com.cloud.server.ManagementServerHostStats;
107+
import com.cloud.server.StatsCollector;
108+
import com.cloud.usage.UsageJobVO;
109+
import com.cloud.usage.dao.UsageJobDao;
107110
import com.cloud.user.Account;
108111
import com.cloud.user.AccountManager;
109112
import com.cloud.utils.Pair;
113+
import com.cloud.utils.db.DbProperties;
114+
import com.cloud.utils.db.DbUtil;
110115
import com.cloud.utils.db.Filter;
111116
import com.cloud.utils.db.SearchBuilder;
112117
import com.cloud.utils.db.SearchCriteria;
118+
import com.cloud.utils.db.TransactionLegacy;
119+
import com.cloud.utils.script.Script;
113120
import com.cloud.vm.UserVmVO;
114121
import com.cloud.vm.VMInstanceVO;
115122
import com.cloud.vm.VirtualMachine;
@@ -119,12 +126,6 @@
119126
import com.cloud.vm.dao.VMInstanceDao;
120127
import com.cloud.vm.dao.VmStatsDao;
121128
import com.google.gson.Gson;
122-
import org.apache.commons.lang3.StringUtils;
123-
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
124-
import org.apache.commons.lang3.builder.ToStringStyle;
125-
import org.apache.log4j.Logger;
126-
127-
import static com.cloud.utils.NumbersUtil.toReadableSize;
128129

129130
public class MetricsServiceImpl extends MutualExclusiveIdsManagerBase implements MetricsService {
130131
private static final Logger LOGGER = Logger.getLogger(MetricsServiceImpl.class);
@@ -878,6 +879,9 @@ public List<Class<?>> getCommands() {
878879
cmdList.add(ListVolumesMetricsCmd.class);
879880
cmdList.add(ListZonesMetricsCmd.class);
880881
cmdList.add(ListVMsUsageHistoryCmd.class);
882+
883+
// separate Admin commands
884+
cmdList.add(ListVMsMetricsCmdByAdmin.class);
881885
return cmdList;
882886
}
883887

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
// under the License.
1717
package com.cloud.api.query;
1818

19+
import java.lang.reflect.InvocationTargetException;
20+
import java.lang.reflect.Method;
1921
import java.util.ArrayList;
2022
import java.util.Arrays;
2123
import java.util.Collections;
@@ -65,7 +67,6 @@
6567
import org.apache.cloudstack.api.command.admin.storage.ListStorageTagsCmd;
6668
import org.apache.cloudstack.api.command.admin.template.ListTemplatesCmdByAdmin;
6769
import org.apache.cloudstack.api.command.admin.user.ListUsersCmd;
68-
import org.apache.cloudstack.api.command.admin.vm.ListVMsCmdByAdmin;
6970
import org.apache.cloudstack.api.command.admin.zone.ListZonesCmdByAdmin;
7071
import org.apache.cloudstack.api.command.user.account.ListAccountsCmd;
7172
import org.apache.cloudstack.api.command.user.account.ListProjectAccountsCmd;
@@ -971,6 +972,17 @@ public ListResponse<UserVmResponse> searchForUserVMs(ListVMsCmd cmd) {
971972
return response;
972973
}
973974

975+
private Object getObjectPossibleMethodValue(Object obj, String methodName) {
976+
Object result = null;
977+
978+
try {
979+
Method m = obj.getClass().getMethod(methodName);
980+
result = m.invoke(obj);
981+
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException ignored) {}
982+
983+
return result;
984+
}
985+
974986
private Pair<List<UserVmJoinVO>, Integer> searchForUserVMsInternal(ListVMsCmd cmd) {
975987
Account caller = CallContext.current().getCallingAccount();
976988
List<Long> permittedAccounts = new ArrayList<Long>();
@@ -1035,16 +1047,14 @@ private Pair<List<UserVmJoinVO>, Integer> searchForUserVMsInternal(ListVMsCmd cm
10351047
Object backupOfferingId = cmd.getBackupOfferingId();
10361048
Object isHaEnabled = cmd.getHaEnabled();
10371049
Object pod = null;
1038-
Long clusterId = null;
1050+
Object clusterId = null;
10391051
Object hostId = null;
10401052
Object storageId = null;
10411053
if (_accountMgr.isRootAdmin(caller.getId())) {
1042-
if (cmd instanceof ListVMsCmdByAdmin) {
1043-
pod = ((ListVMsCmdByAdmin) cmd).getPodId();
1044-
clusterId = ((ListVMsCmdByAdmin) cmd).getClusterId();
1045-
hostId = ((ListVMsCmdByAdmin) cmd).getHostId();
1046-
storageId = ((ListVMsCmdByAdmin) cmd).getStorageId();
1047-
}
1054+
pod = getObjectPossibleMethodValue(cmd, "getPodId");
1055+
clusterId = getObjectPossibleMethodValue(cmd, "getClusterId");
1056+
hostId = getObjectPossibleMethodValue(cmd, "getHostId");
1057+
storageId = getObjectPossibleMethodValue(cmd, "getStorageId");
10481058
}
10491059

10501060
sb.and("displayName", sb.entity().getDisplayName(), SearchCriteria.Op.LIKE);

0 commit comments

Comments
 (0)