o
    x[h´  ã                   @   sˆ   d dl Z d dlmZ d dlmZmZmZ d dlmZ e  	e
¡Z								ddd	„Z									dd
d„Z			ddd„ZdS )é    N)ÚMutableMapping)ÚnetÚ
url_helperÚutil)Úec2Úlatestúhttp://100.100.100.200é   c              	      sP   t  || ¡}t  |d¡}t j|||||||d�}	t |	j¡}
‡ fdd„‰ ˆ |
ƒS )Nzmeta-data/all©Ússl_detailsÚtimeoutÚretriesÚexception_cbÚ
headers_cbÚheaders_redactc                    sJ   t | tƒr‡ fdd„|  ¡ D ƒS t | tƒr‡ fdd„| D ƒS t ¡ d| ƒS )Nc                    s   i | ]	\}}|ˆ |ƒ“qS © r   )Ú.0ÚkÚv©Ú_process_dict_valuesr   úB/usr/lib/python3/dist-packages/cloudinit/sources/helpers/aliyun.pyÚ
<dictcomp>W   s    zHget_instance_meta_data.<locals>._process_dict_values.<locals>.<dictcomp>c                    s   g | ]}ˆ |ƒ‘qS r   r   )r   Úitemr   r   r   Ú
<listcomp>Y   s    zHget_instance_meta_data.<locals>._process_dict_values.<locals>.<listcomp>Ú )Ú
isinstanceÚdictÚitemsÚlistr   ÚMetadataLeafDecoder)Údr   r   r   r   U   s
   

z4get_instance_meta_data.<locals>._process_dict_values)r   Úcombine_urlÚread_file_or_urlr   Ú	load_jsonÚcontents)Úapi_versionÚmetadata_addressr   r   r   r   r   r   Úud_urlÚresponseÚmeta_data_rawr   r   r   Úget_instance_meta_data   s   
ù	4r+   c	              	   C   s†   t  || ¡}	t  |	|¡}	d}
ddg}||vrt d|¡ |
S zt j|	||||||d�}|j}
W |
S  tyB   t td||	¡ Y |
S w )Nó    z	user-datazvendor-dataz*aliyun datasource not support the item  %sr
   zFailed fetching %s from url %s)	r   r"   ÚLOGÚerrorr#   r%   Ú	Exceptionr   Úlogexc)r&   r'   r   r   r   r   r   r   Ú	item_namer(   ÚdataÚsupport_items_listr)   r   r   r   Úget_instance_data`   s4   þù	þþr4   Tc                 C   s®  di dœ}|st  ¡ }| d d }|sC| ¡ D ]
\}}||kr! nqddd| ¡ i|dœ}| |¡}	|	 d	¡r;d|d
< ||d |< |S tƒ }
| ¡ D ]\}}| |¡}	|	sVqJ||
|< qJt|
 ¡ t jd�}t	|ƒD ]4\}}|
| }| |¡}	d|d d i}d|dd| ¡ i|dœ}|	 d	¡r–d|d
< ||d< ||d |< qht
|d ƒdkrÕ|d  ¡ D ])}|d |  d¡ |d |  dd¡ |d |  dd¡ |d |  dd¡ q«|S )a�  Convert ecs metadata to network config version 2 data dict.

    @param: network_md: 'network' portion of ECS metadata.
    generally formed as {"interfaces": {"macs": {}} where
    'macs' is a dictionary with mac address as key:
    @param: macs_to_nics: Optional dict of mac addresses and nic names. If
    not provided, get_interfaces_by_mac is called to get it from the OS.
    @param: fallback_nic: Optionally provide the primary nic interface name.
    This nic will be guaranteed to minimally have a dhcp4 configuration.
    @param: full_network_config: Boolean set True to configure all networking
    presented by IMDS. This includes rendering secondary IPv4 and IPv6
    addresses on all NICs and rendering network config on secondary NICs.
    If False, only the primary nic will be configured and only with dhcp
    (IPv4/IPv6).

    @return A dict of network config version 2 based on the metadata and macs.
    é   )ÚversionÚ	ethernetsÚ
interfacesÚmacsTFÚ
macaddress)Údhcp4Údhcp6Úmatchúset-nameÚipv6sr<   r7   )Úkeyzroute-metricé   éd   )r;   údhcp4-overridesr<   r=   r>   zdhcp6-overridesrC   NÚrouteszrouting-policy)r   Úget_interfaces_by_macr   ÚlowerÚgetr   ÚsortedÚkeysÚnatural_sort_keyÚ	enumerateÚlenÚpop)Ú
network_mdÚmacs_to_nicsÚfallback_nicÚfull_network_configÚnetcfgÚmacs_metadataÚmacÚnic_nameÚ
dev_configÚnic_metadataÚnic_name_2_mac_mapÚorderd_nic_name_listÚnic_idxÚnic_macÚdhcp_overrider   r   r   Ú#convert_ecs_metadata_network_config…   sb   
ÿ
ü




ÿ

û
r]   )r   r   Nr	   r	   NNN)	r   r   Nr	   r	   NNNN)NNT)ÚloggingÚtypingr   Ú	cloudinitr   r   r   Úcloudinit.sources.helpersr   Ú	getLoggerÚ__name__r-   r+   r4   r]   r   r   r   r   Ú<module>   s8   

øU
÷'ü