forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfields_for.ts
135 lines (124 loc) · 3.84 KB
/
fields_for.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import { estypes } from '@elastic/elasticsearch';
import { schema } from '@kbn/config-schema';
import {
IRouter,
RequestHandler,
RouteValidatorFullConfig,
StartServicesAccessor,
} from '@kbn/core/server';
import { IndexPatternsFetcher } from '../fetcher';
import type { DataViewsServerPluginStart, DataViewsServerPluginStartDependencies } from '../types';
import { DataViewMissingIndices } from '../../common';
const parseMetaFields = (metaFields: string | string[]) => {
let parsedFields: string[] = [];
if (typeof metaFields === 'string') {
parsedFields = JSON.parse(metaFields);
} else {
parsedFields = metaFields;
}
return parsedFields;
};
const path = '/api/index_patterns/_fields_for_wildcard';
type IBody = { index_filter?: estypes.QueryDslQueryContainer } | undefined;
interface IQuery {
pattern: string;
meta_fields: string[];
type?: string;
rollup_index?: string;
allow_no_index?: boolean;
include_unmapped?: boolean;
fields?: string[];
}
const validate: RouteValidatorFullConfig<{}, IQuery, IBody> = {
query: schema.object({
pattern: schema.string(),
meta_fields: schema.oneOf([schema.string(), schema.arrayOf(schema.string())], {
defaultValue: [],
}),
type: schema.maybe(schema.string()),
rollup_index: schema.maybe(schema.string()),
allow_no_index: schema.maybe(schema.boolean()),
include_unmapped: schema.maybe(schema.boolean()),
fields: schema.maybe(schema.arrayOf(schema.string())),
}),
// not available to get request
body: schema.maybe(schema.object({ index_filter: schema.any() })),
};
const handler: RequestHandler<{}, IQuery, IBody> = async (context, request, response) => {
const { asCurrentUser } = (await context.core).elasticsearch.client;
const indexPatterns = new IndexPatternsFetcher(asCurrentUser);
const {
pattern,
meta_fields: metaFields,
type,
rollup_index: rollupIndex,
allow_no_index: allowNoIndex,
include_unmapped: includeUnmapped,
} = request.query;
// not available to get request
const indexFilter = request.body?.index_filter;
let parsedFields: string[] = [];
try {
parsedFields = parseMetaFields(metaFields);
} catch (error) {
return response.badRequest();
}
try {
const { fields, indices } = await indexPatterns.getFieldsForWildcard({
pattern,
metaFields: parsedFields,
type,
rollupIndex,
fieldCapsOptions: {
allow_no_indices: allowNoIndex || false,
includeUnmapped,
},
indexFilter,
fields: request.query.fields,
});
if (indices.length === 0 && allowNoIndex !== true) {
throw new DataViewMissingIndices(pattern);
}
return response.ok({
body: { fields, indices },
headers: {
'content-type': 'application/json',
},
});
} catch (error) {
if (
typeof error === 'object' &&
!!error?.isBoom &&
!!error?.output?.payload &&
typeof error?.output?.payload === 'object'
) {
const payload = error?.output?.payload;
return response.notFound({
body: {
message: payload.message,
attributes: payload,
},
});
} else {
return response.notFound();
}
}
};
export const registerFieldForWildcard = (
router: IRouter,
getStartServices: StartServicesAccessor<
DataViewsServerPluginStartDependencies,
DataViewsServerPluginStart
>
) => {
router.put({ path, validate }, handler);
router.post({ path, validate }, handler);
router.get({ path, validate }, handler);
};