U
    lHJe                     @   s   d dl Z d dlZd dlZd dlZd dlmZmZmZmZ d dl	m
Z
mZmZmZ d dlmZ eeeZdZdZd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Optional)
exceptionshttpsystemutil)AutoAttachCloudInstancezhttp://metadata/computeMetadata/v1/instance/service-accounts/default/identity?audience=contracts.canonical.com&format=full&licenses=TRUEzThttp://metadata.google.internal/computeMetadata/v1/instance/licenses/?recursive=truez&wait_for_change=truez&last_etag={etag}z/sys/class/dmi/id/product_namezGoogle Compute EngineZ8045211386737108299Z6022427724719891830Z599959289349842382Z2592866803419978320)ZxenialZbionicZfocalZjammyc                   @   s   e Zd Zdd Zeejejdddgde	e
ef dddZee
dd	d
ZeedddZee
 dddZedddZeedddZdS )UAAutoAttachGCPInstancec                 C   s
   d | _ d S )Netagself r   5/usr/lib/python3/dist-packages/uaclient/clouds/gcp.py__init__$   s    z UAAutoAttachGCPInstance.__init__g      ?   )Zretry_sleeps)returnc                 C   sj   t jtddidd}|jdkr(d|jiS |jd}|rTd| krTtj	|j|d	tj
|j|jd
d S )NMetadata-FlavorGoogler   )headersZtimeout   identityTokenZerror_descriptionzservice account)Zstatus_codeZ	error_msg)codebody)r   readurl	TOKEN_URLr   r   Z	json_dictgetlowerr   ZGCPServiceAccountErrorCloudMetadataError)r   responseZ
error_descr   r   r   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_viableA   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decoder   )r   tokenZidentityZidentity_dictr   r   r   get_licenses_from_identityK   s    
  z2UAAutoAttachGCPInstance.get_licenses_from_identityc                 C   s$   t  j}|tkr td dS dS )Nz0This series isn't supported for GCP auto-attach.FT)r   get_release_infoseriesGCP_LICENSESLOGinfo)r   r4   r   r   r   should_poll_for_pro_license\   s
    

z3UAAutoAttachGCPInstance.should_poll_for_pro_license)wait_for_changer   c                C   s   t }|r(|t7 }| jr(|tj| jd7 }tj|ddid}|jdkrzdd |jD }|j	
d| _t j}t
||kS t|j |jd	krt nt d S )
Nr   r   r   )r   r   c                 S   s   g | ]}|d  qS )idr   ).0licenser   r   r   
<listcomp>m   s     zBUAAutoAttachGCPInstance.is_pro_license_present.<locals>.<listcomp>r   i  )LICENSES_URLWAIT_FOR_CHANGEr   	LAST_ETAGformatr   r   r   Z	json_listr   r   r   r3   r4   r5   r6   errorr   r   ZCancelProLicensePollingZDelayProLicensePolling)r   r9   Zurlr!   Zlicense_idsr4   r   r   r   is_pro_license_presentc   s    



z.UAAutoAttachGCPInstance.is_pro_license_presentN)__name__
__module____qualname__r   propertyr	   Zretryr   r    r   strr   r"   r#   boolr*   r   r2   r8   rC   r   r   r   r   r   #   s   	r   )r,   r.   Zloggingr$   typingr   r   r   r   Zuaclientr   r   r   r	   Zuaclient.cloudsr
   Z	getLoggerZreplace_top_level_logger_namerD   r6   r   r>   r?   r@   r'   r(   r5   r   r   r   r   r   <module>   s*   