def derive_tenant(url):
    """Derive tenant ID from host

    We consider the standard `subdomain.domain.com` structure to be the
    `tenant_id`.

    There might be multiple applications that are hosted for the subdomain,
    and they may have additional application identifiers at the beginning,
    like 'customers.subdomain.domain.com' or 'backoffice.subdomain.domain.com'.
    In all such cases, we still consider the 3 part structure,
     `subdomain.domain.com`, to be the `tenant_id`.

    """
    from urllib.parse import urlparse
    host = urlparse(url).hostname
    return host
