mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
vmware.py: allow for better search functionality (#15053)
- search entity by path - search vm based on folder - search for a vm based on given id ( uuid/name/dns_name/ip/inventory_path) - search for a cluster by name, in a given datacenter (optionally) - search for objects of a given type in a folder Note: instance uuid is unique to a VM
This commit is contained in:
parent
bbe32a4b5e
commit
3608829b1b
1 changed files with 63 additions and 4 deletions
|
@ -61,7 +61,25 @@ def find_dvspg_by_name(dv_switch, portgroup_name):
|
||||||
|
|
||||||
return None
|
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):
|
def find_cluster_by_name_datacenter(datacenter, cluster_name):
|
||||||
|
|
||||||
host_folder = datacenter.hostFolder
|
host_folder = datacenter.hostFolder
|
||||||
|
@ -70,6 +88,19 @@ def find_cluster_by_name_datacenter(datacenter, cluster_name):
|
||||||
return folder
|
return folder
|
||||||
return None
|
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):
|
def find_datacenter_by_name(content, datacenter_name):
|
||||||
|
|
||||||
|
@ -98,10 +129,35 @@ def find_hostsystem_by_name(content, hostname):
|
||||||
return host
|
return host
|
||||||
return None
|
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
|
||||||
|
|
||||||
def find_vm_by_name(content, vm_name):
|
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)
|
||||||
|
|
||||||
vms = get_all_objs(content, [vim.VirtualMachine])
|
return vm
|
||||||
|
|
||||||
|
|
||||||
|
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:
|
for vm in vms:
|
||||||
if vm.name == vm_name:
|
if vm.name == vm_name:
|
||||||
return vm
|
return vm
|
||||||
|
@ -155,10 +211,13 @@ def connect_to_api(module, disconnect_atexit=True):
|
||||||
atexit.register(connect.Disconnect, service_instance)
|
atexit.register(connect.Disconnect, service_instance)
|
||||||
return service_instance.RetrieveContent()
|
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 = {}
|
obj = {}
|
||||||
container = content.viewManager.CreateContainerView(content.rootFolder, vimtype, True)
|
container = content.viewManager.CreateContainerView(folder, vimtype, recurse)
|
||||||
for managed_object_ref in container.view:
|
for managed_object_ref in container.view:
|
||||||
obj.update({managed_object_ref: managed_object_ref.name})
|
obj.update({managed_object_ref: managed_object_ref.name})
|
||||||
return obj
|
return obj
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue