From f8ae3c4995d8716c46750e3715532e3086a2645a Mon Sep 17 00:00:00 2001 From: Russell Bryant Date: Wed, 4 Jan 2012 16:01:13 -0500 Subject: [PATCH] Split notification strategies out into modules. 1) Add a Strategy class that defines the interface that a particular notification strategy must implement. 2) Split each notification strategy out into its own module. This is in part just an attempt at some "code cleanup", but it also has an advantage that it is easier to make a particular notification strategy be optional and not impose a hard dependency on all of glance. Change-Id: Iab658637c56e764e2b3ec54c67e752db0cbfbe5c --- glance/api/v1/images.py | 2 +- glance/notifier/__init__.py | 72 +++++++++++++++ .../notifier.py => notifier/notify_kombu.py} | 91 +------------------ glance/notifier/notify_log.py | 36 ++++++++ glance/notifier/notify_noop.py | 34 +++++++ glance/notifier/strategy.py | 32 +++++++ glance/tests/unit/test_notifier.py | 9 +- 7 files changed, 185 insertions(+), 91 deletions(-) create mode 100644 glance/notifier/__init__.py rename glance/{common/notifier.py => notifier/notify_kombu.py} (57%) create mode 100644 glance/notifier/notify_log.py create mode 100644 glance/notifier/notify_noop.py create mode 100644 glance/notifier/strategy.py diff --git a/glance/api/v1/images.py b/glance/api/v1/images.py index b886220b46..2783f7e462 100644 --- a/glance/api/v1/images.py +++ b/glance/api/v1/images.py @@ -36,7 +36,6 @@ from glance.api.v1 import controller from glance import image_cache from glance.common import cfg from glance.common import exception -from glance.common import notifier from glance.common import wsgi from glance.common import utils import glance.store @@ -51,6 +50,7 @@ from glance.store import (get_from_backend, get_store_from_scheme, UnsupportedBackend) from glance import registry +from glance import notifier logger = logging.getLogger(__name__) diff --git a/glance/notifier/__init__.py b/glance/notifier/__init__.py new file mode 100644 index 0000000000..707452ccd8 --- /dev/null +++ b/glance/notifier/__init__.py @@ -0,0 +1,72 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2011, OpenStack LLC. +# Copyright 2012, Red Hat, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + + +import datetime +import uuid +import socket + +from glance.common import cfg +from glance.common import exception +from glance.common import utils + + +_STRATEGIES = { + "logging": "glance.notifier.notify_log.LoggingStrategy", + "rabbit": "glance.notifier.notify_kombu.RabbitStrategy", + "noop": "glance.notifier.notify_noop.NoopStrategy", + "default": "glance.notifier.notify_noop.NoopStrategy", +} + + +class Notifier(object): + """Uses a notification strategy to send out messages about events.""" + + opts = [ + cfg.StrOpt('notifier_strategy', default='default') + ] + + def __init__(self, conf, strategy=None): + conf.register_opts(self.opts) + strategy = conf.notifier_strategy + try: + self.strategy = utils.import_class(_STRATEGIES[strategy])(conf) + except KeyError, ImportError: + raise exception.InvalidNotifierStrategy(strategy=strategy) + + @staticmethod + def generate_message(event_type, priority, payload): + return { + "message_id": str(uuid.uuid4()), + "publisher_id": socket.gethostname(), + "event_type": event_type, + "priority": priority, + "payload": payload, + "timestamp": str(datetime.datetime.utcnow()), + } + + def warn(self, event_type, payload): + msg = self.generate_message(event_type, "WARN", payload) + self.strategy.warn(msg) + + def info(self, event_type, payload): + msg = self.generate_message(event_type, "INFO", payload) + self.strategy.info(msg) + + def error(self, event_type, payload): + msg = self.generate_message(event_type, "ERROR", payload) + self.strategy.error(msg) diff --git a/glance/common/notifier.py b/glance/notifier/notify_kombu.py similarity index 57% rename from glance/common/notifier.py rename to glance/notifier/notify_kombu.py index 983e65cf4d..31da12f81b 100644 --- a/glance/common/notifier.py +++ b/glance/notifier/notify_kombu.py @@ -1,12 +1,10 @@ # vim: tabstop=4 shiftwidth=4 softtabstop=4 -# Copyright 2011 OpenStack LLC. -# All Rights Reserved. +# Copyright 2011, OpenStack LLC. # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain # a copy of the License at -# # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software @@ -15,52 +13,17 @@ # License for the specific language governing permissions and limitations # under the License. -import datetime + import json -import logging -import socket -import uuid import kombu.connection import kombu.entity from glance.common import cfg -from glance.common import exception +from glance.notifier import strategy -class NoopStrategy(object): - """A notifier that does nothing when called.""" - - def __init__(self, conf): - pass - - def warn(self, msg): - pass - - def info(self, msg): - pass - - def error(self, msg): - pass - - -class LoggingStrategy(object): - """A notifier that calls logging when called.""" - - def __init__(self, conf): - self.logger = logging.getLogger('glance.notifier.logging_notifier') - - def warn(self, msg): - self.logger.warn(msg) - - def info(self, msg): - self.logger.info(msg) - - def error(self, msg): - self.logger.error(msg) - - -class RabbitStrategy(object): +class RabbitStrategy(strategy.Strategy): """A notifier that puts a message on a queue when called.""" opts = [ @@ -122,49 +85,3 @@ class RabbitStrategy(object): def error(self, msg): self._send_message(msg, "ERROR") - - -class Notifier(object): - """Uses a notification strategy to send out messages about events.""" - - STRATEGIES = { - "logging": LoggingStrategy, - "rabbit": RabbitStrategy, - "noop": NoopStrategy, - "default": NoopStrategy, - } - - opts = [ - cfg.StrOpt('notifier_strategy', default='default') - ] - - def __init__(self, conf, strategy=None): - conf.register_opts(self.opts) - strategy = conf.notifier_strategy - try: - self.strategy = self.STRATEGIES[strategy](conf) - except KeyError: - raise exception.InvalidNotifierStrategy(strategy=strategy) - - @staticmethod - def generate_message(event_type, priority, payload): - return { - "message_id": str(uuid.uuid4()), - "publisher_id": socket.gethostname(), - "event_type": event_type, - "priority": priority, - "payload": payload, - "timestamp": str(datetime.datetime.utcnow()), - } - - def warn(self, event_type, payload): - msg = self.generate_message(event_type, "WARN", payload) - self.strategy.warn(msg) - - def info(self, event_type, payload): - msg = self.generate_message(event_type, "INFO", payload) - self.strategy.info(msg) - - def error(self, event_type, payload): - msg = self.generate_message(event_type, "ERROR", payload) - self.strategy.error(msg) diff --git a/glance/notifier/notify_log.py b/glance/notifier/notify_log.py new file mode 100644 index 0000000000..276b8eb367 --- /dev/null +++ b/glance/notifier/notify_log.py @@ -0,0 +1,36 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2011, OpenStack LLC. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + + +import logging + +from glance.notifier import strategy + + +class LoggingStrategy(strategy.Strategy): + """A notifier that calls logging when called.""" + + def __init__(self, conf): + self.logger = logging.getLogger('glance.notifier.logging_notifier') + + def warn(self, msg): + self.logger.warn(msg) + + def info(self, msg): + self.logger.info(msg) + + def error(self, msg): + self.logger.error(msg) diff --git a/glance/notifier/notify_noop.py b/glance/notifier/notify_noop.py new file mode 100644 index 0000000000..2871273203 --- /dev/null +++ b/glance/notifier/notify_noop.py @@ -0,0 +1,34 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2011, OpenStack LLC. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + + +from glance.notifier import strategy + + +class NoopStrategy(strategy.Strategy): + """A notifier that does nothing when called.""" + + def __init__(self, conf): + pass + + def warn(self, msg): + pass + + def info(self, msg): + pass + + def error(self, msg): + pass diff --git a/glance/notifier/strategy.py b/glance/notifier/strategy.py new file mode 100644 index 0000000000..45a0ca31d2 --- /dev/null +++ b/glance/notifier/strategy.py @@ -0,0 +1,32 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2011, OpenStack LLC. +# Copyright 2012, Red Hat, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + + +class Strategy(object): + """Base class for a notification strategy""" + + def __init__(self, conf): + pass + + def warn(self, msg): + raise NotImplementedError() + + def info(self, msg): + raise NotImplementedError() + + def error(self, msg): + raise NotImplementedError() diff --git a/glance/tests/unit/test_notifier.py b/glance/tests/unit/test_notifier.py index 824bf288c4..24257456ed 100644 --- a/glance/tests/unit/test_notifier.py +++ b/glance/tests/unit/test_notifier.py @@ -19,7 +19,8 @@ import logging import unittest from glance.common import exception -from glance.common import notifier +from glance.common import utils as common_utils +from glance import notifier from glance.tests import utils @@ -85,8 +86,10 @@ class TestRabbitNotifier(unittest.TestCase): """Test AMQP/Rabbit notifier works.""" def setUp(self): - notifier.RabbitStrategy._send_message = self._send_message - notifier.RabbitStrategy.connect = lambda s: None + notify_kombu = common_utils.import_object( + "glance.notifier.notify_kombu") + notify_kombu.RabbitStrategy._send_message = self._send_message + notify_kombu.RabbitStrategy.connect = lambda s: None self.called = False conf = utils.TestConfigOpts({"notifier_strategy": "rabbit"}) self.notifier = notifier.Notifier(conf)