Serialize the notification payload
Notification payload in oslomessaging needs to be serialized correctly, currently it's not. Use json to encode the payload. Closes-Bug: 1294949 Change-Id: Idb2c13981cc1c60ed02445a28220d1dab1529d2b
This commit is contained in:
parent
a4291a82fc
commit
caff15af26
|
@ -31,6 +31,7 @@ from oslo import messaging
|
|||
|
||||
import cinder.context
|
||||
import cinder.exception
|
||||
from cinder.openstack.common import jsonutils
|
||||
|
||||
CONF = cfg.CONF
|
||||
TRANSPORT = None
|
||||
|
@ -60,8 +61,9 @@ def init(conf):
|
|||
TRANSPORT = messaging.get_transport(conf,
|
||||
allowed_remote_exmods=exmods,
|
||||
aliases=TRANSPORT_ALIASES)
|
||||
NOTIFIER = messaging.Notifier(TRANSPORT,
|
||||
serializer=RequestContextSerializer(None))
|
||||
|
||||
serializer = RequestContextSerializer(JsonPayloadSerializer())
|
||||
NOTIFIER = messaging.Notifier(TRANSPORT, serializer=serializer)
|
||||
|
||||
|
||||
def initialized():
|
||||
|
@ -92,6 +94,12 @@ def get_allowed_exmods():
|
|||
return ALLOWED_EXMODS + EXTRA_EXMODS
|
||||
|
||||
|
||||
class JsonPayloadSerializer(messaging.NoOpSerializer):
|
||||
@staticmethod
|
||||
def serialize_entity(context, entity):
|
||||
return jsonutils.to_primitive(entity, convert_instances=True)
|
||||
|
||||
|
||||
class RequestContextSerializer(messaging.Serializer):
|
||||
|
||||
def __init__(self, base):
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
import collections
|
||||
import functools
|
||||
|
||||
import anyjson
|
||||
from oslo import messaging
|
||||
|
||||
from cinder import rpc
|
||||
|
@ -33,19 +34,25 @@ FakeMessage = collections.namedtuple('Message',
|
|||
|
||||
class FakeNotifier(object):
|
||||
|
||||
def __init__(self, transport, publisher_id):
|
||||
def __init__(self, transport, publisher_id, serializer=None):
|
||||
self.transport = transport
|
||||
self.publisher_id = publisher_id
|
||||
for priority in ['debug', 'info', 'warn', 'error', 'critical']:
|
||||
setattr(self, priority,
|
||||
functools.partial(self._notify, priority.upper()))
|
||||
self._serializer = serializer or messaging.serializer.NoOpSerializer()
|
||||
|
||||
def prepare(self, publisher_id=None):
|
||||
if publisher_id is None:
|
||||
publisher_id = self.publisher_id
|
||||
return self.__class__(self.transport, publisher_id)
|
||||
return self.__class__(self.transport, publisher_id, self._serializer)
|
||||
|
||||
def _notify(self, priority, ctxt, event_type, payload):
|
||||
payload = self._serializer.serialize_entity(ctxt, payload)
|
||||
# NOTE(sileht): simulate the kombu serializer
|
||||
# this permit to raise an exception if something have not
|
||||
# been serialized correctly
|
||||
anyjson.serialize(payload)
|
||||
msg = dict(publisher_id=self.publisher_id,
|
||||
priority=priority,
|
||||
event_type=event_type,
|
||||
|
@ -56,5 +63,7 @@ class FakeNotifier(object):
|
|||
def stub_notifier(stubs):
|
||||
stubs.Set(messaging, 'Notifier', FakeNotifier)
|
||||
if rpc.NOTIFIER:
|
||||
serializer = getattr(rpc.NOTIFIER, '_serializer', None)
|
||||
stubs.Set(rpc, 'NOTIFIER', FakeNotifier(rpc.NOTIFIER.transport,
|
||||
rpc.NOTIFIER.publisher_id))
|
||||
rpc.NOTIFIER.publisher_id,
|
||||
serializer=serializer))
|
||||
|
|
Loading…
Reference in New Issue