def create_user_name_to_id_dict(client: Client, users_set: set, org_href):
    """Get a dictionary of users to user ids from a list of user names.

    :param Client client: current client
    :param set users_set: set of user names
    :param str org_href: href of the org to search in

    :return: dict of user name keys and user id values
    :rtype: dict
    :raise Exception is not all id's are found for users
    """
    own_users_set = users_set.copy()
    org = vcd_org.Org(client, org_href)
    org_users = org.list_users()
    user_name_to_id_dict = {}
    for user_str_elem in org_users:
        curr_user_dict = vcd_utils.to_dict(user_str_elem, exclude=[])
        curr_user_name = curr_user_dict['name']
        if curr_user_name in own_users_set:
            user_id = extract_id_from_href(curr_user_dict['href'])
            user_name_to_id_dict[
                curr_user_name] = shared_constants.USER_URN_PREFIX + user_id  # noqa: E501
            own_users_set.remove(curr_user_name)

        # Stop searching if all needed names and ids found
        if len(own_users_set) == 0:
            break
    if len(own_users_set) > 0:
        raise Exception(f"No user ids found for: {list(own_users_set)}")
    return user_name_to_id_dict
示例#2
0
    def native_get_vapp_settings_only_vapp_shared(
            self, def_entity_user_ids: set):  # noqa: E501
        """Get vapp settings in which the defined entity is not also shared.

        :param set def_entity_user_ids: set of user ids that have access to
            the defined entity

        :return: vapp settings in which only the vapp is shared
        :rtype: list
        """
        non_updated_access_settings = []
        vapp_access_settings: lxml.objectify.ObjectifiedElement = \
            self.vapp.get_access_settings()
        # Only add entries in which the defined entity is not shared
        if hasattr(vapp_access_settings, 'AccessSettings'):
            vapp_access_settings_attr = vapp_access_settings.AccessSettings
            for child_obj in vapp_access_settings_attr.getchildren():
                # Get user_urn
                child_obj_attrib = child_obj.getchildren()[0].attrib
                shared_href = child_obj_attrib.get('href')
                user_id = utils.extract_id_from_href(shared_href)
                user_urn = f'{shared_constants.USER_URN_PREFIX}{user_id}'

                # Add entries in which only vapp is shared
                if user_urn not in def_entity_user_ids:
                    user_name = child_obj_attrib.get('name')

                    curr_setting = form_vapp_access_setting_entry(
                        access_level=str(child_obj.AccessLevel),
                        name=user_name,
                        href=shared_href,
                        user_id=user_id)
                    non_updated_access_settings.append(curr_setting)
        return non_updated_access_settings
示例#3
0
    def __init__(self, gateway: vcd_gateway.Gateway,
                 client: vcd_client.Client):
        assert gateway.is_nsxt_backed()

        self._gateway = gateway
        self._client = client
        self._cloudapi_client = \
            pyvcloud_utils.get_cloudapi_client_from_vcd_client(client)
        gateway_id = core_utils.extract_id_from_href(self._gateway.href)
        self._gateway_urn = f'{nsxt_constants.GATEWAY_URN_PREFIX}:' \
                            f'{gateway_id}'
        self._gateway_relative_path = \
            f'{cloudapi_constants.CloudApiResource.EDGE_GATEWAYS}/' \
            f'{self._gateway_urn}'
示例#4
0
def create_org_user_id_to_name_dict(client: vcd_client.Client, org_name):
    """Get a dictionary of users ids to user names.

    :param vcd_client.Client client: current client
    :param str org_name: org name to search for users

    :return: dict of user id keys and user name values
    :rtype: dict
    """
    org_href = client.get_org_by_name(org_name).get('href')
    org = vcd_org.Org(client, org_href)
    users: list = org.list_users()
    user_id_to_name_dict = {}
    for user_str_elem in users:
        curr_user_dict = to_dict(user_str_elem, exclude=[])
        user_name = curr_user_dict['name']
        user_urn = USER_URN_PREFIX + \
            extract_id_from_href(curr_user_dict['href'])
        user_id_to_name_dict[user_urn] = user_name

    return user_id_to_name_dict