Merge from Dragon

This commit is contained in:
Cerberus
2011-05-11 10:42:51 -05:00
5 changed files with 63 additions and 13 deletions

View File

@@ -14,7 +14,6 @@
# under the License.
import datetime
import json
import uuid
from nova import flags
@@ -22,7 +21,7 @@ from nova import utils
FLAGS = flags.FLAGS
flags.DEFINE_string('default_notification_level', 'info',
flags.DEFINE_string('default_notification_level', 'INFO',
'Default notification level for outgoing notifications')
WARN = 'WARN'
@@ -73,4 +72,4 @@ def notify(event_name, publisher_id, event_type, priority, payload):
priority=priority,
payload=payload,
time=str(datetime.datetime.utcnow()))
driver.notify(json.dumps(message))
driver.notify(message)

View File

@@ -0,0 +1,33 @@
# 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.
import json
from nova import flags
from nova import log as logging
FLAGS = flags.FLAGS
class LogNotifier(object):
""" log notifications using nova's default logging system """
def notify(self, payload):
"""Notifies the recipient of the desired event given the model"""
priority = payload.get('priority',
FLAGS.default_notification_level)
priority = priority.lower()
logger = logging.getLogger('nova.notification.%s' % payload['event_type'])
getattr(logger, priority)(json.dumps(payload))

View File

@@ -14,6 +14,6 @@
# under the License.
class NoopNotifier(object):
def notify(self, event_name, model):
def notify(self, payload):
"""Notifies the recipient of the desired event given the model"""
pass

View File

@@ -13,7 +13,6 @@
# License for the specific language governing permissions and limitations
# under the License.
import json
import nova.context
@@ -28,10 +27,12 @@ flags.DEFINE_string('notification_topic', 'notifications',
class RabbitNotifier(object):
"""Sends notifications to a specific RabbitMQ server and topic"""
pass
def notify(self, payload):
"""Sends a notification to the RabbitMQ"""
context = nova.context.get_admin_context()
topic = FLAGS.notification_topic
priority = payload.get('priority',
FLAGS.default_notification_level)
priority = priority.lower()
topic = '%s.%s' % (FLAGS.notification_topic, priority)
rpc.cast(context, topic, payload)

View File

@@ -13,13 +13,14 @@
# License for the specific language governing permissions and limitations
# under the License.
import json
import nova
from nova import context
from nova import flags
from nova import rpc
from nova import notifier
from nova.notifier import no_op_notifier
from nova.notifier import rabbit_notifier
from nova import test
import stubout
@@ -51,8 +52,7 @@ class NotifierTestCase(test.TestCase):
def test_verify_message_format(self):
"""A test to ensure changing the message format is prohibitively
annoying"""
def message_assert(cls, blob):
message = json.loads(blob)
def message_assert(cls, message):
fields = [('publisher_id', 'publisher_id'),
('event_type', 'event_type'),
('priority', 'WARN'),
@@ -72,7 +72,7 @@ class NotifierTestCase(test.TestCase):
self.mock_cast = False
def mock_cast(cls, *args):
self.mock_cast = True
class Mock(object):
pass
self.stubs.Set(nova.rpc, 'cast', mock_cast)
@@ -84,7 +84,7 @@ class NotifierTestCase(test.TestCase):
def test_invalid_priority(self):
def mock_cast(cls, *args):
pass
class Mock(object):
pass
@@ -92,3 +92,20 @@ class NotifierTestCase(test.TestCase):
self.assertRaises(nova.notifier.BadPriorityException,
nova.notifier.notify, 'event_name', 'publisher_id',
'event_type', 'not a priority', dict(a=3))
def test_rabbit_priority_queue(self):
self.stubs.Set(nova.flags.FLAGS, 'notification_driver',
'nova.notifier.rabbit_notifier.RabbitNotifier')
self.stubs.Set(nova.flags.FLAGS, 'notification_topic',
'testnotify')
self.test_topic = None
def mock_cast(context, topic, msg):
self.test_topic = topic
self.stubs.Set(nova.rpc, 'cast', mock_cast)
nova.notifier.notify('event_name', 'publisher_id',
'event_type', 'DEBUG', dict(a=3))
self.assertEqual(self.test_topic, 'testnotify.debug')