From b6d49f89354097d1e7d89b3775d18b3e14e04d7b Mon Sep 17 00:00:00 2001 From: streamich Date: Fri, 4 Dec 2020 16:14:25 +0100 Subject: [PATCH] =?UTF-8?q?refactor:=20=F0=9F=92=A1=20remove=20index=20pat?= =?UTF-8?q?tern=20route=20context=20provider?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../routes/create_index_pattern.ts | 47 ++--- .../routes/delete_index_pattern.ts | 33 ++- .../routes/fields/update_fields.ts | 107 +++++----- .../routes/get_index_pattern.ts | 39 ++-- .../scripted_fields/create_scripted_field.ts | 85 ++++---- .../scripted_fields/delete_scripted_field.ts | 53 +++-- .../scripted_fields/get_scripted_field.ts | 55 +++-- .../scripted_fields/put_scripted_field.ts | 87 ++++---- .../routes/update_index_pattern.ts | 191 +++++++++--------- .../util/assert_index_patterns_context.ts | 36 ---- src/plugins/data/server/plugin.ts | 21 -- src/plugins/data/server/types.ts | 52 ----- 12 files changed, 335 insertions(+), 471 deletions(-) delete mode 100644 src/plugins/data/server/index_patterns/routes/util/assert_index_patterns_context.ts delete mode 100644 src/plugins/data/server/types.ts diff --git a/src/plugins/data/server/index_patterns/routes/create_index_pattern.ts b/src/plugins/data/server/index_patterns/routes/create_index_pattern.ts index c0a468384a3f0..147fcd14f1472 100644 --- a/src/plugins/data/server/index_patterns/routes/create_index_pattern.ts +++ b/src/plugins/data/server/index_patterns/routes/create_index_pattern.ts @@ -20,7 +20,6 @@ import { schema } from '@kbn/config-schema'; import { IndexPatternSpec } from 'src/plugins/data/common'; 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'; @@ -69,31 +68,29 @@ export const registerCreateIndexPatternRoute = ( }, }, router.handleLegacyErrors( - handleErrors( - assertIndexPatternsContext(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 body = req.body; - const indexPattern = await indexPatternsService.createAndSave( - body.index_pattern as IndexPatternSpec, - body.override, - !body.refresh_fields - ); + 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 body = req.body; + const indexPattern = await indexPatternsService.createAndSave( + body.index_pattern as IndexPatternSpec, + body.override, + !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({ + index_pattern: indexPattern.toSpec(), + }), + }); + }) ) ); }; 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 9e25d754f8948..a9a3d10919d2d 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 @@ -19,7 +19,6 @@ 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'; @@ -43,25 +42,23 @@ export const registerDeleteIndexPatternRoute = ( }, }, router.handleLegacyErrors( - handleErrors( - assertIndexPatternsContext(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; + 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; - await indexPatternsService.delete(id); + await indexPatternsService.delete(id); - return res.ok({ - headers: { - 'content-type': 'application/json', - }, - }); - }) - ) + return res.ok({ + headers: { + 'content-type': 'application/json', + }, + }); + }) ) ); }; 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 9b0d1ac95c78f..491254356c019 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 @@ -19,7 +19,6 @@ 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 { serializedFieldFormatSchema } from '../util/schemas'; import type { IndexPatternsServiceProvider } from '../../index_patterns_service'; @@ -65,72 +64,70 @@ export const registerUpdateFieldsRoute = ( }, }, router.handleLegacyErrors( - handleErrors( - assertIndexPatternsContext(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 { - // eslint-disable-next-line @typescript-eslint/naming-convention - refresh_fields = true, - fields, - } = req.body; - const fieldNames = Object.keys(fields); + 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 { + // eslint-disable-next-line @typescript-eslint/naming-convention + refresh_fields = true, + fields, + } = req.body; + const fieldNames = Object.keys(fields); - if (fieldNames.length < 1) { - throw new Error('No fields provided.'); - } + if (fieldNames.length < 1) { + throw new Error('No fields provided.'); + } - const indexPattern = await indexPatternsService.get(id); + const indexPattern = await indexPatternsService.get(id); - let changeCount = 0; - for (const fieldName of fieldNames) { - const field = fields[fieldName]; + let changeCount = 0; + for (const fieldName of fieldNames) { + const field = fields[fieldName]; - if (field.customLabel !== undefined) { - changeCount++; - indexPattern.setFieldCustomLabel(fieldName, field.customLabel); - } + if (field.customLabel !== undefined) { + changeCount++; + indexPattern.setFieldCustomLabel(fieldName, field.customLabel); + } - if (field.count !== undefined) { - changeCount++; - indexPattern.setFieldCount(fieldName, field.count); - } + if (field.count !== undefined) { + changeCount++; + indexPattern.setFieldCount(fieldName, field.count); + } - if (field.format !== undefined) { - changeCount++; - if (field.format) { - indexPattern.setFieldFormat(fieldName, field.format); - } else { - indexPattern.deleteFieldFormat(fieldName); - } + if (field.format !== undefined) { + changeCount++; + if (field.format) { + indexPattern.setFieldFormat(fieldName, field.format); + } else { + indexPattern.deleteFieldFormat(fieldName); } } + } - if (changeCount < 1) { - throw new Error('Change set is empty.'); - } + if (changeCount < 1) { + throw new Error('Change set is empty.'); + } - await indexPatternsService.updateSavedObject(indexPattern); + await indexPatternsService.updateSavedObject(indexPattern); - if (refresh_fields) { - await indexPatternsService.refreshFields(indexPattern); - } + if (refresh_fields) { + await indexPatternsService.refreshFields(indexPattern); + } - 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({ + index_pattern: indexPattern.toSpec(), + }), + }); + }) ) ); }; 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 323dd0512bb59..19c95b378257a 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 @@ -19,7 +19,6 @@ 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'; @@ -43,27 +42,25 @@ export const registerGetIndexPatternRoute = ( }, }, router.handleLegacyErrors( - handleErrors( - assertIndexPatternsContext(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 indexPattern = await indexPatternsService.get(id); + 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 indexPattern = await indexPatternsService.get(id); - 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({ + index_pattern: indexPattern.toSpec(), + }), + }); + }) ) ); }; 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 49020a8ae1e98..07ac3c110cf2e 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 @@ -19,7 +19,6 @@ 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 { fieldSpecSchema } from '../util/schemas'; import type { IndexPatternsServiceProvider } from '../../index_patterns_service'; @@ -48,56 +47,54 @@ export const registerCreateScriptedFieldRoute = ( }, }, router.handleLegacyErrors( - handleErrors( - assertIndexPatternsContext(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 { - // eslint-disable-next-line @typescript-eslint/naming-convention - refresh_fields = true, - field, - } = 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 { + // eslint-disable-next-line @typescript-eslint/naming-convention + refresh_fields = true, + field, + } = req.body; - if (!field.scripted) { - throw new Error('Only scripted fields can be created.'); - } + if (!field.scripted) { + throw new Error('Only scripted fields can be created.'); + } - const indexPattern = await indexPatternsService.get(id); + const indexPattern = await indexPatternsService.get(id); - if (indexPattern.fields.getByName(field.name)) { - throw new Error(`Field [name = ${field.name}] already exists.`); - } + if (indexPattern.fields.getByName(field.name)) { + throw new Error(`Field [name = ${field.name}] already exists.`); + } - indexPattern.fields.add({ - ...field, - aggregatable: true, - searchable: true, - }); + indexPattern.fields.add({ + ...field, + aggregatable: true, + searchable: true, + }); - await indexPatternsService.updateSavedObject(indexPattern); - if (refresh_fields) { - await indexPatternsService.refreshFields(indexPattern); - } + await indexPatternsService.updateSavedObject(indexPattern); + if (refresh_fields) { + await indexPatternsService.refreshFields(indexPattern); + } - const fieldObject = indexPattern.fields.getByName(field.name); - if (!fieldObject) throw new Error(`Could not create a field [name = ${field.name}].`); + const 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/scripted_fields/delete_scripted_field.ts b/src/plugins/data/server/index_patterns/routes/scripted_fields/delete_scripted_field.ts index a5951bf69b5da..92ddddb4ae93d 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 @@ -20,7 +20,6 @@ import { schema } from '@kbn/config-schema'; 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'; @@ -48,39 +47,37 @@ export const registerDeleteScriptedFieldRoute = ( }, }, router.handleLegacyErrors( - handleErrors( - assertIndexPatternsContext(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; + 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 indexPattern = await indexPatternsService.get(id); - const field = indexPattern.fields.getByName(name); + const indexPattern = await indexPatternsService.get(id); + const field = indexPattern.fields.getByName(name); - if (!field) { - throw new ErrorIndexPatternFieldNotFound(id, name); - } + if (!field) { + throw new ErrorIndexPatternFieldNotFound(id, name); + } - if (!field.scripted) { - throw new Error('Only scripted fields can be deleted.'); - } + if (!field.scripted) { + throw new Error('Only scripted fields can be deleted.'); + } - indexPattern.fields.remove(field); + indexPattern.fields.remove(field); - await indexPatternsService.updateSavedObject(indexPattern); + await indexPatternsService.updateSavedObject(indexPattern); - return res.ok({ - headers: { - 'content-type': 'application/json', - }, - }); - }) - ) + return res.ok({ + headers: { + 'content-type': 'application/json', + }, + }); + }) ) ); }; 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 194ae78294270..551f244a6d1a4 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 @@ -20,7 +20,6 @@ import { schema } from '@kbn/config-schema'; 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'; @@ -48,38 +47,36 @@ export const registerGetScriptedFieldRoute = ( }, }, router.handleLegacyErrors( - handleErrors( - assertIndexPatternsContext(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; + 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 indexPattern = await indexPatternsService.get(id); - const field = indexPattern.fields.getByName(name); + const indexPattern = await indexPatternsService.get(id); + const field = indexPattern.fields.getByName(name); - if (!field) { - throw new ErrorIndexPatternFieldNotFound(id, name); - } + if (!field) { + throw new ErrorIndexPatternFieldNotFound(id, name); + } - if (!field.scripted) { - throw new Error('Only scripted fields can be retrieved.'); - } + if (!field.scripted) { + throw new Error('Only scripted fields can be retrieved.'); + } - return res.ok({ - headers: { - 'content-type': 'application/json', - }, - body: JSON.stringify({ - field: field.toSpec(), - }), - }); - }) - ) + return res.ok({ + headers: { + 'content-type': 'application/json', + }, + body: JSON.stringify({ + field: field.toSpec(), + }), + }); + }) ) ); }; 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 866ed3747afcc..e409897365772 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 @@ -19,7 +19,6 @@ 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 { fieldSpecSchema } from '../util/schemas'; import type { IndexPatternsServiceProvider } from '../../index_patterns_service'; @@ -48,57 +47,55 @@ export const registerPutScriptedFieldRoute = ( }, }, router.handleLegacyErrors( - handleErrors( - assertIndexPatternsContext(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 { - // eslint-disable-next-line @typescript-eslint/naming-convention - refresh_fields = true, - field, - } = 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 { + // eslint-disable-next-line @typescript-eslint/naming-convention + refresh_fields = true, + field, + } = req.body; - if (!field.scripted) { - throw new Error('Only scripted fields can be put.'); - } + if (!field.scripted) { + throw new Error('Only scripted fields can be put.'); + } - const indexPattern = await indexPatternsService.get(id); + const indexPattern = await indexPatternsService.get(id); - const oldFieldObject = indexPattern.fields.getByName(field.name); - if (!!oldFieldObject) { - indexPattern.fields.remove(oldFieldObject); - } + const oldFieldObject = indexPattern.fields.getByName(field.name); + if (!!oldFieldObject) { + indexPattern.fields.remove(oldFieldObject); + } - indexPattern.fields.add({ - ...field, - aggregatable: true, - searchable: true, - }); + indexPattern.fields.add({ + ...field, + aggregatable: true, + searchable: true, + }); - await indexPatternsService.updateSavedObject(indexPattern); - if (refresh_fields) { - await indexPatternsService.refreshFields(indexPattern); - } + await indexPatternsService.updateSavedObject(indexPattern); + if (refresh_fields) { + await indexPatternsService.refreshFields(indexPattern); + } - const fieldObject = indexPattern.fields.getByName(field.name); - if (!fieldObject) throw new Error(`Could not create a field [name = ${field.name}].`); + const 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 95d3e7b9406ed..6d6486ec90314 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 @@ -19,7 +19,6 @@ 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 { fieldSpecSchema, serializedFieldFormatSchema } from './util/schemas'; import type { IndexPatternsServiceProvider } from '../index_patterns_service'; @@ -65,103 +64,101 @@ export const registerUpdateIndexPatternRoute = ( }, }, router.handleLegacyErrors( - handleErrors( - assertIndexPatternsContext(async (ctx, req, res) => { - const savedObjectsClient = ctx.core.savedObjects.client; - const elasticsearchClient = ctx.core.elasticsearch.client.asCurrentUser; - const indexPatternsService = await indexPatternsProvider.createIndexPatternsService( - savedObjectsClient, - elasticsearchClient + 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 indexPattern = await indexPatternsService.get(id); + + const { + // eslint-disable-next-line @typescript-eslint/naming-convention + refresh_fields = true, + index_pattern: { + title, + timeFieldName, + intervalName, + sourceFilters, + fieldFormats, + type, + typeMeta, + fields, + }, + } = req.body; + + let changeCount = 0; + let doRefreshFields = false; + + if (title !== undefined && title !== indexPattern.title) { + changeCount++; + indexPattern.title = title; + } + + if (timeFieldName !== undefined && timeFieldName !== indexPattern.timeFieldName) { + changeCount++; + indexPattern.timeFieldName = timeFieldName; + } + + if (intervalName !== undefined && intervalName !== indexPattern.intervalName) { + changeCount++; + indexPattern.intervalName = intervalName; + } + + if (sourceFilters !== undefined) { + changeCount++; + indexPattern.sourceFilters = sourceFilters; + } + + if (fieldFormats !== undefined) { + changeCount++; + indexPattern.fieldFormatMap = fieldFormats; + } + + if (type !== undefined) { + changeCount++; + indexPattern.type = type; + } + + if (typeMeta !== undefined) { + changeCount++; + indexPattern.typeMeta = typeMeta; + } + + if (fields !== undefined) { + changeCount++; + doRefreshFields = true; + indexPattern.fields.replaceAll( + Object.values(fields || {}).map((field) => ({ + ...field, + aggregatable: true, + searchable: true, + })) ); - const id = req.params.id; - - const indexPattern = await indexPatternsService.get(id); - - const { - // eslint-disable-next-line @typescript-eslint/naming-convention - refresh_fields = true, - index_pattern: { - title, - timeFieldName, - intervalName, - sourceFilters, - fieldFormats, - type, - typeMeta, - fields, - }, - } = req.body; - - let changeCount = 0; - let doRefreshFields = false; - - if (title !== undefined && title !== indexPattern.title) { - changeCount++; - indexPattern.title = title; - } - - if (timeFieldName !== undefined && timeFieldName !== indexPattern.timeFieldName) { - changeCount++; - indexPattern.timeFieldName = timeFieldName; - } - - if (intervalName !== undefined && intervalName !== indexPattern.intervalName) { - changeCount++; - indexPattern.intervalName = intervalName; - } - - if (sourceFilters !== undefined) { - changeCount++; - indexPattern.sourceFilters = sourceFilters; - } - - if (fieldFormats !== undefined) { - changeCount++; - indexPattern.fieldFormatMap = fieldFormats; - } - - if (type !== undefined) { - changeCount++; - indexPattern.type = type; - } - - if (typeMeta !== undefined) { - changeCount++; - indexPattern.typeMeta = typeMeta; - } - - if (fields !== undefined) { - changeCount++; - doRefreshFields = true; - indexPattern.fields.replaceAll( - Object.values(fields || {}).map((field) => ({ - ...field, - aggregatable: true, - searchable: true, - })) - ); - } - - if (changeCount < 1) { - throw new Error('Index pattern change set is empty.'); - } - - await indexPatternsService.updateSavedObject(indexPattern); - - if (doRefreshFields && refresh_fields) { - await indexPatternsService.refreshFields(indexPattern); - } - - return res.ok({ - headers: { - 'content-type': 'application/json', - }, - body: JSON.stringify({ - index_pattern: indexPattern.toSpec(), - }), - }); - }) - ) + } + + if (changeCount < 1) { + throw new Error('Index pattern change set is empty.'); + } + + await indexPatternsService.updateSavedObject(indexPattern); + + if (doRefreshFields && refresh_fields) { + await indexPatternsService.refreshFields(indexPattern); + } + + return res.ok({ + headers: { + 'content-type': 'application/json', + }, + body: JSON.stringify({ + index_pattern: indexPattern.toSpec(), + }), + }); + }) ) ); }; diff --git a/src/plugins/data/server/index_patterns/routes/util/assert_index_patterns_context.ts b/src/plugins/data/server/index_patterns/routes/util/assert_index_patterns_context.ts deleted file mode 100644 index d70827bdac900..0000000000000 --- a/src/plugins/data/server/index_patterns/routes/util/assert_index_patterns_context.ts +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import { RequestHandler, RequestHandlerContext, RouteMethod } from '../../../../../../core/server'; -import { IndexPatternsRouteContext, IndexPatternsRequestHandler } from '../../../types'; - -const isTagsRouteContext = (context: RequestHandlerContext): context is IndexPatternsRouteContext => - !!context.indexPatterns && !!context.indexPatterns.indexPatterns; - -/** - * This higher order request handler makes sure that `ctx.indexPatterns` - * property is present. - */ -export const assertIndexPatternsContext = ( - handler: IndexPatternsRequestHandler -): RequestHandler => (context, request, response) => { - return isTagsRouteContext(context) - ? handler(context, request, response) - : response.badRequest({ body: 'IndexPatternsRequestHandlerContext is not registered.' }); -}; diff --git a/src/plugins/data/server/plugin.ts b/src/plugins/data/server/plugin.ts index 5293ca1ac65e9..bf2c703456493 100644 --- a/src/plugins/data/server/plugin.ts +++ b/src/plugins/data/server/plugin.ts @@ -94,28 +94,7 @@ export class DataServerPlugin this.queryService.setup(core); this.autocompleteService.setup(core); this.kqlTelemetryService.setup(core, { usageCollection }); - this.indexPatterns.setup(core, { expressions }); - core.http.registerRouteHandlerContext( - 'indexPatterns', - async (context): Promise> => { - try { - const savedObjectsClient = context.core.savedObjects.client; - const elasticsearchClient = context.core.elasticsearch.client.asCurrentUser; - - const indexPatterns = await this.indexPatterns.createIndexPatternsService( - savedObjectsClient, - elasticsearchClient - ); - - return { - indexPatterns, - }; - } catch { - return {}; - } - } - ); core.uiSettings.register(getUiSettings()); diff --git a/src/plugins/data/server/types.ts b/src/plugins/data/server/types.ts deleted file mode 100644 index 4e138efb0ce4e..0000000000000 --- a/src/plugins/data/server/types.ts +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import { - IKibanaResponse, - KibanaRequest, - KibanaResponseFactory, - RequestHandlerContext, - RouteMethod, -} from 'src/core/server'; -import type { IndexPatternsContract } from '../common'; - -export interface IndexPatternsRequestHandlerContext { - indexPatterns: IndexPatternsContract; -} - -declare module 'src/core/server' { - interface RequestHandlerContext { - indexPatterns?: Partial; - } -} - -export type IndexPatternsRouteContext = RequestHandlerContext & - Required>; - -export type IndexPatternsRequestHandler< - P = unknown, - Q = unknown, - B = unknown, - Method extends RouteMethod = any, - ResponseFactory extends KibanaResponseFactory = KibanaResponseFactory -> = ( - context: IndexPatternsRouteContext, - request: KibanaRequest, - response: ResponseFactory -) => IKibanaResponse | Promise>;