Code cleanup

This commit is contained in:
Cerberus
2011-05-11 10:40:54 -05:00
parent ab016fa19b
commit c45d6af682
2 changed files with 18 additions and 16 deletions

View File

@@ -15,6 +15,7 @@
import datetime import datetime
import json import json
import uuid
from nova import flags from nova import flags
from nova import utils from nova import utils
@@ -41,6 +42,7 @@ def notify(event_name, publisher_id, event_type, priority, payload):
Message format is as follows: Message format is as follows:
message_id - a UUID representing the id for this notification
publisher_id - the source worker_type.host of the message publisher_id - the source worker_type.host of the message
timestamp - the GMT timestamp the notification was sent at timestamp - the GMT timestamp the notification was sent at
event_type - the literal type of event (ex. Instance Creation) event_type - the literal type of event (ex. Instance Creation)
@@ -48,14 +50,15 @@ def notify(event_name, publisher_id, event_type, priority, payload):
the set (DEBUG, WARN, INFO, ERROR, CRITICAL) the set (DEBUG, WARN, INFO, ERROR, CRITICAL)
payload - A python dictionary of attributes payload - A python dictionary of attributes
The payload will be constructed as a dictionary of the above attributes, The message body will be constructed as a dictionary of the above
and converted into a JSON dump, which will then be sent via the transport attributes, and converted into a JSON dump, which will then be sent
mechanism defined by the driver. via the transport mechanism defined by the driver.
Message example: Message example:
{ 'publisher_id': 'compute.host1', {'message_id': str(uuid.uuid4()),
'timestamp': '2011-05-09 22:00:14.621831', 'publisher_id': 'compute.host1',
'timestamp': datetime.datetime.utcnow(),
'priority': 'WARN', 'priority': 'WARN',
'event_type': 'compute.create_instance', 'event_type': 'compute.create_instance',
'payload': {'instance_id': 12, ... }} 'payload': {'instance_id': 12, ... }}
@@ -64,7 +67,8 @@ def notify(event_name, publisher_id, event_type, priority, payload):
if priority not in log_levels: if priority not in log_levels:
raise BadPriorityException('%s not in valid priorities' % priority) raise BadPriorityException('%s not in valid priorities' % priority)
driver = utils.import_class(FLAGS.notification_driver)() driver = utils.import_class(FLAGS.notification_driver)()
message = dict(publisher_id=publisher_id, message = dict(message_id=str(uuid.uuid4()),
publisher_id=publisher_id,
event_type=event_type, event_type=event_type,
priority=priority, priority=priority,
payload=payload, payload=payload,

View File

@@ -53,12 +53,13 @@ class NotifierTestCase(test.TestCase):
annoying""" annoying"""
def message_assert(cls, blob): def message_assert(cls, blob):
message = json.loads(blob) message = json.loads(blob)
fields = [ ('publisher_id', 'publisher_id'), fields = [('publisher_id', 'publisher_id'),
('event_type', 'event_type'), ('event_type', 'event_type'),
('priority', 'WARN'), ('priority', 'WARN'),
('payload', dict(a=3))] ('payload', dict(a=3))]
for k, v in fields: for k, v in fields:
self.assertEqual(message[k], v) self.assertEqual(message[k], v)
self.assertTrue(len(message['message_id']) > 0)
self.stubs.Set(nova.notifier.no_op_notifier.NoopNotifier, 'notify', self.stubs.Set(nova.notifier.no_op_notifier.NoopNotifier, 'notify',
message_assert) message_assert)
@@ -81,9 +82,6 @@ class NotifierTestCase(test.TestCase):
self.assertEqual(self.mock_cast, True) self.assertEqual(self.mock_cast, True)
def test_invalid_priority(self): def test_invalid_priority(self):
self.stubs.Set(nova.flags.FLAGS, 'notification_driver',
'nova.notifier.rabbit_notifier.RabbitNotifier')
self.mock_cast = False
def mock_cast(cls, *args): def mock_cast(cls, *args):
pass pass