def get_default_domain(request, get_name=True):
    """Gets the default domain object to use when creating Identity object.

    Returns the domain context if is set, otherwise return the domain
    of the logon user.

    :param get_name: Whether to get the domain name from Keystone if the
        context isn't set.  Setting this to False prevents an unnecessary call
        to Keystone if only the domain ID is needed.
    """
    domain_id = request.session.get("domain_context", None)
    domain_name = request.session.get("domain_context_name", None)
    # if running in Keystone V3 or later
    if VERSIONS.active >= 3 and domain_id is None:
        # if no domain context set, default to user's domain
        domain_id = request.user.user_domain_id
        domain_name = request.user.user_domain_name
        if get_name and not request.user.is_federated:
            try:
                domain = domain_get(request, domain_id)
                domain_name = domain.name
            except exceptions.NotAuthorized:
                # NOTE (knasim-wrs): Retrieving domain information
                # is an admin URL operation. As a pre-check, such
                # operations would be Forbidden if the logon user does
                # not have an 'admin' role on the current project.
                #
                # Since this can be a common occurence and can cause
                # incessant warning logging in the horizon logs,
                # we recognize this condition and return the user's
                # domain information instead.
                LOG.debug("Cannot retrieve domain information for "
                          "user (%(user)s) that does not have an admin role "
                          "on project (%(project)s)",
                          {'user': request.user.username,
                           'project': request.user.project_name})
            except Exception:
                LOG.warning("Unable to retrieve Domain: %s", domain_id)
    domain = base.APIDictWrapper({"id": domain_id,
                                  "name": domain_name})
    return domain
Gets the default domain object to use when creating Identity object.

    Returns the domain context if is set, otherwise return the domain
    of the logon user.

    :param get_name: Whether to get the domain name from Keystone if the
        context isn't set.  Setting this to False prevents an unnecessary call
        to Keystone if only the domain ID is needed.