2222import java .util .List ;
2323
2424import com .cloud .hypervisor .vmware .util .VmwareHelper ;
25+ import com .cloud .utils .StringUtils ;
2526import org .apache .cloudstack .vm .UnmanagedInstanceTO ;
2627import org .apache .commons .collections .CollectionUtils ;
2728import org .apache .log4j .Logger ;
4041import com .vmware .vim25 .TraversalSpec ;
4142import com .vmware .vim25 .VirtualEthernetCardDistributedVirtualPortBackingInfo ;
4243import com .vmware .vim25 .RetrieveOptions ;
44+ import com .vmware .vim25 .RetrieveResult ;
45+ import com .vmware .vim25 .InvalidPropertyFaultMsg ;
46+ import com .vmware .vim25 .RuntimeFaultFaultMsg ;
4347
4448import com .cloud .hypervisor .vmware .util .VmwareContext ;
4549import com .cloud .utils .Pair ;
@@ -162,9 +166,11 @@ public VirtualMachineMO checkIfVmAlreadyExistsInVcenter(String vmNameOnVcenter,
162166 return null ;
163167 }
164168
165- public List <UnmanagedInstanceTO > getVmsOnDatacenter (Integer maxObjects , boolean nextPage ) throws Exception {
169+ public Pair < String , List <UnmanagedInstanceTO >> getVmsOnDatacenter (Integer maxObjects , String token ) throws Exception {
166170 List <UnmanagedInstanceTO > vms = new ArrayList <>();
167- List <ObjectContent > ocs = getVmPropertiesOnDatacenterVmFolder (new String [] {"name" }, maxObjects , nextPage );
171+ Pair <String , List <ObjectContent >> objectContents = getVmPropertiesOnDatacenterVmFolder (new String [] {"name" }, maxObjects , token );
172+ Pair <String , List <UnmanagedInstanceTO >> retval = new Pair <>(objectContents .first (), vms );
173+ List <ObjectContent > ocs = objectContents .second ();
168174 if (ocs != null ) {
169175 for (ObjectContent oc : ocs ) {
170176 ManagedObjectReference vmMor = oc .getObj ();
@@ -183,7 +189,7 @@ public List<UnmanagedInstanceTO> getVmsOnDatacenter(Integer maxObjects, boolean
183189 }
184190 }
185191
186- return vms ;
192+ return retval ;
187193 }
188194
189195 public List <HostMO > getAllHostsOnDatacenter () throws Exception {
@@ -311,19 +317,32 @@ public List<ObjectContent> getDatastorePropertiesOnDatacenter(String[] propertyP
311317
312318 }
313319
314- public List <ObjectContent > getVmPropertiesOnDatacenterVmFolder (String [] propertyPaths ) throws Exception {
315- return getVmPropertiesOnDatacenterVmFolder (propertyPaths , null , false );
320+ public List <ObjectContent > getVmPropertiesOnDatacenterVmFolder (String [] propertyPaths ) throws InvalidPropertyFaultMsg , RuntimeFaultFaultMsg {
321+ return getVmPropertiesOnDatacenterVmFolder (propertyPaths , null , null ). second ( );
316322 }
317323
318324 /**
319325 *
320326 * @param propertyPaths Vmware side property names to query, for instance {"name"}
321327 * @param maxObjects the number of objects to retrieve
322- * @param nextPage restart the query or continue a previous query
328+ * @param tokenForPriorQuery restart the query or continue a previous query
323329 * @return The propertyPaths requested for the objects of type "VirtualMachine" in a list are found/returned by the DC
324330 * @throws Exception generic {code}Exception{code} as thrown by Vmware.
325331 */
326- public List <ObjectContent > getVmPropertiesOnDatacenterVmFolder (String [] propertyPaths , Integer maxObjects , boolean nextPage ) throws Exception {
332+ public Pair <String , List <ObjectContent >> getVmPropertiesOnDatacenterVmFolder (String [] propertyPaths , Integer maxObjects , String tokenForPriorQuery ) throws InvalidPropertyFaultMsg , RuntimeFaultFaultMsg {
333+ if (StringUtils .isNotBlank (tokenForPriorQuery )) {
334+ return retrieveNextSetOfPropertiesOnDatacenterVmFolder (tokenForPriorQuery );
335+ } else {
336+ return retrieveNextSetOfPropertiesOnDatacenterVmFolder (propertyPaths , maxObjects );
337+ }
338+ }
339+
340+ private Pair <String , List <ObjectContent >> retrieveNextSetOfPropertiesOnDatacenterVmFolder (String tokenForPriorQuery ) throws InvalidPropertyFaultMsg , RuntimeFaultFaultMsg {
341+ RetrieveResult result = _context .getService ().continueRetrievePropertiesEx (_context .getPropertyCollector (), tokenForPriorQuery );
342+ return createReturnObjectPair (result );
343+ }
344+
345+ private Pair <String , List <ObjectContent >> retrieveNextSetOfPropertiesOnDatacenterVmFolder (String [] propertyPaths , Integer maxObjects ) throws InvalidPropertyFaultMsg , RuntimeFaultFaultMsg {
327346 PropertySpec pSpec = new PropertySpec ();
328347 pSpec .setType ("VirtualMachine" );
329348 pSpec .getPathSet ().addAll (Arrays .asList (propertyPaths ));
@@ -355,11 +374,18 @@ public List<ObjectContent> getVmPropertiesOnDatacenterVmFolder(String[] property
355374 pfSpecArr .add (pfSpec );
356375
357376 RetrieveOptions ro = new RetrieveOptions ();
358- if (maxObjects != null && maxObjects > 0 ) {
377+ if (maxObjects != null && maxObjects > 0 ) {
359378 ro .setMaxObjects (maxObjects );
360379 }
361380
362- return _context .getService ().retrievePropertiesEx (_context .getPropertyCollector (), pfSpecArr , ro ).getObjects ();
381+ RetrieveResult result = _context .getService ().retrievePropertiesEx (_context .getPropertyCollector (), pfSpecArr , ro );
382+ return createReturnObjectPair (result );
383+ }
384+
385+ private static Pair <String , List <ObjectContent >> createReturnObjectPair (RetrieveResult result ) {
386+ String tokenForRetrievingNewResults = result .getToken ();
387+ List <ObjectContent > listOfObjects = result .getObjects ();
388+ return new Pair <>(tokenForRetrievingNewResults , listOfObjects );
363389 }
364390
365391 public static Pair <DatacenterMO , String > getOwnerDatacenter (VmwareContext context , ManagedObjectReference morEntity ) throws Exception {
0 commit comments