diff --git a/x-pack/plugins/file_upload/server/routes.ts b/x-pack/plugins/file_upload/server/routes.ts index 8e6651ed891c..847a57afb391 100644 --- a/x-pack/plugins/file_upload/server/routes.ts +++ b/x-pack/plugins/file_upload/server/routes.ts @@ -185,7 +185,7 @@ export function fileUploadRoutes(coreSetup: CoreSetup, 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, 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)); } diff --git a/x-pack/test/api_integration/apis/file_upload/index.ts b/x-pack/test/api_integration/apis/file_upload/index.ts index cf0159997fa1..30fd1ae81959 100644 --- a/x-pack/test/api_integration/apis/file_upload/index.ts +++ b/x-pack/test/api_integration/apis/file_upload/index.ts @@ -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')); }); } diff --git a/x-pack/test/api_integration/apis/file_upload/index_exists.ts b/x-pack/test/api_integration/apis/file_upload/index_exists.ts new file mode 100644 index 000000000000..4e014105ab7a --- /dev/null +++ b/x-pack/test/api_integration/apis/file_upload/index_exists.ts @@ -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); + }); + }); +};