2222import java .util .Arrays ;
2323import java .util .List ;
2424
25- import com .cloud .hypervisor .vmware .util .VmwareHelper ;
2625import com .cloud .utils .StringUtils ;
27- import org .apache .cloudstack .utils .reflectiontostringbuilderutils .ReflectionToStringBuilderUtils ;
2826import org .apache .cloudstack .vm .UnmanagedInstanceTO ;
2927import org .apache .commons .collections .CollectionUtils ;
3028import org .apache .log4j .Logger ;
@@ -81,7 +79,7 @@ public VirtualMachineMO findVm(String vmName) throws Exception {
8179 s_logger .warn ("Custom field " + CustomFieldConstants .CLOUD_VM_INTERNAL_NAME + " is not registered ?!" );
8280 }
8381 String instanceNameCustomField = "value[" + key + "]" ;
84- List <ObjectContent > ocs = getVmPropertiesOnDatacenterVmFolder (new String [] {"name" , instanceNameCustomField });
82+ List <ObjectContent > ocs = getVmProperties (new String [] {"name" , instanceNameCustomField });
8583 return HypervisorHostHelper .findVmFromObjectContent (_context , ocs .toArray (new ObjectContent [0 ]), vmName , instanceNameCustomField );
8684 }
8785
@@ -92,7 +90,7 @@ public List<VirtualMachineMO> findVmByNameAndLabel(String vmLabel) throws Except
9290
9391 List <VirtualMachineMO > list = new ArrayList <>();
9492
95- List <ObjectContent > ocs = getVmPropertiesOnDatacenterVmFolder (new String [] {"name" , String .format ("value[%d]" , key )});
93+ List <ObjectContent > ocs = getVmProperties (new String [] {"name" , String .format ("value[%d]" , key )});
9694 if (CollectionUtils .isNotEmpty (ocs )) {
9795 for (ObjectContent oc : ocs ) {
9896 List <DynamicProperty > props = oc .getPropSet ();
@@ -125,7 +123,7 @@ public VirtualMachineMO checkIfVmAlreadyExistsInVcenter(String vmNameOnVcenter,
125123 s_logger .warn ("Custom field " + CustomFieldConstants .CLOUD_VM_INTERNAL_NAME + " is not registered ?!" );
126124 }
127125
128- List <ObjectContent > ocs = getVmPropertiesOnDatacenterVmFolder (new String [] {"name" , String .format ("value[%d]" , key )});
126+ List <ObjectContent > ocs = getVmProperties (new String [] {"name" , String .format ("value[%d]" , key )});
129127 if (CollectionUtils .isNotEmpty (ocs )) {
130128 for (ObjectContent oc : ocs ) {
131129 List <DynamicProperty > props = oc .getPropSet ();
@@ -151,31 +149,15 @@ public VirtualMachineMO checkIfVmAlreadyExistsInVcenter(String vmNameOnVcenter,
151149 return null ;
152150 }
153151
154- public Pair <String , List <UnmanagedInstanceTO >> getVmsOnDatacenter (Integer maxObjects , String token ) throws InvalidPropertyFaultMsg , RuntimeFaultFaultMsg , InvocationTargetException , NoSuchMethodException , IllegalAccessException {
152+ public Pair <String , List <UnmanagedInstanceTO >> getVms (Integer maxObjects , String token ) throws InvalidPropertyFaultMsg , RuntimeFaultFaultMsg , InvocationTargetException , NoSuchMethodException , IllegalAccessException {
155153 List <UnmanagedInstanceTO > vms = new ArrayList <>();
156- Pair <String , List <ObjectContent >> objectContents = getVmPropertiesOnDatacenterVmFolder (new String [] {"name" }, maxObjects , token );
154+ Pair <String , List <ObjectContent >> objectContents = getVmProperties (new String [] {"name" }, maxObjects , token );
157155 if (s_logger .isDebugEnabled ()) {
158156 s_logger .debug (String .format ("returning token %s for future retrievals, currently %d objects retrieved." , objectContents .first (), objectContents .second ().size ()));
159157 }
160158 Pair <String , List <UnmanagedInstanceTO >> retval = new Pair <>(objectContents .first (), vms );
161- List <ObjectContent > ocs = objectContents .second ();
162- if (ocs != null ) {
163- for (ObjectContent oc : ocs ) {
164- ManagedObjectReference vmMor = oc .getObj ();
165- if (vmMor != null ) {
166- VirtualMachineMO vmMo = new VirtualMachineMO (_context , vmMor );
167- try {
168- if (!vmMo .isTemplate ()) {
169- HostMO hostMO = vmMo .getRunningHost ();
170- UnmanagedInstanceTO unmanagedInstance = VmwareHelper .getUnmanagedInstance (hostMO , vmMo );
171- vms .add (unmanagedInstance );
172- }
173- } catch (Exception e ) {
174- s_logger .debug (String .format ("Unexpected error checking unmanaged instance %s, excluding it: %s" , vmMo .getVmName (), e .getMessage ()), e );
175- }
176- }
177- }
178- }
159+
160+ objectContentToUnmanagedInstanceTO (objectContents , vms );
179161
180162 return retval ;
181163 }
@@ -207,7 +189,7 @@ public ManagedObjectReference findDatastore(String name) throws Exception {
207189 return null ;
208190 }
209191
210- public ManagedObjectReference findHost (String name ) throws Exception {
192+ public ManagedObjectReference findHost (String name ) throws InvalidPropertyFaultMsg , RuntimeFaultFaultMsg {
211193 List <ObjectContent > ocs = getHostPropertiesOnDatacenterHostFolder (new String [] {"name" });
212194
213195 if (ocs != null ) {
@@ -291,8 +273,8 @@ public List<ObjectContent> getDatastorePropertiesOnDatacenter(String[] propertyP
291273
292274 }
293275
294- public List <ObjectContent > getVmPropertiesOnDatacenterVmFolder (String [] propertyPaths ) throws InvalidPropertyFaultMsg , RuntimeFaultFaultMsg {
295- return getVmPropertiesOnDatacenterVmFolder (propertyPaths , null , null ).second ();
276+ public List <ObjectContent > getVmProperties (String [] propertyPaths ) throws InvalidPropertyFaultMsg , RuntimeFaultFaultMsg {
277+ return getVmProperties (propertyPaths , null , null ).second ();
296278 }
297279
298280 /**
@@ -304,26 +286,21 @@ public List<ObjectContent> getVmPropertiesOnDatacenterVmFolder(String[] property
304286 * @throws InvalidPropertyFaultMsg property does not exist as thrown by Vmware.
305287 * @throws RuntimeFaultFaultMsg generic vmware runtime exception
306288 */
307- public Pair <String , List <ObjectContent >> getVmPropertiesOnDatacenterVmFolder (String [] propertyPaths , Integer maxObjects , String tokenForPriorQuery ) throws InvalidPropertyFaultMsg , RuntimeFaultFaultMsg {
289+ public Pair <String , List <ObjectContent >> getVmProperties (String [] propertyPaths , Integer maxObjects , String tokenForPriorQuery ) throws InvalidPropertyFaultMsg , RuntimeFaultFaultMsg {
308290 if (StringUtils .isNotBlank (tokenForPriorQuery )) {
309291 if (s_logger .isDebugEnabled ()) {
310292 s_logger .debug (String .format ("running repeat query with token \' %s\' " , tokenForPriorQuery ));
311293 }
312- return retrieveNextSetOfPropertiesOnDatacenterVmFolder (tokenForPriorQuery );
294+ return retrieveNextSetOfProperties (tokenForPriorQuery );
313295 } else {
314296 if (s_logger .isDebugEnabled ()) {
315297 s_logger .debug (String .format ("running query for %d propertypaths and max %d objects" , propertyPaths .length , maxObjects ));
316298 }
317- return retrieveNextSetOfPropertiesOnDatacenterVmFolder (propertyPaths , maxObjects );
299+ return retrieveNextSetOfProperties (propertyPaths , maxObjects );
318300 }
319301 }
320302
321- private Pair <String , List <ObjectContent >> retrieveNextSetOfPropertiesOnDatacenterVmFolder (String tokenForPriorQuery ) throws InvalidPropertyFaultMsg , RuntimeFaultFaultMsg {
322- RetrieveResult result = _context .getService ().continueRetrievePropertiesEx (_context .getPropertyCollector (), tokenForPriorQuery );
323- return createReturnObjectPair (result );
324- }
325-
326- private Pair <String , List <ObjectContent >> retrieveNextSetOfPropertiesOnDatacenterVmFolder (String [] propertyPaths , Integer maxObjects ) throws InvalidPropertyFaultMsg , RuntimeFaultFaultMsg {
303+ private Pair <String , List <ObjectContent >> retrieveNextSetOfProperties (String [] propertyPaths , Integer maxObjects ) throws InvalidPropertyFaultMsg , RuntimeFaultFaultMsg {
327304 PropertySpec pSpec = new PropertySpec ();
328305 pSpec .setType ("VirtualMachine" );
329306 pSpec .getPathSet ().addAll (Arrays .asList (propertyPaths ));
@@ -363,15 +340,6 @@ private Pair<String, List<ObjectContent>> retrieveNextSetOfPropertiesOnDatacente
363340 return createReturnObjectPair (result );
364341 }
365342
366- private static Pair <String , List <ObjectContent >> createReturnObjectPair (RetrieveResult result ) {
367- if (s_logger .isDebugEnabled ()) {
368- s_logger .debug ("vmware result : " + ReflectionToStringBuilderUtils .reflectCollection (result ));
369- }
370- String tokenForRetrievingNewResults = result .getToken ();
371- List <ObjectContent > listOfObjects = result .getObjects ();
372- return new Pair <>(tokenForRetrievingNewResults , listOfObjects );
373- }
374-
375343 public static Pair <DatacenterMO , String > getOwnerDatacenter (VmwareContext context , ManagedObjectReference morEntity ) throws Exception {
376344
377345 PropertySpec pSpec = new PropertySpec ();
0 commit comments