Merge "Split notification strategies out into modules."

This commit is contained in:
Jenkins 2012-01-10 16:19:45 +00:00 committed by Gerrit Code Review
commit 7f4b1c5899
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)