diff --git a/src/plugins/data/server/index_patterns/routes.ts b/src/plugins/data/server/index_patterns/routes.ts index 6c7b1bf1c9b0c..d0e6789d96ccf 100644 --- a/src/plugins/data/server/index_patterns/routes.ts +++ b/src/plugins/data/server/index_patterns/routes.ts @@ -50,19 +50,19 @@ export function registerRoutes( // Index Patterns API registerCreateIndexPatternRoute(router, indexPatternsProvider); - registerGetIndexPatternRoute(router); - registerDeleteIndexPatternRoute(router); - registerUpdateIndexPatternRoute(router); + registerGetIndexPatternRoute(router, indexPatternsProvider); + registerDeleteIndexPatternRoute(router, indexPatternsProvider); + registerUpdateIndexPatternRoute(router, indexPatternsProvider); // Fields API - registerUpdateFieldsRoute(router); + registerUpdateFieldsRoute(router, indexPatternsProvider); // Scripted Field API - registerCreateScriptedFieldRoute(router); - registerPutScriptedFieldRoute(router); - registerGetScriptedFieldRoute(router); - registerDeleteScriptedFieldRoute(router); - registerUpdateScriptedFieldRoute(router); + registerCreateScriptedFieldRoute(router, indexPatternsProvider); + registerPutScriptedFieldRoute(router, indexPatternsProvider); + registerGetScriptedFieldRoute(router, indexPatternsProvider); + registerDeleteScriptedFieldRoute(router, indexPatternsProvider); + registerUpdateScriptedFieldRoute(router, indexPatternsProvider); router.get( { diff --git a/src/plugins/data/server/index_patterns/routes/delete_index_pattern.ts b/src/plugins/data/server/index_patterns/routes/delete_index_pattern.ts index 5811189839140..9e25d754f8948 100644 --- a/src/plugins/data/server/index_patterns/routes/delete_index_pattern.ts +++ b/src/plugins/data/server/index_patterns/routes/delete_index_pattern.ts @@ -21,8 +21,12 @@ import { schema } from '@kbn/config-schema'; import { IRouter } from '../../../../../core/server'; import { assertIndexPatternsContext } from './util/assert_index_patterns_context'; import { handleErrors } from './util/handle_errors'; +import type { IndexPatternsServiceProvider } from '../index_patterns_service'; -export const registerDeleteIndexPatternRoute = (router: IRouter) => { +export const registerDeleteIndexPatternRoute = ( + router: IRouter, + indexPatternsProvider: IndexPatternsServiceProvider +) => { router.delete( { path: '/api/index_patterns/index_pattern/{id}', @@ -41,10 +45,15 @@ export const registerDeleteIndexPatternRoute = (router: IRouter) => { router.handleLegacyErrors( handleErrors( assertIndexPatternsContext(async (ctx, req, res) => { - const ip = ctx.indexPatterns.indexPatterns!; + const savedObjectsClient = ctx.core.savedObjects.client; + const elasticsearchClient = ctx.core.elasticsearch.client.asCurrentUser; + const indexPatternsService = await indexPatternsProvider.createIndexPatternsService( + savedObjectsClient, + elasticsearchClient + ); const id = req.params.id; - await ip.delete(id); + await indexPatternsService.delete(id); return res.ok({ headers: { diff --git a/src/plugins/data/server/index_patterns/routes/fields/update_fields.ts b/src/plugins/data/server/index_patterns/routes/fields/update_fields.ts index 5aed2cbfc09b3..9b0d1ac95c78f 100644 --- a/src/plugins/data/server/index_patterns/routes/fields/update_fields.ts +++ b/src/plugins/data/server/index_patterns/routes/fields/update_fields.ts @@ -22,8 +22,12 @@ import { IRouter } from '../../../../../../core/server'; import { assertIndexPatternsContext } from '../util/assert_index_patterns_context'; import { handleErrors } from '../util/handle_errors'; import { serializedFieldFormatSchema } from '../util/schemas'; +import type { IndexPatternsServiceProvider } from '../../index_patterns_service'; -export const registerUpdateFieldsRoute = (router: IRouter) => { +export const registerUpdateFieldsRoute = ( + router: IRouter, + indexPatternsProvider: IndexPatternsServiceProvider +) => { router.post( { path: '/api/index_patterns/index_pattern/{id}/fields', @@ -63,7 +67,12 @@ export const registerUpdateFieldsRoute = (router: IRouter) => { router.handleLegacyErrors( handleErrors( assertIndexPatternsContext(async (ctx, req, res) => { - const ip = ctx.indexPatterns.indexPatterns!; + const savedObjectsClient = ctx.core.savedObjects.client; + const elasticsearchClient = ctx.core.elasticsearch.client.asCurrentUser; + const indexPatternsService = await indexPatternsProvider.createIndexPatternsService( + savedObjectsClient, + elasticsearchClient + ); const id = req.params.id; const { // eslint-disable-next-line @typescript-eslint/naming-convention @@ -76,7 +85,7 @@ export const registerUpdateFieldsRoute = (router: IRouter) => { throw new Error('No fields provided.'); } - const indexPattern = await ip.get(id); + const indexPattern = await indexPatternsService.get(id); let changeCount = 0; for (const fieldName of fieldNames) { @@ -106,10 +115,10 @@ export const registerUpdateFieldsRoute = (router: IRouter) => { throw new Error('Change set is empty.'); } - await ip.updateSavedObject(indexPattern); + await indexPatternsService.updateSavedObject(indexPattern); if (refresh_fields) { - await ip.refreshFields(indexPattern); + await indexPatternsService.refreshFields(indexPattern); } return res.ok({ diff --git a/src/plugins/data/server/index_patterns/routes/get_index_pattern.ts b/src/plugins/data/server/index_patterns/routes/get_index_pattern.ts index 3157813d5a902..323dd0512bb59 100644 --- a/src/plugins/data/server/index_patterns/routes/get_index_pattern.ts +++ b/src/plugins/data/server/index_patterns/routes/get_index_pattern.ts @@ -21,8 +21,12 @@ import { schema } from '@kbn/config-schema'; import { IRouter } from '../../../../../core/server'; import { assertIndexPatternsContext } from './util/assert_index_patterns_context'; import { handleErrors } from './util/handle_errors'; +import type { IndexPatternsServiceProvider } from '../index_patterns_service'; -export const registerGetIndexPatternRoute = (router: IRouter) => { +export const registerGetIndexPatternRoute = ( + router: IRouter, + indexPatternsProvider: IndexPatternsServiceProvider +) => { router.get( { path: '/api/index_patterns/index_pattern/{id}', @@ -41,9 +45,14 @@ export const registerGetIndexPatternRoute = (router: IRouter) => { router.handleLegacyErrors( handleErrors( assertIndexPatternsContext(async (ctx, req, res) => { - const ip = ctx.indexPatterns.indexPatterns!; + const savedObjectsClient = ctx.core.savedObjects.client; + const elasticsearchClient = ctx.core.elasticsearch.client.asCurrentUser; + const indexPatternsService = await indexPatternsProvider.createIndexPatternsService( + savedObjectsClient, + elasticsearchClient + ); const id = req.params.id; - const indexPattern = await ip.get(id); + const indexPattern = await indexPatternsService.get(id); return res.ok({ headers: { diff --git a/src/plugins/data/server/index_patterns/routes/scripted_fields/create_scripted_field.ts b/src/plugins/data/server/index_patterns/routes/scripted_fields/create_scripted_field.ts index 6f84f2d729c13..49020a8ae1e98 100644 --- a/src/plugins/data/server/index_patterns/routes/scripted_fields/create_scripted_field.ts +++ b/src/plugins/data/server/index_patterns/routes/scripted_fields/create_scripted_field.ts @@ -22,8 +22,12 @@ import { IRouter } from '../../../../../../core/server'; import { assertIndexPatternsContext } from '../util/assert_index_patterns_context'; import { handleErrors } from '../util/handle_errors'; import { fieldSpecSchema } from '../util/schemas'; +import type { IndexPatternsServiceProvider } from '../../index_patterns_service'; -export const registerCreateScriptedFieldRoute = (router: IRouter) => { +export const registerCreateScriptedFieldRoute = ( + router: IRouter, + indexPatternsProvider: IndexPatternsServiceProvider +) => { router.post( { path: '/api/index_patterns/index_pattern/{id}/scripted_field', @@ -46,7 +50,12 @@ export const registerCreateScriptedFieldRoute = (router: IRouter) => { router.handleLegacyErrors( handleErrors( assertIndexPatternsContext(async (ctx, req, res) => { - const ip = ctx.indexPatterns.indexPatterns!; + const savedObjectsClient = ctx.core.savedObjects.client; + const elasticsearchClient = ctx.core.elasticsearch.client.asCurrentUser; + const indexPatternsService = await indexPatternsProvider.createIndexPatternsService( + savedObjectsClient, + elasticsearchClient + ); const id = req.params.id; const { // eslint-disable-next-line @typescript-eslint/naming-convention @@ -58,7 +67,7 @@ export const registerCreateScriptedFieldRoute = (router: IRouter) => { throw new Error('Only scripted fields can be created.'); } - const indexPattern = await ip.get(id); + const indexPattern = await indexPatternsService.get(id); if (indexPattern.fields.getByName(field.name)) { throw new Error(`Field [name = ${field.name}] already exists.`); @@ -70,9 +79,9 @@ export const registerCreateScriptedFieldRoute = (router: IRouter) => { searchable: true, }); - await ip.updateSavedObject(indexPattern); + await indexPatternsService.updateSavedObject(indexPattern); if (refresh_fields) { - await ip.refreshFields(indexPattern); + await indexPatternsService.refreshFields(indexPattern); } const fieldObject = indexPattern.fields.getByName(field.name); diff --git a/src/plugins/data/server/index_patterns/routes/scripted_fields/delete_scripted_field.ts b/src/plugins/data/server/index_patterns/routes/scripted_fields/delete_scripted_field.ts index f9138742c5ff6..a5951bf69b5da 100644 --- a/src/plugins/data/server/index_patterns/routes/scripted_fields/delete_scripted_field.ts +++ b/src/plugins/data/server/index_patterns/routes/scripted_fields/delete_scripted_field.ts @@ -22,8 +22,12 @@ import { IRouter } from '../../../../../../core/server'; import { ErrorIndexPatternFieldNotFound } from '../../error'; import { assertIndexPatternsContext } from '../util/assert_index_patterns_context'; import { handleErrors } from '../util/handle_errors'; +import type { IndexPatternsServiceProvider } from '../../index_patterns_service'; -export const registerDeleteScriptedFieldRoute = (router: IRouter) => { +export const registerDeleteScriptedFieldRoute = ( + router: IRouter, + indexPatternsProvider: IndexPatternsServiceProvider +) => { router.delete( { path: '/api/index_patterns/index_pattern/{id}/scripted_field/{name}', @@ -46,11 +50,16 @@ export const registerDeleteScriptedFieldRoute = (router: IRouter) => { router.handleLegacyErrors( handleErrors( assertIndexPatternsContext(async (ctx, req, res) => { - const ip = ctx.indexPatterns.indexPatterns!; + const savedObjectsClient = ctx.core.savedObjects.client; + const elasticsearchClient = ctx.core.elasticsearch.client.asCurrentUser; + const indexPatternsService = await indexPatternsProvider.createIndexPatternsService( + savedObjectsClient, + elasticsearchClient + ); const id = req.params.id; const name = req.params.name; - const indexPattern = await ip.get(id); + const indexPattern = await indexPatternsService.get(id); const field = indexPattern.fields.getByName(name); if (!field) { @@ -63,7 +72,7 @@ export const registerDeleteScriptedFieldRoute = (router: IRouter) => { indexPattern.fields.remove(field); - await ip.updateSavedObject(indexPattern); + await indexPatternsService.updateSavedObject(indexPattern); return res.ok({ headers: { diff --git a/src/plugins/data/server/index_patterns/routes/scripted_fields/get_scripted_field.ts b/src/plugins/data/server/index_patterns/routes/scripted_fields/get_scripted_field.ts index 0d8612b1a68a7..194ae78294270 100644 --- a/src/plugins/data/server/index_patterns/routes/scripted_fields/get_scripted_field.ts +++ b/src/plugins/data/server/index_patterns/routes/scripted_fields/get_scripted_field.ts @@ -22,8 +22,12 @@ import { IRouter } from '../../../../../../core/server'; import { ErrorIndexPatternFieldNotFound } from '../../error'; import { assertIndexPatternsContext } from '../util/assert_index_patterns_context'; import { handleErrors } from '../util/handle_errors'; +import type { IndexPatternsServiceProvider } from '../../index_patterns_service'; -export const registerGetScriptedFieldRoute = (router: IRouter) => { +export const registerGetScriptedFieldRoute = ( + router: IRouter, + indexPatternsProvider: IndexPatternsServiceProvider +) => { router.get( { path: '/api/index_patterns/index_pattern/{id}/scripted_field/{name}', @@ -46,11 +50,16 @@ export const registerGetScriptedFieldRoute = (router: IRouter) => { router.handleLegacyErrors( handleErrors( assertIndexPatternsContext(async (ctx, req, res) => { - const ip = ctx.indexPatterns.indexPatterns!; + const savedObjectsClient = ctx.core.savedObjects.client; + const elasticsearchClient = ctx.core.elasticsearch.client.asCurrentUser; + const indexPatternsService = await indexPatternsProvider.createIndexPatternsService( + savedObjectsClient, + elasticsearchClient + ); const id = req.params.id; const name = req.params.name; - const indexPattern = await ip.get(id); + const indexPattern = await indexPatternsService.get(id); const field = indexPattern.fields.getByName(name); if (!field) { diff --git a/src/plugins/data/server/index_patterns/routes/scripted_fields/put_scripted_field.ts b/src/plugins/data/server/index_patterns/routes/scripted_fields/put_scripted_field.ts index 92c37a0fd36ae..866ed3747afcc 100644 --- a/src/plugins/data/server/index_patterns/routes/scripted_fields/put_scripted_field.ts +++ b/src/plugins/data/server/index_patterns/routes/scripted_fields/put_scripted_field.ts @@ -22,8 +22,12 @@ import { IRouter } from '../../../../../../core/server'; import { assertIndexPatternsContext } from '../util/assert_index_patterns_context'; import { handleErrors } from '../util/handle_errors'; import { fieldSpecSchema } from '../util/schemas'; +import type { IndexPatternsServiceProvider } from '../../index_patterns_service'; -export const registerPutScriptedFieldRoute = (router: IRouter) => { +export const registerPutScriptedFieldRoute = ( + router: IRouter, + indexPatternsProvider: IndexPatternsServiceProvider +) => { router.put( { path: '/api/index_patterns/index_pattern/{id}/scripted_field', @@ -46,7 +50,12 @@ export const registerPutScriptedFieldRoute = (router: IRouter) => { router.handleLegacyErrors( handleErrors( assertIndexPatternsContext(async (ctx, req, res) => { - const ip = ctx.indexPatterns.indexPatterns!; + const savedObjectsClient = ctx.core.savedObjects.client; + const elasticsearchClient = ctx.core.elasticsearch.client.asCurrentUser; + const indexPatternsService = await indexPatternsProvider.createIndexPatternsService( + savedObjectsClient, + elasticsearchClient + ); const id = req.params.id; const { // eslint-disable-next-line @typescript-eslint/naming-convention @@ -58,7 +67,7 @@ export const registerPutScriptedFieldRoute = (router: IRouter) => { throw new Error('Only scripted fields can be put.'); } - const indexPattern = await ip.get(id); + const indexPattern = await indexPatternsService.get(id); const oldFieldObject = indexPattern.fields.getByName(field.name); if (!!oldFieldObject) { @@ -71,9 +80,9 @@ export const registerPutScriptedFieldRoute = (router: IRouter) => { searchable: true, }); - await ip.updateSavedObject(indexPattern); + await indexPatternsService.updateSavedObject(indexPattern); if (refresh_fields) { - await ip.refreshFields(indexPattern); + await indexPatternsService.refreshFields(indexPattern); } const fieldObject = indexPattern.fields.getByName(field.name); diff --git a/src/plugins/data/server/index_patterns/routes/scripted_fields/update_scripted_field.ts b/src/plugins/data/server/index_patterns/routes/scripted_fields/update_scripted_field.ts index b7e3b666ace81..1c258758dbda8 100644 --- a/src/plugins/data/server/index_patterns/routes/scripted_fields/update_scripted_field.ts +++ b/src/plugins/data/server/index_patterns/routes/scripted_fields/update_scripted_field.ts @@ -21,11 +21,14 @@ import { schema } from '@kbn/config-schema'; import { FieldSpec } from 'src/plugins/data/common'; import { IRouter } from '../../../../../../core/server'; import { ErrorIndexPatternFieldNotFound } from '../../error'; -import { assertIndexPatternsContext } from '../util/assert_index_patterns_context'; import { handleErrors } from '../util/handle_errors'; import { fieldSpecSchemaFields } from '../util/schemas'; +import type { IndexPatternsServiceProvider } from '../../index_patterns_service'; -export const registerUpdateScriptedFieldRoute = (router: IRouter) => { +export const registerUpdateScriptedFieldRoute = ( + router: IRouter, + indexPatternsProvider: IndexPatternsServiceProvider +) => { router.post( { path: '/api/index_patterns/index_pattern/{id}/scripted_field/{name}', @@ -65,56 +68,59 @@ export const registerUpdateScriptedFieldRoute = (router: IRouter) => { }, }, router.handleLegacyErrors( - handleErrors( - assertIndexPatternsContext(async (ctx, req, res) => { - const ip = ctx.indexPatterns.indexPatterns!; - const id = req.params.id; - const name = req.params.name; - const { - // eslint-disable-next-line @typescript-eslint/naming-convention - refresh_fields = true, - } = req.body; + handleErrors(async (ctx, req, res) => { + const savedObjectsClient = ctx.core.savedObjects.client; + const elasticsearchClient = ctx.core.elasticsearch.client.asCurrentUser; + const indexPatternsService = await indexPatternsProvider.createIndexPatternsService( + savedObjectsClient, + elasticsearchClient + ); + const id = req.params.id; + const name = req.params.name; + const { + // eslint-disable-next-line @typescript-eslint/naming-convention + refresh_fields = true, + } = req.body; - const field = ({ ...req.body.field, name } as unknown) as FieldSpec; + const field = ({ ...req.body.field, name } as unknown) as FieldSpec; - const indexPattern = await ip.get(id); - let fieldObject = indexPattern.fields.getByName(field.name); + const indexPattern = await indexPatternsService.get(id); + let fieldObject = indexPattern.fields.getByName(field.name); - if (!fieldObject) { - throw new ErrorIndexPatternFieldNotFound(id, name); - } + if (!fieldObject) { + throw new ErrorIndexPatternFieldNotFound(id, name); + } - if (!fieldObject.scripted) { - throw new Error('Only scripted fields can be updated.'); - } + if (!fieldObject.scripted) { + throw new Error('Only scripted fields can be updated.'); + } - const oldSpec = fieldObject.toSpec(); + const oldSpec = fieldObject.toSpec(); - indexPattern.fields.remove(fieldObject); - indexPattern.fields.add({ - ...oldSpec, - ...field, - }); + indexPattern.fields.remove(fieldObject); + indexPattern.fields.add({ + ...oldSpec, + ...field, + }); - await ip.updateSavedObject(indexPattern); - if (refresh_fields) { - await ip.refreshFields(indexPattern); - } + await indexPatternsService.updateSavedObject(indexPattern); + if (refresh_fields) { + await indexPatternsService.refreshFields(indexPattern); + } - fieldObject = indexPattern.fields.getByName(field.name); - if (!fieldObject) throw new Error(`Could not create a field [name = ${field.name}].`); + fieldObject = indexPattern.fields.getByName(field.name); + if (!fieldObject) throw new Error(`Could not create a field [name = ${field.name}].`); - return res.ok({ - headers: { - 'content-type': 'application/json', - }, - body: JSON.stringify({ - field: fieldObject.toSpec(), - index_pattern: indexPattern.toSpec(), - }), - }); - }) - ) + return res.ok({ + headers: { + 'content-type': 'application/json', + }, + body: JSON.stringify({ + field: fieldObject.toSpec(), + index_pattern: indexPattern.toSpec(), + }), + }); + }) ) ); }; diff --git a/src/plugins/data/server/index_patterns/routes/update_index_pattern.ts b/src/plugins/data/server/index_patterns/routes/update_index_pattern.ts index d6bfbe65889e9..95d3e7b9406ed 100644 --- a/src/plugins/data/server/index_patterns/routes/update_index_pattern.ts +++ b/src/plugins/data/server/index_patterns/routes/update_index_pattern.ts @@ -22,6 +22,7 @@ import { IRouter } from '../../../../../core/server'; import { assertIndexPatternsContext } from './util/assert_index_patterns_context'; import { handleErrors } from './util/handle_errors'; import { fieldSpecSchema, serializedFieldFormatSchema } from './util/schemas'; +import type { IndexPatternsServiceProvider } from '../index_patterns_service'; const indexPatternUpdateSchema = schema.object({ title: schema.maybe(schema.string()), @@ -40,7 +41,10 @@ const indexPatternUpdateSchema = schema.object({ fields: schema.maybe(schema.recordOf(schema.string(), fieldSpecSchema)), }); -export const registerUpdateIndexPatternRoute = (router: IRouter) => { +export const registerUpdateIndexPatternRoute = ( + router: IRouter, + indexPatternsProvider: IndexPatternsServiceProvider +) => { router.post( { path: '/api/index_patterns/index_pattern/{id}', @@ -63,10 +67,15 @@ export const registerUpdateIndexPatternRoute = (router: IRouter) => { router.handleLegacyErrors( handleErrors( assertIndexPatternsContext(async (ctx, req, res) => { - const ip = ctx.indexPatterns.indexPatterns!; + const savedObjectsClient = ctx.core.savedObjects.client; + const elasticsearchClient = ctx.core.elasticsearch.client.asCurrentUser; + const indexPatternsService = await indexPatternsProvider.createIndexPatternsService( + savedObjectsClient, + elasticsearchClient + ); const id = req.params.id; - const indexPattern = await ip.get(id); + const indexPattern = await indexPatternsService.get(id); const { // eslint-disable-next-line @typescript-eslint/naming-convention @@ -137,10 +146,10 @@ export const registerUpdateIndexPatternRoute = (router: IRouter) => { throw new Error('Index pattern change set is empty.'); } - await ip.updateSavedObject(indexPattern); + await indexPatternsService.updateSavedObject(indexPattern); if (doRefreshFields && refresh_fields) { - await ip.refreshFields(indexPattern); + await indexPatternsService.refreshFields(indexPattern); } return res.ok({