o
    ÙKÕi(  ã                   @   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mZ d dlmZ e e e¡¡ZdZdZdZdZd	Zd
ZdgddgddgddgddgdœZG dd„ deƒZdS )é    N)ÚAnyÚDictÚListÚOptional)Ú
exceptionsÚhttpÚsecret_managerÚsystemÚutil)ÚPublicCloudAutoAttachInstancezˆhttp://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 EngineÚ8045211386737108299Ú6022427724719891830Ú7427070211152946628Ú599959289349842382Ú7818678586103571931Ú2592866803419978320Ú160084055690847662Ú2176054482269786025Ú4168323874790319525)ÚxenialÚbionicÚfocalÚjammyÚnoblec                   @   s˜   e Zd Zdd„ Zeejej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efdd„Zdedefdd„ZdS )ÚGCPAutoAttachInstancec                 C   s
   d | _ d S )N©Úetag©Úself© r   ú5/usr/lib/python3/dist-packages/uaclient/clouds/gcp.pyÚ__init__%   s   
zGCPAutoAttachInstance.__init__)g      à?é   r"   )Úretry_sleepsÚreturnc                 C   st   t jtddidd�}|jdkrtj |j¡ d|jiS |j 	d¡}|r1d| 
¡ v r1tj|j|d	�‚tj|j|jd
�‚)NúMetadata-FlavorÚGoogler"   )ÚheadersÚtimeoutéÈ   ÚidentityTokenÚerror_descriptionzservice account)Ústatus_codeÚ	error_msg)ÚcodeÚbody)r   ÚreadurlÚ	TOKEN_URLr.   r   ÚsecretsÚ
add_secretr/   Ú	json_dictÚgetÚlowerr   ÚGCPServiceAccountErrorÚCloudMetadataError)r   ÚresponseÚ
error_descr   r   r    Úidentity_doc,   s   
ÿ

ÿÿz"GCPAutoAttachInstance.identity_docc                 C   s   dS )NÚgcpr   r   r   r   r    Ú
cloud_type?   s   z GCPAutoAttachInstance.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	   Ú	load_fileÚGCP_PRODUCT_NAMEÚstrip)r   Úproduct_namer   r   r    Ú	is_viableC   s
   
zGCPAutoAttachInstance.is_viablec                 C   sN   | j d }t | d¡d d ¡}t | d¡¡}| di ¡ di ¡ dg ¡S )	zñGet 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-8ÚgoogleÚcompute_engineÚ
license_id)r;   Úbase64Úurlsafe_b64decodeÚsplitÚjsonÚloadsÚdecoder5   )r   ÚtokenÚidentityÚidentity_dictr   r   r    Úget_licenses_from_identityM   s   

ýz0GCPAutoAttachInstance.get_licenses_from_identityc                 C   s$   t  ¡ j}|tv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   rV   r   r   r    Úshould_poll_for_pro_license^   s
   

z1GCPAutoAttachInstance.should_poll_for_pro_licenseÚwait_for_changec                C   s¶   t }|r|t7 }| jr|tj| jd�7 }tj|ddid�}|jdkrHdd„ |jD ƒ}|j	 
d¡| _t ¡ j}t 
|g ¡D ]	}||v rE d	S q<d
S t |j¡ |jdkrWt ¡ ‚t ¡ ‚)Nr   r%   r&   )r'   r)   c                 S   s   g | ]}|d  ‘qS )Úidr   )Ú.0Úlicenser   r   r    Ú
<listcomp>o   s    z@GCPAutoAttachInstance.is_pro_license_present.<locals>.<listcomp>r   TFi�  )ÚLICENSES_URLÚWAIT_FOR_CHANGEr   Ú	LAST_ETAGÚformatr   r0   r.   Ú	json_listr'   r5   r	   rU   rV   rW   rX   Úerrorr/   r   ÚCancelProLicensePollingÚDelayProLicensePolling)r   r[   Úurlr9   Úlicense_idsrV   Úlicr   r   r    Úis_pro_license_presente   s&   

ÿ
z,GCPAutoAttachInstance.is_pro_license_presentN)Ú__name__Ú
__module__Ú__qualname__r!   Úpropertyr
   Úretryr   r8   r   Ústrr   r;   r=   ÚboolrF   r   rT   rZ   rk   r   r   r   r    r   $   s    	r   )rK   rN   Úloggingr>   Útypingr   r   r   r   Úuaclientr   r   r   r	   r
   Úuaclient.cloudsr   Ú	getLoggerÚreplace_top_level_logger_namerl   rX   r1   r`   ra   rb   rA   rC   rW   r   r   r   r   r    Ú<module>   s.    ÿÿû	