Merge "Tweak base objects/fields for notification"

This commit is contained in:
Jenkins 2016-11-23 16:17:07 +00:00 committed by Gerrit Code Review
commit 4a3708b6c9
3 changed files with 34 additions and 22 deletions

View File

@ -158,15 +158,15 @@ class Json(fields.FieldType):
class NotificationPriority(fields.Enum):
# The priorities here are derived from oslo_messaging.notify.notifier
ALL = (
AUDIT, CRITICAL, DEBUG, INFO, ERROR, SAMPLE, WARN,
AUDIT, CRITICAL, ERROR, WARN, INFO, DEBUG, SAMPLE,
) = (
'audit', 'critical', 'debug', 'info', 'error', 'sample', 'warn',
'audit', 'critical', 'error', 'warn', 'info', 'debug', 'sample',
)
def __init__(self):
super(NotificationPriority, self).__init__(
valid_values=NotificationPriority.ALL)
super(NotificationPriority, self).__init__(self.ALL)
class NotificationPhase(fields.Enum):
@ -178,21 +178,32 @@ class NotificationPhase(fields.Enum):
)
def __init__(self):
super(NotificationPhase, self).__init__(
valid_values=NotificationPhase.ALL)
super(NotificationPhase, self).__init__(self.ALL)
class NotificationAction(fields.Enum):
# This is a combination of cluster actions and node actions
ALL = (
UPDATE,
CLUSTER_CREATE, CLUSTER_DELETE, CLUSTER_UPDATE,
CLUSTER_ADD_NODES, CLUSTER_DEL_NODES, CLUSTER_REPLACE_NODES,
CLUSTER_SCALE_OUT, CLUSTER_SCALE_IN, CLUSTER_RESIZE,
CLUSTER_ATTACH_POLICY, CLUSTER_DETACH_POLICY, CLUSTER_UPDATE_POLICY,
CLUSTER_CHECK, CLUSTER_RECOVER,
NODE_CREATE, NODE_DELETE, NODE_UPDATE,
NODE_CHECK, NODE_RECOVER
) = (
'update',
'create', 'delete', 'update',
'add_nodes', 'del_nodes', 'replace_nodes',
'scale_out', 'scale_in', 'resize',
'attach_policy', 'detach_policy', 'update_policy',
'check', 'recover',
'create', 'delete', 'update',
'check', 'recover',
)
def __init__(self):
super(NotificationAction, self).__init__(
valid_values=NotificationAction.ALL)
super(NotificationAction, self).__init__(self.ALL)
class Name(fields.String):

View File

@ -51,12 +51,12 @@ class NotificationPublisher(NotificationObject):
VERSION = '1.0'
fields = {
'host': fields.StringField(nullable=False),
'binary': fields.StringField(nullable=False),
'host': fields.StringField(),
'binary': fields.StringField(),
}
@classmethod
def from_service_obj(cls, service):
def from_service(cls, service):
return cls(host=service.host, binary=service.binary)
@property
@ -81,7 +81,7 @@ class NotificationBase(NotificationObject):
def _emit(self, context, event_type, publisher_id, payload):
notifier = messaging.get_notifier(publisher_id)
notify = getattr(notifier, self.priority)
notify(context, event_type=event_type, payload=payload)
notify(context, event_type, payload)
def emit(self, context):
"""Send the notification."""

View File

@ -103,9 +103,9 @@ class TestNotificationBase(test_base.SenlinTestCase):
self.notification = TestNotification(
event_type=base.EventType(
object='test_object',
action=fields.NotificationAction.UPDATE,
action=fields.NotificationAction.CLUSTER_UPDATE,
phase=fields.NotificationPhase.START),
publisher=base.NotificationPublisher.from_service_obj(
publisher=base.NotificationPublisher.from_service(
self.service_obj),
priority=fields.NotificationPriority.INFO,
payload=self.payload)
@ -117,9 +117,9 @@ class TestNotificationBase(test_base.SenlinTestCase):
mock_notify = mock_notifier.prepare.return_value.info
self.assertTrue(mock_notify.called)
self.assertEqual(mock_notify.call_args[0][0], mock_context)
self.assertEqual(mock_notify.call_args[1]['event_type'],
self.assertEqual(mock_notify.call_args[0][1],
expected_event_type)
actual_payload = mock_notify.call_args[1]['payload']
actual_payload = mock_notify.call_args[0][2]
self.assertJsonEqual(expected_payload, actual_payload)
@mock.patch('senlin.common.messaging.NOTIFIER')
@ -139,7 +139,7 @@ class TestNotificationBase(test_base.SenlinTestCase):
def test_emit_with_host_and_binary_as_publisher(self, mock_notifier):
event_type = base.EventType(
object='test_object',
action=fields.NotificationAction.UPDATE)
action=fields.NotificationAction.CLUSTER_UPDATE)
publisher = base.NotificationPublisher(host='fake-host',
binary='senlin-fake')
@ -161,9 +161,10 @@ class TestNotificationBase(test_base.SenlinTestCase):
@mock.patch('senlin.common.messaging.NOTIFIER')
def test_emit_event_type_without_phase(self, mock_notifier):
noti = TestNotification(
event_type=base.EventType(object='test_object',
action=fields.NotificationAction.UPDATE),
publisher=base.NotificationPublisher.from_service_obj(
event_type=base.EventType(
object='test_object',
action=fields.NotificationAction.CLUSTER_UPDATE),
publisher=base.NotificationPublisher.from_service(
self.service_obj),
priority=fields.NotificationPriority.INFO,
payload=self.payload)