Skip to content

Commit b4ec3f7

Browse files
committed
add maximum results number to retrieval of unregistered VMs on vmware
1 parent f9b1767 commit b4ec3f7

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,6 +1106,7 @@ public class ApiConstants {
11061106
public static final String PARAMETER_DESCRIPTION_IS_TAG_A_RULE = "Whether the informed tag is a JS interpretable rule or not.";
11071107

11081108
public static final String NFS_MOUNT_OPTIONS = "nfsmountopts";
1109+
public static final String MAX_NUMBER = "maxnumber";
11091110

11101111
/**
11111112
* This enum specifies IO Drivers, each option controls specific policies on I/O.

plugins/hypervisors/vmware/src/main/java/com/cloud/hypervisor/vmware/manager/VmwareManagerImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1593,6 +1593,7 @@ public List<UnmanagedInstanceTO> listVMsInDatacenter(ListVmwareDcVmsCmd cmd) {
15931593
String datacenterName = cmd.getDatacenterName();
15941594
String username = cmd.getUsername();
15951595
String password = cmd.getPassword();
1596+
Integer maxObjects = cmd.getMaxNumber();
15961597
Long existingVcenterId = cmd.getExistingVcenterId();
15971598
String keyword = cmd.getKeyword();
15981599

@@ -1632,7 +1633,7 @@ public List<UnmanagedInstanceTO> listVMsInDatacenter(ListVmwareDcVmsCmd cmd) {
16321633
s_logger.error(msg);
16331634
throw new InvalidParameterValueException(msg);
16341635
}
1635-
List<UnmanagedInstanceTO> instances = dcMo.getAllVmsOnDatacenter();
1636+
List<UnmanagedInstanceTO> instances = dcMo.getVmsOnDatacenter(maxObjects);
16361637
return StringUtils.isBlank(keyword) ? instances :
16371638
instances.stream().filter(x -> x.getName().toLowerCase().contains(keyword.toLowerCase())).collect(Collectors.toList());
16381639
} catch (Exception e) {

plugins/hypervisors/vmware/src/main/java/org/apache/cloudstack/api/command/admin/zone/ListVmwareDcVmsCmd.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ public class ListVmwareDcVmsCmd extends BaseListCmd {
7070
@Parameter(name = ApiConstants.PASSWORD, type = CommandType.STRING, description = "The password for specified username.")
7171
private String password;
7272

73+
@Parameter(name = ApiConstants.MAX_NUMBER, type = CommandType.INTEGER, description = "The maximum number of results to return.")
74+
private Integer maxNumber;
75+
7376
public String getVcenter() {
7477
return vcenter;
7578
}
@@ -82,6 +85,10 @@ public String getPassword() {
8285
return password;
8386
}
8487

88+
public Integer getMaxNumber() {
89+
return maxNumber;
90+
}
91+
8592
public String getDatacenterName() {
8693
return datacenterName;
8794
}

vmware-base/src/main/java/com/cloud/hypervisor/vmware/mo/DatacenterMO.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import com.vmware.vim25.SelectionSpec;
4040
import com.vmware.vim25.TraversalSpec;
4141
import com.vmware.vim25.VirtualEthernetCardDistributedVirtualPortBackingInfo;
42+
import com.vmware.vim25.RetrieveOptions;
4243

4344
import com.cloud.hypervisor.vmware.util.VmwareContext;
4445
import com.cloud.utils.Pair;
@@ -161,9 +162,9 @@ public VirtualMachineMO checkIfVmAlreadyExistsInVcenter(String vmNameOnVcenter,
161162
return null;
162163
}
163164

164-
public List<UnmanagedInstanceTO> getAllVmsOnDatacenter() throws Exception {
165+
public List<UnmanagedInstanceTO> getVmsOnDatacenter(Integer maxObjects) throws Exception {
165166
List<UnmanagedInstanceTO> vms = new ArrayList<>();
166-
List<ObjectContent> ocs = getVmPropertiesOnDatacenterVmFolder(new String[] {"name"});
167+
List<ObjectContent> ocs = getVmPropertiesOnDatacenterVmFolder(new String[] {"name"}, maxObjects);
167168
if (ocs != null) {
168169
for (ObjectContent oc : ocs) {
169170
ManagedObjectReference vmMor = oc.getObj();
@@ -311,6 +312,10 @@ public List<ObjectContent> getDatastorePropertiesOnDatacenter(String[] propertyP
311312
}
312313

313314
public List<ObjectContent> getVmPropertiesOnDatacenterVmFolder(String[] propertyPaths) throws Exception {
315+
return getVmPropertiesOnDatacenterVmFolder(propertyPaths, null);
316+
}
317+
318+
public List<ObjectContent> getVmPropertiesOnDatacenterVmFolder(String[] propertyPaths, Integer maxObjects) throws Exception {
314319
PropertySpec pSpec = new PropertySpec();
315320
pSpec.setType("VirtualMachine");
316321
pSpec.getPathSet().addAll(Arrays.asList(propertyPaths));
@@ -338,10 +343,15 @@ public List<ObjectContent> getVmPropertiesOnDatacenterVmFolder(String[] property
338343
PropertyFilterSpec pfSpec = new PropertyFilterSpec();
339344
pfSpec.getPropSet().add(pSpec);
340345
pfSpec.getObjectSet().add(oSpec);
341-
List<PropertyFilterSpec> pfSpecArr = new ArrayList<PropertyFilterSpec>();
346+
List<PropertyFilterSpec> pfSpecArr = new ArrayList<>();
342347
pfSpecArr.add(pfSpec);
343348

344-
return _context.getService().retrieveProperties(_context.getPropertyCollector(), pfSpecArr);
349+
RetrieveOptions ro = new RetrieveOptions();
350+
if(maxObjects != null && maxObjects > 0) {
351+
ro.setMaxObjects(maxObjects);
352+
}
353+
354+
return _context.getService().retrievePropertiesEx(_context.getPropertyCollector(), pfSpecArr, ro).getObjects();
345355
}
346356

347357
public static Pair<DatacenterMO, String> getOwnerDatacenter(VmwareContext context, ManagedObjectReference morEntity) throws Exception {

0 commit comments

Comments
 (0)