o
    f                     @   s   d dl Z d dlZd dlZd dlZd dlmZ d dlmZ d dlm	Z	 e
eZdedefddZdedefd	d
ZdefddZdefddZdefddZG dd de jZdS )    N)Optional)dmi)report_diagnostic_eventsystem_uuidreturnc                 C   sJ   zt | }W n ty   d| }t|tjd  w tt j|jdS )a  Byte swap system uuid.

    Azure always uses little-endian for the first three fields in the uuid.
    This behavior was made strict in SMBIOS 2.6+, but Linux and dmidecode
    follow RFC 4122 and assume big-endian for earlier SMBIOS versions.

    Azure's gen1 VMs use SMBIOS 2.3 which requires byte swapping to match
    compute.vmId presented by IMDS.

    Azure's gen2 VMs use SMBIOS 3.1 which does not require byte swapping.

    :raises ValueError: if UUID is invalid.
    zFailed to parse system uuid: logger_func)bytes)uuidUUID
ValueErrorr   LOGerrorstrbytes_le)r   original_uuidmsg r   B/usr/lib/python3/dist-packages/cloudinit/sources/azure/identity.pybyte_swap_system_uuid   s   
r   c                 C   s   t  rt| S | S )z!Determine VM ID from system uuid.)
is_vm_gen1r   r   r   r   r   convert_system_uuid_to_vm_id)   s   r   c                   C   s$   t jdrdS t jdrdS dS )zZDetermine if VM is gen1 or gen2.

    Gen2 guests use UEFI while gen1 is legacy BIOS.
    z/sys/firmware/efiFz/dev/efiT)ospathexistsr   r   r   r   r   1   s
   r   c                  C   s2   t d} | du rtd|  } td|  | S )z Query system uuid in lower-case.zsystem-uuidNzfailed to read system-uuidzRead product uuid: %s)r   read_dmi_dataRuntimeErrorlowerr   debugr   r   r   r   query_system_uuidA   s   
r    c                  C   s   t  } t| S )zQuery VM ID from system.)r    r   r   r   r   r   query_vm_idM   s   r!   c                   @   s&   e Zd ZdZeded  fddZdS )ChassisAssetTagz 7783-7084-3265-9085-8269-3286-77r   c                 C   sZ   t d}z| |}W n ty   td| tjd Y dS w td||jf tjd |S )zCheck platform environment to report if this datasource may run.

        :returns: ChassisAssetTag if matching tag found, else None.
        zchassis-asset-tagzNon-Azure chassis asset tag: %rr   Nz Azure chassis asset tag: %r (%s))r   r   r   r   r   r   name)cls	asset_tagtagr   r   r   query_systemV   s   
zChassisAssetTag.query_systemN)__name__
__module____qualname__AZURE_CLOUDclassmethodr   r'   r   r   r   r   r"   S   s    r"   )enumloggingr   r
   typingr   	cloudinitr   cloudinit.sources.helpers.azurer   	getLoggerr(   r   r   r   r   boolr   r    r!   Enumr"   r   r   r   r   <module>   s   
