U
    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 EngineZ8045211386737108299Z6022427724719891830Z7427070211152946628Z599959289349842382Z7818678586103571931Z2592866803419978320Z160084055690847662Z2176054482269786025Z4168323874790319525)ZxenialZbionicZfocalZjammyZnoblec                   @   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 )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      ?   )Zretry_sleeps)returnc                 C   sx   t jtddidd}|jdkr6tj|j d|jiS |j	d}|rbd|
 krbt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secretsZ
add_secretr   Z	json_dictgetlowerr   ZGCPServiceAccountErrorCloudMetadataError)r   responseZ
error_descr   r   r   identity_doc,   s$      

  z"GCPAutoAttachInstance.identity_docc                 C   s   dS )NZ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	   Z	load_fileGCP_PRODUCT_NAMEstrip)r   Z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-8ZgoogleZcompute_engineZ
license_id)r#   base64Zurlsafe_b64decodesplitjsonloadsdecoder   )r   tokenZidentityZidentity_dictr   r   r   get_licenses_from_identityM   s    
  z0GCPAutoAttachInstance.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   r5   r   r   r   should_poll_for_pro_license^   s
    

z1GCPAutoAttachInstance.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rdd |jD }|j	
d| _t j}t
|g D ]}||krx d	S qxd
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>o   s     z@GCPAutoAttachInstance.is_pro_license_present.<locals>.<listcomp>r   TFi  )LICENSES_URLWAIT_FOR_CHANGEr   	LAST_ETAGformatr   r   r   Z	json_listr   r   r	   r4   r5   r6   r7   errorr   r   ZCancelProLicensePollingZDelayProLicensePolling)r   r:   Zurlr"   Zlicense_idsr5   Zlicr   r   r   is_pro_license_presente   s$    



z,GCPAutoAttachInstance.is_pro_license_presentN)__name__
__module____qualname__r   propertyr
   Zretryr   r!   r   strr   r#   r$   boolr+   r   r3   r9   rD   r   r   r   r   r   $   s   	r   )r-   r/   Zloggingr%   typingr   r   r   r   Zuaclientr   r   r   r	   r
   Zuaclient.cloudsr   Z	getLoggerZreplace_top_level_logger_namerE   r7   r   r?   r@   rA   r(   r)   r6   r   r   r   r   r   <module>   s,   	