U
    .b}9                     @   s`   d Z ddlZddlZddlmZmZmZ eeZ	dZ
di iZG dd dZG dd	 d	eZdS )
zResolves regions and endpoints.

This module implements endpoint resolution, including resolving endpoints for a
given service and region and resolving the available endpoints for a service
in a specific AWS partition.
    N)EndpointVariantErrorNoRegionErrorUnknownRegionErrorz{service}.{region}.{dnsSuffix}	endpointsc                   @   s,   e Zd ZdZdddZdd Zdd	d
ZdS )BaseEndpointResolverz3Resolves regions and endpoints. Must be subclassed.Nc                 C   s   t dS )a7  Resolves an endpoint for a service and region combination.

        :type service_name: string
        :param service_name: Name of the service to resolve an endpoint for
            (e.g., s3)

        :type region_name: string
        :param region_name: Region/endpoint name to resolve (e.g., us-east-1)
            if no region is provided, the first found partition-wide endpoint
            will be used if available.

        :rtype: dict
        :return: Returns a dict containing the following keys:
            - partition: (string, required) Resolved partition name
            - endpointName: (string, required) Resolved endpoint name
            - hostname: (string, required) Hostname to use for this endpoint
            - sslCommonName: (string) sslCommonName to use for this endpoint.
            - credentialScope: (dict) Signature version 4 credential scope
              - region: (string) region name override when signing.
              - service: (string) service name override when signing.
            - signatureVersions: (list<string>) A list of possible signature
              versions, including s3, v4, v2, and s3v4
            - protocols: (list<string>) A list of supported protocols
              (e.g., http, https)
            - ...: Other keys may be included as well based on the metadata
        NNotImplementedError)selfservice_nameregion_name r   4/tmp/pip-unpacked-wheel-7df6wvge/botocore/regions.pyconstruct_endpoint$   s    z'BaseEndpointResolver.construct_endpointc                 C   s   t dS )zLists the partitions available to the endpoint resolver.

        :return: Returns a list of partition names (e.g., ["aws", "aws-cn"]).
        Nr   )r	   r   r   r   get_available_partitionsA   s    z-BaseEndpointResolver.get_available_partitionsawsFc                 C   s   t dS )a  Lists the endpoint names of a particular partition.

        :type service_name: string
        :param service_name: Name of a service to list endpoint for (e.g., s3)

        :type partition_name: string
        :param partition_name: Name of the partition to limit endpoints to.
            (e.g., aws for the public AWS endpoints, aws-cn for AWS China
            endpoints, aws-us-gov for AWS GovCloud (US) Endpoints, etc.

        :type allow_non_regional: bool
        :param allow_non_regional: Set to True to include endpoints that are
             not regional endpoints (e.g., s3-external-1,
             fips-us-gov-west-1, etc).
        :return: Returns a list of endpoint names (e.g., ["us-east-1"]).
        Nr   )r	   r
   partition_nameallow_non_regionalr   r   r   get_available_endpointsH   s    z,BaseEndpointResolver.get_available_endpoints)N)r   F)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   !   s   
   r   c                   @   s   e Zd ZdZddgZdd Zd%ddZd	d
 Zd&ddZd'ddZ	d(ddZ
dd Zd)ddZdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ ZdS )*EndpointResolverz7Resolves endpoints based on partition endpoint metadatazaws-isoz	aws-iso-bc                 C   s   d|krt d|| _dS )zA
        :param endpoint_data: A dict of partition data.
        
partitionsz%Missing "partitions" in endpoint dataN)
ValueError_endpoint_data)r	   endpoint_datar   r   r   __init__c   s    zEndpointResolver.__init__r   c                 C   sB   | j d D ]2}|d |krq
|d }||kr.q
|| d   S d S )Nr   	partitionservicesr   )r   )r	   r
   r   r   r   r   r   r   get_service_endpoints_datak   s    z+EndpointResolver.get_service_endpoints_datac                 C   s&   g }| j d D ]}||d  q|S )Nr   r   )r   append)r	   resultr   r   r   r   r   t   s    z)EndpointResolver.get_available_partitionsFNc                 C   s   g }| j d D ]}|d |kr q|d }||kr2q|| d }|D ]J}	|	|d k}
|rz|
rz| ||	 |}|r||	 qB|s|
rB||	 qBq|S )Nr   r   r   r   regions)r   _retrieve_variant_datar!   )r	   r
   r   r   endpoint_variant_tagsr"   r   r   Zservice_endpointsendpoint_nameZis_regional_endpointZvariant_datar   r   r   r   z   s(     z(EndpointResolver.get_available_endpointsc                 C   s\   | j d D ]L}|d |kr
|rJ| |d|}|rVd|krV|d   S q
|d   S q
d S )Nr   r   defaults	dnsSuffix)r   r$   get)r	   r   r%   r   variantr   r   r   get_partition_dns_suffix   s     z)EndpointResolver.get_partition_dns_suffixc           	      C   s   |dkr|r|d krd}|d k	rhd }| j d D ]}|d |kr.|}q.|d k	rd| |||||d}|S d S | j d D ]6}|r|d | jkrqr| |||||}|rr|  S qrd S )NZs3z	us-east-1r   r   T)r   _endpoint_for_partition!_UNSUPPORTED_DUALSTACK_PARTITIONS)	r	   r
   r   r   use_dualstack_endpointuse_fips_endpointZvalid_partitionr   r"   r   r   r   r      sN    	z#EndpointResolver.construct_endpointc                 C   s8   | j d D ]}| ||r
|d   S q
t|ddd S )Nr   r   z,No partition found for provided region_name.)r   	error_msg)r   _region_matchr   )r	   r   r   r   r   r   get_partition_for_region   s    z)EndpointResolver.get_partition_for_regionc                 C   s   |d }|r,|| j kr,d| }tdg|d|d |t}	|d kr\d|	krV|	d }nt |||	|||d}
||	d kr| jf |
S | ||s|r|	d}|	d	d
}|r|std||| ||
d< | jf |
S td|| | jf |
S d S )Nr   z@Dualstack endpoints are currently not supported for %s partition	dualstacktagsr0   r   ZpartitionEndpoint)r   r
   service_datar&   r.   r/   r   ZisRegionalizedTz'Using partition endpoint for %s, %s: %sr&   z*Creating a regex based endpoint for %s, %s)	r-   r   r)   DEFAULT_SERVICE_DATAr   _resolver1   LOGdebug)r	   r   r
   r   r.   r/   Zforce_partitionr   r0   r6   Zresolve_kwargsZpartition_endpointZis_regionalizedr   r   r   r,      sZ    	 


z(EndpointResolver._endpoint_for_partitionc                 C   s0   ||d krdS d|kr,t |d |S dS )Nr#   TZregionRegexF)recompilematch)r	   r   r   r   r   r   r1   !  s
    zEndpointResolver._region_matchc                 C   s>   | dg }|D ](}t|d t|kr| }|  S qd S )Nvariantsr5   )r)   setcopy)r	   r   r5   r>   r*   r"   r   r   r   r$   (  s
    z'EndpointResolver._retrieve_variant_datac                 C   s$   g }|r| d |r | d |S )Nr3   Zfips)r!   )r	   r.   r/   r5   r   r   r   _create_tag_list/  s    

z!EndpointResolver._create_tag_listc                 C   s4   i }|||fD ] }|  ||}|r| || q|S N)r$   _merge_keys)r	   r5   r   service_defaultspartition_defaultsr"   r>   r*   r   r   r   _resolve_variant7  s    z!EndpointResolver._resolve_variantc                 C   s$  | di  |i }| dr,td|  | di }| di }	| ||}
|
r| |
|||	}|i krd| d| }t|
|d| || n|}d|kr|d |d< |d	 |d	< ||d
< | || | |	| | ||d |||d |d< d|kr | ||d |||d |d< |S )Nr   
deprecatedz5Client is configured with the deprecated endpoint: %sr'   zEndpoint does not exist for z in region r4   r(   r   ZendpointNamehostnameZsslCommonName)r)   r9   warningrA   rF   r   rC   _expand_template)r	   r   r
   r6   r&   r.   r/   r   rD   rE   r5   r"   r0   r   r   r   r8   A  s`    	 
   
zEndpointResolver._resolvec                 C   s"   |D ]}||kr|| ||< qd S rB   r   )r	   Z	from_datar"   keyr   r   r   rC     s    zEndpointResolver._merge_keysc                 C   s   |j |||dS )N)ZserviceZregionr(   )format)r	   r   templater
   r&   r(   r   r   r   rJ     s
      z!EndpointResolver._expand_template)r   )r   FN)N)NNFF)F)r   r   r   r   r-   r   r    r   r   r+   r   r2   r,   r1   r$   rA   rF   r8   rC   rJ   r   r   r   r   r   ^   s4   
		   
 
    
2 
A
Br   )r   loggingr;   Zbotocore.exceptionsr   r   r   	getLoggerr   r9   ZDEFAULT_URI_TEMPLATEr7   r   r   r   r   r   r   <module>   s   
=