U
    Ld                     @   s   d dl m Z  d dlmZmZ d dlZd dlmZ d dlmZ d dl	m
Z
 d dlmZmZ eeZdZG d	d
 d
ZdddeeeeedddZeeedddZeedddZedddZdS )    )time)DictOptionalN)log)util)report_diagnostic_event)UrlErrorreadurlzhttp://169.254.169.254/metadatac                   @   sB   e Zd ZdZdddddeeee dddd	Zed
ddZ	dS )ReadUrlRetryHandleraF  Manager for readurl retry behavior using exception_callback().

    :param logging_backoff: Backoff to limit logging.
    :param max_connection_errors: Number of connection errors to retry on.
    :param retry_codes: Set of http codes to retry on.
    :param retry_deadline: Optional time()-based deadline to retry until.
          ?
   )    i  i  Nlogging_backoffmax_connection_errorsretry_codesretry_deadline)r   r   r   returnc                C   s(   || _ || _|| _|| _d| _d| _d S )Nr   r   )r   r   r   r   _logging_threshold_request_count)selfr   r   r   r    r   >/usr/lib/python3/dist-packages/cloudinit/sources/azure/imds.py__init__   s    zReadUrlRetryHandler.__init__r   c                 C   s   |  j d7  _ t|ts.td| tjd dS d}| jd k	rNt | jkrNd}nd}t|jt	j
r~|  jd8  _| jdk rd}n|jd k	r|j| jkrd}| j | jkr|  j| j9  _nd}|s|std| j |f tjd |S )N   z1Polling IMDS failed with unexpected exception: %rZlogger_funcFTr   z1Polling IMDS failed attempt %d with exception: %r)r   
isinstancer   r   LOGwarningr   r   causerequestsConnectionErrorr   coder   r   r   info)r   Zreq_argsZ	exceptionr   Zretryr   r   r   exception_callback1   sB    


z&ReadUrlRetryHandler.exception_callback)
__name__
__module____qualname____doc__floatintr   r   boolr&   r   r   r   r   r
      s   r
   T   )log_responsetimeout)urlr   r/   r0   r   c             
   C   sf   t |d}zt| |jddid||d}W n6 tk
r^ } ztd| tjd  W 5 d}~X Y nX |jS )	a'  Fetch URL from IMDS.

    :param url: url to fetch.
    :param log_response: log responses in readurl().
    :param retry_deadline: time()-based deadline to retry until.
    :param timeout: Read/connection timeout in seconds for readurl().

    :raises UrlError: on error fetching metadata.
    r   MetadatatrueTZexception_cbZheadersZinfiniteZlog_req_respr0   z&Failed to fetch metadata from IMDS: %sr   N)r
   r	   r&   r   r   r   r    contents)r1   r   r/   r0   handlerresponseerrorr   r   r   
_fetch_url\   s"    

r:   )r1   r   r   c              
   C   sT   t | |d}zt|W S  tk
rN } ztd| tjd  W 5 d}~X Y nX dS )zFetch IMDS metadata.

    :param url: url to fetch.
    :param retry_deadline: time()-based deadline to retry until.

    :raises UrlError: on error fetching metadata.
    :raises ValueError: on error parsing metadata.
    r2   z&Failed to parse metadata from IMDS: %sr   N)r:   r   Z	load_json
ValueErrorr   r   r    )r1   r   Zmetadatar9   r   r   r   _fetch_metadata   s    r<   )r   r   c              
   C   st   zt d }t|| dW S  tk
rn } z:|jdkr\tdtjd t d }t|| d W Y S  W 5 d}~X Y nX dS )zFetch extended metadata, falling back to non-extended as required.

    :param retry_deadline: time()-based deadline to retry until.

    :raises UrlError: on error fetching metadata.
    :raises ValueError: on error parsing metadata.
    z./instance?api-version=2021-08-01&extended=truer2   i  z,Falling back to IMDS api-version: 2019-06-01r   z /instance?api-version=2019-06-01N)IMDS_URLr<   r   r$   r   r   r    )r   r1   r9   r   r   r    fetch_metadata_with_api_fallback   s    
r>   r   c                  C   sT   t d } tddddd}t| |jddid	d
dd}td|jd  dtjd |jS )zhFetch extended metadata, falling back to non-extended as required.

    :raises UrlError: on error.
    z'/reprovisiondata?api-version=2019-06-01g       @r   )r   r   Nr   r3   r4   TFr.   r5   zPolled IMDS r   z time(s)r   )	r=   r
   r	   r&   r   r   r   debugr6   )r1   r7   r8   r   r   r   fetch_reprovision_data   s(    		r@   )r   typingr   r   r"   Z	cloudinitr   Zloggingr   Zcloudinit.sources.helpers.azurer   Zcloudinit.url_helperr   r	   Z	getLoggerr'   r   r=   r
   strr+   r-   r,   bytesr:   r<   r>   r@   r   r   r   r   <module>   s0   
L&