-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
140 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
src/plugins/data/server/index_patterns/routes/default_index_pattern.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<DataPluginStartDependencies, DataPluginStart> | ||
) => { | ||
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, | ||
}, | ||
}); | ||
}) | ||
); | ||
}; |
40 changes: 40 additions & 0 deletions
40
test/api_integration/apis/index_patterns/default_index_pattern/default_index_pattern.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
}); | ||
}); | ||
} |
15 changes: 15 additions & 0 deletions
15
test/api_integration/apis/index_patterns/default_index_pattern/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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')); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters