<s> from pyVmomi import vim <EOL> from vmwarelib . actions import BaseAction <EOL> class GetVMs ( BaseAction ) : <EOL> def run ( self , ids = None , names = None , datastores = None , datastore_clusters = None , <EOL> resource_pools = None , vapps = None , hosts = None , folders = None , clusters = None , <EOL> datacenters = None , virtual_switches = None , no_recursion = False ) : <EOL> si = self . si <EOL> si_content = si . RetrieveContent ( ) <EOL> props = [ '<STR_LIT:name>' , '<STR_LIT>' ] <EOL> moid_to_vm = { } <EOL> vms_from_vmids = [ ] <EOL> if ids : <EOL> vms_from_vmids = [ vim . VirtualMachine ( moid , stub = si . _stub ) for moid in ids ] <EOL> GetVMs . __add_vm_properties_to_map_from_vm_array ( moid_to_vm , vms_from_vmids ) <EOL> vms_from_names = [ ] <EOL> if names : <EOL> container = si_content . viewManager . CreateContainerView ( si_content . rootFolder , <EOL> [ vim . VirtualMachine ] , True ) <EOL> for vm in container . view : <EOL> if vm . name in names : <EOL> vms_from_names . append ( vm ) <EOL> GetVMs . __add_vm_properties_to_map_from_vm_array ( moid_to_vm , vms_from_names ) <EOL> vms_from_datastores = [ ] <EOL> if datastores : <EOL> vim_datastores = [ vim . Datastore ( moid , stub = si . _stub ) for moid in datastores ] <EOL> for ds in vim_datastores : <EOL> vms_from_datastores . extend ( ds . vm ) <EOL> GetVMs . __add_vm_properties_to_map_from_vm_array ( moid_to_vm , vms_from_datastores ) <EOL> vms_from_datastore_clusters = [ ] <EOL> if datastore_clusters : <EOL> vim_datastore_clusters = [ <EOL> vim . StoragePod ( moid , stub = si . _stub ) for moid in datastore_clusters <EOL> ] <EOL> for ds_cl in vim_datastore_clusters : <EOL> for ds in ds_cl . childEntity : <EOL> vms_from_datastore_clusters . extend ( ds . vm ) <EOL> GetVMs . __add_vm_properties_to_map_from_vm_array ( moid_to_vm , <EOL> vms_from_datastore_clusters ) <EOL> vms_from_virtual_switches = [ ] <EOL> if virtual_switches : <EOL> vim_virtual_switches = [ <EOL> vim . DistributedVirtualSwitch ( moid , stub = si . _stub ) for moid in virtual_switches <EOL> ] <EOL> for vswitch in vim_virtual_switches : <EOL> for pg in vswitch . portgroup : <EOL> vms_from_virtual_switches . extend ( pg . vm ) <EOL> GetVMs . __add_vm_properties_to_map_from_vm_array ( moid_to_vm , vms_from_virtual_switches ) <EOL> vms_from_containers = [ ] <EOL> containers = [ ] <EOL> if resource_pools : <EOL> containers += [ vim . ResourcePool ( moid , stub = si . _stub ) for moid in resource_pools ] <EOL> if vapps : <EOL> containers += [ vim . VirtualApp ( moid , stub = si . _stub ) for moid in vapps ] <EOL> if hosts : <EOL> containers += [ vim . HostSystem ( moid , stub = si . _stub ) for moid in hosts ] <EOL> if folders : <EOL> containers += [ vim . Folder ( moid , stub = si . _stub ) for moid in folders ] <EOL> if clusters : <EOL> containers += [ vim . ComputeResource ( moid , stub = si . _stub ) for moid in clusters ] <EOL> if datacenters : <EOL> containers += [ vim . Datacenter ( moid , stub = si . _stub ) for moid in datacenters ] <EOL> for cont in containers : <EOL> objView = si_content . viewManager . CreateContainerView ( cont , [ vim . VirtualMachine ] , <EOL> not no_recursion ) <EOL> tSpec = vim . PropertyCollector . TraversalSpec ( name = '<STR_LIT>' , path = '<STR_LIT>' , skip = False , <EOL> type = vim . view . ContainerView ) <EOL> pSpec = vim . PropertyCollector . PropertySpec ( all = False , pathSet = props , <EOL> type = vim . VirtualMachine ) <EOL> oSpec = vim . PropertyCollector . ObjectSpec ( obj = objView , selectSet = [ tSpec ] , skip = False ) <EOL> pfSpec = vim . PropertyCollector . FilterSpec ( objectSet = [ oSpec ] , propSet = [ pSpec ] , <EOL> reportMissingObjectsInResults = False ) <EOL> retOptions = vim . PropertyCollector . RetrieveOptions ( ) <EOL> retProps = si_content . propertyCollector . RetrievePropertiesEx ( specSet = [ pfSpec ] , <EOL> options = retOptions ) <EOL> vms_from_containers += retProps . objects <EOL> while retProps . token : <EOL> retProps = si_content . propertyCollector . ContinueRetrievePropertiesEx ( <EOL> token = retProps . token ) <EOL> vms_from_containers += retProps . objects <EOL> objView . Destroy ( ) <EOL> for vm in vms_from_containers : <EOL> if vm . obj . _GetMoId ( ) not in moid_to_vm : <EOL> moid_to_vm [ vm . obj . _GetMoId ( ) ] = { <EOL> "<STR_LIT>" : vm . obj . _GetMoId ( ) , <EOL> "<STR_LIT:name>" : vm . propSet [ <NUM_LIT:0> ] . val , <EOL> "<STR_LIT>" : vm . propSet [ <NUM_LIT:1> ] . val <EOL> } <EOL> return moid_to_vm . values ( ) <EOL> @ staticmethod <EOL> def __add_vm_properties_to_map_from_vm_array ( vm_map , vm_array ) : <EOL> for vm in vm_array : <EOL> if vm . _GetMoId ( ) not in vm_map : <EOL> vm_map [ vm . _GetMoId ( ) ] = { <EOL> "<STR_LIT>" : vm . _GetMoId ( ) , <EOL> "<STR_LIT:name>" : vm . name , <EOL> "<STR_LIT>" : vm . runtime . powerState <EOL> } </s>
