Skip to content

Commit

Permalink
default index pattern reset api
Browse files Browse the repository at this point in the history
  • Loading branch information
Dosant committed May 26, 2021
1 parent 48d8c00 commit 3a89bd2
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,19 @@ 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);
}

return null;
};

getDefaultId = async (): Promise<string | null> => {
const defaultIndexPatternId = await this.config.get('defaultIndex');
return defaultIndexPatternId ?? null;
};

/**
* Optionally set default index pattern, unless force = true
* @param id
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/data/server/index_patterns/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -42,6 +43,7 @@ export function registerRoutes(
registerGetIndexPatternRoute(router, getStartServices);
registerDeleteIndexPatternRoute(router, getStartServices);
registerUpdateIndexPatternRoute(router, getStartServices);
registerManageDefaultIndexPatternRoutes(router, getStartServices);

// Fields API
registerUpdateFieldsRoute(router, getStartServices);
Expand Down
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,
},
});
})
);
};
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
});
});
}
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'));
});
}
1 change: 1 addition & 0 deletions test/api_integration/apis/index_patterns/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
});
}

0 comments on commit 3a89bd2

Please sign in to comment.