-
Notifications
You must be signed in to change notification settings - Fork 919
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move decideClient and decideLegacy client to data source plugin commo…
…n util (#7079) Signed-off-by: Zhongnan Su <[email protected]> (cherry picked from commit ddf1a41) Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
4f30080
commit 110725f
Showing
11 changed files
with
128 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 0 additions & 21 deletions
21
src/plugins/data/server/search/opensearch_search/decide_client.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
// eslint-disable-next-line @osd/eslint/no-restricted-paths | ||
import { coreMock } from '../../../../core/server/mocks'; | ||
// eslint-disable-next-line @osd/eslint/no-restricted-paths | ||
import { RequestHandlerContext } from '../../../../core/server'; | ||
import { IOpenSearchSearchRequest } from '../../../data/common'; | ||
import { decideClient, decideLegacyClient } from './decide_client'; | ||
|
||
const context: RequestHandlerContext = { | ||
core: { | ||
...coreMock.createRequestHandlerContext(), | ||
}, | ||
dataSource: { | ||
opensearch: { | ||
getClient: jest.fn(), | ||
legacy: { | ||
getClient: jest.fn(), | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
describe('decideClient', () => { | ||
const request: IOpenSearchSearchRequest = { | ||
dataSourceId: 'dataSourceId', | ||
}; | ||
|
||
it('should return defaultOpenSearchClient when dataSourceId is not provided', async () => { | ||
const result = await decideClient(context, { ...request, dataSourceId: undefined }); | ||
expect(result).toBe(context.core.opensearch.client.asCurrentUser); | ||
}); | ||
|
||
it('should return defaultOpenSearchClientWithLongNumeralsSupport when withLongNumeralsSupport is true', async () => { | ||
const result = await decideClient(context, { ...request, dataSourceId: undefined }, true); | ||
expect(result).toBe(context.core.opensearch.client.asCurrentUserWithLongNumeralsSupport); | ||
}); | ||
|
||
it('should return client from dataSource when dataSourceId is provided', async () => { | ||
const dataSourceClient = jest.fn(); | ||
(context.dataSource.opensearch.getClient as jest.Mock).mockResolvedValueOnce(dataSourceClient); | ||
|
||
const result = await decideClient(context, request); | ||
expect(result).toBe(dataSourceClient); | ||
expect(context.dataSource.opensearch.getClient).toHaveBeenCalledWith(request.dataSourceId); | ||
}); | ||
}); | ||
|
||
describe('decideLegacyClient', () => { | ||
const request = { | ||
query: { | ||
data_source: 'dataSourceId', | ||
}, | ||
}; | ||
|
||
it('should return callAsCurrentUser when dataSourceId is not provided', async () => { | ||
const result = await decideLegacyClient(context, { ...request, query: {} }); | ||
expect(result).toBe(context.core.opensearch.legacy.client.callAsCurrentUser); | ||
}); | ||
|
||
it('should return legacy client from dataSource when dataSourceId is provided', async () => { | ||
const dataSourceClient = jest.fn(); | ||
(context.dataSource.opensearch.legacy.getClient as jest.Mock).mockReturnValueOnce({ | ||
callAPI: dataSourceClient, | ||
}); | ||
|
||
const result = await decideLegacyClient(context, request); | ||
expect(result).toBe(dataSourceClient); | ||
expect(context.dataSource.opensearch.legacy.getClient).toHaveBeenCalledWith( | ||
request.query.data_source | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
* Any modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
// eslint-disable-next-line @osd/eslint/no-restricted-paths | ||
import { RequestHandlerContext, OpenSearchClient, LegacyAPICaller } from 'src/core/server'; | ||
import { IOpenSearchSearchRequest } from '../../../data/common'; | ||
|
||
export const decideClient = async ( | ||
context: RequestHandlerContext, | ||
request: IOpenSearchSearchRequest, | ||
withLongNumeralsSupport: boolean = false | ||
): Promise<OpenSearchClient> => { | ||
const defaultOpenSearchClient = withLongNumeralsSupport | ||
? context.core.opensearch.client.asCurrentUserWithLongNumeralsSupport | ||
: context.core.opensearch.client.asCurrentUser; | ||
|
||
return request.dataSourceId && context.dataSource | ||
? await context.dataSource.opensearch.getClient(request.dataSourceId) | ||
: defaultOpenSearchClient; | ||
}; | ||
|
||
export const decideLegacyClient = async ( | ||
context: RequestHandlerContext, | ||
request: any | ||
): Promise<LegacyAPICaller> => { | ||
const dataSourceId = request.query.data_source; | ||
return dataSourceId | ||
? (context.dataSource.opensearch.legacy.getClient(dataSourceId).callAPI as LegacyAPICaller) | ||
: context.core.opensearch.legacy.client.callAsCurrentUser; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
export * from './decide_client'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters