o >fF@sddlZddlZddlZddlZddlmZmZmZmZddl m Z m Z m Z m Z mZddlmZeeeZdZdZdZdZd Zd Zd d d dddZGdddeZdS)N)AnyDictListOptional) exceptionshttpsecret_managersystemutil)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_namezGoogle Compute Engine8045211386737108299602242772471989183059995928934984238225928668034199783202176054482269786025)xenialbionicfocaljammynoblec@seZdZddZeejejgddde e e ffddZ ede fdd Z edefd d Zdee fd d ZdefddZdedefddZdS)UAAutoAttachGCPInstancecCs d|_dS)Netagselfr5/usr/lib/python3/dist-packages/uaclient/clouds/gcp.py__init__%s z UAAutoAttachGCPInstance.__init__)g?r) retry_sleepsreturncCsttjtddidd}|jdkrtj|jd|jiS|j d}|r1d| vr1t j |j|d t j |j|jd ) NMetadata-FlavorGoogler)headerstimeout identityTokenerror_descriptionzservice account) status_code error_msg)codebody)rreadurl TOKEN_URLr*rsecrets add_secretr+ json_dictgetlowerrGCPServiceAccountErrorCloudMetadataError)rresponse error_descrrr identity_doc,s    z$UAAutoAttachGCPInstance.identity_doccCsdS)Ngcprrrrr cloud_type?sz"UAAutoAttachGCPInstance.cloud_typecCs*tjtrtt}t|krdSdS)z$This machine is a viable GCPInstanceTF)ospathexistsDMI_PRODUCT_NAMEr load_fileGCP_PRODUCT_NAMEstrip)r product_namerrr is_viableCs   z!UAAutoAttachGCPInstance.is_viablecCsN|jd}t|ddd}t|d}|dididgS) 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&.rz===zutf-8googlecompute_engine license_id)r7base64urlsafe_b64decodesplitjsonloadsdecoder1)rtokenidentity identity_dictrrrget_licenses_from_identityMs  z2UAAutoAttachGCPInstance.get_licenses_from_identitycCs$tj}|tvrtddSdS)Nz0This series isn't supported for GCP auto-attach.FT)r get_release_infoseries GCP_LICENSESLOGinfo)rrRrrrshould_poll_for_pro_license^s  z3UAAutoAttachGCPInstance.should_poll_for_pro_licensewait_for_changecCst}|r|t7}|jr|tj|jd7}tj|ddid}|jdkr=dd|jD}|j d|_t j }t ||vSt|j|jd krLtt) Nrr!r")r#r%cSsg|]}|dqS)idr).0licenserrr oszBUAAutoAttachGCPInstance.is_pro_license_present..ri) LICENSES_URLWAIT_FOR_CHANGEr LAST_ETAGformatrr,r* json_listr#r1r rQrRrSrTerrorr+rCancelProLicensePollingDelayProLicensePolling)rrWurlr5 license_idsrRrrris_pro_license_presentes    z.UAAutoAttachGCPInstance.is_pro_license_presentN)__name__ __module__ __qualname__rpropertyr retryrr4rstrrr7r9boolrBrrPrVrfrrrrr$s r)rGrJloggingr:typingrrrruaclientrrrr r uaclient.cloudsr getLoggerreplace_top_level_logger_namergrTr-r\r]r^r=r?rSrrrrrs.