3 L(ÌYÖã @s dZddlZddlZddlZddlZddlmZddlmZ ddl m Z ddl m Z ddl m Z ejeƒZdjeje jd ƒƒZd jeje jd ƒƒZd Zd ZeeiZyeejddƒƒZWnek rÎdZYnXefdd„Zedfdd„Zdd„Zddd„Z ddd„Z!dS)z‘Provides helper methods for talking to the Compute Engine metadata server. See https://cloud.google.com/compute/docs/metadata for more details. éN)Ú http_client)Úparse)Ú_helpers)Úenvironment_vars)Ú exceptionszhttp://{}/computeMetadata/v1/zmetadata.google.internalz http://{}z169.254.169.254zmetadata-flavorZGoogleZGCE_METADATA_TIMEOUTéc CsVy0|tdt|d�}|jjtƒ}|jtjko0|tkSt j k rPt j dƒdSXdS)aIChecks to see if the metadata server is available. Args: request (google.auth.transport.Request): A callable used to make HTTP requests. timeout (int): How long to wait for the metadata server to respond. Returns: bool: True if the metadata server is reachable, False otherwise. ÚGET)ÚurlÚmethodÚheadersÚtimeoutz+Compute Engine Metadata server unavailable.FN) Ú_METADATA_IP_ROOTÚ_METADATA_HEADERSr ÚgetÚ_METADATA_FLAVOR_HEADERÚstatusrÚOKÚ_METADATA_FLAVOR_VALUErÚTransportErrorÚ_LOGGERÚinfo)Úrequestr ÚresponseZmetadata_flavor©rúS/private/tmp/pip-build-nl73fm5q/google-auth/google/auth/compute_engine/_metadata.pyÚping5s   rFc Cs²tj||ƒ}i}|rd|d<tj||ƒ}||dtd�}|jtjkr”tj|j ƒ}|j ddkrŽy t j |ƒSt k rŠtjdj|ƒƒ‚Yq’Xq®|Sntjdj||j|j ƒ|ƒ‚d S) a?Fetch a resource from the metadata server. Args: request (google.auth.transport.Request): A callable used to make HTTP requests. path (str): The resource to retrieve. For example, ``'instance/service-accounts/default'``. root (str): The full path to the metadata server root. recursive (bool): Whether to do a recursive query of metadata. See https://cloud.google.com/compute/docs/metadata#aggcontents for more details. Returns: Union[Mapping, str]: If the metadata server returns JSON, a mapping of the decoded JSON is return. Otherwise, the response content is returned as a string. Raises: google.auth.exceptions.TransportError: if an error occurred while retrieving metadata. ÚtrueÚ recursiver)r r r z content-typezapplication/jsonzLReceived invalid JSON from the Google Compute Enginemetadata service: {:.20}z]Failed to retrieve {} from the Google Compute Enginemetadata service. Status: {} Response: {}N)ÚurlparseÚurljoinrZ update_queryrrrrÚ from_bytesÚdatar ÚjsonÚloadsÚ ValueErrorrrÚformat) rÚpathÚrootrÚbase_urlZ query_paramsr rÚcontentrrrrTs&     rcCs t|dƒS)aOGet the Google Cloud Project ID from the metadata server. Args: request (google.auth.transport.Request): A callable used to make HTTP requests. Returns: str: The project ID Raises: google.auth.exceptions.TransportError: if an error occurred while retrieving metadata. zproject/project-id)r)rrrrÚget_project_id†sr*ÚdefaultcCst|dj|ƒdd�S)aÉGet information about a service account from the metadata server. Args: request (google.auth.transport.Request): A callable used to make HTTP requests. service_account (str): The string 'default' or a service account email address. The determines which service account for which to acquire information. Returns: Mapping: The service account's information, for example:: { 'email': '...', 'scopes': ['scope', ...], 'aliases': ['default', '...'] } Raises: google.auth.exceptions.TransportError: if an error occurred while retrieving metadata. zinstance/service-accounts/{0}/T)r)rr%)rÚservice_accountrrrÚget_service_account_info—sr-cCs4t|dj|ƒƒ}tjƒtj|dd�}|d|fS)a-Get the OAuth 2.0 access token for a service account. Args: request (google.auth.transport.Request): A callable used to make HTTP requests. service_account (str): The string 'default' or a service account email address. The determines which service account for which to acquire an access token. Returns: Union[str, datetime]: The access token and its expiration. Raises: google.auth.exceptions.TransportError: if an error occurred while retrieving metadata. z#instance/service-accounts/{0}/tokenZ expires_in)ÚsecondsZ access_token)rr%rÚutcnowÚdatetimeÚ timedelta)rr,Z token_jsonZ token_expiryrrrÚget_service_account_token´s   r2)r+)r+)"Ú__doc__r0r"ÚloggingÚosZ six.movesrZsix.moves.urllibrrZ google.authrrrÚ getLoggerÚ__name__rr%ÚgetenvZGCE_METADATA_ROOTZ_METADATA_ROOTZGCE_METADATA_IPr rrrÚintZ_METADATA_DEFAULT_TIMEOUTr$rrr*r-r2rrrrÚs4        2