o
    i$                     @  s\   d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 dddZG dd dZdS )z8Resolve the active tenant for an incoming Flask request.    )annotations)Request)Session)get_control_plane_engine)Tenantrequestr   returndict[str, str]c                 C  s   | j dpd }|stdt }t|dd?}|t|}|du r+td| |jr3|j	dkr:t
d	| |j|j|jpBd|jpJ|jpJ|j|jpNdd
W  d   S 1 s[w   Y  dS )a  Resolve a tenant from the ``X-Tenant-ID`` header.

    Raises:
        ValueError: when the request does not include a tenant identifier.
        LookupError: when the tenant cannot be found.
        PermissionError: when the tenant exists but is inactive.
    zX-Tenant-ID zMissing X-Tenant-ID headerT)futureNzTenant not found: activezTenant inactive: )	tenant_idslugdb_namestorage_keyplan)headersgetstrip
ValueErrorr   r   r   LookupError	is_activestatusPermissionErrorr   r   data_db_namer   plan_id)r   r   enginesessiontenant r   #platform/tenants/tenant_resolver.pyresolve_tenant   s"   $r!   c                   @  s   e Zd ZdZd	ddZdS )
TenantResolverz4Compatibility wrapper around :func:`resolve_tenant`.r   r   r   r	   c                 C  s   t |S )N)r!   )selfr   r   r   r    resolve_from_request+   s   z#TenantResolver.resolve_from_requestNr   r   r   r	   )__name__
__module____qualname____doc__r$   r   r   r   r    r"   (   s    r"   Nr%   )r)   
__future__r   flaskr   sqlalchemy.ormr   config.control_planer   platform.tenants.modelsr   r!   r"   r   r   r   r    <module>   s    
