507
def get_vm_ref_from_uuid(session, instance_uuid):
508
"""Get reference to the VM with the uuid specified."""
509
vms = session._call_method(vim_util, "get_objects",
510
"VirtualMachine", ["name"])
512
if vm.propSet[0].val == instance_uuid:
516
def get_vm_ref(session, instance):
517
"""Get reference to the VM through uuid or vm name."""
518
vm_ref = get_vm_ref_from_uuid(session, instance['uuid'])
520
vm_ref = get_vm_ref_from_name(session, instance['name'])
522
raise exception.InstanceNotFound(instance_id=instance['uuid'])
526
def get_host_ref_from_id(session, host_id, property_list=None):
527
"""Get a host reference object for a host_id string."""
529
if property_list is None:
530
property_list = ['name']
532
host_refs = session._call_method(
533
vim_util, "get_objects",
534
"HostSystem", property_list)
536
for ref in host_refs:
537
if ref.obj.value == host_id:
541
def get_host_id_from_vm_ref(session, vm_ref):
543
This method allows you to find the managed object
544
ID of the host running a VM. Since vMotion can
545
change the value, you should not presume that this
546
is a value that you can cache for very long and
547
should be prepared to allow for it to change.
549
:param session: a vSphere API connection
550
:param vm_ref: a reference object to the running VM
551
:return: the host_id running the virtual machine
554
# to prevent typographical errors below
555
property_name = 'runtime.host'
557
# a property collector in VMware vSphere Management API
558
# is a set of local representations of remote values.
559
# property_set here, is a local representation of the
560
# properties we are querying for.
561
property_set = session._call_method(
562
vim_util, "get_object_properties",
563
None, vm_ref, vm_ref._type, [property_name])
565
prop = property_from_property_set(
566
property_name, property_set)
569
prop = prop.val.value
571
# reaching here represents an impossible state
573
"Virtual Machine %s exists without a runtime.host!"
579
def property_from_property_set(property_name, property_set):
581
Use this method to filter property collector results.
583
Because network traffic is expensive, multiple
584
VMwareAPI calls will sometimes pile-up properties
585
to be collected. That means results may contain
586
many different values for multiple purposes.
588
This helper will filter a list for a single result
589
and filter the properties of that result to find
590
the single value of whatever type resides in that
591
result. This could be a ManagedObjectReference ID
594
:param property_name: name of property you want
595
:param property_set: all results from query
596
:return: the value of the property.
599
for prop in property_set:
600
p = _property_from_propSet(prop.propSet, property_name)
605
def _property_from_propSet(propSet, name='name'):
611
def get_host_ref_for_vm(session, instance, props):
612
"""Get the ESXi host running a VM by its name."""
614
vm_ref = get_vm_ref(session, instance)
615
host_id = get_host_id_from_vm_ref(session, vm_ref)
616
return get_host_ref_from_id(session, host_id, props)
619
def get_host_name_for_vm(session, instance):
620
"""Get the ESXi host running a VM by its name."""
621
host_ref = get_host_ref_for_vm(session, instance, ['name'])
622
return get_host_name_from_host_ref(host_ref)
625
def get_host_name_from_host_ref(host_ref):
626
p = _property_from_propSet(host_ref.propSet)
502
631
def get_cluster_ref_from_name(session, cluster_name):
503
632
"""Get reference to the cluster with the name specified."""
504
633
cls = session._call_method(vim_util, "get_objects",