Require a transport to construct a Notifier

Rather than construct it from cfg.CONF by default, just require that one
is supplied.
This commit is contained in:
Mark McLoughlin 2013-06-24 07:12:32 +01:00
parent cfeaf2a292
commit 351eb2d9e4

@ -21,7 +21,6 @@ import logging
from oslo.config import cfg from oslo.config import cfg
from stevedore import named from stevedore import named
from oslo import messaging
from oslo.messaging.openstack.common import timeutils from oslo.messaging.openstack.common import timeutils
from oslo.messaging.openstack.common import uuidutils from oslo.messaging.openstack.common import uuidutils
from oslo.messaging import serializer as msg_serializer from oslo.messaging import serializer as msg_serializer
@ -70,54 +69,51 @@ class Notifier(object):
'event_type': 'compute.create_instance', 'event_type': 'compute.create_instance',
'payload': {'instance_id': 12, ... }} 'payload': {'instance_id': 12, ... }}
A Notifier object can be instantiated with a configuration object and a A Notifier object can be instantiated with a transport object and a
publisher ID: publisher ID:
notifier = notifier.Notifier(cfg.CONF, 'compute.host1') notifier = notifier.Notifier(get_transport(CONF), 'compute.host1')
and notifications are sent via drivers chosen with the notification_driver and notifications are sent via drivers chosen with the notification_driver
config option, on the topics consen with the notification_topics config config option and on the topics consen with the notification_topics config
option, on a transport contstructed using the supplied configuration. option.
Alternatively, a Notifier object can be instantiated with a specific Alternatively, a Notifier object can be instantiated with a specific
driver, topic or transport: driver or topic:
notifier = notifier.Notifier(cfg.CONF, notifier = notifier.Notifier(RPC_TRANSPORT,
'compute.host', 'compute.host',
driver='messaging', driver='messaging',
topic='notifications', topic='notifications')
transport=RPC_TRANSPORT)
""" """
def __init__(self, conf, publisher_id, def __init__(self, transport, publisher_id,
driver=None, topic=None, driver=None, topic=None,
transport=None, serializer=None): serializer=None):
"""Construct a Notifier object. """Construct a Notifier object.
:param conf: user configuration, used for e.g. notification_driver :param transport: the transport to use for sending messages
:type conf: a cfg.ConfigOpts instance :type transport: oslo.messaging.Transport
:param publisher_id: field in notifications sent, e.g. 'compute.host1' :param publisher_id: field in notifications sent, e.g. 'compute.host1'
:type publisher_id: str :type publisher_id: str
:param driver: a driver to lookup from oslo.messaging.notify.drivers :param driver: a driver to lookup from oslo.messaging.notify.drivers
:type driver: str :type driver: str
:param topic: the topic which to send messages on :param topic: the topic which to send messages on
:type topic: str :type topic: str
:param transport: the transport to use for sending messages
:type transport: oslo.messaging.Transport
:param serializer: an optional entity serializer :param serializer: an optional entity serializer
:type serializer: Serializer :type serializer: Serializer
""" """
self.conf = conf self.conf = transport.conf
self.conf.register_opts(_notifier_opts) self.conf.register_opts(_notifier_opts)
self.transport = transport
self.publisher_id = publisher_id self.publisher_id = publisher_id
self._driver_names = ([driver] if driver is not None self._driver_names = ([driver] if driver is not None
else conf.notification_driver) else self.conf.notification_driver)
self._topics = ([topic] if topic is not None self._topics = ([topic] if topic is not None
else conf.notification_topics) else self.conf.notification_topics)
self._transport = transport or messaging.get_transport(conf)
self._serializer = serializer or msg_serializer.NoOpSerializer() self._serializer = serializer or msg_serializer.NoOpSerializer()
self._driver_mgr = named.NamedExtensionManager( self._driver_mgr = named.NamedExtensionManager(
@ -127,7 +123,7 @@ class Notifier(object):
invoke_args=[self.conf], invoke_args=[self.conf],
invoke_kwds={ invoke_kwds={
'topics': self._topics, 'topics': self._topics,
'transport': self._transport, 'transport': self.transport,
}, },
) )