Skip to content

Commit

Permalink
Move decideClient and decideLegacy client to data source plugin commo…
Browse files Browse the repository at this point in the history
…n util (#7079) (#7138)

(cherry picked from commit ddf1a41)

Signed-off-by: Zhongnan Su <[email protected]>
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
1 parent 353f509 commit 91e2ce0
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 45 deletions.
1 change: 0 additions & 1 deletion src/plugins/data/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ export {
FieldDescriptor as IndexPatternFieldDescriptor,
shouldReadFieldFromDocValues, // used only in logstash_fields fixture
FieldDescriptor,
decideClient,
} from './index_patterns';

export {
Expand Down
1 change: 0 additions & 1 deletion src/plugins/data/server/index_patterns/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,3 @@
export * from './utils';
export { IndexPatternsFetcher, FieldDescriptor, shouldReadFieldFromDocValues } from './fetcher';
export { IndexPatternsService, IndexPatternsServiceStart } from './index_patterns_service';
export { decideClient } from './routes';
21 changes: 4 additions & 17 deletions src/plugins/data/server/index_patterns/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[]) => {
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -154,13 +151,3 @@ export function registerRoutes(http: HttpServiceSetup) {
}
);
}

export const decideClient = 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;
};
21 changes: 0 additions & 21 deletions src/plugins/data/server/search/opensearch_search/decide_client.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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<SharedGlobalConfig>,
Expand Down
76 changes: 76 additions & 0 deletions src/plugins/data_source/common/util/decide_client.test.ts
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
);
});
});
38 changes: 38 additions & 0 deletions src/plugins/data_source/common/util/decide_client.ts
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;
};
6 changes: 6 additions & 0 deletions src/plugins/data_source/common/util/index.ts
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';
2 changes: 1 addition & 1 deletion src/plugins/data_source/opensearch_dashboards.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
"ui": true,
"requiredPlugins": ["opensearchDashboardsUtils"],
"optionalPlugins": [],
"extraPublicDirs": ["common/data_sources"]
"extraPublicDirs": ["common/data_sources", "common/util"]
}
1 change: 0 additions & 1 deletion src/plugins/data_source/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,4 @@ export {
DataSourcePluginSetup,
DataSourcePluginStart,
DataSourcePluginRequestContext,
DataSourceError,
} from './types';
4 changes: 2 additions & 2 deletions src/plugins/vis_type_timeseries/server/lib/get_fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 91e2ce0

Please sign in to comment.