Skip to content

Commit

Permalink
[Search Profiler] Migrate server to new es-js client (#88725) (#89175)
Browse files Browse the repository at this point in the history
  • Loading branch information
alisonelizabeth committed Jan 25, 2021
1 parent d38268e commit 9f6e555
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 10 deletions.
20 changes: 10 additions & 10 deletions x-pack/plugins/searchprofiler/server/routes/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ export const register = ({ router, getLicenseStatus, log }: RouteDependencies) =
});
}

const {
core: { elasticsearch },
} = ctx;

const {
body: { query, index },
} = request;
Expand All @@ -46,21 +42,25 @@ export const register = ({ router, getLicenseStatus, log }: RouteDependencies) =
body: JSON.stringify(parsed, null, 2),
};
try {
const resp = await elasticsearch.legacy.client.callAsCurrentUser('search', body);
const client = ctx.core.elasticsearch.client.asCurrentUser;
const resp = await client.search(body);

return response.ok({
body: {
ok: true,
resp,
resp: resp.body,
},
});
} catch (err) {
log.error(err);
const { statusCode, body: errorBody } = err;

return response.customError({
statusCode: err.status || 500,
body: err.body
statusCode: statusCode || 500,
body: errorBody
? {
message: err.message,
attributes: err.body,
message: errorBody.error?.reason,
attributes: errorBody,
}
: err,
});
Expand Down
1 change: 1 addition & 0 deletions x-pack/test/api_integration/apis/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ export default function ({ loadTestFile }: FtrProviderContext) {
loadTestFile(require.resolve('./transform'));
loadTestFile(require.resolve('./lists'));
loadTestFile(require.resolve('./upgrade_assistant'));
loadTestFile(require.resolve('./searchprofiler'));
});
}
13 changes: 13 additions & 0 deletions x-pack/test/api_integration/apis/searchprofiler/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { FtrProviderContext } from '../../ftr_provider_context';

export default function ({ loadTestFile }: FtrProviderContext) {
describe('Search Profiler', () => {
loadTestFile(require.resolve('./searchprofiler'));
});
}
56 changes: 56 additions & 0 deletions x-pack/test/api_integration/apis/searchprofiler/searchprofiler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import expect from '@kbn/expect';
import { FtrProviderContext } from '../../ftr_provider_context';

const API_BASE_PATH = '/api/searchprofiler';

export default function ({ getService }: FtrProviderContext) {
const supertest = getService('supertest');

describe('Profile', () => {
it('should return profile results for a valid index', async () => {
const payload = {
index: '_all',
query: {
query: {
match_all: {},
},
},
};

const { body } = await supertest
.post(`${API_BASE_PATH}/profile`)
.set('kbn-xsrf', 'xxx')
.set('Content-Type', 'application/json;charset=UTF-8')
.send(payload)
.expect(200);

expect(body.ok).to.eql(true);
});

it('should return error for invalid index', async () => {
const payloadWithInvalidIndex = {
index: 'index_does_not_exist',
query: {
query: {
match_all: {},
},
},
};

const { body } = await supertest
.post(`${API_BASE_PATH}/execute`)
.set('kbn-xsrf', 'xxx')
.set('Content-Type', 'application/json;charset=UTF-8')
.send(payloadWithInvalidIndex)
.expect(404);

expect(body.error).to.eql('Not Found');
});
});
}

0 comments on commit 9f6e555

Please sign in to comment.