Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[data views] data_views REST API #112916

Merged
merged 30 commits into from
Jan 6, 2022
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
6e2a575
initial stab at changing endpoints to data_view while keeping existin…
mattkime Sep 23, 2021
b42ff5a
partial progress
mattkime Oct 15, 2021
b0c0d7d
partial progress
mattkime Oct 15, 2021
22485c3
Merge branch 'master' into data_views_api
mattkime Oct 15, 2021
4a4338e
partial progress on multiple fields returned
mattkime Oct 17, 2021
a114342
partial progress on multiple fields returned
mattkime Oct 17, 2021
e7df4ac
partial progress on multiple fields returned
mattkime Oct 17, 2021
27416f5
Merge branch 'master' into data_views_api
mattkime Oct 17, 2021
09182f7
Merge branch 'master' into data_views_api
mattkime Oct 21, 2021
50db8fb
Merge branch 'main' into data_views_api
mattkime Nov 1, 2021
eb49058
Merge branch 'main' into data_views_api
kibanamachine Nov 20, 2021
999c623
Merge branch 'data_views_api' of github.com:mattkime/kibana into data…
mattkime Dec 5, 2021
6933d46
Merge branch 'main' into data_views_api
mattkime Dec 5, 2021
e75718c
correct response - index_pattern => data_view
mattkime Dec 5, 2021
98332f6
fix legacy key
mattkime Dec 5, 2021
e0a5ac7
Merge branch 'main' into data_views_api
mattkime Dec 13, 2021
ef52e1d
update runtime field responses to return array of fields instead of s…
mattkime Dec 15, 2021
7f17b21
Merge branch 'main' into data_views_api
mattkime Dec 17, 2021
beb9add
complete tests for data views api
mattkime Dec 18, 2021
dc8154c
Merge branch 'main' into data_views_api
mattkime Dec 18, 2021
84caf52
fix export
mattkime Dec 18, 2021
19260da
more tests
mattkime Dec 18, 2021
13e355d
Merge branch 'main' into data_views_api
mattkime Dec 21, 2021
8a9d5a2
Merge branch 'main' into data_views_api
mattkime Dec 21, 2021
aed8175
Merge branch 'main' into data_views_api
mattkime Dec 22, 2021
bb0353e
fix types for data view create
mattkime Dec 22, 2021
4b9110f
Merge branch 'main' into data_views_api
mattkime Dec 22, 2021
c3133c2
Merge branch 'main' into data_views_api
kibanamachine Dec 29, 2021
fab6638
Merge branch 'main' into data_views_api
kibanamachine Jan 6, 2022
ad69069
Merge branch 'main' into data_views_api
kibanamachine Jan 6, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/plugins/data_views/server/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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.
*/

export const SERVICE_PATH = '/api/data_views';
export const SERVICE_PATH_LEGACY = '/api/index_patterns';
export const DATA_VIEW_PATH = `${SERVICE_PATH}/data_view`;
export const DATA_VIEW_PATH_LEGACY = `${SERVICE_PATH_LEGACY}/index_pattern`;
export const SPECIFIC_DATA_VIEW_PATH = `${DATA_VIEW_PATH}/{id}`;
export const SPECIFIC_DATA_VIEW_PATH_LEGACY = `${DATA_VIEW_PATH_LEGACY}/{id}`;
export const RUNTIME_FIELD_PATH = `${SPECIFIC_DATA_VIEW_PATH}/runtime_field`;
export const RUNTIME_FIELD_PATH_LEGACY = `${SPECIFIC_DATA_VIEW_PATH_LEGACY}/runtime_field`;
export const SPECIFIC_RUNTIME_FIELD_PATH = `${RUNTIME_FIELD_PATH}/{name}`;
export const SPECIFIC_RUNTIME_FIELD_PATH_LEGACY = `${RUNTIME_FIELD_PATH_LEGACY}/{name}`;

export const SCRIPTED_FIELD_PATH = `${SPECIFIC_DATA_VIEW_PATH}/scripted_field`;
export const SCRIPTED_FIELD_PATH_LEGACY = `${SPECIFIC_DATA_VIEW_PATH_LEGACY}/scripted_field`;
export const SPECIFIC_SCRIPTED_FIELD_PATH = `${SCRIPTED_FIELD_PATH}/{name}`;
export const SPECIFIC_SCRIPTED_FIELD_PATH_LEGACY = `${SCRIPTED_FIELD_PATH_LEGACY}/{name}`;

export const SERVICE_KEY = 'data_view';
export const SERVICE_KEY_LEGACY = 'index_pattern';
export type SERVICE_KEY_TYPE = typeof SERVICE_KEY | typeof SERVICE_KEY_LEGACY;
21 changes: 21 additions & 0 deletions src/plugins/data_views/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,24 @@ export type {
DataViewsServerPluginStart as PluginStart,
};
export { DataViewsServerPlugin as Plugin };

export {
SERVICE_PATH,
SERVICE_PATH_LEGACY,
DATA_VIEW_PATH,
DATA_VIEW_PATH_LEGACY,
SPECIFIC_DATA_VIEW_PATH,
SPECIFIC_DATA_VIEW_PATH_LEGACY,
RUNTIME_FIELD_PATH,
RUNTIME_FIELD_PATH_LEGACY,
SPECIFIC_RUNTIME_FIELD_PATH,
SPECIFIC_RUNTIME_FIELD_PATH_LEGACY,
SCRIPTED_FIELD_PATH,
SCRIPTED_FIELD_PATH_LEGACY,
SPECIFIC_SCRIPTED_FIELD_PATH,
SPECIFIC_SCRIPTED_FIELD_PATH_LEGACY,
SERVICE_KEY,
SERVICE_KEY_LEGACY,
} from './constants';

export type { SERVICE_KEY_TYPE } from './constants';
105 changes: 81 additions & 24 deletions src/plugins/data_views/server/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,60 @@
import { schema } from '@kbn/config-schema';
import { HttpServiceSetup, StartServicesAccessor } from 'kibana/server';
import { IndexPatternsFetcher } from './fetcher';
import { registerCreateIndexPatternRoute } from './routes/create_index_pattern';
import { registerGetIndexPatternRoute } from './routes/get_index_pattern';
import { registerDeleteIndexPatternRoute } from './routes/delete_index_pattern';
import { registerUpdateIndexPatternRoute } from './routes/update_index_pattern';
import { registerUpdateFieldsRoute } from './routes/fields/update_fields';
import {
registerCreateDataViewRoute,
registerCreateDataViewRouteLegacy,
} from './routes/create_index_pattern';
import {
registerGetDataViewRoute,
registerGetDataViewRouteLegacy,
} from './routes/get_index_pattern';
import {
registerDeleteDataViewRoute,
registerDeleteDataViewRouteLegacy,
} from './routes/delete_index_pattern';
import {
registerUpdateDataViewRoute,
registerUpdateDataViewRouteLegacy,
} from './routes/update_index_pattern';
import {
registerUpdateFieldsRoute,
registerUpdateFieldsRouteLegacy,
} from './routes/fields/update_fields';
import { registerCreateScriptedFieldRoute } from './routes/scripted_fields/create_scripted_field';
import { registerPutScriptedFieldRoute } from './routes/scripted_fields/put_scripted_field';
import { registerGetScriptedFieldRoute } from './routes/scripted_fields/get_scripted_field';
import { registerDeleteScriptedFieldRoute } from './routes/scripted_fields/delete_scripted_field';
import { registerUpdateScriptedFieldRoute } from './routes/scripted_fields/update_scripted_field';
import {
registerManageDefaultDataViewRoute,
registerManageDefaultDataViewRouteLegacy,
} from './routes/default_index_pattern';
import type { DataViewsServerPluginStart, DataViewsServerPluginStartDependencies } from './types';
import { registerManageDefaultIndexPatternRoutes } from './routes/default_index_pattern';
import { registerCreateRuntimeFieldRoute } from './routes/runtime_fields/create_runtime_field';
import { registerGetRuntimeFieldRoute } from './routes/runtime_fields/get_runtime_field';
import { registerDeleteRuntimeFieldRoute } from './routes/runtime_fields/delete_runtime_field';
import { registerPutRuntimeFieldRoute } from './routes/runtime_fields/put_runtime_field';
import { registerUpdateRuntimeFieldRoute } from './routes/runtime_fields/update_runtime_field';
import { registerHasUserIndexPatternRoute } from './routes/has_user_index_pattern';
import {
registerCreateRuntimeFieldRoute,
registerCreateRuntimeFieldRouteLegacy,
} from './routes/runtime_fields/create_runtime_field';
import {
registerGetRuntimeFieldRoute,
registerGetRuntimeFieldRouteLegacy,
} from './routes/runtime_fields/get_runtime_field';
import {
registerDeleteRuntimeFieldRoute,
registerDeleteRuntimeFieldRouteLegacy,
} from './routes/runtime_fields/delete_runtime_field';
import {
registerPutRuntimeFieldRoute,
registerPutRuntimeFieldRouteLegacy,
} from './routes/runtime_fields/put_runtime_field';
import {
registerUpdateRuntimeFieldRoute,
registerUpdateRuntimeFieldRouteLegacy,
} from './routes/runtime_fields/update_runtime_field';
import {
registerHasUserDataViewRoute,
registerHasUserDataViewRouteLegacy,
} from './routes/has_user_index_pattern';

export function registerRoutes(
http: HttpServiceSetup,
Expand All @@ -47,17 +83,37 @@ export function registerRoutes(

const router = http.createRouter();

// Index Patterns API
registerCreateIndexPatternRoute(router, getStartServices);
registerGetIndexPatternRoute(router, getStartServices);
registerDeleteIndexPatternRoute(router, getStartServices);
registerUpdateIndexPatternRoute(router, getStartServices);
registerManageDefaultIndexPatternRoutes(router, getStartServices);
registerHasUserIndexPatternRoute(router, getStartServices);
// Data Views API
registerCreateDataViewRoute(router, getStartServices);
registerGetDataViewRoute(router, getStartServices);
registerDeleteDataViewRoute(router, getStartServices);
registerUpdateDataViewRoute(router, getStartServices);
registerManageDefaultDataViewRoute(router, getStartServices);
registerHasUserDataViewRoute(router, getStartServices);

// Fields API
registerUpdateFieldsRoute(router, getStartServices);

// Runtime Fields API
registerCreateRuntimeFieldRoute(router, getStartServices);
registerGetRuntimeFieldRoute(router, getStartServices);
registerDeleteRuntimeFieldRoute(router, getStartServices);
registerPutRuntimeFieldRoute(router, getStartServices);
registerUpdateRuntimeFieldRoute(router, getStartServices);

// ###
// Legacy Index Pattern API
// ###
registerCreateDataViewRouteLegacy(router, getStartServices);
registerGetDataViewRouteLegacy(router, getStartServices);
registerDeleteDataViewRouteLegacy(router, getStartServices);
registerUpdateDataViewRouteLegacy(router, getStartServices);
registerManageDefaultDataViewRouteLegacy(router, getStartServices);
registerHasUserDataViewRouteLegacy(router, getStartServices);

// Fields API
registerUpdateFieldsRouteLegacy(router, getStartServices);

// Scripted Field API
registerCreateScriptedFieldRoute(router, getStartServices);
registerPutScriptedFieldRoute(router, getStartServices);
Expand All @@ -66,11 +122,12 @@ export function registerRoutes(
registerUpdateScriptedFieldRoute(router, getStartServices);

// Runtime Fields API
registerCreateRuntimeFieldRoute(router, getStartServices);
registerGetRuntimeFieldRoute(router, getStartServices);
registerDeleteRuntimeFieldRoute(router, getStartServices);
registerPutRuntimeFieldRoute(router, getStartServices);
registerUpdateRuntimeFieldRoute(router, getStartServices);
registerCreateRuntimeFieldRouteLegacy(router, getStartServices);
registerGetRuntimeFieldRouteLegacy(router, getStartServices);
registerDeleteRuntimeFieldRouteLegacy(router, getStartServices);
registerPutRuntimeFieldRouteLegacy(router, getStartServices);
registerUpdateRuntimeFieldRouteLegacy(router, getStartServices);
// ###

router.get(
{
Expand Down
112 changes: 65 additions & 47 deletions src/plugins/data_views/server/routes/create_index_pattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,21 @@
*/

import { schema } from '@kbn/config-schema';
import { IndexPatternSpec } from 'src/plugins/data_views/common';
import { DataViewSpec } from 'src/plugins/data_views/common';
import { handleErrors } from './util/handle_errors';
import {
fieldSpecSchema,
runtimeFieldSpecSchema,
serializedFieldFormatSchema,
} from './util/schemas';
import { IRouter, StartServicesAccessor } from '../../../../core/server';
import type { DataViewsServerPluginStart, DataViewsServerPluginStartDependencies } from '../types';
import type { DataViewsServerPluginStartDependencies, DataViewsServerPluginStart } from '../types';
import {
DATA_VIEW_PATH,
DATA_VIEW_PATH_LEGACY,
SERVICE_KEY,
SERVICE_KEY_LEGACY,
} from '../constants';

const indexPatternSpecSchema = schema.object({
title: schema.string(),
Expand Down Expand Up @@ -46,51 +52,63 @@ const indexPatternSpecSchema = schema.object({
runtimeFieldMap: schema.maybe(schema.recordOf(schema.string(), runtimeFieldSpecSchema)),
});

export const registerCreateIndexPatternRoute = (
router: IRouter,
getStartServices: StartServicesAccessor<
DataViewsServerPluginStartDependencies,
DataViewsServerPluginStart
>
) => {
router.post(
{
path: '/api/index_patterns/index_pattern',
validate: {
body: schema.object({
override: schema.maybe(schema.boolean({ defaultValue: false })),
refresh_fields: schema.maybe(schema.boolean({ defaultValue: false })),
index_pattern: indexPatternSpecSchema,
}),
const registerCreateDataViewRouteFactory =
(path: string, serviceKey: string) =>
(
router: IRouter,
getStartServices: StartServicesAccessor<
DataViewsServerPluginStartDependencies,
DataViewsServerPluginStart
>
) => {
router.post(
{
path,
validate: {
body: schema.object({
override: schema.maybe(schema.boolean({ defaultValue: false })),
refresh_fields: schema.maybe(schema.boolean({ defaultValue: false })),
[serviceKey]: indexPatternSpecSchema,
}),
},
},
},
router.handleLegacyErrors(
handleErrors(async (ctx, req, res) => {
const savedObjectsClient = ctx.core.savedObjects.client;
const elasticsearchClient = ctx.core.elasticsearch.client.asCurrentUser;
const [, , { indexPatternsServiceFactory }] = await getStartServices();
const indexPatternsService = await indexPatternsServiceFactory(
savedObjectsClient,
elasticsearchClient,
req
);
const body = req.body;
router.handleLegacyErrors(
handleErrors(async (ctx, req, res) => {
const savedObjectsClient = ctx.core.savedObjects.client;
const elasticsearchClient = ctx.core.elasticsearch.client.asCurrentUser;
const [, , { dataViewsServiceFactory }] = await getStartServices();
const indexPatternsService = await dataViewsServiceFactory(
savedObjectsClient,
elasticsearchClient,
req
);
const body = req.body;

const indexPattern = await indexPatternsService.createAndSave(
body.index_pattern as IndexPatternSpec,
body.override,
!body.refresh_fields
);
const indexPattern = await indexPatternsService.createAndSave(
body[serviceKey] as DataViewSpec,
body.override as boolean,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like type inference from schema got broken because of the dynamic schema approach we took here? :(

!body.refresh_fields
);

return res.ok({
headers: {
'content-type': 'application/json',
},
body: JSON.stringify({
index_pattern: indexPattern.toSpec(),
}),
});
})
)
);
};
return res.ok({
headers: {
'content-type': 'application/json',
},
body: JSON.stringify({
[serviceKey]: indexPattern.toSpec(),
}),
});
})
)
);
};

export const registerCreateDataViewRoute = registerCreateDataViewRouteFactory(
DATA_VIEW_PATH,
SERVICE_KEY
);

export const registerCreateDataViewRouteLegacy = registerCreateDataViewRouteFactory(
DATA_VIEW_PATH_LEGACY,
SERVICE_KEY_LEGACY
);
Loading