From 3a89bd2512347e38511fd3badd0aa20b8d12d383 Mon Sep 17 00:00:00 2001 From: Anton Dosov Date: Wed, 26 May 2021 16:40:50 +0200 Subject: [PATCH] default index pattern reset api --- .../index_patterns/index_patterns.ts | 7 +- .../data/server/index_patterns/routes.ts | 2 + .../routes/default_index_pattern.ts | 76 +++++++++++++++++++ .../default_index_pattern.ts | 40 ++++++++++ .../default_index_pattern/index.ts | 15 ++++ .../apis/index_patterns/index.js | 1 + 6 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 src/plugins/data/server/index_patterns/routes/default_index_pattern.ts create mode 100644 test/api_integration/apis/index_patterns/default_index_pattern/default_index_pattern.ts create mode 100644 test/api_integration/apis/index_patterns/default_index_pattern/index.ts diff --git a/src/plugins/data/common/index_patterns/index_patterns/index_patterns.ts b/src/plugins/data/common/index_patterns/index_patterns/index_patterns.ts index 04d2785137719..31d2f8556e7ef 100644 --- a/src/plugins/data/common/index_patterns/index_patterns/index_patterns.ts +++ b/src/plugins/data/common/index_patterns/index_patterns/index_patterns.ts @@ -192,7 +192,7 @@ export class IndexPatternsService { * Get default index pattern */ getDefault = async () => { - const defaultIndexPatternId = await this.config.get('defaultIndex'); + const defaultIndexPatternId = await this.getDefaultId(); if (defaultIndexPatternId) { return await this.get(defaultIndexPatternId); } @@ -200,6 +200,11 @@ export class IndexPatternsService { return null; }; + getDefaultId = async (): Promise => { + const defaultIndexPatternId = await this.config.get('defaultIndex'); + return defaultIndexPatternId ?? null; + }; + /** * Optionally set default index pattern, unless force = true * @param id diff --git a/src/plugins/data/server/index_patterns/routes.ts b/src/plugins/data/server/index_patterns/routes.ts index 84199fe60b997..9bff590b54f1c 100644 --- a/src/plugins/data/server/index_patterns/routes.ts +++ b/src/plugins/data/server/index_patterns/routes.ts @@ -20,6 +20,7 @@ import { registerGetScriptedFieldRoute } from './routes/scripted_fields/get_scri import { registerDeleteScriptedFieldRoute } from './routes/scripted_fields/delete_scripted_field'; import { registerUpdateScriptedFieldRoute } from './routes/scripted_fields/update_scripted_field'; import type { DataPluginStart, DataPluginStartDependencies } from '../plugin'; +import { registerManageDefaultIndexPatternRoutes } from './routes/default_index_pattern'; export function registerRoutes( http: HttpServiceSetup, @@ -42,6 +43,7 @@ export function registerRoutes( registerGetIndexPatternRoute(router, getStartServices); registerDeleteIndexPatternRoute(router, getStartServices); registerUpdateIndexPatternRoute(router, getStartServices); + registerManageDefaultIndexPatternRoutes(router, getStartServices); // Fields API registerUpdateFieldsRoute(router, getStartServices); diff --git a/src/plugins/data/server/index_patterns/routes/default_index_pattern.ts b/src/plugins/data/server/index_patterns/routes/default_index_pattern.ts new file mode 100644 index 0000000000000..8f352e82aa9bd --- /dev/null +++ b/src/plugins/data/server/index_patterns/routes/default_index_pattern.ts @@ -0,0 +1,76 @@ +/* + * 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 { schema } from '@kbn/config-schema'; +import { IRouter, StartServicesAccessor } from '../../../../../core/server'; +import type { DataPluginStart, DataPluginStartDependencies } from '../../plugin'; +import { handleErrors } from './util/handle_errors'; + +export const registerManageDefaultIndexPatternRoutes = ( + router: IRouter, + getStartServices: StartServicesAccessor +) => { + router.get( + { + path: '/api/index_patterns/default', + validate: {}, + }, + handleErrors(async (ctx, req, res) => { + const savedObjectsClient = ctx.core.savedObjects.client; + const elasticsearchClient = ctx.core.elasticsearch.client.asCurrentUser; + const [, , { indexPatterns }] = await getStartServices(); + const indexPatternsService = await indexPatterns.indexPatternsServiceFactory( + savedObjectsClient, + elasticsearchClient + ); + + const defaultIndexPatternId = await indexPatternsService.getDefaultId(); + + return res.ok({ + body: { + index_pattern_id: defaultIndexPatternId, + }, + }); + }) + ); + + router.post( + { + path: '/api/index_patterns/default', + validate: { + body: schema.object({ + index_pattern_id: schema.string({ + minLength: 1, + maxLength: 1_000, + }), + force: schema.boolean({ defaultValue: false }), + }), + }, + }, + handleErrors(async (ctx, req, res) => { + const savedObjectsClient = ctx.core.savedObjects.client; + const elasticsearchClient = ctx.core.elasticsearch.client.asCurrentUser; + const [, , { indexPatterns }] = await getStartServices(); + const indexPatternsService = await indexPatterns.indexPatternsServiceFactory( + savedObjectsClient, + elasticsearchClient + ); + + const newDefaultId = req.body.index_pattern_id; + const force = req.body.force; + + await indexPatternsService.setDefault(newDefaultId, force); + + return res.ok({ + body: { + acknowledged: true, + }, + }); + }) + ); +}; diff --git a/test/api_integration/apis/index_patterns/default_index_pattern/default_index_pattern.ts b/test/api_integration/apis/index_patterns/default_index_pattern/default_index_pattern.ts new file mode 100644 index 0000000000000..a550a897c80a9 --- /dev/null +++ b/test/api_integration/apis/index_patterns/default_index_pattern/default_index_pattern.ts @@ -0,0 +1,40 @@ +/* + * 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 expect from '@kbn/expect'; +import { FtrProviderContext } from '../../../ftr_provider_context'; + +export default function ({ getService }: FtrProviderContext) { + const supertest = getService('supertest'); + + describe('default index pattern api', () => { + const newId = () => `default-id-${Date.now()}-${Math.random()}`; + it('can set default index pattern', async () => { + const defaultId = newId(); + const response1 = await supertest.post('/api/index_patterns/default').send({ + index_pattern_id: defaultId, + force: true, + }); + expect(response1.status).to.be(200); + expect(response1.body.acknowledged).to.be(true); + + const response2 = await supertest.get('/api/index_patterns/default'); + expect(response2.status).to.be(200); + expect(response2.body.index_pattern_id).to.be(defaultId); + + const response3 = await supertest.post('/api/index_patterns/default').send({ + index_pattern_id: newId(), + // no force this time, so this new default shouldn't be set + }); + + expect(response3.status).to.be(200); + const response4 = await supertest.get('/api/index_patterns/default'); + expect(response4.body.index_pattern_id).to.be(defaultId); // original default id is used + }); + }); +} diff --git a/test/api_integration/apis/index_patterns/default_index_pattern/index.ts b/test/api_integration/apis/index_patterns/default_index_pattern/index.ts new file mode 100644 index 0000000000000..7517c87ade25b --- /dev/null +++ b/test/api_integration/apis/index_patterns/default_index_pattern/index.ts @@ -0,0 +1,15 @@ +/* + * 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 { FtrProviderContext } from '../../../ftr_provider_context'; + +export default function ({ loadTestFile }: FtrProviderContext) { + describe('default index pattern', () => { + loadTestFile(require.resolve('./default_index_pattern')); + }); +} diff --git a/test/api_integration/apis/index_patterns/index.js b/test/api_integration/apis/index_patterns/index.js index 8d279a5783703..9c1e1bba0ab9a 100644 --- a/test/api_integration/apis/index_patterns/index.js +++ b/test/api_integration/apis/index_patterns/index.js @@ -14,5 +14,6 @@ export default function ({ loadTestFile }) { loadTestFile(require.resolve('./index_pattern_crud')); loadTestFile(require.resolve('./scripted_fields_crud')); loadTestFile(require.resolve('./fields_api')); + loadTestFile(require.resolve('./default_index_pattern')); }); }