From 1ceecd395801fc5ab1c99e7677209cc420ea5fa4 Mon Sep 17 00:00:00 2001 From: Anton Dosov Date: Thu, 27 May 2021 18:02:25 +0200 Subject: [PATCH] [Index patterns] Default index pattern REST API (#100691) --- docs/api/index-patterns.asciidoc | 9 +- docs/api/index-patterns/default-get.asciidoc | 55 ++++++++++++ docs/api/index-patterns/default-set.asciidoc | 84 +++++++++++++++++++ ...ublic.indexpatternsservice.getdefaultid.md | 13 +++ ...lugins-data-public.indexpatternsservice.md | 3 +- ...-public.indexpatternsservice.setdefault.md | 2 +- ...erver.indexpatternsservice.getdefaultid.md | 13 +++ ...lugins-data-server.indexpatternsservice.md | 3 +- ...-server.indexpatternsservice.setdefault.md | 2 +- ...rver.searchstrategydependencies.request.md | 11 +++ .../index_patterns/index_patterns.ts | 12 ++- src/plugins/data/public/public.api.md | 3 +- .../data/server/index_patterns/routes.ts | 2 + .../routes/default_index_pattern.ts | 78 +++++++++++++++++ src/plugins/data/server/server.api.md | 3 +- .../default_index_pattern.ts | 49 +++++++++++ .../default_index_pattern/index.ts | 15 ++++ .../apis/index_patterns/index.js | 1 + 18 files changed, 347 insertions(+), 11 deletions(-) create mode 100644 docs/api/index-patterns/default-get.asciidoc create mode 100644 docs/api/index-patterns/default-set.asciidoc create mode 100644 docs/development/plugins/data/public/kibana-plugin-plugins-data-public.indexpatternsservice.getdefaultid.md create mode 100644 docs/development/plugins/data/server/kibana-plugin-plugins-data-server.indexpatternsservice.getdefaultid.md create mode 100644 docs/development/plugins/data/server/kibana-plugin-plugins-data-server.searchstrategydependencies.request.md 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/docs/api/index-patterns.asciidoc b/docs/api/index-patterns.asciidoc index 47906e1761138..79d2f164fb8c3 100644 --- a/docs/api/index-patterns.asciidoc +++ b/docs/api/index-patterns.asciidoc @@ -15,13 +15,16 @@ The following index patterns APIs are available: ** <> to create {kib} index pattern ** <> to partially updated {kib} index pattern ** <> to delete {kib} index pattern +* Default index pattern + ** <> to retrieve a default index pattern + ** <> to set a default index pattern * Fields - ** <> to change field metadata, such as `count`, `customLabel` and `format`. - - + ** <> to change field metadata, such as `count`, `customLabel` and `format` include::index-patterns/get.asciidoc[] include::index-patterns/create.asciidoc[] include::index-patterns/update.asciidoc[] include::index-patterns/delete.asciidoc[] +include::index-patterns/default-get.asciidoc[] +include::index-patterns/default-set.asciidoc[] include::index-patterns/update-fields.asciidoc[] diff --git a/docs/api/index-patterns/default-get.asciidoc b/docs/api/index-patterns/default-get.asciidoc new file mode 100644 index 0000000000000..5c7e8e01ce399 --- /dev/null +++ b/docs/api/index-patterns/default-get.asciidoc @@ -0,0 +1,55 @@ +[[index-patterns-api-default-get]] +=== Get default index pattern API +++++ +Get default index pattern +++++ + +experimental[] Retrieve a default index pattern ID. Kibana UI uses default index pattern unless user picks a different one. + +[[index-patterns-api-default-get-request]] +==== Request + +`GET :/api/index_patterns/default` + +`GET :/s//api/index_patterns/default` + +[[index-patterns-api-default-get-params]] +==== Path parameters + +`space_id`:: +(Optional, string) An identifier for the space. If `space_id` is not provided in the URL, the default space is used. + +[[index-patterns-api-default-get-codes]] +==== Response code + +`200`:: +Indicates a successful call. + +[[index-patterns-api-default-get-example]] +==== Example + +Retrieve the default index pattern id: + +[source,sh] +-------------------------------------------------- +$ curl -X GET api/index_patterns/default +-------------------------------------------------- +// KIBANA + +The API returns an ID of a default index pattern: + +[source,sh] +-------------------------------------------------- +{ + "index_pattern_id": "..." +} +-------------------------------------------------- + +In case there is no default index pattern, the API returns: + +[source,sh] +-------------------------------------------------- +{ + "index_pattern_id": null +} +-------------------------------------------------- diff --git a/docs/api/index-patterns/default-set.asciidoc b/docs/api/index-patterns/default-set.asciidoc new file mode 100644 index 0000000000000..3b3cec6be6324 --- /dev/null +++ b/docs/api/index-patterns/default-set.asciidoc @@ -0,0 +1,84 @@ +[[index-patterns-api-default-set]] +=== Set default index pattern API +++++ +Set default index pattern +++++ + +experimental[] Set a default index pattern ID. Kibana UI will use default index pattern unless user picks a different one. +The API doesn't validate if given `index_pattern_id` is a valid id. + +[[index-patterns-api-default-set-request]] +==== Request + +`POST :/api/index_patterns/default` + +`POST :/s//api/index_patterns/default` + +[[index-patterns-api-default-set-params]] +==== Path parameters + +`space_id`:: +(Optional, string) An identifier for the space. If `space_id` is not provided in the URL, the default space is used. + +[[index-patterns-api-default-set-body]] +==== Request body + +`index_pattern_id`:: (Required, `string` or `null`) Sets a default index pattern id. Use `null` to unset a default index pattern. + +`force`:: (Optional, boolean) Updates existing default index pattern id. The default is `false`. + + +[[index-patterns-api-default-set-codes]] +==== Response code + +`200`:: +Indicates a successful call. + +[[index-patterns-api-default-set-example]] +==== Example + +Set the default index pattern id if none is set: + +[source,sh] +-------------------------------------------------- +$ curl -X POST api/index_patterns/default +{ + "index_pattern_id": "..." +} +-------------------------------------------------- +// KIBANA + + +Upsert the default index pattern: + +[source,sh] +-------------------------------------------------- +$ curl -X POST api/index_patterns/default +{ + "index_pattern_id": "...", + "force": true +} +-------------------------------------------------- +// KIBANA + +Unset the default index pattern: + +[source,sh] +-------------------------------------------------- +$ curl -X POST api/index_patterns/default +{ + "index_pattern_id": null, + "force": true +} +-------------------------------------------------- +// KIBANA + +The API returns: + +[source,sh] +-------------------------------------------------- +{ + "acknowledged": true +} +-------------------------------------------------- + diff --git a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.indexpatternsservice.getdefaultid.md b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.indexpatternsservice.getdefaultid.md new file mode 100644 index 0000000000000..3b64ce079b522 --- /dev/null +++ b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.indexpatternsservice.getdefaultid.md @@ -0,0 +1,13 @@ + + +[Home](./index.md) > [kibana-plugin-plugins-data-public](./kibana-plugin-plugins-data-public.md) > [IndexPatternsService](./kibana-plugin-plugins-data-public.indexpatternsservice.md) > [getDefaultId](./kibana-plugin-plugins-data-public.indexpatternsservice.getdefaultid.md) + +## IndexPatternsService.getDefaultId property + +Get default index pattern id + +Signature: + +```typescript +getDefaultId: () => Promise; +``` diff --git a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.indexpatternsservice.md b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.indexpatternsservice.md index 1511de18cab51..26b393a5fb5b6 100644 --- a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.indexpatternsservice.md +++ b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.indexpatternsservice.md @@ -27,6 +27,7 @@ export declare class IndexPatternsService | [get](./kibana-plugin-plugins-data-public.indexpatternsservice.get.md) | | (id: string) => Promise<IndexPattern> | Get an index pattern by id. Cache optimized | | [getCache](./kibana-plugin-plugins-data-public.indexpatternsservice.getcache.md) | | () => Promise<SavedObject<IndexPatternSavedObjectAttrs>[] | null | undefined> | | | [getDefault](./kibana-plugin-plugins-data-public.indexpatternsservice.getdefault.md) | | () => Promise<IndexPattern | null> | Get default index pattern | +| [getDefaultId](./kibana-plugin-plugins-data-public.indexpatternsservice.getdefaultid.md) | | () => Promise<string | null> | Get default index pattern id | | [getFieldsForIndexPattern](./kibana-plugin-plugins-data-public.indexpatternsservice.getfieldsforindexpattern.md) | | (indexPattern: IndexPattern | IndexPatternSpec, options?: GetFieldsOptions | undefined) => Promise<any> | Get field list by providing an index patttern (or spec) | | [getFieldsForWildcard](./kibana-plugin-plugins-data-public.indexpatternsservice.getfieldsforwildcard.md) | | (options: GetFieldsOptions) => Promise<any> | Get field list by providing { pattern } | | [getIds](./kibana-plugin-plugins-data-public.indexpatternsservice.getids.md) | | (refresh?: boolean) => Promise<string[]> | Get list of index pattern ids | @@ -34,7 +35,7 @@ export declare class IndexPatternsService | [getTitles](./kibana-plugin-plugins-data-public.indexpatternsservice.gettitles.md) | | (refresh?: boolean) => Promise<string[]> | Get list of index pattern titles | | [refreshFields](./kibana-plugin-plugins-data-public.indexpatternsservice.refreshfields.md) | | (indexPattern: IndexPattern) => Promise<void> | Refresh field list for a given index pattern | | [savedObjectToSpec](./kibana-plugin-plugins-data-public.indexpatternsservice.savedobjecttospec.md) | | (savedObject: SavedObject<IndexPatternAttributes>) => IndexPatternSpec | Converts index pattern saved object to index pattern spec | -| [setDefault](./kibana-plugin-plugins-data-public.indexpatternsservice.setdefault.md) | | (id: string, force?: boolean) => Promise<void> | Optionally set default index pattern, unless force = true | +| [setDefault](./kibana-plugin-plugins-data-public.indexpatternsservice.setdefault.md) | | (id: string | null, force?: boolean) => Promise<void> | Optionally set default index pattern, unless force = true | ## Methods diff --git a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.indexpatternsservice.setdefault.md b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.indexpatternsservice.setdefault.md index 2bf8eaa03d1ae..1d216e781c7bb 100644 --- a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.indexpatternsservice.setdefault.md +++ b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.indexpatternsservice.setdefault.md @@ -9,5 +9,5 @@ Optionally set default index pattern, unless force = true Signature: ```typescript -setDefault: (id: string, force?: boolean) => Promise; +setDefault: (id: string | null, force?: boolean) => Promise; ``` diff --git a/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.indexpatternsservice.getdefaultid.md b/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.indexpatternsservice.getdefaultid.md new file mode 100644 index 0000000000000..107d1e4e94a0d --- /dev/null +++ b/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.indexpatternsservice.getdefaultid.md @@ -0,0 +1,13 @@ + + +[Home](./index.md) > [kibana-plugin-plugins-data-server](./kibana-plugin-plugins-data-server.md) > [IndexPatternsService](./kibana-plugin-plugins-data-server.indexpatternsservice.md) > [getDefaultId](./kibana-plugin-plugins-data-server.indexpatternsservice.getdefaultid.md) + +## IndexPatternsService.getDefaultId property + +Get default index pattern id + +Signature: + +```typescript +getDefaultId: () => Promise; +``` diff --git a/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.indexpatternsservice.md b/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.indexpatternsservice.md index d55a6e9b325a2..f5e845ced3cd1 100644 --- a/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.indexpatternsservice.md +++ b/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.indexpatternsservice.md @@ -27,6 +27,7 @@ export declare class IndexPatternsService | [get](./kibana-plugin-plugins-data-server.indexpatternsservice.get.md) | | (id: string) => Promise<IndexPattern> | Get an index pattern by id. Cache optimized | | [getCache](./kibana-plugin-plugins-data-server.indexpatternsservice.getcache.md) | | () => Promise<SavedObject<IndexPatternSavedObjectAttrs>[] | null | undefined> | | | [getDefault](./kibana-plugin-plugins-data-server.indexpatternsservice.getdefault.md) | | () => Promise<IndexPattern | null> | Get default index pattern | +| [getDefaultId](./kibana-plugin-plugins-data-server.indexpatternsservice.getdefaultid.md) | | () => Promise<string | null> | Get default index pattern id | | [getFieldsForIndexPattern](./kibana-plugin-plugins-data-server.indexpatternsservice.getfieldsforindexpattern.md) | | (indexPattern: IndexPattern | IndexPatternSpec, options?: GetFieldsOptions | undefined) => Promise<any> | Get field list by providing an index patttern (or spec) | | [getFieldsForWildcard](./kibana-plugin-plugins-data-server.indexpatternsservice.getfieldsforwildcard.md) | | (options: GetFieldsOptions) => Promise<any> | Get field list by providing { pattern } | | [getIds](./kibana-plugin-plugins-data-server.indexpatternsservice.getids.md) | | (refresh?: boolean) => Promise<string[]> | Get list of index pattern ids | @@ -34,7 +35,7 @@ export declare class IndexPatternsService | [getTitles](./kibana-plugin-plugins-data-server.indexpatternsservice.gettitles.md) | | (refresh?: boolean) => Promise<string[]> | Get list of index pattern titles | | [refreshFields](./kibana-plugin-plugins-data-server.indexpatternsservice.refreshfields.md) | | (indexPattern: IndexPattern) => Promise<void> | Refresh field list for a given index pattern | | [savedObjectToSpec](./kibana-plugin-plugins-data-server.indexpatternsservice.savedobjecttospec.md) | | (savedObject: SavedObject<IndexPatternAttributes>) => IndexPatternSpec | Converts index pattern saved object to index pattern spec | -| [setDefault](./kibana-plugin-plugins-data-server.indexpatternsservice.setdefault.md) | | (id: string, force?: boolean) => Promise<void> | Optionally set default index pattern, unless force = true | +| [setDefault](./kibana-plugin-plugins-data-server.indexpatternsservice.setdefault.md) | | (id: string | null, force?: boolean) => Promise<void> | Optionally set default index pattern, unless force = true | ## Methods diff --git a/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.indexpatternsservice.setdefault.md b/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.indexpatternsservice.setdefault.md index 708d645a79f1a..6dc584341eef3 100644 --- a/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.indexpatternsservice.setdefault.md +++ b/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.indexpatternsservice.setdefault.md @@ -9,5 +9,5 @@ Optionally set default index pattern, unless force = true Signature: ```typescript -setDefault: (id: string, force?: boolean) => Promise; +setDefault: (id: string | null, force?: boolean) => Promise; ``` diff --git a/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.searchstrategydependencies.request.md b/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.searchstrategydependencies.request.md new file mode 100644 index 0000000000000..18163bfebde7e --- /dev/null +++ b/docs/development/plugins/data/server/kibana-plugin-plugins-data-server.searchstrategydependencies.request.md @@ -0,0 +1,11 @@ + + +[Home](./index.md) > [kibana-plugin-plugins-data-server](./kibana-plugin-plugins-data-server.md) > [SearchStrategyDependencies](./kibana-plugin-plugins-data-server.searchstrategydependencies.md) > [request](./kibana-plugin-plugins-data-server.searchstrategydependencies.request.md) + +## SearchStrategyDependencies.request property + +Signature: + +```typescript +request: KibanaRequest; +``` 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..66e66051a6370 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,12 +200,20 @@ export class IndexPatternsService { return null; }; + /** + * Get default index pattern id + */ + getDefaultId = async (): Promise => { + const defaultIndexPatternId = await this.config.get('defaultIndex'); + return defaultIndexPatternId ?? null; + }; + /** * Optionally set default index pattern, unless force = true * @param id * @param force */ - setDefault = async (id: string, force = false) => { + setDefault = async (id: string | null, force = false) => { if (force || !this.config.get('defaultIndex')) { await this.config.set('defaultIndex', id); } diff --git a/src/plugins/data/public/public.api.md b/src/plugins/data/public/public.api.md index 57aa2298039da..fde7075d9e760 100644 --- a/src/plugins/data/public/public.api.md +++ b/src/plugins/data/public/public.api.md @@ -1624,6 +1624,7 @@ export class IndexPatternsService { // (undocumented) getCache: () => Promise[] | null | undefined>; getDefault: () => Promise; + getDefaultId: () => Promise; getFieldsForIndexPattern: (indexPattern: IndexPattern | IndexPatternSpec, options?: GetFieldsOptions | undefined) => Promise; // Warning: (ae-forgotten-export) The symbol "GetFieldsOptions" needs to be exported by the entry point index.d.ts getFieldsForWildcard: (options: GetFieldsOptions) => Promise; @@ -1635,7 +1636,7 @@ export class IndexPatternsService { getTitles: (refresh?: boolean) => Promise; refreshFields: (indexPattern: IndexPattern) => Promise; savedObjectToSpec: (savedObject: SavedObject) => IndexPatternSpec; - setDefault: (id: string, force?: boolean) => Promise; + setDefault: (id: string | null, force?: boolean) => Promise; updateSavedObject(indexPattern: IndexPattern, saveAttempts?: number, ignoreErrors?: boolean): Promise; } 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..cf5986943eb37 --- /dev/null +++ b/src/plugins/data/server/index_patterns/routes/default_index_pattern.ts @@ -0,0 +1,78 @@ +/* + * 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.nullable( + 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/src/plugins/data/server/server.api.md b/src/plugins/data/server/server.api.md index b1c90667c2d71..4abf430252164 100644 --- a/src/plugins/data/server/server.api.md +++ b/src/plugins/data/server/server.api.md @@ -962,6 +962,7 @@ class IndexPatternsService { // (undocumented) getCache: () => Promise[] | null | undefined>; getDefault: () => Promise; + getDefaultId: () => Promise; getFieldsForIndexPattern: (indexPattern: IndexPattern | IndexPatternSpec, options?: GetFieldsOptions | undefined) => Promise; // Warning: (ae-forgotten-export) The symbol "GetFieldsOptions" needs to be exported by the entry point index.d.ts getFieldsForWildcard: (options: GetFieldsOptions) => Promise; @@ -973,7 +974,7 @@ class IndexPatternsService { getTitles: (refresh?: boolean) => Promise; refreshFields: (indexPattern: IndexPattern) => Promise; savedObjectToSpec: (savedObject: SavedObject_2) => IndexPatternSpec; - setDefault: (id: string, force?: boolean) => Promise; + setDefault: (id: string | null, force?: boolean) => Promise; updateSavedObject(indexPattern: IndexPattern, saveAttempts?: number, ignoreErrors?: boolean): Promise; } 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..b12600f5ce4f3 --- /dev/null +++ b/test/api_integration/apis/index_patterns/default_index_pattern/default_index_pattern.ts @@ -0,0 +1,49 @@ +/* + * 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 + + const response5 = await supertest.post('/api/index_patterns/default').send({ + index_pattern_id: null, + force: true, + }); + expect(response5.status).to.be(200); + + const response6 = await supertest.get('/api/index_patterns/default'); + expect(response6.body.index_pattern_id).to.be(null); + }); + }); +} 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')); }); }