summaryrefslogtreecommitdiffstats
path: root/nova/openstack/common/notifier/list_notifier.py
blob: 7c2ec6e6da06483a87343738fa74a2fb7d55e5c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# Copyright 2011 OpenStack LLC.
# All Rights Reserved.
#
#    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 nova.openstack.common import cfg
from nova.openstack.common import importutils
from nova.openstack.common import log as logging


list_notifier_drivers_opt = cfg.MultiStrOpt('list_notifier_drivers',
        default=['nova.openstack.common.notifier.no_op_notifier'],
        help='List of drivers to send notifications')

CONF = cfg.CONF
CONF.register_opt(list_notifier_drivers_opt)

LOG = logging.getLogger(__name__)

drivers = None


class ImportFailureNotifier(object):
    """Noisily re-raises some exception over-and-over when notify is called."""

    def __init__(self, exception):
        self.exception = exception

    def notify(self, context, message):
        raise self.exception


def _get_drivers():
    """Instantiates and returns drivers based on the flag values."""
    global drivers
    if drivers is None:
        drivers = []
        for notification_driver in CONF.list_notifier_drivers:
            try:
                drivers.append(importutils.import_module(notification_driver))
            except ImportError as e:
                drivers.append(ImportFailureNotifier(e))
    return drivers


def add_driver(notification_driver):
    """Add a notification driver at runtime."""
    # Make sure the driver list is initialized.
    _get_drivers()
    if isinstance(notification_driver, basestring):
        # Load and add
        try:
            drivers.append(importutils.import_module(notification_driver))
        except ImportError as e:
            drivers.append(ImportFailureNotifier(e))
    else:
        # Driver is already loaded; just add the object.
        drivers.append(notification_driver)


def _object_name(obj):
    name = []
    if hasattr(obj, '__module__'):
        name.append(obj.__module__)
    if hasattr(obj, '__name__'):
        name.append(obj.__name__)
    else:
        name.append(obj.__class__.__name__)
    return '.'.join(name)


def remove_driver(notification_driver):
    """Remove a notification driver at runtime."""
    # Make sure the driver list is initialized.
    _get_drivers()
    removed = False
    if notification_driver in drivers:
        # We're removing an object.  Easy.
        drivers.remove(notification_driver)
        removed = True
    else:
        # We're removing a driver by name.  Search for it.
        for driver in drivers:
            if _object_name(driver) == notification_driver:
                drivers.remove(driver)
                removed = True

    if not removed:
        raise ValueError("Cannot remove; %s is not in list" %
                         notification_driver)


def notify(context, message):
    """Passes notification to multiple notifiers in a list."""
    for driver in _get_drivers():
        try:
            driver.notify(context, message)
        except Exception as e:
            LOG.exception(_("Problem '%(e)s' attempting to send to "
                            "notification driver %(driver)s."), locals())


def _reset_drivers():
    """Used by unit tests to reset the drivers."""
    global drivers
    drivers = None