ó
‚¾^Yc           @   sÍ   d  Z  d d l Z d d l Z d d l Z d d l m Z d d l m Z d d l	 m
 Z
 d d l	 m Z d d l	 m Z d j e j d	 d
 ƒ ƒ Z i d d 6Z e d d „ Z d d „ Z d d „ Z d S(   s   Provides helper methods for talking to the Compute Engine metadata server.

See https://cloud.google.com/compute/docs/metadata
iÿÿÿÿN(   t   http_client(   t   parse(   t   _helpers(   t   client(   t	   transports   http://{}/computeMetadata/v1/t   GCE_METADATA_ROOTs   metadata.google.internalt   Googles   Metadata-Flavorc         C   s©   t  j | | ƒ } t j | d | ƒ } t j |  | d t ƒ\ } } | j t j	 k rŠ t j
 | ƒ } | d d k rƒ t j | ƒ S| Sn t j d j | | ƒ ƒ ‚ d S(   s©  Fetch a resource from the metadata server.

    Args:
        http: an object to be used to make HTTP requests.
        path: A string indicating the resource to retrieve. For example,
            'instance/service-accounts/default'
        root: A string indicating the full path to the metadata server root.
        recursive: A boolean indicating whether to do a recursive query of
            metadata. See
            https://cloud.google.com/compute/docs/metadata#aggcontents

    Returns:
        A dictionary if the metadata server returns JSON, otherwise a string.

    Raises:
        http_client.HTTPException if an error corrured while
        retrieving metadata.
    t	   recursivet   headerss   content-types   application/jsonsT   Failed to retrieve {0} from the Google Compute Enginemetadata service. Response:
{1}N(   t   urlparset   urljoinR   t   _add_query_parameterR   t   requestt   METADATA_HEADERSt   statusR    t   OKt   _from_bytest   jsont   loadst   HTTPExceptiont   format(   t   httpt   patht   rootR   t   urlt   responset   contentt   decoded(    (    sD   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/contrib/_metadata.pyt   get%   s    t   defaultc         C   s   t  |  d j | ƒ d t ƒS(   sq  Get information about a service account from the metadata server.

    Args:
        http: an object to be used to make HTTP requests.
        service_account: An email specifying the service account for which to
            look up information. Default will be information for the "default"
            service account of the current compute engine instance.

    Returns:
         A dictionary with information about the specified service account,
         for example:

            {
                'email': '...',
                'scopes': ['scope', ...],
                'aliases': ['default', '...']
            }
    s   instance/service-accounts/{0}/R   (   R   R   t   True(   R   t   service_account(    (    sD   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/contrib/_metadata.pyt   get_service_account_infoJ   s    c         C   sF   t  |  d j | ƒ ƒ } t j ƒ  t j d | d ƒ } | d | f S(   s#  Fetch an oauth token for the

    Args:
        http: an object to be used to make HTTP requests.
        service_account: An email specifying the service account this token
            should represent. Default will be a token for the "default" service
            account of the current compute engine instance.

    Returns:
         A tuple of (access token, token expiration), where access token is the
         access token as a string and token expiration is a datetime object
         that indicates when the access token will expire.
    s#   instance/service-accounts/{0}/tokent   secondst
   expires_int   access_token(   R   R   R   t   _UTCNOWt   datetimet	   timedelta(   R   R   t
   token_jsont   token_expiry(    (    sD   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/contrib/_metadata.pyt	   get_tokenc   s    (   t   __doc__R%   R   t   ost	   six.movesR    t   six.moves.urllibR   R	   t   oauth2clientR   R   R   R   t   getenvt   METADATA_ROOTR   t   NoneR   R    R)   (    (    (    sD   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/contrib/_metadata.pyt   <module>   s   %