Esempio n. 1
0
def direct_get_container(node, part, account, container, marker=None,
                         limit=None, prefix=None, delimiter=None,
                         conn_timeout=5, response_timeout=15):
    """
    Get container listings directly from the container server.

    :param node: node dictionary from the ring
    :param part: partition the container is on
    :param account: account name
    :param container: container name
    :param marker: marker query
    :param limit: query limit
    :param prefix: prefix query
    :param delimeter: delimeter for the query
    :param conn_timeout: timeout in seconds for establishing the connection
    :param response_timeout: timeout in seconds for getting the response
    :returns: a tuple of (response headers, a list of objects) The response
              headers will be a dict and all header names will be lowercase.
    """
    path = '/%s/%s' % (account, container)
    qs = 'format=json'
    if marker:
        qs += '&marker=%s' % quote(marker)
    if limit:
        qs += '&limit=%d' % limit
    if prefix:
        qs += '&prefix=%s' % quote(prefix)
    if delimiter:
        qs += '&delimiter=%s' % quote(delimiter)
    with Timeout(conn_timeout):
        conn = http_connect(node['ip'], node['port'], node['device'], part,
                            'GET', path, query_string='format=json')
    with Timeout(response_timeout):
        resp = conn.getresponse()
    if resp.status < 200 or resp.status >= 300:
        resp.read()
        raise ClientException(
            'Container server %s:%s direct GET %s gave stats %s' % (node['ip'],
                node['port'], repr('/%s/%s%s' % (node['device'], part, path)),
                resp.status),
            http_host=node['ip'], http_port=node['port'],
            http_device=node['device'], http_status=resp.status,
            http_reason=resp.reason)
    resp_headers = {}
    for header, value in resp.getheaders():
        resp_headers[header.lower()] = value
    if resp.status == 204:
        resp.read()
        return resp_headers, []
    return resp_headers, json_loads(resp.read())
Esempio n. 2
0
    def test_no_json(self):
        # first break simplejson
        try:
            import simplejson
        except ImportError:
            # not installed, so we don't have to break it for these tests
            pass
        else:
            delattr(simplejson, 'loads')

        # then break json
        try:
            import json
        except ImportError:
            # not installed, so we don't have to break it for these tests
            _orig_dumps = None
        else:
            # before we break json, grab a copy of the orig_dumps function
            _orig_dumps = json.dumps
            delattr(json, 'loads')
            reload(c)

        if _orig_dumps:
            # basic test of swift.common.client.json_loads using json.loads
            data = {
                'string': 'value',
                'int': 0,
                'bool': True,
                'none': None,
            }
            json_string = _orig_dumps(data)
        else:
            # even more basic test using a hand encoded json string
            data = ['value1', 'value2']
            json_string = "['value1', 'value2']"

        self.assertEquals(data, c.json_loads(json_string))
        self.assertRaises(AttributeError, c.json_loads, self)
Esempio n. 3
0
    def test_no_json(self):
        # first break simplejson
        try:
            import simplejson
        except ImportError:
            # not installed, so we don't have to break it for these tests
            pass
        else:
            delattr(simplejson, 'loads')

        # then break json
        try:
            import json
        except ImportError:
            # not installed, so we don't have to break it for these tests
            _orig_dumps = None
        else:
            # before we break json, grab a copy of the orig_dumps function
            _orig_dumps = json.dumps
            delattr(json, 'loads')
            reload(c)

        if _orig_dumps:
            # basic test of swift.common.client.json_loads using json.loads
            data = {
                'string': 'value',
                'int': 0,
                'bool': True,
                'none': None,
            }
            json_string = _orig_dumps(data)
        else:
            # even more basic test using a hand encoded json string
            data = ['value1', 'value2']
            json_string = "['value1', 'value2']"

        self.assertEquals(data, c.json_loads(json_string))
        self.assertRaises(AttributeError, c.json_loads, self)
Esempio n. 4
0
def direct_get_account(node,
                       part,
                       account,
                       marker=None,
                       limit=None,
                       prefix=None,
                       delimiter=None,
                       conn_timeout=5,
                       response_timeout=15):
    """
    Get listings directly from the account server.

    :param node: node dictionary from the ring
    :param part: partition the account is on
    :param account: account name
    :param marker: marker query
    :param limit: query limit
    :param prefix: prefix query
    :param delimeter: delimeter for the query
    :param conn_timeout: timeout in seconds for establishing the connection
    :param response_timeout: timeout in seconds for getting the response
    :returns: a tuple of (response headers, a list of containers) The response
              headers will be a dict and all header names will be lowercase.
    """
    path = '/' + account
    qs = 'format=json'
    if marker:
        qs += '&marker=%s' % quote(marker)
    if limit:
        qs += '&limit=%d' % limit
    if prefix:
        qs += '&prefix=%s' % quote(prefix)
    if delimiter:
        qs += '&delimiter=%s' % quote(delimiter)
    with Timeout(conn_timeout):
        conn = http_connect(node['ip'],
                            node['port'],
                            node['device'],
                            part,
                            'GET',
                            path,
                            query_string='format=json')
    with Timeout(response_timeout):
        resp = conn.getresponse()
    if resp.status < 200 or resp.status >= 300:
        resp.read()
        raise ClientException(
            'Account server %s:%s direct GET %s gave status %s' %
            (node['ip'], node['port'],
             repr('/%s/%s%s' % (node['device'], part, path)), resp.status),
            http_host=node['ip'],
            http_port=node['port'],
            http_device=node['device'],
            http_status=resp.status,
            http_reason=resp.reason)
    resp_headers = {}
    for header, value in resp.getheaders():
        resp_headers[header.lower()] = value
    if resp.status == 204:
        resp.read()
        return resp_headers, []
    return resp_headers, json_loads(resp.read())