~ubuntu-branches/ubuntu/quantal/nova/quantal-proposed

« back to all changes in this revision

Viewing changes to nova/notifier/list_notifier.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2012-08-16 14:04:11 UTC
  • mto: This revision was merged to the branch mainline in revision 84.
  • Revision ID: package-import@ubuntu.com-20120816140411-0mr4n241wmk30t9l
Tags: upstream-2012.2~f3
ImportĀ upstreamĀ versionĀ 2012.2~f3

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright 2011 OpenStack LLC.
2
 
# All Rights Reserved.
3
 
#
4
 
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
5
 
#    not use this file except in compliance with the License. You may obtain
6
 
#    a copy of the License at
7
 
#
8
 
#         http://www.apache.org/licenses/LICENSE-2.0
9
 
#
10
 
#    Unless required by applicable law or agreed to in writing, software
11
 
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
 
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
 
#    License for the specific language governing permissions and limitations
14
 
#    under the License.
15
 
 
16
 
from nova import flags
17
 
from nova.openstack.common import cfg
18
 
from nova.openstack.common import importutils
19
 
from nova.openstack.common import log as logging
20
 
 
21
 
 
22
 
list_notifier_drivers_opt = cfg.MultiStrOpt('list_notifier_drivers',
23
 
        default=['nova.notifier.no_op_notifier'],
24
 
        help='List of drivers to send notifications')
25
 
 
26
 
FLAGS = flags.FLAGS
27
 
FLAGS.register_opt(list_notifier_drivers_opt)
28
 
 
29
 
LOG = logging.getLogger(__name__)
30
 
 
31
 
drivers = None
32
 
 
33
 
 
34
 
class ImportFailureNotifier(object):
35
 
    """Noisily re-raises some exception over-and-over when notify is called."""
36
 
 
37
 
    def __init__(self, exception):
38
 
        self.exception = exception
39
 
 
40
 
    def notify(self, context, message):
41
 
        raise self.exception
42
 
 
43
 
 
44
 
def _get_drivers():
45
 
    """Instantiates and returns drivers based on the flag values."""
46
 
    global drivers
47
 
    if not drivers:
48
 
        drivers = []
49
 
        for notification_driver in FLAGS.list_notifier_drivers:
50
 
            try:
51
 
                drivers.append(importutils.import_module(notification_driver))
52
 
            except ImportError as e:
53
 
                drivers.append(ImportFailureNotifier(e))
54
 
    return drivers
55
 
 
56
 
 
57
 
def notify(context, message):
58
 
    """Passes notification to multiple notifiers in a list."""
59
 
    for driver in _get_drivers():
60
 
        try:
61
 
            driver.notify(context, message)
62
 
        except Exception as e:
63
 
            LOG.exception(_("Problem '%(e)s' attempting to send to "
64
 
                            "notification driver %(driver)s."), locals())
65
 
 
66
 
 
67
 
def _reset_drivers():
68
 
    """Used by unit tests to reset the drivers."""
69
 
    global drivers
70
 
    drivers = None