
Ke                 @   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 j e j e   Z d Z d Z d Z d Z d	 Z d
 Z d d d d d d d d i Z Gd 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xenialZ8045211386737108299ZbionicZ6022427724719891830ZfocalZ599959289349842382ZjammyZ2592866803419978320c               @   s   e  Z d  Z d d   Z e e j e j d d d d g 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__Zretry_sleepsg      ?   )returnc             C   s   t  j t d d d i d d } | j d k r= d | j i S| j j d  } | r d	 | j   k r t j	 d
 | j d |   t j
 d | j d | j   d  S)NheaderszMetadata-FlavorGoogleZtimeoutr      identityTokenZerror_descriptionzservice accountZ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 j t  r7 t j t  } t | j   k r7 d Sd 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   sm   |  j  d } t j | j d  d d  } t j | j d   } | j d i   j d i   j 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   s0   t  j   j } | t k r, t j d  d Sd S)Nz0This series isn't supported for GCP auto-attach.FT)r   get_release_infoseriesGCP_LICENSESLOGinfo)r   r1   r   r   r   should_poll_for_pro_license\   s
    z3UAAutoAttachGCPInstance.should_poll_for_pro_license)wait_for_changer   c            C   s   t  } | r8 | t 7} |  j r8 | t j d |  j  7} t j | d d d i } | j d k r d d   | j D } | j	 j
 d  |  _ t j   j } t j
 |  | k St j | j  | j d k r t j    n t j    d  S)	Nr   r   zMetadata-Flavorr   r   c             S   s   g  |  ] } | d   q S)idr   ).0licenser   r   r   
<listcomp>m   s   	 zBUAAutoAttachGCPInstance.is_pro_license_present.<locals>.<listcomp>i  )LICENSES_URLWAIT_FOR_CHANGEr   	LAST_ETAGformatr   r   r   Z	json_listr   r   r   r0   r1   r2   r3   errorr   r   ZCancelProLicensePollingZDelayProLicensePolling)r   r6   Zurlr   Zlicense_idsr1   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   r/   r5   r@   r   r   r   r   r   #   s   %	r   )r)   r+   Zloggingr!   Ztypingr   r   r   r   Zuaclientr   r   r   r	   Zuaclient.cloudsr
   Z	getLoggerZreplace_top_level_logger_namerA   r3   r   r;   r<   r=   r$   r%   r2   r   r   r   r   r   <module>   s$   ""