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 )
zÛResolves 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 )z�Lists 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rŠqr|  |||||¡}|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Ø|sØt 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rˆd|› 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   
=