@ -61,7 +61,25 @@ def find_dvspg_by_name(dv_switch, portgroup_name):
return None
def find_entity_child_by_path ( content , entityRootFolder , path ) :
entity = entityRootFolder
searchIndex = content . searchIndex
paths = path . split ( " / " )
try :
for path in paths :
entity = searchIndex . FindChild ( entity , path )
if entity . name == paths [ - 1 ] :
return entity
except :
pass
return None
# Maintain for legacy, or remove with 2.1 ?
# Should be replaced with find_cluster_by_name
def find_cluster_by_name_datacenter ( datacenter , cluster_name ) :
host_folder = datacenter . hostFolder
@ -70,6 +88,19 @@ def find_cluster_by_name_datacenter(datacenter, cluster_name):
return folder
return None
def find_cluster_by_name ( content , cluster_name , datacenter = None ) :
if datacenter :
folder = datacenter . hostFolder
else :
folder = content . rootFolder
clusters = get_all_objs ( content , [ vim . ClusterComputeResource ] , folder )
for cluster in clusters :
if cluster . name == cluster_name :
return cluster
return None
def find_datacenter_by_name ( content , datacenter_name ) :
@ -98,10 +129,35 @@ def find_hostsystem_by_name(content, hostname):
return host
return None
def find_vm_by_id ( content , vm_id , vm_id_type = " vm_name " , datacenter = None , cluster = None ) :
""" UUID is unique to a VM, every other id returns the first match. """
si = content . searchIndex
vm = None
if vm_id_type == ' dns_name ' :
vm = si . FindByDnsName ( datacenter = datacenter , dnsName = vm_id , vmSearch = True )
elif vm_id_type == ' inventory_path ' :
vm = si . FindByInventoryPath ( inventoryPath = vm_id )
if type ( vm ) != type ( vim . VirtualMachine ) :
vm = None
elif vm_id_type == ' uuid ' :
vm = si . FindByUuid ( datacenter = datacenter , instanceUuid = vm_id , vmSearch = True )
elif vm_id_type == ' ip ' :
vm = si . FindByIp ( datacenter = datacenter , ip = vm_id , vmSearch = True )
elif vm_id_type == ' vm_name ' :
folder = None
if cluster :
folder = cluster
elif datacenter :
folder = datacenter . hostFolder
vm = find_vm_by_name ( content , vm_id , folder )
return vm
def find_vm_by_name ( content , vm_name ) :
vms = get_all_objs ( content , [ vim . VirtualMachine ] )
def find_vm_by_name ( content , vm_name , folder = None , recurse = True ) :
vms = get_all_objs ( content , [ vim . VirtualMachine ] , folder , recurse = True )
for vm in vms :
if vm . name == vm_name :
return vm
@ -155,10 +211,13 @@ def connect_to_api(module, disconnect_atexit=True):
atexit . register ( connect . Disconnect , service_instance )
return service_instance . RetrieveContent ( )
def get_all_objs ( content , vimtype ) :
def get_all_objs ( content , vimtype , folder = None , recurse = True ) :
if not folder :
folder = content . rootFolder
obj = { }
container = content . viewManager . CreateContainerView ( content. rootFolder , vimtype , Tru e)
container = content . viewManager . CreateContainerView ( folder, vimtype , recurs e)
for managed_object_ref in container . view :
obj . update ( { managed_object_ref : managed_object_ref . name } )
return obj