-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
query_default_field.ts
70 lines (66 loc) · 1.99 KB
/
query_default_field.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { schema } from '@kbn/config-schema';
import { addDefaultField } from '../lib/query_default_field';
import { versionCheckHandlerWrapper } from '../lib/es_version_precheck';
import { RouteDependencies } from '../types';
/**
* Adds routes for detecting and fixing 6.x Metricbeat indices that need the
* `index.query.default_field` index setting added.
*
* @param server
*/
export function registerQueryDefaultFieldRoutes({ router }: RouteDependencies) {
router.post(
{
path: '/api/upgrade_assistant/add_query_default_field/{indexName}',
validate: {
params: schema.object({
indexName: schema.string(),
}),
body: schema.object({
fieldTypes: schema.arrayOf(schema.string()),
otherFields: schema.arrayOf(schema.string(), { defaultValue: undefined }),
}),
},
},
versionCheckHandlerWrapper(
async (
{
core: {
elasticsearch: {
legacy: { client },
},
},
},
request,
response
) => {
try {
const { indexName } = request.params;
const { fieldTypes, otherFields } = request.body as {
fieldTypes: string[];
otherFields?: string[];
};
return response.ok({
body: await addDefaultField(
client,
indexName,
new Set(fieldTypes),
otherFields ? new Set(otherFields) : undefined
),
});
} catch (e) {
const status = e.status || e.statusCode;
if (status === 403) {
return response.forbidden({ body: e });
}
return response.internalError({ body: e });
}
}
)
);
}