
    g(                         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j"                   ej$                  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e      Zy)    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                       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)GCPAutoAttachInstancec                     d | _         y )Netagselfs    5/usr/lib/python3/dist-packages/uaclient/clouds/gcp.py__init__zGCPAutoAttachInstance.__init__%   s     	    )g      ?   r%   )retry_sleepsreturnc                    t        j                  t        ddid      }|j                  dk(  r7t        j
                  j                  |j                         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_URLr2   r	   secrets
add_secretr3   	json_dictgetlowerr   GCPServiceAccountErrorCloudMetadataError)r!   response
error_descs      r"   identity_docz"GCPAutoAttachInstance.identity_doc,   s     << 18<a
 ==C""--hmm<#X]]33''++,?@
+z/?/?/AA33$MMZ  ++X]]
 	
r$   c                      y)Ngcp r    s    r"   
cloud_typez GCPAutoAttachInstance.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GCPAutoAttachInstance.is_viableC   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)r?   base64urlsafe_b64decodesplitjsonloadsdecoder9   )r!   tokenidentityidentity_dicts       r"   get_licenses_from_identityz0GCPAutoAttachInstance.get_licenses_from_identityM   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!   r_   s     r"   should_poll_for_pro_licensez1GCPAutoAttachInstance.should_poll_for_pro_license^   s1    ((*11%HHGHr$   wait_for_changec                   t         }|r8|t        z  }| j                  r#|t        j	                  | j                        z  }t        j                  |ddi      }|j                  dk(  r||j                  D cg c]  }|d   	 }}|j                  j                  d      | _        t        j                         j                  }t        j                  |g       D ]  }||v s y y	t        j!                  |j"                         |j                  d
k(  rt%        j&                         t%        j(                         c c}w )Nr   r)   r*   )r+   r-   idr   TFi  )LICENSES_URLWAIT_FOR_CHANGEr   	LAST_ETAGformatr   r4   r2   	json_listr+   r9   r
   r^   r_   r`   ra   errorr3   r   CancelProLicensePollingDelayProLicensePolling)r!   rd   urlr=   licenselicense_idsr_   lics           r"   is_pro_license_presentz,GCPAutoAttachInstance.is_pro_license_presente   s   ?"Cyyy''TYY'77<<.?-JK==C8@8J8JKW74=KKK ((,,V4DI,,.55F#''3  +%  		(-- ==C44663355 Ls   7D;N)__name__
__module____qualname__r#   propertyr   retryr   r<   r   strr   r?   rC   boolrM   r   r\   rc   rs   rB   r$   r"   r   r   $   s     TZZ
--KH
d38n 
 I 
" C   4  
DI 
"T 6 6$ 6r$   r   )rS   rV   loggingrE   typingr   r   r   r   uaclientr   r   r	   r
   r   uaclient.cloudsr   	getLoggerreplace_top_level_logger_namert   ra   r5   rg   rh   ri   rH   rJ   r`   r   rB   r$   r"   <module>r      s       	 , , C C 9g:::8DE  
  *	3 *  %%$&;<"$9:#%9:#%:;W69 W6r$   