Merge "Query string functionality for containers"

This commit is contained in:
Jenkins 2016-06-14 19:36:45 +00:00 committed by Gerrit Code Review
commit 07c960d46f
3 changed files with 57 additions and 10 deletions

@ -17,7 +17,7 @@ in the `OpenStack wiki`__.
__ http://docs.openstack.org/infra/manual/developers.html __ http://docs.openstack.org/infra/manual/developers.html
This code is based on original the client previously included with This code is based on the original client previously included with
`OpenStack's Swift`__ The python-swiftclient is licensed under the `OpenStack's Swift`__ The python-swiftclient is licensed under the
Apache License like the rest of OpenStack. Apache License like the rest of OpenStack.

@ -827,7 +827,8 @@ def post_account(url, token, headers, http_conn=None, response_dict=None,
def get_container(url, token, container, marker=None, limit=None, def get_container(url, token, container, marker=None, limit=None,
prefix=None, delimiter=None, end_marker=None, prefix=None, delimiter=None, end_marker=None,
path=None, http_conn=None, path=None, http_conn=None,
full_listing=False, service_token=None, headers=None): full_listing=False, service_token=None, headers=None,
query_string=None):
""" """
Get a listing of objects for the container. Get a listing of objects for the container.
@ -846,6 +847,7 @@ def get_container(url, token, container, marker=None, limit=None,
of 10000 listings of 10000 listings
:param service_token: service auth token :param service_token: service auth token
:param headers: additional headers to include in the request :param headers: additional headers to include in the request
:param query_string: if set will be appended with '?' to generated path
:returns: a tuple of (response headers, a list of objects) 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. headers will be a dict and all header names will be lowercase.
:raises ClientException: HTTP GET request failed :raises ClientException: HTTP GET request failed
@ -889,6 +891,8 @@ def get_container(url, token, container, marker=None, limit=None,
qs += '&end_marker=%s' % quote(end_marker) qs += '&end_marker=%s' % quote(end_marker)
if path: if path:
qs += '&path=%s' % quote(path) qs += '&path=%s' % quote(path)
if query_string:
qs += '&%s' % query_string.lstrip('?')
if service_token: if service_token:
headers['X-Service-Token'] = service_token headers['X-Service-Token'] = service_token
method = 'GET' method = 'GET'
@ -950,7 +954,7 @@ def head_container(url, token, container, http_conn=None, headers=None,
def put_container(url, token, container, headers=None, http_conn=None, def put_container(url, token, container, headers=None, http_conn=None,
response_dict=None, service_token=None): response_dict=None, service_token=None, query_string=None):
""" """
Create a container Create a container
@ -963,6 +967,7 @@ def put_container(url, token, container, headers=None, http_conn=None,
:param response_dict: an optional dictionary into which to place :param response_dict: an optional dictionary into which to place
the response - status, reason and headers the response - status, reason and headers
:param service_token: service auth token :param service_token: service auth token
:param query_string: if set will be appended with '?' to generated path
:raises ClientException: HTTP PUT request failed :raises ClientException: HTTP PUT request failed
""" """
if http_conn: if http_conn:
@ -978,6 +983,8 @@ def put_container(url, token, container, headers=None, http_conn=None,
headers['X-Service-Token'] = service_token headers['X-Service-Token'] = service_token
if 'content-length' not in (k.lower() for k in headers): if 'content-length' not in (k.lower() for k in headers):
headers['Content-Length'] = '0' headers['Content-Length'] = '0'
if query_string:
path += '?' + query_string.lstrip('?')
conn.request(method, path, '', headers) conn.request(method, path, '', headers)
resp = conn.getresponse() resp = conn.getresponse()
body = resp.read() body = resp.read()
@ -1031,7 +1038,8 @@ def post_container(url, token, container, headers, http_conn=None,
def delete_container(url, token, container, http_conn=None, def delete_container(url, token, container, http_conn=None,
response_dict=None, service_token=None): response_dict=None, service_token=None,
query_string=None):
""" """
Delete a container Delete a container
@ -1043,6 +1051,7 @@ def delete_container(url, token, container, http_conn=None,
:param response_dict: an optional dictionary into which to place :param response_dict: an optional dictionary into which to place
the response - status, reason and headers the response - status, reason and headers
:param service_token: service auth token :param service_token: service auth token
:param query_string: if set will be appended with '?' to generated path
:raises ClientException: HTTP DELETE request failed :raises ClientException: HTTP DELETE request failed
""" """
if http_conn: if http_conn:
@ -1053,6 +1062,8 @@ def delete_container(url, token, container, http_conn=None,
headers = {'X-Auth-Token': token} headers = {'X-Auth-Token': token}
if service_token: if service_token:
headers['X-Service-Token'] = service_token headers['X-Service-Token'] = service_token
if query_string:
path += '?' + query_string.lstrip('?')
method = 'DELETE' method = 'DELETE'
conn.request(method, path, '', headers) conn.request(method, path, '', headers)
resp = conn.getresponse() resp = conn.getresponse()
@ -1625,7 +1636,7 @@ class Connection(object):
def get_container(self, container, marker=None, limit=None, prefix=None, def get_container(self, container, marker=None, limit=None, prefix=None,
delimiter=None, end_marker=None, path=None, delimiter=None, end_marker=None, path=None,
full_listing=False, headers=None): full_listing=False, headers=None, query_string=None):
"""Wrapper for :func:`get_container`""" """Wrapper for :func:`get_container`"""
# TODO(unknown): With full_listing=True this will restart the entire # TODO(unknown): With full_listing=True this will restart the entire
# listing with each retry. Need to make a better version that just # listing with each retry. Need to make a better version that just
@ -1633,22 +1644,27 @@ class Connection(object):
return self._retry(None, get_container, container, marker=marker, return self._retry(None, get_container, container, marker=marker,
limit=limit, prefix=prefix, delimiter=delimiter, limit=limit, prefix=prefix, delimiter=delimiter,
end_marker=end_marker, path=path, end_marker=end_marker, path=path,
full_listing=full_listing, headers=headers) full_listing=full_listing, headers=headers,
query_string=query_string)
def put_container(self, container, headers=None, response_dict=None): def put_container(self, container, headers=None, response_dict=None,
query_string=None):
"""Wrapper for :func:`put_container`""" """Wrapper for :func:`put_container`"""
return self._retry(None, put_container, container, headers=headers, return self._retry(None, put_container, container, headers=headers,
response_dict=response_dict) response_dict=response_dict,
query_string=query_string)
def post_container(self, container, headers, response_dict=None): def post_container(self, container, headers, response_dict=None):
"""Wrapper for :func:`post_container`""" """Wrapper for :func:`post_container`"""
return self._retry(None, post_container, container, headers, return self._retry(None, post_container, container, headers,
response_dict=response_dict) response_dict=response_dict)
def delete_container(self, container, response_dict=None): def delete_container(self, container, response_dict=None,
query_string=None):
"""Wrapper for :func:`delete_container`""" """Wrapper for :func:`delete_container`"""
return self._retry(None, delete_container, container, return self._retry(None, delete_container, container,
response_dict=response_dict) response_dict=response_dict,
query_string=query_string)
def head_object(self, container, obj, headers=None): def head_object(self, container, obj, headers=None):
"""Wrapper for :func:`head_object`""" """Wrapper for :func:`head_object`"""

@ -772,6 +772,16 @@ class TestGetContainer(MockHttpTest):
}), }),
]) ])
def test_query_string(self):
c.http_connection = self.fake_http_connection(
200, query_string="format=json&hello=20", body=b'[]')
c.get_container('http://www.test.com', 'asdf', 'asdf',
query_string="hello=20")
self.assertRequests([
('GET', '/asdf?format=json&hello=20', '', {
'x-auth-token': 'asdf'}),
])
class TestHeadContainer(MockHttpTest): class TestHeadContainer(MockHttpTest):
@ -831,6 +841,17 @@ class TestPutContainer(MockHttpTest):
'content-length': '0'}), 'content-length': '0'}),
]) ])
def test_query_string(self):
c.http_connection = self.fake_http_connection(200,
query_string="hello=20")
c.put_container('http://www.test.com', 'asdf', 'asdf',
query_string="hello=20")
for req in self.iter_request_log():
self.assertEqual(req['method'], 'PUT')
self.assertEqual(req['parsed_path'].path, '/asdf')
self.assertEqual(req['parsed_path'].query, 'hello=20')
self.assertEqual(req['headers']['x-auth-token'], 'asdf')
class TestDeleteContainer(MockHttpTest): class TestDeleteContainer(MockHttpTest):
@ -843,6 +864,16 @@ class TestDeleteContainer(MockHttpTest):
'x-auth-token': 'token'}), 'x-auth-token': 'token'}),
]) ])
def test_query_string(self):
c.http_connection = self.fake_http_connection(200,
query_string="hello=20")
c.delete_container('http://www.test.com', 'token', 'container',
query_string="hello=20")
self.assertRequests([
('DELETE', 'http://www.test.com/container?hello=20', '', {
'x-auth-token': 'token'})
])
class TestGetObject(MockHttpTest): class TestGetObject(MockHttpTest):