[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:
Walter Rafelsberger 2019-02-05 10:14:45 +01:00 committed by GitHub
parent 70d9f45757
commit f2e3aff333
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -4,14 +4,24 @@
* you may not use this file except in compliance with the Elastic License.
*/
import Boom from 'boom';
import _ from 'lodash';
import { i18n } from '@kbn/i18n';
import { callWithRequestFactory } from '../client/call_with_request_factory';
import { isAnnotationsFeatureAvailable } from '../lib/check_annotations';
import { wrapError } from '../client/errors';
import { annotationServiceProvider } from '../models/annotation_service';
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) {
server.route({
method: 'POST',
@ -30,7 +40,12 @@ export function annotationRoutes(server, commonRouteConfig) {
server.route({
method: 'PUT',
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 { indexAnnotation } = annotationServiceProvider(callWithRequest);
const username = _.get(request, 'auth.credentials.username', ANNOTATION_USER_UNKNOWN);
@ -45,7 +60,12 @@ export function annotationRoutes(server, commonRouteConfig) {
server.route({
method: 'DELETE',
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 annotationId = request.params.annotationId;
const { deleteAnnotation } = annotationServiceProvider(callWithRequest);