o
    K&b7                     @   s~   d dl Z d dlZd dlZd dlmZmZmZ d dlmZ d dl	m
Z
 d dlmZ dZdZdZd	d
dddZG dd deZdS )    N)AnyDictList)	HTTPError)util)AutoAttachCloudInstancezhttp://metadata/computeMetadata/v1/instance/service-accounts/default/identity?audience=contracts.canonical.com&format=full&licenses=TRUEz/sys/class/dmi/id/product_namezGoogle Compute EngineZ8045211386737108299Z6022427724719891830Z599959289349842382Z2592866803419978320)ZxenialZbionicZfocalZjammyc                   @   sn   e Zd Zeejeg dddeee	f fddZ
edefddZedefdd	Zdee fd
dZdS )UAAutoAttachGCPInstance)         )Zretry_sleepsreturnc                 C   s   t jtddid\}}d|iS )NzMetadata-FlavorZGoogle)ZheadersidentityToken)r   Zreadurl	TOKEN_URL)selfZurl_responseZ_headers r   5/usr/lib/python3/dist-packages/uaclient/clouds/gcp.pyidentity_doc   s   
z$UAAutoAttachGCPInstance.identity_docc                 C   s   dS )NZgcpr   )r   r   r   r   
cloud_type'   s   z"UAAutoAttachGCPInstance.cloud_typec                 C   s*   t jtrtt}t| krdS dS )z$This machine is a viable GCPInstanceTF)ospathexistsDMI_PRODUCT_NAMEr   Z	load_fileGCP_PRODUCT_NAMEstrip)r   Zproduct_namer   r   r   	is_viable+   s
   
z!UAAutoAttachGCPInstance.is_viablec                 C   sN   | j d }t|dd d }t|d}|di di dg S )	zGet a list of licenses from the GCP metadata.

        Instance identity token (jwt) carries a list of licenses
        associated with the instance itself.

        Returns an empty list if licenses are not present in the metadata.
        r   .r	   z===zutf-8ZgoogleZcompute_engineZ
license_id)r   base64Zurlsafe_b64decodesplitjsonloadsdecodeget)r   tokenidentityZidentity_dictr   r   r   get_licenses_from_identity5   s   

z2UAAutoAttachGCPInstance.get_licenses_from_identityN)__name__
__module____qualname__propertyr   Zretryr   r   strr   r   r   boolr   r   r$   r   r   r   r   r      s    	r   )r   r   r   typingr   r   r   Zurllib.errorr   Zuaclientr   Zuaclient.cloudsr   r   r   r   ZGCP_LICENSESr   r   r   r   r   <module>   s"    