
    |f                         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Z G d de      Zy)    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 Engine804521138673710829960224277247198918305999592893498423822592866803419978320)xenialbionicfocaljammyc                       e Zd Zd Ze ej                  ej                  g d      de	e
ef   fd              Zede
fd       Zedefd       Zdee
   fdZdefd	Zd
edefdZy)UAAutoAttachGCPInstancec                     d | _         y )Netagselfs    5/usr/lib/python3/dist-packages/uaclient/clouds/gcp.py__init__z UAAutoAttachGCPInstance.__init__$   s     	    )g      ?   r   )retry_sleepsreturnc                 n   t        j                  t        ddid      }|j                  dk(  rd|j                  iS |j
                  j                  d      }|r3d|j                         v r!t        j                  |j                  |	      t        j                  |j                  |j                  
      )NMetadata-FlavorGoogler   )headerstimeout   identityTokenerror_descriptionzservice account)status_code	error_msg)codebody)r   readurl	TOKEN_URLr+   r,   	json_dictgetlowerr   GCPServiceAccountErrorCloudMetadataError)r   response
error_descs      r   identity_docz$UAAutoAttachGCPInstance.identity_doc+   s     << 18<a
 ==C#X]]33''++,?@
+z/?/?/AA33$MMZ  ++X]]
 	
r   c                      y)Ngcp r   s    r   
cloud_typez"UAAutoAttachGCPInstance.cloud_type=   s    r   c                     t         j                  j                  t              r1t	        j
                  t              }t        |j                         k(  ryy)z$This machine is a viable GCPInstanceTF)ospathexistsDMI_PRODUCT_NAMEr	   	load_fileGCP_PRODUCT_NAMEstrip)r   product_names     r   	is_viablez!UAAutoAttachGCPInstance.is_viableA   s=     77>>*+!++,<=L<#5#5#77r   c                     | j                   d   }t        j                  |j                  d      d   dz         }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-8googlecompute_engine
license_id)r6   base64urlsafe_b64decodesplitjsonloadsdecoder0   )r   tokenidentityidentity_dicts       r   get_licenses_from_identityz2UAAutoAttachGCPInstance.get_licenses_from_identityK   s|     !!/2++EKK,<Q,?%,GH

8??7#;<h+S!2&Sr"	
r   c                 |    t        j                         j                  }|t        vrt        j                  d       yy)Nz0This series isn't supported for GCP auto-attach.FT)r	   get_release_infoseriesGCP_LICENSESLOGinfo)r   rV   s     r   should_poll_for_pro_licensez3UAAutoAttachGCPInstance.should_poll_for_pro_license\   s1    ((*11%HHGHr   wait_for_changec                j   t         }|r8|t        z  }| j                  r#|t        j	                  | j                        z  }t        j                  |ddi      }|j                  dk(  rq|j                  D cg c]  }|d   	 }}|j                  j                  d      | _        t        j                         j                  }t        j                  |      |v S t        j!                  |j"                         |j                  dk(  rt%        j&                         t%        j(                         c c}w )	Nr   r"   r#   )r$   r&   idr   i  )LICENSES_URLWAIT_FOR_CHANGEr   	LAST_ETAGformatr   r-   r+   	json_listr$   r0   r	   rU   rV   rW   rX   errorr,   r   CancelProLicensePollingDelayProLicensePolling)r   r[   urlr4   licenselicense_idsrV   s          r   is_pro_license_presentz.UAAutoAttachGCPInstance.is_pro_license_presentc   s    ?"Cyyy''TYY'77<<.?-JK==C8@8J8JKW74=KKK ((,,V4DI,,.55F##F+{::		(-- ==C44663355 Ls   7D0N)__name__
__module____qualname__r   propertyr
   retryr   r3   r   strr   r6   r:   boolrD   r   rS   rZ   ri   r9   r   r   r   r   #   s     TZZ
--KH
d38n 
 I 
  C   4  
DI 
"T 6 6$ 6r   r   )rJ   rM   loggingr<   typingr   r   r   r   uaclientr   r   r	   r
   uaclient.cloudsr   	getLoggerreplace_top_level_logger_namerj   rX   r.   r^   r_   r`   r?   rA   rW   r   r9   r   r   <module>rw      s       	 , , 3 3 3g:::8DE  
  *	3 *  $#!"	S65 S6r   