예제 #1
0
 def test__add_query_parameter(self):
     self.assertEqual(_helpers._add_query_parameter('/action', 'a', None),
                      '/action')
     self.assertEqual(_helpers._add_query_parameter('/action', 'a', 'b'),
                      '/action?a=b')
     self.assertEqual(
         _helpers._add_query_parameter('/action?a=b', 'a', 'c'),
         '/action?a=c')
     # Order is non-deterministic.
     self.assertIn(_helpers._add_query_parameter('/action?a=b', 'c', 'd'),
                   ['/action?a=b&c=d', '/action?c=d&a=b'])
     self.assertEqual(_helpers._add_query_parameter('/action', 'a', ' ='),
                      '/action?a=+%3D')
예제 #2
0
def get(http, path, root=METADATA_ROOT, recursive=None):
    """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.
    """
    url = urlparse.urljoin(root, path)
    url = _helpers._add_query_parameter(url, 'recursive', recursive)

    response, content = transport.request(http, url, headers=METADATA_HEADERS)

    if response.status == http_client.OK:
        decoded = _helpers._from_bytes(content)
        if response['content-type'] == 'application/json':
            return json.loads(decoded)
        else:
            return decoded
    else:
        raise http_client.HTTPException(
            'Failed to retrieve {0} from the Google Compute Engine'
            'metadata service. Response:\n{1}'.format(url, response))
예제 #3
0
            def get(self):
                error = self.request.get('error')
                if error:
                    errormsg = self.request.get('error_description', error)
                    self.response.out.write(
                        'The authorization request failed: {0}'.format(
                            _safe_html(errormsg)))
                else:
                    user = users.get_current_user()
                    decorator._create_flow(self)
                    credentials = decorator.flow.step2_exchange(
                        self.request.params)
                    decorator._storage_class(
                        decorator._credentials_class,
                        None,
                        decorator._credentials_property_name,
                        user=user).put(credentials)
                    redirect_uri = _parse_state_value(
                        str(self.request.get('state')), user)
                    if redirect_uri is None:
                        self.response.out.write(
                            'The authorization request failed')
                        return

                    if (decorator._token_response_param
                            and credentials.token_response):
                        resp_json = json.dumps(credentials.token_response)
                        redirect_uri = _helpers._add_query_parameter(
                            redirect_uri, decorator._token_response_param,
                            resp_json)

                    self.redirect(redirect_uri)
예제 #4
0
            def get(self):
                error = self.request.get('error')
                if error:
                    errormsg = self.request.get('error_description', error)
                    self.response.out.write(
                        'The authorization request failed: {0}'.format(
                            _safe_html(errormsg)))
                else:
                    user = users.get_current_user()
                    decorator._create_flow(self)
                    credentials = decorator.flow.step2_exchange(
                        self.request.params)
                    decorator._storage_class(
                        decorator._credentials_class, None,
                        decorator._credentials_property_name,
                        user=user).put(credentials)
                    redirect_uri = _parse_state_value(
                        str(self.request.get('state')), user)
                    if redirect_uri is None:
                        self.response.out.write(
                            'The authorization request failed')
                        return

                    if (decorator._token_response_param and
                            credentials.token_response):
                        resp_json = json.dumps(credentials.token_response)
                        redirect_uri = _helpers._add_query_parameter(
                            redirect_uri, decorator._token_response_param,
                            resp_json)

                    self.redirect(redirect_uri)
예제 #5
0
def get(http, path, root=METADATA_ROOT, recursive=None):
    """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.
    """
    url = urlparse.urljoin(root, path)
    url = _helpers._add_query_parameter(url, 'recursive', recursive)

    response, content = transport.request(
        http, url, headers=METADATA_HEADERS)

    if response.status == http_client.OK:
        decoded = _helpers._from_bytes(content)
        if response['content-type'] == 'application/json':
            return json.loads(decoded)
        else:
            return decoded
    else:
        raise http_client.HTTPException(
            'Failed to retrieve {0} from the Google Compute Engine'
            'metadata service. Response:\n{1}'.format(url, response))
예제 #6
0
 def test__add_query_parameter(self):
     self.assertEqual(
         _helpers._add_query_parameter('/action', 'a', None),
         '/action')
     self.assertEqual(
         _helpers._add_query_parameter('/action', 'a', 'b'),
         '/action?a=b')
     self.assertEqual(
         _helpers._add_query_parameter('/action?a=b', 'a', 'c'),
         '/action?a=c')
     # Order is non-deterministic.
     self.assertIn(
         _helpers._add_query_parameter('/action?a=b', 'c', 'd'),
         ['/action?a=b&c=d', '/action?c=d&a=b'])
     self.assertEqual(
         _helpers._add_query_parameter('/action', 'a', ' ='),
         '/action?a=+%3D')