o
    ϴfF                     @   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dddddZG dd deZdS )    N)AnyDictListOptional)
exceptionshttpsecret_manager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 Engine8045211386737108299602242772471989183059995928934984238225928668034199783202176054482269786025)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 )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      ?   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$UAAutoAttachGCPInstance.identity_docc                 C   s   dS )N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	   	load_fileGCP_PRODUCT_NAMEstrip)r   product_namer   r   r   	is_viableC   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-8googlecompute_engine
license_id)r7   base64urlsafe_b64decodesplitjsonloadsdecoder1   )r   tokenidentityidentity_dictr   r   r   get_licenses_from_identityM   s   

z2UAAutoAttachGCPInstance.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   rR   r   r   r   should_poll_for_pro_license^   s
   

z3UAAutoAttachGCPInstance.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r=dd |jD }|j	
d| _t j}t
||v S t|j |jd	krLt t )
Nr   r!   r"   )r#   r%   c                 S   s   g | ]}|d  qS )idr   ).0licenser   r   r   
<listcomp>o   s    zBUAAutoAttachGCPInstance.is_pro_license_present.<locals>.<listcomp>r   i  )LICENSES_URLWAIT_FOR_CHANGEr   	LAST_ETAGformatr   r,   r*   	json_listr#   r1   r	   rQ   rR   rS   rT   errorr+   r   CancelProLicensePollingDelayProLicensePolling)r   rW   urlr5   license_idsrR   r   r   r   is_pro_license_presente   s   


z.UAAutoAttachGCPInstance.is_pro_license_presentN)__name__
__module____qualname__r   propertyr
   retryr   r4   r   strr   r7   r9   boolrB   r   rP   rV   rf   r   r   r   r   r   $   s    	r   )rG   rJ   loggingr:   typingr   r   r   r   uaclientr   r   r   r	   r
   uaclient.cloudsr   	getLoggerreplace_top_level_logger_namerg   rT   r-   r\   r]   r^   r=   r?   rS   r   r   r   r   r   <module>   s.    	