Skip to content

Commit

Permalink
[nodejs client] update types and modify tests in src/core/server/open…
Browse files Browse the repository at this point in the history
…search

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: opensearch-project#1193
Partical Resolved: opensearch-project/opensearch-js#837
Signed-off-by: Anan Zhuang <ananzh@amazon.com>
  • Loading branch information
ananzh committed Feb 1, 2022
1 parent decd7c1 commit 74c324f
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/core/server/opensearch/client/cluster_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
2 changes: 1 addition & 1 deletion src/core/server/opensearch/client/configure_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
1 change: 0 additions & 1 deletion src/core/server/opensearch/client/mocks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ describe('Mocked client', () => {
});

it('nested level API methods should be mocked', () => {
expectMocked(client.asyncSearch.get);
expectMocked(client.nodes.info);
});

Expand Down
4 changes: 2 additions & 2 deletions src/core/server/opensearch/client/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,9 @@ const createErrorTransportRequestPromise = (err: any): MockedTransportRequestPro
return promise as MockedTransportRequestPromise<never>;
};

function createApiResponse(opts: Partial<ApiResponse> = {}): ApiResponse {
function createApiResponse<TResponse = Record<string, any>>(opts: Partial<ApiResponse> = {}): ApiResponse<TResponse> {
return {
body: {},
body: {} as any,
statusCode: 200,
headers: {},
warnings: [],
Expand Down
12 changes: 6 additions & 6 deletions src/core/server/opensearch/client/retry_call_cluster.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);
});

Expand All @@ -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);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -97,7 +98,7 @@ export interface PollOpenSearchNodesVersionOptions {
interface NodeInfo {
version: string;
ip: string;
http: {
http?: {
publish_address: string;
};
name: string;
Expand All @@ -115,6 +116,7 @@ export interface NodesVersionCompatibility {
incompatibleNodes: NodeInfo[];
warningNodes: NodeInfo[];
opensearchDashboardsVersion: string;
nodesInfoRequestError?: Error;
}

function getHumanizedNodeName(node: NodeInfo) {
Expand Down Expand Up @@ -225,8 +227,8 @@ export const pollOpenSearchNodesVersion = ({
})
).pipe(
map(({ body }) => body),
catchError((_err: any) => {
return of({ nodes: {} });
catchError((nodesInfoRequestError: any) => {
return of({ nodes: {}, nodesInfoRequestError});
})
)
)
Expand All @@ -238,8 +240,8 @@ export const pollOpenSearchNodesVersion = ({
})
).pipe(
map(({ body }) => body),
catchError((_err) => {
return of({ nodes: {} });
catchError((nodesInfoRequestError: any) => {
return of({ nodes: {}, nodesInfoRequestError });
})
);
}
Expand Down

0 comments on commit 74c324f

Please sign in to comment.