Ejemplo n.º 1
0
def send_service_update(context,
                        failed_service,
                        state,
                        service='infra-optim',
                        host=None):
    """Emit an service failed notification."""
    if state == o_service.ServiceStatus.FAILED:
        priority = wfields.NotificationPriority.WARNING
        status_update = ServiceStatusUpdatePayload(
            old_state=o_service.ServiceStatus.ACTIVE,
            state=o_service.ServiceStatus.FAILED)
    else:
        priority = wfields.NotificationPriority.INFO
        status_update = ServiceStatusUpdatePayload(
            old_state=o_service.ServiceStatus.FAILED,
            state=o_service.ServiceStatus.ACTIVE)
    versioned_payload = ServiceUpdatePayload(failed_service=failed_service,
                                             status_update=status_update)

    notification = ServiceUpdateNotification(
        priority=priority,
        event_type=notificationbase.EventType(
            object='service', action=wfields.NotificationAction.UPDATE),
        publisher=notificationbase.NotificationPublisher(host=host
                                                         or CONF.host,
                                                         binary=service),
        payload=versioned_payload)

    notification.emit(context)
Ejemplo n.º 2
0
def send_update(context, action, service='infra-optim',
                host=None, old_state=None):
    """Emit an action.update notification."""
    action_plan_payload = _get_action_plan_payload(action)

    state_update = ActionStateUpdatePayload(
        old_state=old_state,
        state=action.state if old_state else None)

    versioned_payload = ActionUpdatePayload(
        action=action,
        state_update=state_update,
        action_plan=action_plan_payload,
    )

    notification = ActionUpdateNotification(
        priority=wfields.NotificationPriority.INFO,
        event_type=notificationbase.EventType(
            object='action',
            action=wfields.NotificationAction.UPDATE),
        publisher=notificationbase.NotificationPublisher(
            host=host or CONF.host,
            binary=service),
        payload=versioned_payload)

    notification.emit(context)
Ejemplo n.º 3
0
def send_execution_notification(context, action, notification_action, phase,
                                priority=wfields.NotificationPriority.INFO,
                                service='infra-optim', host=None):
    """Emit an action execution notification."""
    action_plan_payload = _get_action_plan_payload(action)

    fault = None
    if phase == wfields.NotificationPhase.ERROR:
        fault = exception_notifications.ExceptionPayload.from_exception()

    versioned_payload = ActionExecutionPayload(
        action=action,
        action_plan=action_plan_payload,
        fault=fault,
    )

    notification = ActionExecutionNotification(
        priority=priority,
        event_type=notificationbase.EventType(
            object='action',
            action=notification_action,
            phase=phase),
        publisher=notificationbase.NotificationPublisher(
            host=host or CONF.host,
            binary=service),
        payload=versioned_payload)

    notification.emit(context)
Ejemplo n.º 4
0
    def test_empty_schema(self, mock_notifier):
        non_populated_payload = self.TestNotificationPayloadEmptySchema(
            extra_field='test string')
        noti = self.TestNotificationEmptySchema(
            event_type=notificationbase.EventType(
                object='test_object',
                action=wfields.NotificationAction.UPDATE),
            publisher=notificationbase.NotificationPublisher(
                host='fake-host', binary='watcher-fake'),
            priority=wfields.NotificationPriority.INFO,
            payload=non_populated_payload)

        mock_context = mock.Mock()
        mock_context.to_dict.return_value = {}
        noti.emit(mock_context)

        self._verify_notification(
            mock_notifier,
            mock_context,
            expected_event_type='test_object.update',
            expected_payload={
                'watcher_object.name': 'TestNotificationPayloadEmptySchema',
                'watcher_object.data': {'extra_field': 'test string'},
                'watcher_object.version': '1.0',
                'watcher_object.namespace': 'watcher'})
Ejemplo n.º 5
0
def send_cancel_notification(context,
                             action_plan,
                             action,
                             phase=None,
                             priority=wfields.NotificationPriority.INFO,
                             service='infra-optim',
                             host=None):
    """Emit an action_plan cancel notification."""
    audit_payload, strategy_payload = _get_common_payload(action_plan)

    fault = None
    if phase == wfields.NotificationPhase.ERROR:
        fault = exception_notifications.ExceptionPayload.from_exception()

    versioned_payload = ActionPlanCancelPayload(
        action_plan=action_plan,
        audit=audit_payload,
        strategy=strategy_payload,
        fault=fault,
    )

    notification = ActionPlanCancelNotification(
        priority=priority,
        event_type=notificationbase.EventType(object='action_plan',
                                              action=action,
                                              phase=phase),
        publisher=notificationbase.NotificationPublisher(host=host
                                                         or CONF.host,
                                                         binary=service),
        payload=versioned_payload)

    notification.emit(context)
Ejemplo n.º 6
0
def send_update(context, audit, service='infra-optim',
                host=None, old_state=None):
    """Emit an audit.update notification."""
    goal_payload, strategy_payload = _get_common_payload(audit)

    state_update = AuditStateUpdatePayload(
        old_state=old_state,
        state=audit.state if old_state else None)

    versioned_payload = AuditUpdatePayload(
        audit=audit,
        state_update=state_update,
        goal=goal_payload,
        strategy=strategy_payload,
    )

    notification = AuditUpdateNotification(
        priority=wfields.NotificationPriority.INFO,
        event_type=notificationbase.EventType(
            object='audit',
            action=wfields.NotificationAction.UPDATE),
        publisher=notificationbase.NotificationPublisher(
            host=host or CONF.host,
            binary=service),
        payload=versioned_payload)

    notification.emit(context)
Ejemplo n.º 7
0
    def test_not_possible_to_emit_if_not_populated(self, mock_notifier):
        non_populated_payload = self.TestNotificationPayload(
            extra_field='test string')
        noti = self.TestNotification(
            event_type=notificationbase.EventType(
                object='test_object',
                action=wfields.NotificationAction.UPDATE),
            publisher=notificationbase.NotificationPublisher(
                host='fake-host', binary='watcher-fake'),
            priority=wfields.NotificationPriority.INFO,
            payload=non_populated_payload)

        mock_context = mock.Mock()
        self.assertRaises(exception.NotificationPayloadError,
                          noti.emit, mock_context)
        self.assertFalse(mock_notifier.called)
Ejemplo n.º 8
0
    def test_no_emit_level_too_low(self, mock_notifier):
        # Make sure notification doesn't emit when set notification
        # level < config level
        self.config(notification_level='warning')
        notif = self.TestNotification(
            event_type=notificationbase.EventType(
                object='test_object',
                action=wfields.NotificationAction.UPDATE,
                phase=wfields.NotificationPhase.START),
            publisher=notificationbase.NotificationPublisher(
                host='fake-host', binary='watcher-fake'),
            priority=wfields.NotificationPriority.INFO,
            payload=self.payload)

        mock_context = mock.Mock()
        notif.emit(mock_context)

        self.assertFalse(mock_notifier.called)
Ejemplo n.º 9
0
    def test_no_emit_notifs_disabled(self, mock_notifier):
        # Make sure notifications aren't emitted when notification_level
        # isn't defined, indicating notifications should be disabled
        self.config(notification_level=None)
        notif = self.TestNotification(
            event_type=notificationbase.EventType(
                object='test_object',
                action=wfields.NotificationAction.UPDATE,
                phase=wfields.NotificationPhase.START),
            publisher=notificationbase.NotificationPublisher(
                host='fake-host', binary='watcher-fake'),
            priority=wfields.NotificationPriority.INFO,
            payload=self.payload)

        mock_context = mock.Mock()
        notif.emit(mock_context)

        self.assertFalse(mock_notifier.called)
Ejemplo n.º 10
0
    def test_emit_event_type_without_phase(self, mock_notifier):
        noti = self.TestNotification(
            event_type=notificationbase.EventType(
                object='test_object',
                action=wfields.NotificationAction.UPDATE),
            publisher=notificationbase.NotificationPublisher(
                host='fake-host', binary='watcher-fake'),
            priority=wfields.NotificationPriority.INFO,
            payload=self.payload)

        mock_context = mock.Mock()
        mock_context.to_dict.return_value = {}
        noti.emit(mock_context)

        self._verify_notification(mock_notifier,
                                  mock_context,
                                  expected_event_type='test_object.update',
                                  expected_payload=self.expected_payload)
Ejemplo n.º 11
0
def send_delete(context, action, service='infra-optim', host=None):
    """Emit an action.delete notification."""
    action_plan_payload = _get_action_plan_payload(action)

    versioned_payload = ActionDeletePayload(
        action=action,
        action_plan=action_plan_payload,
    )

    notification = ActionDeleteNotification(
        priority=wfields.NotificationPriority.INFO,
        event_type=notificationbase.EventType(
            object='action', action=wfields.NotificationAction.DELETE),
        publisher=notificationbase.NotificationPublisher(host=host
                                                         or CONF.host,
                                                         binary=service),
        payload=versioned_payload)

    notification.emit(context)
Ejemplo n.º 12
0
    def setUp(self):
        super(TestNotificationBase, self).setUp()

        self.my_obj = self.TestObject(field_1='test1',
                                      field_2=42,
                                      not_important_field=13)

        self.payload = self.TestNotificationPayload(extra_field='test string')
        self.payload.populate_schema(source_field=self.my_obj)

        self.notification = self.TestNotification(
            event_type=notificationbase.EventType(
                object='test_object',
                action=wfields.NotificationAction.UPDATE,
                phase=wfields.NotificationPhase.START),
            publisher=notificationbase.NotificationPublisher(
                host='fake-host', binary='watcher-fake'),
            priority=wfields.NotificationPriority.INFO,
            payload=self.payload)
Ejemplo n.º 13
0
def send_delete(context, audit, service='infra-optim', host=None):
    goal_payload, strategy_payload = _get_common_payload(audit)

    versioned_payload = AuditDeletePayload(
        audit=audit,
        goal=goal_payload,
        strategy=strategy_payload,
    )

    notification = AuditDeleteNotification(
        priority=wfields.NotificationPriority.INFO,
        event_type=notificationbase.EventType(
            object='audit', action=wfields.NotificationAction.DELETE),
        publisher=notificationbase.NotificationPublisher(host=host
                                                         or CONF.host,
                                                         binary=service),
        payload=versioned_payload)

    notification.emit(context)
Ejemplo n.º 14
0
def send_create(context, action_plan, service='infra-optim', host=None):
    """Emit an action_plan.create notification."""
    audit_payload, strategy_payload = _get_common_payload(action_plan)

    versioned_payload = ActionPlanCreatePayload(
        action_plan=action_plan,
        audit=audit_payload,
        strategy=strategy_payload,
    )

    notification = ActionPlanCreateNotification(
        priority=wfields.NotificationPriority.INFO,
        event_type=notificationbase.EventType(
            object='action_plan', action=wfields.NotificationAction.CREATE),
        publisher=notificationbase.NotificationPublisher(host=host
                                                         or CONF.host,
                                                         binary=service),
        payload=versioned_payload)

    notification.emit(context)