[file upload] use index exists API for /internal/file_upload/index_exists route (#98471)

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Nathan Reese 2021-05-05 07:59:29 -06:00 committed by GitHub
parent 86b688934a
commit 9a3c508184
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 15 deletions

View file

@ -185,7 +185,7 @@ export function fileUploadRoutes(coreSetup: CoreSetup<StartDeps, unknown>, logge
/**
* @apiGroup FileDataVisualizer
*
* @api {post} /internal/file_upload/index_exists ES Field caps wrapper checks if index exists
* @api {post} /internal/file_upload/index_exists ES indices exists wrapper checks if index exists
* @apiName IndexExists
*/
router.post(
@ -200,20 +200,10 @@ export function fileUploadRoutes(coreSetup: CoreSetup<StartDeps, unknown>, logge
},
async (context, request, response) => {
try {
const { index } = request.body;
const options = {
index: [index],
fields: ['*'],
ignore_unavailable: true,
allow_no_indices: true,
};
const { body } = await context.core.elasticsearch.client.asCurrentUser.fieldCaps(options);
const exists = Array.isArray(body.indices) && body.indices.length !== 0;
return response.ok({
body: { exists },
});
const {
body: indexExists,
} = await context.core.elasticsearch.client.asCurrentUser.indices.exists(request.body);
return response.ok({ body: { exists: indexExists } });
} catch (e) {
return response.customError(wrapError(e));
}

View file

@ -10,5 +10,6 @@ import { FtrProviderContext } from '../../ftr_provider_context';
export default function ({ loadTestFile }: FtrProviderContext) {
describe('File upload', function () {
loadTestFile(require.resolve('./has_import_permission'));
loadTestFile(require.resolve('./index_exists'));
});
}

View file

@ -0,0 +1,48 @@
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import expect from '@kbn/expect';
import { FtrProviderContext } from '../../ftr_provider_context';
export default ({ getService }: FtrProviderContext) => {
const esArchiver = getService('esArchiver');
const supertest = getService('supertest');
describe('POST /internal/file_upload/index_exists', () => {
before(async () => {
await esArchiver.loadIfNeeded('logstash_functional');
});
after(async () => {
await esArchiver.unload('logstash_functional');
});
it('should return true when index exists', async () => {
const resp = await supertest
.post(`/internal/file_upload/index_exists`)
.set('kbn-xsrf', 'kibana')
.send({
index: 'logstash-2015.09.22',
})
.expect(200);
expect(resp.body.exists).to.be(true);
});
it('should return false when index does not exists', async () => {
const resp = await supertest
.post(`/internal/file_upload/index_exists`)
.set('kbn-xsrf', 'kibana')
.send({
index: 'myNewIndex',
})
.expect(200);
expect(resp.body.exists).to.be(false);
});
});
};