Skip to content

Commit

Permalink
[Watcher] Migrate to new ES client (elastic#97260)
Browse files Browse the repository at this point in the history
* initial migration away from ILegacyScopedClusterClient to
IScopedClusterClient and from "isEsError" to "handleEsError"

* re-instate ignore: [404]

* remove use of ignore_unavailable

* get the correct payload from the response

* fix use of new licensePreRoutingFactory

* fix jest tests

* address CJs feedback and re-add ignore_unavailable, clean up remaining TODOs

* remove legacy client config

* undo renaming as part of destructuring assignment

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
2 people authored and madirey committed May 11, 2021
1 parent 37cded6 commit 5739557
Show file tree
Hide file tree
Showing 21 changed files with 278 additions and 615 deletions.
246 changes: 0 additions & 246 deletions x-pack/plugins/watcher/server/lib/elasticsearch_js_plugin.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,10 @@ import { elasticsearchServiceMock } from '../../../../../../src/core/server/mock
import { fetchAllFromScroll } from './fetch_all_from_scroll';

describe('fetch_all_from_scroll', () => {
let mockScopedClusterClient;
const mockScopedClusterClient = {};

beforeEach(() => {
mockScopedClusterClient = elasticsearchServiceMock.createLegacyScopedClusterClient();

elasticsearchServiceMock
.createLegacyClusterClient()
.asScoped.mockReturnValue(mockScopedClusterClient);
mockScopedClusterClient.asCurrentUser = elasticsearchServiceMock.createElasticsearchClient();
});

describe('#fetchAllFromScroll', () => {
Expand All @@ -33,9 +29,9 @@ describe('fetch_all_from_scroll', () => {
});
});

it('should not call callWithRequest', () => {
it('should not call asCurrentUser.scroll', () => {
return fetchAllFromScroll(mockSearchResults, mockScopedClusterClient).then(() => {
expect(mockScopedClusterClient.callAsCurrentUser).not.toHaveBeenCalled();
expect(mockScopedClusterClient.asCurrentUser.scroll).not.toHaveBeenCalled();
});
});
});
Expand All @@ -62,9 +58,9 @@ describe('fetch_all_from_scroll', () => {
},
};

mockScopedClusterClient.callAsCurrentUser
.mockReturnValueOnce(Promise.resolve(mockResponse1))
.mockReturnValueOnce(Promise.resolve(mockResponse2));
mockScopedClusterClient.asCurrentUser.scroll
.mockResolvedValueOnce({ body: mockResponse1 })
.mockResolvedValueOnce({ body: mockResponse2 });
});

it('should return the hits from the response', () => {
Expand All @@ -75,14 +71,14 @@ describe('fetch_all_from_scroll', () => {
);
});

it('should call callWithRequest', () => {
it('should call asCurrentUser.scroll', () => {
return fetchAllFromScroll(mockInitialSearchResults, mockScopedClusterClient).then(() => {
expect(mockScopedClusterClient.callAsCurrentUser).toHaveBeenCalledTimes(2);
expect(mockScopedClusterClient.asCurrentUser.scroll).toHaveBeenCalledTimes(2);

expect(mockScopedClusterClient.callAsCurrentUser).toHaveBeenNthCalledWith(1, 'scroll', {
expect(mockScopedClusterClient.asCurrentUser.scroll).toHaveBeenNthCalledWith(1, {
body: { scroll: '30s', scroll_id: 'originalScrollId' },
});
expect(mockScopedClusterClient.callAsCurrentUser).toHaveBeenNthCalledWith(2, 'scroll', {
expect(mockScopedClusterClient.asCurrentUser.scroll).toHaveBeenNthCalledWith(2, {
body: { scroll: '30s', scroll_id: 'newScrollId' },
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,30 @@
* 2.0.
*/

import { ILegacyScopedClusterClient } from 'kibana/server';
import { ScrollResponse, Hit } from '@elastic/elasticsearch/api/types';
import { IScopedClusterClient } from 'kibana/server';
import { get } from 'lodash';
import { ES_SCROLL_SETTINGS } from '../../../common/constants';

export function fetchAllFromScroll(
searchResuls: any,
dataClient: ILegacyScopedClusterClient,
hits: any[] = []
): Promise<any> {
const newHits = get(searchResuls, 'hits.hits', []);
const scrollId = get(searchResuls, '_scroll_id');
searchResults: ScrollResponse<unknown>,
dataClient: IScopedClusterClient,
hits: Hit[] = []
): Promise<ScrollResponse['hits']['hits']> {
const newHits = get(searchResults, 'hits.hits', []);
const scrollId = get(searchResults, '_scroll_id');

if (newHits.length > 0) {
hits.push(...newHits);

return dataClient
.callAsCurrentUser('scroll', {
return dataClient.asCurrentUser
.scroll({
body: {
scroll: ES_SCROLL_SETTINGS.KEEPALIVE,
scroll_id: scrollId,
},
})
.then((innerResponse: any) => {
.then(({ body: innerResponse }) => {
return fetchAllFromScroll(innerResponse, dataClient, hits);
});
}
Expand Down
Loading

0 comments on commit 5739557

Please sign in to comment.