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
This commit is contained in:
Russell Bryant 2012-01-04 16:01:13 -05:00
parent d57031ed26
commit f8ae3c4995
7 changed files with 185 additions and 91 deletions

View File

@ -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__)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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

View File

@ -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()

View File

@ -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)