From 74c324f4893c792470fbffd8d2f4f994b92e858b Mon Sep 17 00:00:00 2001 From: Anan Zhuang Date: Fri, 28 Jan 2022 18:58:53 +0000 Subject: [PATCH] [nodejs client] update types and modify tests in src/core/server/opensearch 1. import Client from '@opensearch-project/opensearch/api/new' to hook up the new types 2. remove asynSearch 3. apply type assertion 4. fix undefined and mismatched types Issue Resolved: https://github.com/opensearch-project/OpenSearch-Dashboards/issues/1193 Partical Resolved: https://github.com/opensearch-project/opensearch-js/issues/837 Signed-off-by: Anan Zhuang --- .../server/opensearch/client/cluster_client.ts | 2 +- .../server/opensearch/client/configure_client.ts | 2 +- src/core/server/opensearch/client/mocks.test.ts | 1 - src/core/server/opensearch/client/mocks.ts | 4 ++-- .../opensearch/client/retry_call_cluster.test.ts | 12 ++++++------ .../version_check/ensure_opensearch_version.ts | 14 ++++++++------ 6 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/core/server/opensearch/client/cluster_client.ts b/src/core/server/opensearch/client/cluster_client.ts index 9dfac6daf44b..84cd9e8c6da0 100644 --- a/src/core/server/opensearch/client/cluster_client.ts +++ b/src/core/server/opensearch/client/cluster_client.ts @@ -30,7 +30,7 @@ * GitHub history for details. */ -import { Client } from '@opensearch-project/opensearch'; +import { Client } from '@opensearch-project/opensearch/api/new'; import { Logger } from '../../logging'; import { GetAuthHeaders, Headers, isOpenSearchDashboardsRequest, isRealRequest } from '../../http'; import { ensureRawRequest, filterHeaders } from '../../http/router'; diff --git a/src/core/server/opensearch/client/configure_client.ts b/src/core/server/opensearch/client/configure_client.ts index 792daff56cf5..39a982d0eef6 100644 --- a/src/core/server/opensearch/client/configure_client.ts +++ b/src/core/server/opensearch/client/configure_client.ts @@ -31,7 +31,7 @@ */ import { Buffer } from 'buffer'; import { stringify } from 'querystring'; -import { Client } from '@opensearch-project/opensearch'; +import { Client } from '@opensearch-project/opensearch/api/new'; import { RequestBody } from '@opensearch-project/opensearch/lib/Transport'; import { Logger } from '../../logging'; diff --git a/src/core/server/opensearch/client/mocks.test.ts b/src/core/server/opensearch/client/mocks.test.ts index d36b06bf2408..e07ba3b0580a 100644 --- a/src/core/server/opensearch/client/mocks.test.ts +++ b/src/core/server/opensearch/client/mocks.test.ts @@ -54,7 +54,6 @@ describe('Mocked client', () => { }); it('nested level API methods should be mocked', () => { - expectMocked(client.asyncSearch.get); expectMocked(client.nodes.info); }); diff --git a/src/core/server/opensearch/client/mocks.ts b/src/core/server/opensearch/client/mocks.ts index 9d770b1c615b..aba8dc63df6d 100644 --- a/src/core/server/opensearch/client/mocks.ts +++ b/src/core/server/opensearch/client/mocks.ts @@ -176,9 +176,9 @@ const createErrorTransportRequestPromise = (err: any): MockedTransportRequestPro return promise as MockedTransportRequestPromise; }; -function createApiResponse(opts: Partial = {}): ApiResponse { +function createApiResponse>(opts: Partial = {}): ApiResponse { return { - body: {}, + body: {} as any, statusCode: 200, headers: {}, warnings: [], diff --git a/src/core/server/opensearch/client/retry_call_cluster.test.ts b/src/core/server/opensearch/client/retry_call_cluster.test.ts index fce4d3228462..18f858ba257b 100644 --- a/src/core/server/opensearch/client/retry_call_cluster.test.ts +++ b/src/core/server/opensearch/client/retry_call_cluster.test.ts @@ -30,12 +30,12 @@ * GitHub history for details. */ -import { errors } from '@opensearch-project/opensearch'; +import { errors } from '@opensearch-project/opensearch/api/new'; import { opensearchClientMock } from './mocks'; import { loggingSystemMock } from '../../logging/logging_system.mock'; import { retryCallCluster, migrationRetryCallCluster } from './retry_call_cluster'; -const dummyBody = { foo: 'bar' }; +const dummyBody: any = { foo: 'bar' }; const createErrorReturn = (err: any) => opensearchClientMock.createErrorTransportRequestPromise(err); @@ -51,9 +51,9 @@ describe('retryCallCluster', () => { ...dummyBody, }); - client.asyncSearch.get.mockReturnValue(successReturn); + client.search.mockReturnValue(successReturn); - const result = await retryCallCluster(() => client.asyncSearch.get()); + const result = await retryCallCluster(() => client.search()); expect(result.body).toEqual(dummyBody); }); @@ -62,13 +62,13 @@ describe('retryCallCluster', () => { ...dummyBody, }); - client.asyncSearch.get + client.search .mockImplementationOnce(() => createErrorReturn(new errors.NoLivingConnectionsError('no living connections', {} as any)) ) .mockImplementationOnce(() => successReturn); - const result = await retryCallCluster(() => client.asyncSearch.get()); + const result = await retryCallCluster(() => client.search()); expect(result.body).toEqual(dummyBody); }); diff --git a/src/core/server/opensearch/version_check/ensure_opensearch_version.ts b/src/core/server/opensearch/version_check/ensure_opensearch_version.ts index 6850f2373b71..4e3dcb76308a 100644 --- a/src/core/server/opensearch/version_check/ensure_opensearch_version.ts +++ b/src/core/server/opensearch/version_check/ensure_opensearch_version.ts @@ -44,6 +44,7 @@ import { } from './opensearch_opensearch_dashboards_version_compatability'; import { Logger } from '../../logging'; import type { OpenSearchClient } from '../client'; +import { ApiResponse } from '@opensearch-project/opensearch'; /** * Checks if all nodes in the cluster have the same cluster id node attribute @@ -63,7 +64,7 @@ export const getNodeId = async ( try { const state = await internalClient.cluster.state({ filter_path: [`nodes.*.attributes.${healthcheckAttributeName}`], - }); + }) as ApiResponse; /* Aggregate different cluster_ids from the OpenSearch nodes * if all the nodes have the same cluster_id, retrieve nodes.info from _local node only * Using _cluster/state/nodes to retrieve the cluster_id of each node from master node which is considered to be a lightweight operation @@ -97,7 +98,7 @@ export interface PollOpenSearchNodesVersionOptions { interface NodeInfo { version: string; ip: string; - http: { + http?: { publish_address: string; }; name: string; @@ -115,6 +116,7 @@ export interface NodesVersionCompatibility { incompatibleNodes: NodeInfo[]; warningNodes: NodeInfo[]; opensearchDashboardsVersion: string; + nodesInfoRequestError?: Error; } function getHumanizedNodeName(node: NodeInfo) { @@ -225,8 +227,8 @@ export const pollOpenSearchNodesVersion = ({ }) ).pipe( map(({ body }) => body), - catchError((_err: any) => { - return of({ nodes: {} }); + catchError((nodesInfoRequestError: any) => { + return of({ nodes: {}, nodesInfoRequestError}); }) ) ) @@ -238,8 +240,8 @@ export const pollOpenSearchNodesVersion = ({ }) ).pipe( map(({ body }) => body), - catchError((_err) => { - return of({ nodes: {} }); + catchError((nodesInfoRequestError: any) => { + return of({ nodes: {}, nodesInfoRequestError }); }) ); }