o
    Jh(                     @   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)PublicCloudAutoAttachInstancezhttp://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 )	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)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.    	