diff --git a/src/plugins/data/server/index.ts b/src/plugins/data/server/index.ts index 5fe531729283..289e390070e2 100644 --- a/src/plugins/data/server/index.ts +++ b/src/plugins/data/server/index.ts @@ -147,7 +147,6 @@ export { FieldDescriptor as IndexPatternFieldDescriptor, shouldReadFieldFromDocValues, // used only in logstash_fields fixture FieldDescriptor, - decideClient, } from './index_patterns'; export { diff --git a/src/plugins/data/server/index_patterns/index.ts b/src/plugins/data/server/index_patterns/index.ts index 771aa9c09ab8..b2e832294e41 100644 --- a/src/plugins/data/server/index_patterns/index.ts +++ b/src/plugins/data/server/index_patterns/index.ts @@ -31,4 +31,3 @@ export * from './utils'; export { IndexPatternsFetcher, FieldDescriptor, shouldReadFieldFromDocValues } from './fetcher'; export { IndexPatternsService, IndexPatternsServiceStart } from './index_patterns_service'; -export { decideClient } from './routes'; diff --git a/src/plugins/data/server/index_patterns/routes.ts b/src/plugins/data/server/index_patterns/routes.ts index 8b3c7139ffc0..db24365c94f0 100644 --- a/src/plugins/data/server/index_patterns/routes.ts +++ b/src/plugins/data/server/index_patterns/routes.ts @@ -29,12 +29,9 @@ */ import { schema } from '@osd/config-schema'; -import { - HttpServiceSetup, - LegacyAPICaller, - RequestHandlerContext, -} from 'opensearch-dashboards/server'; +import { HttpServiceSetup, RequestHandlerContext } from 'opensearch-dashboards/server'; import { IndexPatternsFetcher } from './fetcher'; +import { decideLegacyClient } from '../../../data_source/common/util/'; export function registerRoutes(http: HttpServiceSetup) { const parseMetaFields = (metaFields: string | string[]) => { @@ -62,7 +59,7 @@ export function registerRoutes(http: HttpServiceSetup) { }, }, async (context, request, response) => { - const callAsCurrentUser = await decideClient(context, request); + const callAsCurrentUser = await decideLegacyClient(context, request); const indexPatterns = new IndexPatternsFetcher(callAsCurrentUser); const { pattern, meta_fields: metaFields } = request.query; @@ -122,7 +119,7 @@ export function registerRoutes(http: HttpServiceSetup) { }, }, async (context: RequestHandlerContext, request: any, response: any) => { - const callAsCurrentUser = await decideClient(context, request); + const callAsCurrentUser = await decideLegacyClient(context, request); const indexPatterns = new IndexPatternsFetcher(callAsCurrentUser); const { pattern, interval, look_back: lookBack, meta_fields: metaFields } = request.query; @@ -154,13 +151,3 @@ export function registerRoutes(http: HttpServiceSetup) { } ); } - -export const decideClient = async ( - context: RequestHandlerContext, - request: any -): Promise => { - const dataSourceId = request.query.data_source; - return dataSourceId - ? (context.dataSource.opensearch.legacy.getClient(dataSourceId).callAPI as LegacyAPICaller) - : context.core.opensearch.legacy.client.callAsCurrentUser; -}; diff --git a/src/plugins/data/server/search/opensearch_search/decide_client.ts b/src/plugins/data/server/search/opensearch_search/decide_client.ts deleted file mode 100644 index 41e0d5c16277..000000000000 --- a/src/plugins/data/server/search/opensearch_search/decide_client.ts +++ /dev/null @@ -1,21 +0,0 @@ -/* - * Copyright OpenSearch Contributors - * SPDX-License-Identifier: Apache-2.0 - */ - -import { OpenSearchClient, RequestHandlerContext } from 'src/core/server'; -import { IOpenSearchSearchRequest } from '..'; - -export const decideClient = async ( - context: RequestHandlerContext, - request: IOpenSearchSearchRequest, - withLongNumeralsSupport: boolean = false -): Promise => { - 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; -}; diff --git a/src/plugins/data/server/search/opensearch_search/opensearch_search_strategy.ts b/src/plugins/data/server/search/opensearch_search/opensearch_search_strategy.ts index c5c7602bc4f9..94af54ffd18f 100644 --- a/src/plugins/data/server/search/opensearch_search/opensearch_search_strategy.ts +++ b/src/plugins/data/server/search/opensearch_search/opensearch_search_strategy.ts @@ -43,7 +43,7 @@ import { getShardTimeout, shimAbortSignal, } from '..'; -import { decideClient } from './decide_client'; +import { decideClient } from '../../../../data_source/common/util/'; export const opensearchSearchStrategyProvider = ( config$: Observable, diff --git a/src/plugins/data_source/common/util/decide_client.test.ts b/src/plugins/data_source/common/util/decide_client.test.ts new file mode 100644 index 000000000000..d55078333fb9 --- /dev/null +++ b/src/plugins/data_source/common/util/decide_client.test.ts @@ -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 + ); + }); +}); diff --git a/src/plugins/data_source/common/util/decide_client.ts b/src/plugins/data_source/common/util/decide_client.ts new file mode 100644 index 000000000000..f0a507c86214 --- /dev/null +++ b/src/plugins/data_source/common/util/decide_client.ts @@ -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 => { + 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 => { + const dataSourceId = request.query.data_source; + return dataSourceId + ? (context.dataSource.opensearch.legacy.getClient(dataSourceId).callAPI as LegacyAPICaller) + : context.core.opensearch.legacy.client.callAsCurrentUser; +}; diff --git a/src/plugins/data_source/common/util/index.ts b/src/plugins/data_source/common/util/index.ts new file mode 100644 index 000000000000..8322fc1a65c0 --- /dev/null +++ b/src/plugins/data_source/common/util/index.ts @@ -0,0 +1,6 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +export * from './decide_client'; diff --git a/src/plugins/data_source/opensearch_dashboards.json b/src/plugins/data_source/opensearch_dashboards.json index 56a884109364..94c0477b3fc2 100644 --- a/src/plugins/data_source/opensearch_dashboards.json +++ b/src/plugins/data_source/opensearch_dashboards.json @@ -6,5 +6,5 @@ "ui": true, "requiredPlugins": ["opensearchDashboardsUtils"], "optionalPlugins": [], - "extraPublicDirs": ["common/data_sources"] + "extraPublicDirs": ["common/data_sources", "common/util"] } diff --git a/src/plugins/data_source/server/index.ts b/src/plugins/data_source/server/index.ts index b0564dfcedc7..156b76066fbb 100644 --- a/src/plugins/data_source/server/index.ts +++ b/src/plugins/data_source/server/index.ts @@ -24,5 +24,4 @@ export { DataSourcePluginSetup, DataSourcePluginStart, DataSourcePluginRequestContext, - DataSourceError, } from './types'; diff --git a/src/plugins/vis_type_timeseries/server/lib/get_fields.ts b/src/plugins/vis_type_timeseries/server/lib/get_fields.ts index 56a58b43b45e..d01f018d36a4 100644 --- a/src/plugins/vis_type_timeseries/server/lib/get_fields.ts +++ b/src/plugins/vis_type_timeseries/server/lib/get_fields.ts @@ -37,9 +37,9 @@ import { indexPatterns, IndexPatternFieldDescriptor, IndexPatternsFetcher, - decideClient, } from '../../../data/server'; import { ReqFacade } from './search_strategies/strategies/abstract_search_strategy'; +import { decideLegacyClient } from '../../../data_source/common/util/'; export async function getFields( requestContext: RequestHandlerContext, @@ -51,7 +51,7 @@ export async function getFields( // removes the need to refactor many layers of dependencies on "req", and instead just augments the top // level object passed from here. The layers should be refactored fully at some point, but for now // this works and we are still using the New Platform services for these vis data portions. - const client = await decideClient(requestContext, request); + const client = await decideLegacyClient(requestContext, request); const reqFacade: ReqFacade = { requestContext,