60a39ea151239a259d3a9e0013981678141f9d22
[escalator.git] / api / escalator / notifier.py
1 # Copyright 2011, OpenStack Foundation
2 # Copyright 2012, Red Hat, Inc.
3 # Copyright 2013 IBM Corp.
4 #
5 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
6 #    not use this file except in compliance with the License. You may obtain
7 #    a copy of the License at
8 #
9 #         http://www.apache.org/licenses/LICENSE-2.0
10 #
11 #    Unless required by applicable law or agreed to in writing, software
12 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 #    License for the specific language governing permissions and limitations
15 #    under the License.
16
17 from oslo.config import cfg
18 from oslo.log import log as logging
19 import oslo.messaging
20
21 from escalator import i18n
22
23 _ = i18n._
24 _LE = i18n._LE
25
26 notifier_opts = [
27     cfg.StrOpt('default_publisher_id', default="image.localhost",
28                help='Default publisher_id for outgoing notifications.'),
29     cfg.ListOpt('disabled_notifications', default=[],
30                 help='List of disabled notifications. A notification can be '
31                      'given either as a notification type to disable a single '
32                      'event, or as a notification group prefix to disable all '
33                      'events within a group. Example: if this config option '
34                      'is set to ["image.create", "metadef_namespace"], then '
35                      '"image.create" notification will not be sent after '
36                      'image is created and none of the notifications for '
37                      'metadefinition namespaces will be sent.'),
38 ]
39
40 CONF = cfg.CONF
41 CONF.register_opts(notifier_opts)
42
43 LOG = logging.getLogger(__name__)
44
45
46 def get_transport():
47     return oslo.messaging.get_transport(CONF)
48
49
50 class Notifier(object):
51     """Uses a notification strategy to send out messages about events."""
52
53     def __init__(self):
54         publisher_id = CONF.default_publisher_id
55         self._transport = get_transport()
56         self._notifier = oslo.messaging.Notifier(self._transport,
57                                                  publisher_id=publisher_id)
58
59     def warn(self, event_type, payload):
60         self._notifier.warn({}, event_type, payload)
61
62     def info(self, event_type, payload):
63         self._notifier.info({}, event_type, payload)
64
65     def error(self, event_type, payload):
66         self._notifier.error({}, event_type, payload)