def test_create_host_on_maintenance(self, mock_create_notification): mock_create_notification.side_effect = ( exception.HostOnMaintenanceError(host_name="fake_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