Exemple #1
0
def _get_network_obj(session, network_objects, network_name):
    """Gets the network object for the requested network.

    The network object will be used when creating the VM configuration
    spec. The network object contains the relevant network details for
    the specific network type, for example, a distributed port group.

    The method will search for the network_name in the list of
    network_objects.

    :param session: vCenter soap session
    :param network_objects: group of networks
    :param network_name: the requested network
    :return: network object
    """

    network_obj = {}
    # network_objects is actually a RetrieveResult object from vSphere API call
    for obj_content in network_objects:
        # the propset attribute "need not be set" by returning API
        if not hasattr(obj_content, 'propSet'):
            continue
        prop_dict = vm_util.propset_dict(obj_content.propSet)
        network_refs = prop_dict.get('network')
        if network_refs:
            network_refs = network_refs.ManagedObjectReference
            for network in network_refs:
                # Get network properties
                if network._type == 'DistributedVirtualPortgroup':
                    props = session._call_method(
                        vim_util, "get_dynamic_property", network,
                        "DistributedVirtualPortgroup", "config")
                    # NOTE(asomya): This only works on ESXi if the port binding
                    # is set to ephemeral
                    # For a VLAN the network name will be the UUID. For a VXLAN
                    # network this will have a VXLAN prefix and then the
                    # network name.
                    if network_name in props.name:
                        network_obj['type'] = 'DistributedVirtualPortgroup'
                        network_obj['dvpg'] = props.key
                        dvs_props = session._call_method(
                            vim_util, "get_dynamic_property",
                            props.distributedVirtualSwitch,
                            "VmwareDistributedVirtualSwitch", "uuid")
                        network_obj['dvsw'] = dvs_props
                        return network_obj
                else:
                    props = session._call_method(vim_util,
                                                 "get_dynamic_property",
                                                 network, "Network",
                                                 "summary.name")
                    if props == network_name:
                        network_obj['type'] = 'Network'
                        network_obj['name'] = network_name
                        return network_obj
Exemple #2
0
def _get_allowed_datastores(data_stores, datastore_regex):
    allowed = []
    for obj_content in data_stores.objects:
        # the propset attribute "need not be set" by returning API
        if not hasattr(obj_content, "propSet"):
            continue

        propdict = vm_util.propset_dict(obj_content.propSet)
        if _is_datastore_valid(propdict, datastore_regex, ALL_SUPPORTED_DS_TYPES):
            allowed.append(ds_obj.Datastore(ref=obj_content.obj, name=propdict["summary.name"]))

    return allowed
Exemple #3
0
def _select_datastore(
    session, data_stores, best_match, datastore_regex=None, storage_policy=None, allowed_ds_types=ALL_SUPPORTED_DS_TYPES
):
    """Find the most preferable datastore in a given RetrieveResult object.

    :param session: vmwareapi session
    :param data_stores: a RetrieveResult object from vSphere API call
    :param best_match: the current best match for datastore
    :param datastore_regex: an optional regular expression to match names
    :param storage_policy: storage policy for the datastore
    :param allowed_ds_types: a list of acceptable datastore type names
    :return: datastore_ref, datastore_name, capacity, freespace
    """

    if storage_policy:
        matching_ds = _filter_datastores_matching_storage_policy(session, data_stores, storage_policy)
        if not matching_ds:
            return best_match
    else:
        matching_ds = data_stores

    # data_stores is actually a RetrieveResult object from vSphere API call
    for obj_content in matching_ds.objects:
        # the propset attribute "need not be set" by returning API
        if not hasattr(obj_content, "propSet"):
            continue

        propdict = vm_util.propset_dict(obj_content.propSet)
        if _is_datastore_valid(propdict, datastore_regex, allowed_ds_types):
            new_ds = ds_obj.Datastore(
                ref=obj_content.obj,
                name=propdict["summary.name"],
                capacity=propdict["summary.capacity"],
                freespace=propdict["summary.freeSpace"],
            )
            # favor datastores with more free space
            if best_match is None or new_ds.freespace > best_match.freespace:
                best_match = new_ds

    return best_match