3 L(Y @s dZddlZddlZddlZddlZddlmZddlmZ ddl m Z ddl m Z ddl m Z ejeZdjeje jd Zd jeje jd Zd Zd ZeeiZyeejddZWnek rdZYnXefddZedfddZddZdddZ dddZ!dS)zProvides 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_TIMEOUTc CsVy0|tdt|d}|jjt}|jtjko0|tkSt j k rPt j ddSXdS)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)urlmethodheaderstimeoutz+Compute Engine Metadata server unavailable.FN) _METADATA_IP_ROOT_METADATA_HEADERSr get_METADATA_FLAVOR_HEADERstatusrOK_METADATA_FLAVOR_VALUErTransportError_LOGGERinfo)requestr responseZmetadata_flavorrS/private/tmp/pip-build-nl73fm5q/google-auth/google/auth/compute_engine/_metadata.pyping5s   rFc Cstj||}i}|rd|d<tj||}||dtd}|jtjkrtj|j }|j ddkry t j |St k rtjdj|YqXq|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)urlparseurljoinrZ update_queryrrrr from_bytesdatar jsonloads ValueErrorrrformat) rpathrootrbase_urlZ query_paramsr rcontentrrrrTs&     rcCs t|dS)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)rrrrget_project_idsr*defaultcCst|dj|ddS)aGet 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%)rservice_accountrrrget_service_account_infosr-cCs4t|dj|}tjtj|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%rutcnowdatetime timedelta)rr,Z token_jsonZ token_expiryrrrget_service_account_tokens   r2)r+)r+)"__doc__r0r"loggingosZ six.movesrZsix.moves.urllibrrZ google.authrrr getLogger__name__rr%getenvZGCE_METADATA_ROOTZ_METADATA_ROOTZGCE_METADATA_IPr rrrintZ_METADATA_DEFAULT_TIMEOUTr$rrr*r-r2rrrrs4        2