File: //lib/python3/dist-packages/cloudinit/sources/azure/__pycache__/imds.cpython-38.pyc
U
Ӈg$ � @ s� d dl Z d dlZd dlmZ d dlmZmZmZmZ d dl Z d dl
mZ d dlm
Z
d dlmZmZ e �e�ZdZdd � ZG d
d� d�Zdd
d�eeeeed�dd�Zeeed�dd�Zdeee ed�dd�Zed�dd�ZdS )� N)� monotonic)�Dict�Optional�Type�Union)�util)�report_diagnostic_event)�UrlError�readurlzhttp://169.254.169.254/metadatac C s dt t�� �d�S )N�true)ZMetadatazx-ms-client-request-id)�str�uuidZuuid4)Z_url� r �>/usr/lib/python3/dist-packages/cloudinit/sources/azure/imds.py�
headers_cb s
�r c @ sF e Zd ZdZddddd�eee ee dd�dd�Zed �d
d�Z dS )�ReadUrlRetryHandleraK 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 monotonic()-based deadline to retry until.
� �?N)� � � i� ��logging_backoff�max_connection_errors�retry_codes�retry_deadline)r r r �returnc C s. || _ || _|| _|| _d| _d| _d | _d S )Nr r )r r r r �_logging_threshold�_request_count�_last_error)�selfr r r r r r r �__init__% s
zReadUrlRetryHandler.__init__�r c C sJ | j d7 _ t|t�s.td| tjd� dS d}| jd k rNt� | jkrNd}nd}| jd k r�t|j t
j�r�| jd8 _| jdkr�d}n|jd k r�|j| j
kr�d}| j | jkr�| j| j9 _nd}|jd k r�| j|jkr�d}|j| _n:| jd k�st| jt��rt|j | j��s"d}t|j �| _|�s.|�sFtd| j |f tjd� |S )N� z1Polling IMDS failed with unexpected exception: %r�Zlogger_funcFTr z1Polling IMDS failed attempt %d with exception: %r)r �
isinstancer r �LOG�warningr r r �cause�requests�ConnectionError�coder r r r �type)r Z exception�logZretryr r r �exception_callback: sf
���
� �
�
�
�
����z&ReadUrlRetryHandler.exception_callback)
�__name__�
__module__�__qualname__�__doc__�floatr �intr �boolr- r r r r r s ��r T� )�log_response�timeout)�url�
retry_handlerr6 r7 r c
C sX zt | |jtd||d�}W n6 tk
rP } ztd| tjd� � W 5 d}~X Y nX |jS )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.
T�Zexception_cbr ZinfiniteZlog_req_respr7 z&Failed to fetch metadata from IMDS: %sr# N)r
r- r r r r% r&