
    x[h                     x    S SK r S SKrS SKJr  S SKJr  S SKJr  \R                  " \5      r	S r
S rS	S jrS rg)
    N)dmi)net)
url_helperc           
      $   S n/ n[         R                  " 5       n[        R                  SU5        U R	                  S5       H  n[        R                  SU5        UR	                  S5      nXS;  a  [        SU< SU< 35      eUR	                  S5      nUR	                  U5      n[        R                  S	UUUUR	                  S
5      5        SUUS.n/ n	UR	                  S5       H+  n
U" XR	                  S5      5      nU	R                  U5        M-     XS'   UR                  U5        M     U R	                  S5      (       aH  [        R                  SU R	                  S5      5        UR                  SU R	                  S5      S.5        SUS.$ )a  
Convert the UpCloud network metadata description into
Cloud-init's version 1 netconfig format.

Example JSON:
{
  "interfaces": [
    {
      "index": 1,
      "ip_addresses": [
        {
          "address": "94.237.105.53",
          "dhcp": true,
          "dns": [
            "94.237.127.9",
            "94.237.40.9"
          ],
          "family": "IPv4",
          "floating": false,
          "gateway": "94.237.104.1",
          "network": "94.237.104.0/22"
        },
        {
          "address": "94.237.105.50",
          "dhcp": false,
          "dns": [],
          "family": "IPv4",
          "floating": true,
          "gateway": "",
          "network": "94.237.105.50/32"
        }
      ],
      "mac": "32:d5:ba:4a:36:e7",
      "network_id": "031457f4-0f8c-483c-96f2-eccede02909c",
      "type": "public"
    },
    {
      "index": 2,
      "ip_addresses": [
        {
          "address": "10.6.3.27",
          "dhcp": true,
          "dns": [],
          "family": "IPv4",
          "floating": false,
          "gateway": "10.6.0.1",
          "network": "10.6.0.0/22"
        }
      ],
      "mac": "32:d5:ba:4a:84:cc",
      "network_id": "03d82553-5bea-4132-b29a-e1cf67ec2dd1",
      "type": "utility"
    },
    {
      "index": 3,
      "ip_addresses": [
        {
          "address": "2a04:3545:1000:720:38d6:baff:fe4a:63e7",
          "dhcp": true,
          "dns": [
            "2a04:3540:53::1",
            "2a04:3544:53::1"
          ],
          "family": "IPv6",
          "floating": false,
          "gateway": "2a04:3545:1000:720::1",
          "network": "2a04:3545:1000:720::/64"
        }
      ],
      "mac": "32:d5:ba:4a:63:e7",
      "network_id": "03000000-0000-4000-8046-000000000000",
      "type": "public"
    },
    {
      "index": 4,
      "ip_addresses": [
        {
          "address": "172.30.1.10",
          "dhcp": true,
          "dns": [],
          "family": "IPv4",
          "floating": false,
          "gateway": "172.30.1.1",
          "network": "172.30.1.0/24"
        }
      ],
      "mac": "32:d5:ba:4a:8a:e1",
      "network_id": "035a0a4a-77b4-4de5-820d-189fc8135714",
      "type": "private"
    }
  ],
  "dns": [
    "94.237.127.9",
    "94.237.40.9"
  ]
}
c                    U R                  S5      (       a  SnU R                  S5      S:X  a  SnSU0$ SnU R                  S5      S:X  a  SnUSU R                  S	5      S
.nU R                  S5      (       a  U R                  S5      US'   SU R                  S5      ;   a&  U R                  S5      R                  S5      S   US'   XR                  S5      :w  a*  U R                  S5      (       a  U R                  S5      US'   U$ )NdhcpfamilyIPv6zipv6_dhcpv6-statelesstypestaticstatic6autoaddress)r   controlr   gateway/network   netmaskdnsdns_nameservers)getsplit)ip_addrr   	dhcp_typestatic_typesubparts        C/usr/lib/python3/dist-packages/cloudinit/sources/helpers/upcloud.py_get_subnet_config8convert_to_network_config_v1.<locals>._get_subnet_configr   s    ;;vI{{8$. 4	I&&;;x F*#K{{9-
 ;;y!!!(Y!7GI'++i((!(Y!7!=!=c!B1!EGI++e$$U););)0U);G%&    zNIC mapping: %s
interfaceszConsidering %smacz3Did not find network interface on system with mac 'z'. Cannot apply configuration: r   z4Found %s interface '%s' with address '%s' (index %d)indexphysical)r   namemac_addressip_addressesr   subnetszSetting DNS nameservers to %s
nameserver)r   r   r   )versionconfig)cloudnetget_interfaces_by_macLOGdebugr   RuntimeErrorappend)r,   r   nic_configsmacs_to_interfaces	raw_ifacer'   
iface_type
sysfs_name	interfacer)   
ip_addresssub_parts               r   convert_to_network_config_v1r;      s{   F: K!779II!34ZZ-			"I.mmE*0 	+  ]]6*
'++K8
		BMM'"	
 &
	 #--7J)*jj6GHHNN8$ 8  ')9%E .H zz%		16::e3DE!fjj.?@	
 K00r!   c                     [        U 5      $ )N)r;   )r,   s    r   convert_network_configr=      s    '//r!   c                     [         R                  " XX#S9nUR                  5       (       d  [        SU -  5      e[        R
                  " UR                  R                  5       5      $ )N)timeoutsec_betweenretrieszunable to read metadata at %s)r   readurlokr1   jsonloadscontentsdecode)urlr?   r@   rA   responses        r   read_metadatarJ      sS    !!+H ;;==:S@AA::h''..011r!   c                      [         R                  " S5      n U S:w  a  g[         R                  " S5      nU(       a  [        R                  SU5        SU4$ Sn[        R	                  U5        [        U5      e)Nzsystem-manufacturerUpCloud)FNzsystem-uuidz2system identified via SMBIOS as UpCloud server: %szsystem identified via SMBIOS as a UpCloud server, but did not provide an ID. Please contact support viahttps://hub.upcloud.com or via email with support@upcloud.comT)r   read_dmi_datar/   r0   criticalr1   )vendor_nameserver_uuidmsgs      r   read_sysinforR      su     ##$9:Ki##M2K		@+	
 L 	
 	S3r!   )   rS      )rD   logging	cloudinitr   r   r-   r   	getLogger__name__r/   r;   r=   rJ   rR    r!   r   <module>rZ      s<   
    %  !n1b02r!   