[ML] Adds an integrity check to creating, updating and deleting annotations. (#29969)
Adds an integrity check to make sure the required index and aliases are present before creating, updating or deleting annotations.
This commit is contained in:
parent
70d9f45757
commit
f2e3aff333
1 changed files with 22 additions and 2 deletions
|
@ -4,14 +4,24 @@
|
||||||
* you may not use this file except in compliance with the Elastic License.
|
* you may not use this file except in compliance with the Elastic License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import Boom from 'boom';
|
||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
|
import { i18n } from '@kbn/i18n';
|
||||||
|
|
||||||
import { callWithRequestFactory } from '../client/call_with_request_factory';
|
import { callWithRequestFactory } from '../client/call_with_request_factory';
|
||||||
|
import { isAnnotationsFeatureAvailable } from '../lib/check_annotations';
|
||||||
import { wrapError } from '../client/errors';
|
import { wrapError } from '../client/errors';
|
||||||
import { annotationServiceProvider } from '../models/annotation_service';
|
import { annotationServiceProvider } from '../models/annotation_service';
|
||||||
|
|
||||||
import { ANNOTATION_USER_UNKNOWN } from '../../common/constants/annotations';
|
import { ANNOTATION_USER_UNKNOWN } from '../../common/constants/annotations';
|
||||||
|
|
||||||
|
function getAnnotationsFeatureUnavailableErrorMessage() {
|
||||||
|
return Boom.badRequest(
|
||||||
|
i18n.translate('xpack.ml.routes.annotations.annotationsFeatureUnavailableErrorMessage', {
|
||||||
|
defaultMessage: 'Index and aliases required for the annotations feature have not been created.',
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
export function annotationRoutes(server, commonRouteConfig) {
|
export function annotationRoutes(server, commonRouteConfig) {
|
||||||
server.route({
|
server.route({
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
|
@ -30,7 +40,12 @@ export function annotationRoutes(server, commonRouteConfig) {
|
||||||
server.route({
|
server.route({
|
||||||
method: 'PUT',
|
method: 'PUT',
|
||||||
path: '/api/ml/annotations/index',
|
path: '/api/ml/annotations/index',
|
||||||
handler(request) {
|
async handler(request) {
|
||||||
|
const annotationsFeatureAvailable = await isAnnotationsFeatureAvailable(server);
|
||||||
|
if (annotationsFeatureAvailable === false) {
|
||||||
|
return getAnnotationsFeatureUnavailableErrorMessage();
|
||||||
|
}
|
||||||
|
|
||||||
const callWithRequest = callWithRequestFactory(server, request);
|
const callWithRequest = callWithRequestFactory(server, request);
|
||||||
const { indexAnnotation } = annotationServiceProvider(callWithRequest);
|
const { indexAnnotation } = annotationServiceProvider(callWithRequest);
|
||||||
const username = _.get(request, 'auth.credentials.username', ANNOTATION_USER_UNKNOWN);
|
const username = _.get(request, 'auth.credentials.username', ANNOTATION_USER_UNKNOWN);
|
||||||
|
@ -45,7 +60,12 @@ export function annotationRoutes(server, commonRouteConfig) {
|
||||||
server.route({
|
server.route({
|
||||||
method: 'DELETE',
|
method: 'DELETE',
|
||||||
path: '/api/ml/annotations/delete/{annotationId}',
|
path: '/api/ml/annotations/delete/{annotationId}',
|
||||||
handler(request) {
|
async handler(request) {
|
||||||
|
const annotationsFeatureAvailable = await isAnnotationsFeatureAvailable(server);
|
||||||
|
if (annotationsFeatureAvailable === false) {
|
||||||
|
return getAnnotationsFeatureUnavailableErrorMessage();
|
||||||
|
}
|
||||||
|
|
||||||
const callWithRequest = callWithRequestFactory(server, request);
|
const callWithRequest = callWithRequestFactory(server, request);
|
||||||
const annotationId = request.params.annotationId;
|
const annotationId = request.params.annotationId;
|
||||||
const { deleteAnnotation } = annotationServiceProvider(callWithRequest);
|
const { deleteAnnotation } = annotationServiceProvider(callWithRequest);
|
||||||
|
|
Loading…
Reference in a new issue