
                             S r SSKrSSKrSSKrSSKrSSKJr  SSKJr	  SSK
Jr  SSK
Jr  SSK
Jr  SR                  \R                  " S	S
5      5      rSS0r\S4S jrSS jrSS jrg)zProvides helper methods for talking to the Compute Engine metadata server.

See https://cloud.google.com/compute/docs/metadata
    N)http_client)parse)_helpers)client)utilzhttp://{}/computeMetadata/v1/GCE_METADATA_ROOTzmetadata.google.internalzMetadata-FlavorGooglec                 l   [         R                  " X!5      n[        R                  " USU5      nU " U[        S9u  pVUR
                  [        R                  :X  a7  [        R                  " U5      nUS   S:X  a  [        R                  " U5      $ U$ [        R                  " SR                  XE5      5      e)a  Fetch a resource from the metadata server.

Args:
    path: A string indicating the resource to retrieve. For example,
        'instance/service-accounts/defualt'
    http_request: A callable that matches the method
        signature of httplib2.Http.request. Used to make the request to the
        metadataserver.
    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:
    httplib2.Httplib2Error if an error corrured while retrieving metadata.
	recursive)headerszcontent-typezapplication/jsonzTFailed to retrieve {0} from the Google Compute Enginemetadata service. Response:
{1})urlparseurljoinr   _add_query_parameterMETADATA_HEADERSstatusr   OKr   _from_bytesjsonloadshttplib2HttpLib2Errorformat)http_requestpathrootr   urlresponsecontentdecodeds           1lib/third_party/oauth2client/contrib/_metadata.pygetr!   &   s    ( 

4
&C

#
#Ci
@C$ H
 +..(&&w/N#'99::g&&N$$//5vc/DF 	F    c                 6    [        U SR                  U5      SS9$ )a  Get information about a service account from the metadata server.

Args:
    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.
    http_request: A callable that matches the method
        signature of httplib2.Http.request. Used to make the request to the
        metadata server.
Returns:
     A dictionary with information about the specified service account,
     for example:

        {
            'email': '...',
            'scopes': ['scope', ...],
            'aliases': ['default', '...']
        }
zinstance/service-accounts/{0}/T)r   )r!   r   )r   service_accounts     r    get_service_account_infor%   N   s%    ( (//@ r"   c                     [        U SR                  U5      5      n[        R                  " 5       [        R
                  " US   S9-   nUS   U4$ )a^  Fetch an oauth token for the

Args:
    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.
    http_request: A callable that matches the method
        signature of httplib2.Http.request. Used to make the request to the
        metadataserver.

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.
z#instance/service-accounts/{0}/token
expires_in)secondsaccess_token)r!   r   r   _UTCNOWdatetime	timedelta)r   r$   
token_jsontoken_expirys       r    	get_tokenr/   h   sX      -44_EGJ >>#h&8&8<('* *Ln%|33r"   )default)__doc__r+   r   osr   	six.movesr   six.moves.urllibr   r   oauth2clientr   r   r   r   getenvMETADATA_ROOTr   r!   r%   r/    r"   r    <module>r9      sm   
   	  ! . !   066II!#=>@%x0  "/$ %FP44r"   