Rename transport helpers to utils
This is a simple patch that renames two "helpers" modules to make them consistent with the storage driver naming scheme. Change-Id: Idffe4523e0ab095286cfea6890fbffe5c36a40d9
This commit is contained in:
parent
1a7c1a5e8d
commit
c548e10a48
@ -20,64 +20,64 @@ import falcon
|
||||
import json
|
||||
import testtools
|
||||
|
||||
from marconi.transport.wsgi import helpers
|
||||
from marconi.transport.wsgi import utils
|
||||
|
||||
|
||||
class TestWSGIHelpers(testtools.TestCase):
|
||||
class TestWSGIutils(testtools.TestCase):
|
||||
|
||||
def test_get_checked_field_missing(self):
|
||||
doc = {}
|
||||
|
||||
self.assertRaises(falcon.HTTPBadRequest,
|
||||
helpers.get_checked_field, doc, 'openstack', int)
|
||||
utils.get_checked_field, doc, 'openstack', int)
|
||||
|
||||
self.assertRaises(falcon.HTTPBadRequest,
|
||||
helpers.get_checked_field, doc, 42, int)
|
||||
utils.get_checked_field, doc, 42, int)
|
||||
|
||||
doc = {'openstac': 10}
|
||||
|
||||
self.assertRaises(falcon.HTTPBadRequest,
|
||||
helpers.get_checked_field, doc, 'openstack', int)
|
||||
utils.get_checked_field, doc, 'openstack', int)
|
||||
|
||||
def test_get_checked_field_bad_type(self):
|
||||
doc = {'openstack': '10'}
|
||||
|
||||
self.assertRaises(falcon.HTTPBadRequest,
|
||||
helpers.get_checked_field, doc, 'openstack', int)
|
||||
utils.get_checked_field, doc, 'openstack', int)
|
||||
|
||||
doc = {'openstack': 10, 'openstack-mq': 'test'}
|
||||
|
||||
self.assertRaises(falcon.HTTPBadRequest,
|
||||
helpers.get_checked_field, doc, 'openstack', str)
|
||||
utils.get_checked_field, doc, 'openstack', str)
|
||||
|
||||
doc = {'openstack': '[1, 2]'}
|
||||
|
||||
self.assertRaises(falcon.HTTPBadRequest,
|
||||
helpers.get_checked_field, doc, 'openstack', list)
|
||||
utils.get_checked_field, doc, 'openstack', list)
|
||||
|
||||
def test_get_checked_field(self):
|
||||
doc = {'hello': 'world', 'teh answer': 42, 'question': []}
|
||||
|
||||
value = helpers.get_checked_field(doc, 'hello', str)
|
||||
value = utils.get_checked_field(doc, 'hello', str)
|
||||
self.assertEquals(value, 'world')
|
||||
|
||||
value = helpers.get_checked_field(doc, 'teh answer', int)
|
||||
value = utils.get_checked_field(doc, 'teh answer', int)
|
||||
self.assertEquals(value, 42)
|
||||
|
||||
value = helpers.get_checked_field(doc, 'question', list)
|
||||
value = utils.get_checked_field(doc, 'question', list)
|
||||
self.assertEquals(value, [])
|
||||
|
||||
def test_filter_missing(self):
|
||||
doc = {'body': {'event': 'start_backup'}}
|
||||
spec = (('tag', dict),)
|
||||
self.assertRaises(falcon.HTTPBadRequest,
|
||||
helpers.filter, doc, spec)
|
||||
utils.filter, doc, spec)
|
||||
|
||||
def test_filter_bad_type(self):
|
||||
doc = {'ttl': '300', 'bogus': 'yogabbagabba'}
|
||||
spec = [('ttl', int)]
|
||||
self.assertRaises(falcon.HTTPBadRequest,
|
||||
helpers.filter, doc, spec)
|
||||
utils.filter, doc, spec)
|
||||
|
||||
def test_filter(self):
|
||||
doc = {'body': {'event': 'start_backup'}}
|
||||
@ -85,24 +85,24 @@ class TestWSGIHelpers(testtools.TestCase):
|
||||
def spec():
|
||||
yield ('body', dict)
|
||||
|
||||
filtered = helpers.filter(doc, spec())
|
||||
filtered = utils.filter(doc, spec())
|
||||
self.assertEqual(filtered, doc)
|
||||
|
||||
doc = {'ttl': 300, 'bogus': 'yogabbagabba'}
|
||||
spec = [('ttl', int)]
|
||||
filtered = helpers.filter(doc, spec)
|
||||
filtered = utils.filter(doc, spec)
|
||||
self.assertEqual(filtered, {'ttl': 300})
|
||||
|
||||
doc = {'body': {'event': 'start_backup'}, 'ttl': 300}
|
||||
spec = (('body', dict), ('ttl', int))
|
||||
filtered = helpers.filter(doc, spec)
|
||||
filtered = utils.filter(doc, spec)
|
||||
self.assertEqual(filtered, doc)
|
||||
|
||||
def test_filter_star(self):
|
||||
doc = {'ttl': 300, 'body': {'event': 'start_backup'}}
|
||||
|
||||
spec = [('body', '*'), ('ttl', '*')]
|
||||
filtered = helpers.filter(doc, spec)
|
||||
filtered = utils.filter(doc, spec)
|
||||
|
||||
self.assertEqual(filtered, doc)
|
||||
|
||||
@ -112,14 +112,14 @@ class TestWSGIHelpers(testtools.TestCase):
|
||||
document = json.dumps(obj, ensure_ascii=False)
|
||||
stream = io.StringIO(document)
|
||||
spec = [('body', dict), ('id', basestring)]
|
||||
filtered_object, = helpers.filter_stream(stream, len(document), spec)
|
||||
filtered_object, = utils.filter_stream(stream, len(document), spec)
|
||||
|
||||
self.assertEqual(filtered_object, obj)
|
||||
|
||||
stream.seek(0)
|
||||
self.assertRaises(falcon.HTTPBadRequest,
|
||||
helpers.filter_stream, stream, len(document), spec,
|
||||
doctype=helpers.JSONArray)
|
||||
utils.filter_stream, stream, len(document), spec,
|
||||
doctype=utils.JSONArray)
|
||||
|
||||
def test_filter_stream_expect_array(self):
|
||||
array = [{u'body': {u'x': 1}}, {u'body': {u'x': 2}}]
|
||||
@ -127,12 +127,12 @@ class TestWSGIHelpers(testtools.TestCase):
|
||||
document = json.dumps(array, ensure_ascii=False)
|
||||
stream = io.StringIO(document)
|
||||
spec = [('body', dict)]
|
||||
filtered_objects = list(helpers.filter_stream(
|
||||
stream, len(document), spec, doctype=helpers.JSONArray))
|
||||
filtered_objects = list(utils.filter_stream(
|
||||
stream, len(document), spec, doctype=utils.JSONArray))
|
||||
|
||||
self.assertEqual(filtered_objects, array)
|
||||
|
||||
stream.seek(0)
|
||||
self.assertRaises(falcon.HTTPBadRequest,
|
||||
helpers.filter_stream, stream, len(document), spec,
|
||||
doctype=helpers.JSONObject)
|
||||
utils.filter_stream, stream, len(document), spec,
|
||||
doctype=utils.JSONObject)
|
@ -18,9 +18,9 @@ import falcon
|
||||
from marconi.common import config
|
||||
import marconi.openstack.common.log as logging
|
||||
from marconi.storage import exceptions as storage_exceptions
|
||||
from marconi.transport import helpers
|
||||
from marconi.transport import utils
|
||||
from marconi.transport.wsgi import exceptions as wsgi_exceptions
|
||||
from marconi.transport.wsgi import helpers as wsgi_helpers
|
||||
from marconi.transport.wsgi import utils as wsgi_utils
|
||||
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
@ -55,8 +55,8 @@ class CollectionResource(object):
|
||||
|
||||
# Read claim metadata (e.g., TTL) and raise appropriate
|
||||
# HTTP errors as needed.
|
||||
metadata, = wsgi_helpers.filter_stream(req.stream, req.content_length,
|
||||
CLAIM_POST_SPEC)
|
||||
metadata, = wsgi_utils.filter_stream(req.stream, req.content_length,
|
||||
CLAIM_POST_SPEC)
|
||||
|
||||
# Claim some messages
|
||||
try:
|
||||
@ -87,7 +87,7 @@ class CollectionResource(object):
|
||||
del msg['id']
|
||||
|
||||
resp.location = req.path + '/' + cid
|
||||
resp.body = helpers.to_json(resp_msgs)
|
||||
resp.body = utils.to_json(resp_msgs)
|
||||
resp.status = falcon.HTTP_201
|
||||
else:
|
||||
resp.status = falcon.HTTP_204
|
||||
@ -134,7 +134,7 @@ class ItemResource(object):
|
||||
del meta['id']
|
||||
|
||||
resp.content_location = req.relative_uri
|
||||
resp.body = helpers.to_json(meta)
|
||||
resp.body = utils.to_json(meta)
|
||||
resp.status = falcon.HTTP_200
|
||||
|
||||
def on_patch(self, req, resp, project_id, queue_name, claim_id):
|
||||
@ -151,8 +151,8 @@ class ItemResource(object):
|
||||
|
||||
# Read claim metadata (e.g., TTL) and raise appropriate
|
||||
# HTTP errors as needed.
|
||||
metadata, = wsgi_helpers.filter_stream(req.stream, req.content_length,
|
||||
CLAIM_PATCH_SPEC)
|
||||
metadata, = wsgi_utils.filter_stream(req.stream, req.content_length,
|
||||
CLAIM_PATCH_SPEC)
|
||||
|
||||
try:
|
||||
self.claim_controller.update(queue_name,
|
||||
@ -188,7 +188,7 @@ class ItemResource(object):
|
||||
raise wsgi_exceptions.HTTPServiceUnavailable(description)
|
||||
|
||||
|
||||
# TODO(kgriffs): Clean up/optimize and move to wsgi.helpers
|
||||
# TODO(kgriffs): Clean up/optimize and move to wsgi.utils
|
||||
def _msg_uri_from_claim(base_path, msg_id, claim_id):
|
||||
return '/'.join(
|
||||
[base_path, 'messages', msg_id]
|
||||
|
@ -20,9 +20,9 @@ import falcon
|
||||
from marconi.common import config
|
||||
import marconi.openstack.common.log as logging
|
||||
from marconi.storage import exceptions as storage_exceptions
|
||||
from marconi.transport import helpers
|
||||
from marconi.transport import utils
|
||||
from marconi.transport.wsgi import exceptions as wsgi_exceptions
|
||||
from marconi.transport.wsgi import helpers as wsgi_helpers
|
||||
from marconi.transport.wsgi import utils as wsgi_utils
|
||||
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
@ -73,7 +73,7 @@ class CollectionResource(object):
|
||||
uuid = req.get_header('Client-ID', required=True)
|
||||
|
||||
# TODO(kgriffs): Optimize
|
||||
kwargs = helpers.purge({
|
||||
kwargs = utils.purge({
|
||||
'marker': req.get_param('marker'),
|
||||
'limit': req.get_param_as_int('limit'),
|
||||
'echo': req.get_param_as_bool('echo'),
|
||||
@ -146,11 +146,11 @@ class CollectionResource(object):
|
||||
raise wsgi_exceptions.HTTPBadRequestBody(description)
|
||||
|
||||
# Pull out just the fields we care about
|
||||
messages = wsgi_helpers.filter_stream(
|
||||
messages = wsgi_utils.filter_stream(
|
||||
req.stream,
|
||||
req.content_length,
|
||||
MESSAGE_POST_SPEC,
|
||||
doctype=wsgi_helpers.JSONArray)
|
||||
doctype=wsgi_utils.JSONArray)
|
||||
|
||||
# Verify that at least one message was provided.
|
||||
try:
|
||||
@ -199,7 +199,7 @@ class CollectionResource(object):
|
||||
|
||||
hrefs = [req.path + '/' + id for id in message_ids]
|
||||
body = {'resources': hrefs, 'partial': partial}
|
||||
resp.body = helpers.to_json(body)
|
||||
resp.body = utils.to_json(body)
|
||||
|
||||
def on_get(self, req, resp, project_id, queue_name):
|
||||
LOG.debug(_("Messages collection GET - queue: %(queue)s, "
|
||||
@ -219,7 +219,7 @@ class CollectionResource(object):
|
||||
resp.status = falcon.HTTP_204
|
||||
return
|
||||
|
||||
resp.body = helpers.to_json(response)
|
||||
resp.body = utils.to_json(response)
|
||||
|
||||
def on_delete(self, req, resp, project_id, queue_name):
|
||||
# NOTE(zyuan): Attempt to delete the whole message collection
|
||||
@ -273,7 +273,7 @@ class ItemResource(object):
|
||||
del message['id']
|
||||
|
||||
resp.content_location = req.relative_uri
|
||||
resp.body = helpers.to_json(message)
|
||||
resp.body = utils.to_json(message)
|
||||
resp.status = falcon.HTTP_200
|
||||
|
||||
def on_delete(self, req, resp, project_id, queue_name, message_id):
|
||||
|
@ -18,7 +18,7 @@ import falcon
|
||||
from marconi.common import config
|
||||
import marconi.openstack.common.log as logging
|
||||
from marconi.storage import exceptions as storage_exceptions
|
||||
from marconi.transport import helpers
|
||||
from marconi.transport import utils
|
||||
from marconi.transport.wsgi import exceptions as wsgi_exceptions
|
||||
|
||||
|
||||
@ -52,7 +52,7 @@ class Resource(object):
|
||||
raise wsgi_exceptions.HTTPServiceUnavailable(description)
|
||||
|
||||
resp.content_location = req.path
|
||||
resp.body = helpers.to_json(resp_dict)
|
||||
resp.body = utils.to_json(resp_dict)
|
||||
resp.status = falcon.HTTP_200
|
||||
|
||||
def on_put(self, req, resp, project_id, queue_name):
|
||||
@ -67,8 +67,8 @@ class Resource(object):
|
||||
|
||||
# Deserialize queue metadata
|
||||
try:
|
||||
metadata = helpers.read_json(req.stream, req.content_length)
|
||||
except helpers.MalformedJSON:
|
||||
metadata = utils.read_json(req.stream, req.content_length)
|
||||
except utils.MalformedJSON:
|
||||
description = _('Request body could not be parsed.')
|
||||
raise wsgi_exceptions.HTTPBadRequestBody(description)
|
||||
except Exception as ex:
|
||||
|
@ -16,7 +16,7 @@
|
||||
import falcon
|
||||
|
||||
import marconi.openstack.common.log as logging
|
||||
from marconi.transport import helpers
|
||||
from marconi.transport import utils
|
||||
from marconi.transport.wsgi import exceptions as wsgi_exceptions
|
||||
|
||||
|
||||
@ -88,7 +88,7 @@ class CollectionResource(object):
|
||||
|
||||
def on_get(self, req, resp, project_id):
|
||||
# TODO(kgriffs): Optimize
|
||||
kwargs = helpers.purge({
|
||||
kwargs = utils.purge({
|
||||
'marker': req.get_param('marker'),
|
||||
'limit': req.get_param_as_int('limit'),
|
||||
'detailed': req.get_param_as_bool('detailed'),
|
||||
@ -125,5 +125,5 @@ class CollectionResource(object):
|
||||
}
|
||||
|
||||
resp.content_location = req.relative_uri
|
||||
resp.body = helpers.to_json(response_body)
|
||||
resp.body = utils.to_json(response_body)
|
||||
resp.status = falcon.HTTP_200
|
||||
|
@ -17,7 +17,7 @@ import falcon
|
||||
|
||||
import marconi.openstack.common.log as logging
|
||||
from marconi.storage import exceptions as storage_exceptions
|
||||
from marconi.transport import helpers
|
||||
from marconi.transport import utils
|
||||
from marconi.transport.wsgi import exceptions as wsgi_exceptions
|
||||
|
||||
|
||||
@ -50,7 +50,7 @@ class Resource(object):
|
||||
del oldest['id']
|
||||
|
||||
resp.content_location = req.path
|
||||
resp.body = helpers.to_json(resp_dict)
|
||||
resp.body = utils.to_json(resp_dict)
|
||||
resp.status = falcon.HTTP_200
|
||||
|
||||
except storage_exceptions.DoesNotExist:
|
||||
|
@ -16,7 +16,7 @@
|
||||
|
||||
import marconi.openstack.common.log as logging
|
||||
|
||||
from marconi.transport import helpers
|
||||
from marconi.transport import utils
|
||||
from marconi.transport.wsgi import exceptions
|
||||
|
||||
|
||||
@ -55,14 +55,14 @@ def filter_stream(stream, len, spec, doctype=JSONObject):
|
||||
# TODO(kgriffs): read_json should stream the resulting list
|
||||
# of messages, returning a generator rather than buffering
|
||||
# everything in memory (bp/streaming-serialization).
|
||||
document = helpers.read_json(stream, len)
|
||||
document = utils.read_json(stream, len)
|
||||
|
||||
except helpers.MalformedJSON as ex:
|
||||
except utils.MalformedJSON as ex:
|
||||
LOG.exception(ex)
|
||||
description = _('Body could not be parsed.')
|
||||
raise exceptions.HTTPBadRequestBody(description)
|
||||
|
||||
except helpers.OverflowedJSONInteger as ex:
|
||||
except utils.OverflowedJSONInteger as ex:
|
||||
LOG.exception(ex)
|
||||
description = _('JSON contains integer that is too large.')
|
||||
raise exceptions.HTTPBadRequestBody(description)
|
Loading…
Reference in New Issue
Block a user