def setUp(self, mock_rpc): super(NotificationAPITestCase, self).setUp() self.notification_api = ha_api.NotificationAPI() self.req = fakes.HTTPRequest.blank('/v1/notifications', use_admin_context=True) self.context = self.req.environ['masakari.context'] self.failover_segment = fakes_data.create_fake_failover_segment( name="segment1", id=1, description="something", service_type="COMPUTE", recovery_method="auto", uuid=uuidsentinel.fake_segment) self.host = fakes_data.create_fake_host( name="host_1", id=1, reserved=False, on_maintenance=False, type="fake", control_attributes="fake-control_attributes", uuid=uuidsentinel.fake_host_1) self.notification = fakes_data.create_fake_notification( type="VM", id=1, payload={ 'event': 'STOPPED', 'host_status': 'NORMAL', 'cluster_status': 'ONLINE' }, source_host_uuid=uuidsentinel.fake_host, generated_time=NOW, status="running", notification_uuid=uuidsentinel.fake_notification) self.exception_duplicate = exception.DuplicateNotification( host='host_1', type='COMPUTE_HOST')
def test_create_duplicate_notification(self, mock_create_notification): mock_create_notification.side_effect = exception.DuplicateNotification( type="COMPUTE_HOST") body = { "notification": {"hostname": "fake_host", "payload": {"event": "STOPPED", "host_status": "NORMAL", "cluster_status": "ONLINE"}, "type": "COMPUTE_HOST", "generated_time": str(NOW)}} self.assertRaises(exc.HTTPConflict, self.controller.create, self.req, body=body)
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) notification.create() self.engine_rpcapi.process_notification(context, notification) return notification