def get_placement(service_instance, datacenter, placement=None):
    '''
    To create a virtual machine a resource pool needs to be supplied, we would like to use the strictest as possible.

    datacenter
        Name of the datacenter

    placement
        Dictionary with the placement info, cluster, host resource pool name

    return
        Resource pool, cluster and host object if any applies
    '''
    log.trace('Retrieving placement information')
    resourcepool_object, placement_object = None, None
    if 'host' in placement:
        host_objects = get_hosts(service_instance, datacenter_name=datacenter, host_names=[placement['host']])
        if not host_objects:
            raise salt.exceptions.VMwareObjectRetrievalError(' '.join([
                'The specified host',
                '{0} cannot be found.'.format(placement['host'])]))
        try:
            host_props = \
                get_properties_of_managed_object(host_objects[0],
                                                 properties=['resourcePool'])
            resourcepool_object = host_props['resourcePool']
        except vmodl.query.InvalidProperty:
            traversal_spec = vmodl.query.PropertyCollector.TraversalSpec(
                path='parent',
                skip=True,
                type=vim.HostSystem,
                selectSet=[vmodl.query.PropertyCollector.TraversalSpec(
                    path='resourcePool',
                    skip=False,
                    type=vim.ClusterComputeResource)])
            resourcepools = get_mors_with_properties(service_instance,
                                                     vim.ResourcePool,
                                                     container_ref=host_objects[0],
                                                     property_list=['name'],
                                                     traversal_spec=traversal_spec)
            if resourcepools:
                resourcepool_object = resourcepools[0]['object']
            else:
                raise salt.exceptions.VMwareObjectRetrievalError(
                    'The resource pool of host {0} cannot be found.'.format(placement['host']))
        placement_object = host_objects[0]
    elif 'resourcepool' in placement:
        resourcepool_objects = get_resource_pools(service_instance,
                                                  [placement['resourcepool']],
                                                  datacenter_name=datacenter)
        if len(resourcepool_objects) > 1:
            raise salt.exceptions.VMwareMultipleObjectsError(' '.join([
                'Multiple instances are available of the',
                'specified host {}.'.format(placement['host'])]))
        resourcepool_object = resourcepool_objects[0]
        res_props = get_properties_of_managed_object(resourcepool_object,
                                                     properties=['parent'])
        if 'parent' in res_props:
            placement_object = res_props['parent']
        else:
            raise salt.exceptions.VMwareObjectRetrievalError(' '.join([
                'The resource pool\'s parent',
                'object is not defined']))
    elif 'cluster' in placement:
        datacenter_object = get_datacenter(service_instance, datacenter)
        cluster_object = get_cluster(datacenter_object, placement['cluster'])
        clus_props = get_properties_of_managed_object(cluster_object,
                                                      properties=['resourcePool'])
        if 'resourcePool' in clus_props:
            resourcepool_object = clus_props['resourcePool']
        else:
            raise salt.exceptions.VMwareObjectRetrievalError(' '.join([
                'The cluster\'s resource pool',
                'object is not defined']))
        placement_object = cluster_object
    else:
        # We are checking the schema for this object, this exception should never be raised
        raise salt.exceptions.VMwareObjectRetrievalError(' '.join([
            'Placement is not defined.']))
    return (resourcepool_object, placement_object)
def get_placement ( service_instance , datacenter , placement = None ) : log . trace ( 'Retrieving placement information' ) resourcepool_object , placement_object = None , None if 'host' in placement : host_objects = get_hosts ( service_instance , datacenter_name = datacenter , host_names = [ placement [ 'host' ] ] ) if not host_objects : raise salt . exceptions . VMwareObjectRetrievalError ( ' ' . join ( [ 'The specified host' , '{0} cannot be found.' . format ( placement [ 'host' ] ) ] ) ) try : host_props = get_properties_of_managed_object ( host_objects [ 0 ] , properties = [ 'resourcePool' ] ) resourcepool_object = host_props [ 'resourcePool' ] except vmodl . query . InvalidProperty : traversal_spec = vmodl . query . PropertyCollector . TraversalSpec ( path = 'parent' , skip = True , type = vim . HostSystem , selectSet = [ vmodl . query . PropertyCollector . TraversalSpec ( path = 'resourcePool' , skip = False , type = vim . ClusterComputeResource ) ] ) resourcepools = get_mors_with_properties ( service_instance , vim . ResourcePool , container_ref = host_objects [ 0 ] , property_list = [ 'name' ] , traversal_spec = traversal_spec ) if resourcepools : resourcepool_object = resourcepools [ 0 ] [ 'object' ] else : raise salt . exceptions . VMwareObjectRetrievalError ( 'The resource pool of host {0} cannot be found.' . format ( placement [ 'host' ] ) ) placement_object = host_objects [ 0 ] elif 'resourcepool' in placement : resourcepool_objects = get_resource_pools ( service_instance , [ placement [ 'resourcepool' ] ] , datacenter_name = datacenter ) if len ( resourcepool_objects ) > 1 : raise salt . exceptions . VMwareMultipleObjectsError ( ' ' . join ( [ 'Multiple instances are available of the' , 'specified host {}.' . format ( placement [ 'host' ] ) ] ) ) resourcepool_object = resourcepool_objects [ 0 ] res_props = get_properties_of_managed_object ( resourcepool_object , properties = [ 'parent' ] ) if 'parent' in res_props : placement_object = res_props [ 'parent' ] else : raise salt . exceptions . VMwareObjectRetrievalError ( ' ' . join ( [ 'The resource pool\'s parent' , 'object is not defined' ] ) ) elif 'cluster' in placement : datacenter_object = get_datacenter ( service_instance , datacenter ) cluster_object = get_cluster ( datacenter_object , placement [ 'cluster' ] ) clus_props = get_properties_of_managed_object ( cluster_object , properties = [ 'resourcePool' ] ) if 'resourcePool' in clus_props : resourcepool_object = clus_props [ 'resourcePool' ] else : raise salt . exceptions . VMwareObjectRetrievalError ( ' ' . join ( [ 'The cluster\'s resource pool' , 'object is not defined' ] ) ) placement_object = cluster_object else : raise salt . exceptions . VMwareObjectRetrievalError ( ' ' . join ( [ 'Placement is not defined.' ] ) ) return ( resourcepool_object , placement_object )
To create a virtual machine a resource pool needs to be supplied, we would like to use the strictest as possible.

    datacenter
        Name of the datacenter

    placement
        Dictionary with the placement info, cluster, host resource pool name

    return
        Resource pool, cluster and host object if any applies