-
Notifications
You must be signed in to change notification settings - Fork 8.1k
/
register_update_route.ts
53 lines (44 loc) · 1.67 KB
/
register_update_route.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { schema } from '@kbn/config-schema';
import { TemplateDeserialized } from '../../../../common';
import { RouteDependencies } from '../../../types';
import { addBasePath } from '..';
import { templateSchema } from './validate_schemas';
import { saveTemplate, doesTemplateExist } from './lib';
const bodySchema = templateSchema;
const paramsSchema = schema.object({
name: schema.string(),
});
export function registerUpdateRoute({ router, lib: { handleEsError } }: RouteDependencies) {
router.put(
{
path: addBasePath('/index_templates/{name}'),
validate: { body: bodySchema, params: paramsSchema },
},
async (context, request, response) => {
const { client } = (await context.core).elasticsearch;
const { name } = request.params as typeof paramsSchema.type;
const template = request.body as TemplateDeserialized;
try {
const {
_kbnMeta: { isLegacy },
} = template;
// Verify the template exists (ES will throw 404 if not)
const templateExists = await doesTemplateExist({ name, client, isLegacy });
if (!templateExists) {
return response.notFound();
}
// Next, update index template
const responseBody = await saveTemplate({ template, client, isLegacy });
return response.ok({ body: responseBody });
} catch (error) {
return handleEsError({ error, response });
}
}
);
}