Skip to content

Commit 259ba31

Browse files
committed
fix vms listing with tags, effectively tagged jobs
Signed-off-by: Abhishek Kumar <[email protected]>
1 parent b7f8fa3 commit 259ba31

5 files changed

Lines changed: 63 additions & 38 deletions

File tree

plugins/integrations/veeam-control-service/src/main/java/org/apache/cloudstack/veeam/adapter/ServerAdapter.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -973,12 +973,19 @@ public VnicProfile getVnicProfile(String uuid) {
973973
}
974974

975975
@ApiAccess(command = ListVMsCmd.class)
976-
public List<Vm> listAllInstances(Long offset, Long limit) {
976+
public List<Vm> listAllInstances(boolean includeTags, boolean includeDisks, boolean includeNics,
977+
boolean allContent, Long offset, Long limit) {
977978
Filter filter = new Filter(UserVmJoinVO.class, "id", true, offset, limit);
978979
Pair<List<Long>, String> ownerDetails = getResourceOwnerFilters();
979980
List<UserVmJoinVO> vms = userVmJoinDao.listByHypervisorTypeAndOwners(Hypervisor.HypervisorType.KVM,
980981
ownerDetails.first(), ownerDetails.second(), filter);
981-
return UserVmJoinVOToVmConverter.toVmList(vms, this::getHostById, this::getDetailsByInstanceId);
982+
return UserVmJoinVOToVmConverter.toVmList(vms,
983+
this::getHostById,
984+
this::getDetailsByInstanceId,
985+
includeTags ? this::listTagsByInstanceId : null,
986+
includeDisks ? this::listDiskAttachmentsByInstanceId : null,
987+
includeNics ? this::listNicsByInstance : null,
988+
allContent);
982989
}
983990

984991
@ApiAccess(command = ListVMsCmd.class)
@@ -988,7 +995,8 @@ public Vm getInstance(String uuid, boolean includeTags, boolean includeDisks, bo
988995
if (vo == null) {
989996
throw new InvalidParameterValueException("VM with ID " + uuid + " not found");
990997
}
991-
return UserVmJoinVOToVmConverter.toVm(vo, this::getHostById,
998+
return UserVmJoinVOToVmConverter.toVm(vo,
999+
this::getHostById,
9921000
this::getDetailsByInstanceId,
9931001
includeTags ? this::listTagsByInstanceId : null,
9941002
includeDisks ? this::listDiskAttachmentsByInstanceId : null,

plugins/integrations/veeam-control-service/src/main/java/org/apache/cloudstack/veeam/api/DataCentersRouteHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ protected void handleGetStorageDomainsByDcId(final String id, final HttpServletR
122122
throws IOException {
123123
try {
124124
ListQuery query = ListQuery.fromRequest(req);
125-
List<StorageDomain> storageDomains = serverAdapter.listStorageDomainsByDcId(id, query.getPage(),
125+
List<StorageDomain> storageDomains = serverAdapter.listStorageDomainsByDcId(id, query.getOffset(),
126126
query.getMax());
127127
NamedList<StorageDomain> response = NamedList.of("storage_domain", storageDomains);
128128
io.getWriter().write(resp, HttpServletResponse.SC_OK, response, outFormat);
@@ -138,7 +138,7 @@ protected void handleGetNetworksByDcId(final String id, final HttpServletRequest
138138
throws IOException {
139139
try {
140140
ListQuery query = ListQuery.fromRequest(req);
141-
List<Network> networks = serverAdapter.listNetworksByDcId(id, query.getPage(),
141+
List<Network> networks = serverAdapter.listNetworksByDcId(id, query.getOffset(),
142142
query.getMax());
143143
NamedList<Network> response = NamedList.of("network", networks);
144144
io.getWriter().write(resp, HttpServletResponse.SC_OK, response, outFormat);

plugins/integrations/veeam-control-service/src/main/java/org/apache/cloudstack/veeam/api/VmsRouteHandler.java

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import java.io.IOException;
2121
import java.util.List;
22-
import java.util.Set;
2322

2423
import javax.inject.Inject;
2524
import javax.servlet.http.HttpServletRequest;
@@ -42,7 +41,6 @@
4241
import org.apache.cloudstack.veeam.utils.Negotiation;
4342
import org.apache.cloudstack.veeam.utils.PathUtil;
4443
import org.apache.commons.collections.CollectionUtils;
45-
import org.apache.commons.lang3.StringUtils;
4644

4745
import com.cloud.exception.InvalidParameterValueException;
4846
import com.cloud.utils.component.ManagerBase;
@@ -233,7 +231,12 @@ protected void handleGet(final HttpServletRequest req, final HttpServletResponse
233231
Negotiation.OutFormat outFormat, VeeamControlServlet io) throws IOException {
234232
try {
235233
ListQuery query = ListQuery.fromRequest(req);
236-
final List<Vm> result = serverAdapter.listAllInstances(query.getOffset(), query.getLimit());
234+
final List<Vm> result = serverAdapter.listAllInstances(query.followContains("tags"),
235+
query.followContains("disk_attachments.disk"),
236+
query.followContains("nics.reporteddevices"),
237+
query.isAllContent(),
238+
query.getOffset(),
239+
query.getLimit());
237240
NamedList<Vm> response = NamedList.of("vm", result);
238241
io.getWriter().write(resp, HttpServletResponse.SC_OK, response, outFormat);
239242
} catch (CloudRuntimeException e) {
@@ -255,22 +258,13 @@ protected void handlePost(final HttpServletRequest req, final HttpServletRespons
255258

256259
protected void handleGetById(final String id, final HttpServletRequest req, final HttpServletResponse resp,
257260
final Negotiation.OutFormat outFormat, final VeeamControlServlet io) throws IOException {
258-
String followStr = req.getParameter("follow");
259-
boolean includeTags = false;
260-
boolean includeDisks = false;
261-
boolean includeNics = false;
262-
if (StringUtils.isNotBlank(followStr)) {
263-
Set<String> followParts = java.util.Arrays.stream(followStr.split(","))
264-
.map(String::trim)
265-
.filter(s -> !s.isEmpty())
266-
.collect(java.util.stream.Collectors.toSet());
267-
includeTags = followParts.contains("tags");
268-
includeDisks = followParts.contains("disk_attachments.disk");
269-
includeNics = followParts.contains("nics.reporteddevices");
270-
}
271-
boolean allContent = Boolean.parseBoolean(req.getParameter("all_content"));
272261
try {
273-
Vm response = serverAdapter.getInstance(id, includeTags, includeDisks, includeNics, allContent);
262+
ListQuery query = ListQuery.fromRequest(req);
263+
Vm response = serverAdapter.getInstance(id,
264+
query.followContains("tags"),
265+
query.followContains("disk_attachments.disk"),
266+
query.followContains("nics.reporteddevices"),
267+
query.isAllContent());
274268
io.getWriter().write(resp, HttpServletResponse.SC_OK, response, outFormat);
275269
} catch (InvalidParameterValueException e) {
276270
io.notFound(resp, e.getMessage(), outFormat);

plugins/integrations/veeam-control-service/src/main/java/org/apache/cloudstack/veeam/api/converter/UserVmJoinVOToVmConverter.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,13 @@ private UserVmJoinVOToVmConverter() {
5656
*
5757
* @param src UserVmJoinVO
5858
*/
59-
public static Vm toVm(final UserVmJoinVO src, final Function<Long, HostJoinVO> hostResolver,
60-
final Function<Long, Map<String, String>> detailsResolver,
61-
final Function<Long, List<Tag>> tagsResolver,
62-
final Function<Long, List<DiskAttachment>> disksResolver,
63-
final Function<UserVmJoinVO, List<Nic>> nicsResolver,
64-
final boolean allContent) {
59+
public static Vm toVm(final UserVmJoinVO src,
60+
final Function<Long, HostJoinVO> hostResolver,
61+
final Function<Long, Map<String, String>> detailsResolver,
62+
final Function<Long, List<Tag>> tagsResolver,
63+
final Function<Long, List<DiskAttachment>> disksResolver,
64+
final Function<UserVmJoinVO, List<Nic>> nicsResolver,
65+
final boolean allContent) {
6566
if (src == null) {
6667
return null;
6768
}
@@ -190,10 +191,15 @@ private static Vm.Initialization getOvfInitialization(Vm vm, UserVmJoinVO vo) {
190191
return initialization;
191192
}
192193

193-
public static List<Vm> toVmList(final List<UserVmJoinVO> srcList, final Function<Long, HostJoinVO> hostResolver,
194-
final Function<Long, Map<String, String>> detailsResolver) {
194+
public static List<Vm> toVmList(final List<UserVmJoinVO> srcList,
195+
final Function<Long, HostJoinVO> hostResolver,
196+
final Function<Long, Map<String, String>> detailsResolver,
197+
final Function<Long, List<Tag>> tagsResolver,
198+
final Function<Long, List<DiskAttachment>> disksResolver,
199+
final Function<UserVmJoinVO, List<Nic>> nicsResolver,
200+
final boolean allContent) {
195201
return srcList.stream()
196-
.map(v -> toVm(v, hostResolver, detailsResolver, null, null, null, false))
202+
.map(v -> toVm(v, hostResolver, detailsResolver, tagsResolver, disksResolver, nicsResolver, allContent))
197203
.collect(Collectors.toList());
198204
}
199205

plugins/integrations/veeam-control-service/src/main/java/org/apache/cloudstack/veeam/api/request/ListQuery.java

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

1818
package org.apache.cloudstack.veeam.api.request;
1919

20+
import java.util.Arrays;
2021
import java.util.LinkedHashMap;
22+
import java.util.List;
2123
import java.util.Map;
24+
import java.util.stream.Collectors;
2225

2326
import javax.servlet.http.HttpServletRequest;
2427

28+
import org.apache.commons.collections.CollectionUtils;
2529
import org.apache.commons.collections.MapUtils;
2630
import org.apache.commons.lang3.StringUtils;
2731
import org.jetbrains.annotations.NotNull;
@@ -31,6 +35,7 @@ public class ListQuery {
3135
Long max;
3236
Long page;
3337
Map<String, String> search;
38+
List<String> follow;
3439

3540
public boolean isAllContent() {
3641
return allContent;
@@ -48,16 +53,19 @@ public void setMax(Long max) {
4853
this.max = max;
4954
}
5055

51-
public Map<String, String> getSearch() {
52-
return search;
53-
}
54-
5556
public void setSearch(Map<String, String> search) {
5657
this.search = search;
5758
}
5859

59-
public Long getPage() {
60-
return page;
60+
public void setFollow(String followStr) {
61+
if (StringUtils.isBlank(followStr)) {
62+
this.follow = null;
63+
return;
64+
}
65+
this.follow = Arrays.stream(followStr.split(","))
66+
.map(String::trim)
67+
.filter(s -> !s.isEmpty())
68+
.collect(Collectors.toList());
6169
}
6270

6371
public Long getOffset() {
@@ -71,6 +79,13 @@ public Long getLimit() {
7179
return max;
7280
}
7381

82+
public boolean followContains(String part) {
83+
if (CollectionUtils.isEmpty(follow)) {
84+
return false;
85+
}
86+
return follow.contains(part);
87+
}
88+
7489
public static ListQuery fromRequest(HttpServletRequest request) {
7590
ListQuery query = new ListQuery();
7691
if (MapUtils.isEmpty(request.getParameterMap())) {
@@ -89,6 +104,8 @@ public static ListQuery fromRequest(HttpServletRequest request) {
89104
// Ignore invalid max and keep default null value.
90105
}
91106
}
107+
String follow = request.getParameter("follow");
108+
query.setFollow(follow);
92109
Map<String, String> searchItems = getSearchMap(request.getParameter("search"));
93110
if (!searchItems.isEmpty()) {
94111
try {

0 commit comments

Comments
 (0)