
    x[h                         d dl Z d dlmZ d dlmZmZmZ d dlmZ  e j                  e
      Z	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 	 d	dZ	 	 	 d
dZy)    N)MutableMapping)net
url_helperutil)ec2c           	          t        j                  ||       }t        j                  |d      }t        j                  |||||||      }	t        j                  |	j
                        }
fd |
      S )Nzmeta-data/allssl_detailstimeoutretriesexception_cb
headers_cbheaders_redactc                    t        | t              r*| j                         D ci c]  \  }}| |       c}}S t        | t              r| D cg c]
  } |       c}S  t	        j
                         d|       S c c}}w c c}w )N )
isinstancedictitemslistr   MetadataLeafDecoder)dkvitem_process_dict_valuess       B/usr/lib/python3/dist-packages/cloudinit/sources/helpers/aliyun.pyr   z4get_instance_meta_data.<locals>._process_dict_valuesU   su    a;<779E41aA+A..EE4 ;<=4(.==,3**,R33	 F=s   A=B)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   s              @r   get_instance_meta_datar&      sx     ##$4kBF##FO<F**!%H !NN8+<+<=Mh4  ..    c	           	      L   t        j                  ||       }	t        j                  |	|      }	d}
ddg}||vrt        j                  d|       |
S 	 t        j                  |	||||||      }|j
                  }
|
S # t        $ r  t        j                  t        d||	       Y |
S w xY w)Nr'   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$   s                r   get_instance_datar0   `   s     ##$4kBF##FI6FD%}5**		8	
 N..#%!)
    K  NC99fMKNs   (A: :%B#"B#c                    di d}|st        j                         }| d   d   }|sg|j                         D ]  \  }}||k(  s n dddj                         id}|j	                  |      }	|	j	                  d	      rd|d
<   ||d   |<   |S t               }
|j                         D ]  \  }}|j	                  |      }	|	s||
|<     t        |
j                         t         j                        }t        |      D ]`  \  }}|
|   }|j	                  |      }	d|dz   dz  i}d|dd|j                         i|d}|	j	                  d	      r
d|d
<   ||d<   ||d   |<   b t        |d         dk(  rw|d   j                         D ]a  }|d   |   j                  d       |d   |   j                  dd       |d   |   j                  dd       |d   |   j                  dd       c |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ipv6sr9   r4   )keyzroute-metric   d   )r8   dhcp4-overridesr9   r:   r;   zdhcp6-overridesr@   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_overrides                  r   #convert_ecs_metadata_network_configrZ      s]   . *+<F002|,V4M)//1 	MC<'	 "CIIK0 	&

 %((-G$"&Jw(2{H%%++- +X$((-'*8$	+ "!s';'; '';< 3$X.$((1''A+)<=,"GMMO4 

 G$"&Jw,9J()(2{H%3$ 6+1${+002 	FH;)--.?@;)--.?F;)--h=;)--.>E		F
 Mr'   )latesthttp://100.100.100.200N   r]   NNN)	r[   r\   Nr]   r]   NNNN)NNT)loggingtypingr   	cloudinitr   r   r   cloudinit.sources.helpersr   	getLogger__name__r)   r&   r0   rZ    r'   r   <module>re      s}     ! + + )g! -Q/j -"N 	Nr'   