def create(self): if self.obj_attr_is_set('id'): raise exception.ObjectActionError(action='create', reason='already created') updates = self.masakari_obj_get_changes() # NOTE(ShilpaSD): This field doesn't exist in the Notification # db model so don't save it. updates.pop('recovery_workflow_details', None) if 'notification_uuid' not in updates: updates['notification_uuid'] = uuidutils.generate_uuid() LOG.debug('Generated uuid %(uuid)s for notifications', dict(uuid=updates['notification_uuid'])) if 'payload' in updates: updates['payload'] = jsonutils.dumps(updates['payload']) api_utils.notify_about_notification_api( self._context, self, action=fields.EventNotificationAction.NOTIFICATION_CREATE, phase=fields.EventNotificationPhase.START) db_notification = db.notification_create(self._context, updates) api_utils.notify_about_notification_api( self._context, self, action=fields.EventNotificationAction.NOTIFICATION_CREATE, phase=fields.EventNotificationPhase.END) self._from_db_object(self._context, self, db_notification)
def create(self): if self.obj_attr_is_set('id'): raise exception.ObjectActionError(action='create', reason='already created') updates = self.masakari_obj_get_changes() # NOTE(ShilpaSD): This field doesn't exist in the Notification # db model so don't save it. updates.pop('recovery_workflow_details', None) if 'notification_uuid' not in updates: updates['notification_uuid'] = uuidutils.generate_uuid() LOG.debug('Generated uuid %(uuid)s for notifications', dict(uuid=updates['notification_uuid'])) if 'payload' in updates: updates['payload'] = jsonutils.dumps(updates['payload']) api_utils.notify_about_notification_api(self._context, self, action=fields.EventNotificationAction.NOTIFICATION_CREATE, phase=fields.EventNotificationPhase.START) db_notification = db.notification_create(self._context, updates) api_utils.notify_about_notification_api(self._context, self, action=fields.EventNotificationAction.NOTIFICATION_CREATE, phase=fields.EventNotificationPhase.END) self._from_db_object(self._context, self, db_notification)
def create_notification(self, context, notification_data): """Create notification""" # Check whether host from which the notification came is already # present in failover segment or not host_name = notification_data.get('hostname') host_object = objects.Host.get_by_name(context, host_name) host_on_maintenance = host_object.on_maintenance if host_on_maintenance: message = (_("Notification received from host %(host)s of type " "'%(type)s' is ignored as the host is already under " "maintenance.") % { 'host': host_name, 'type': notification_data.get('type') }) raise exception.HostOnMaintenanceError(message=message) notification = objects.Notification(context=context) # Populate notification object for create notification.type = notification_data.get('type') notification.generated_time = notification_data.get('generated_time') notification.source_host_uuid = host_object.uuid notification.payload = notification_data.get('payload') notification.status = fields.NotificationStatus.NEW if self._is_duplicate_notification(context, notification): message = (_("Notification received from host %(host)s of " " type '%(type)s' is duplicate.") % { 'host': host_name, 'type': notification.type }) raise exception.DuplicateNotification(message=message) try: notification.create() self.engine_rpcapi.process_notification(context, notification) except Exception as e: with excutils.save_and_reraise_exception(): tb = traceback.format_exc() api_utils.notify_about_notification_api( context, notification, action=fields.EventNotificationAction.NOTIFICATION_CREATE, phase=fields.EventNotificationPhase.ERROR, exception=e, tb=tb) return notification
def create_notification(self, context, notification_data): """Create notification""" # Check whether host from which the notification came is already # present in failover segment or not host_name = notification_data.get('hostname') host_object = objects.Host.get_by_name(context, host_name) host_on_maintenance = host_object.on_maintenance if host_on_maintenance: message = (_("Notification received from host %(host)s of type " "'%(type)s' is ignored as the host is already under " "maintenance.") % { 'host': host_name, 'type': notification_data.get('type') }) raise exception.HostOnMaintenanceError(message=message) notification = objects.Notification(context=context) # Populate notification object for create notification.type = notification_data.get('type') notification.generated_time = notification_data.get('generated_time') notification.source_host_uuid = host_object.uuid notification.payload = notification_data.get('payload') notification.status = fields.NotificationStatus.NEW if self._is_duplicate_notification(context, notification): message = (_("Notification received from host %(host)s of " " type '%(type)s' is duplicate.") % {'host': host_name, 'type': notification.type}) raise exception.DuplicateNotification(message=message) try: notification.create() self.engine_rpcapi.process_notification(context, notification) except Exception as e: with excutils.save_and_reraise_exception(): tb = traceback.format_exc() api_utils.notify_about_notification_api(context, notification, action=fields.EventNotificationAction.NOTIFICATION_CREATE, phase=fields.EventNotificationPhase.ERROR, exception=e, tb=tb) return notification
def test_notify_about_notification_api( self, mock_from_exception, mock_NotificationApiPayload, mock_NotificationApiNotification, mock_NotificationPublisher, mock_EventType): mock_fault = mock.Mock() mock_from_exception.return_value = mock_fault mock_payload = mock.Mock() mock_NotificationApiPayload.return_value = mock_payload mock_api_notification = mock.Mock() mock_NotificationApiNotification.return_value = mock_api_notification mock_api_notification.emit.return_value = None mock_publisher = mock.Mock() mock_NotificationPublisher.return_value = mock_publisher mock_event_type = mock.Mock() mock_EventType.return_value = mock_event_type mock_context = mock.Mock() notification = notification_obj.Notification() action = fields.EventNotificationAction.NOTIFICATION_CREATE phase = fields.EventNotificationPhase.ERROR e = Exception() api_utils.notify_about_notification_api(mock_context, notification, action=action, phase=phase, exception=e) mock_from_exception.assert_called_once_with(e, None) mock_NotificationApiPayload.assert_called_once_with( notification=notification, fault=mock_fault) mock_NotificationApiNotification.assert_called_once_with( context=mock_context, priority=fields.EventNotificationPriority.ERROR, publisher=mock_publisher, event_type=mock_event_type, payload=mock_payload) mock_NotificationPublisher.assert_called_once_with( context=mock_context, host=socket.gethostname(), binary='masakari-api') mock_api_notification.emit.assert_called_once_with(mock_context)