Skip to content

Commit

Permalink
[nodejs client] hookup js client with dashboards
Browse files Browse the repository at this point in the history
1.change @elastic/elasticsearch to @opensearch-project/opensearch
2.add path in tsconfig.base.json to let nodejs types point to api/new.d.ts
which is the new defined types
3.fix hookup issues in src/core/server/opensearch
4.fix hookup issues in src/core/server/core_usage_data_service.ts
5.fix hookup issues in src/core/server/saved_objects

Issue Resolved: opensearch-project#1193
Issue Resolved: opensearch-project#1224
Issue Resolved: opensearch-project#1216
Partical Resolved: opensearch-project#837

Signed-off-by: Anan Zhuang <ananzh@amazon.com>
  • Loading branch information
ananzh committed Mar 12, 2022
1 parent f7f9c22 commit 46d160e
Show file tree
Hide file tree
Showing 61 changed files with 350 additions and 654 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@
},
"dependencies": {
"@elastic/datemath": "5.0.3",
"@elastic/elasticsearch": "7.10.0-rc.1",
"@elastic/eui": "29.3.2",
"@elastic/good": "^9.0.1-kibana3",
"@elastic/numeral": "^2.5.0",
Expand All @@ -122,6 +121,7 @@
"@hapi/podium": "^4.1.3",
"@hapi/vision": "^6.1.0",
"@hapi/wreck": "^17.1.0",
"@opensearch-project/opensearch": "^1.0.2",
"@osd/ace": "1.0.0",
"@osd/analytics": "1.0.0",
"@osd/apm-config-loader": "1.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/osd-opensearch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"osd:watch": "node scripts/build --watch"
},
"dependencies": {
"@elastic/elasticsearch": "7.10.0-rc.1",
"@opensearch-project/opensearch": "^1.0.2",
"@osd/dev-utils": "1.0.0",
"abort-controller": "^3.0.0",
"chalk": "^4.1.0",
Expand Down
10 changes: 5 additions & 5 deletions src/core/public/public.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
```ts

import { Action } from 'history';
import { ApiResponse } from '@elastic/elasticsearch/lib/Transport';
import { ApiResponse } from '@opensearch-project/opensearch/lib/Transport';
import Boom from '@hapi/boom';
import { ConfigPath } from '@osd/config';
import { EnvironmentMode } from '@osd/config';
Expand All @@ -17,7 +17,7 @@ import { ExclusiveUnion } from '@elastic/eui';
import { History } from 'history';
import { Href } from 'history';
import { IconType } from '@elastic/eui';
import { KibanaClient } from '@elastic/elasticsearch/api/kibana';
import { OpenSearchDashboardsClient } from '@opensearch-project/opensearch/api/opensearch_dashboards';
import { Location } from 'history';
import { LocationDescriptorObject } from 'history';
import { Logger } from '@osd/logging';
Expand All @@ -33,9 +33,9 @@ import React from 'react';
import { RecursiveReadonly } from '@osd/utility-types';
import * as Rx from 'rxjs';
import { ShallowPromise } from '@osd/utility-types';
import { TransportRequestOptions } from '@elastic/elasticsearch/lib/Transport';
import { TransportRequestParams } from '@elastic/elasticsearch/lib/Transport';
import { TransportRequestPromise } from '@elastic/elasticsearch/lib/Transport';
import { TransportRequestOptions } from '@opensearch-project/opensearch/lib/Transport';
import { TransportRequestParams } from '@opensearch-project/opensearch/lib/Transport';
import { TransportRequestPromise } from '@opensearch-project/opensearch/lib/Transport';
import { Type } from '@osd/config-schema';
import { TypeOf } from '@osd/config-schema';
import { UnregisterCallback } from 'history';
Expand Down
10 changes: 6 additions & 4 deletions src/core/server/core_usage_data/core_usage_data_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,12 @@ export class CoreUsageDataService implements CoreService<void, CoreUsageDataStar
index,
this.opensearchDashboardsConfig!.index
),
docsCount: stats['docs.count'],
docsDeleted: stats['docs.deleted'],
storeSizeBytes: stats['store.size'],
primaryStoreSizeBytes: stats['pri.store.size'],
docsCount: stats['docs.count'] ? parseInt(stats['docs.count'], 10) : 0,
docsDeleted: stats['docs.deleted'] ? parseInt(stats['docs.deleted'], 10) : 0,
storeSizeBytes: stats['store.size'] ? parseInt(stats['store.size'], 10) : 0,
primaryStoreSizeBytes: stats['pri.store.size']
? parseInt(stats['pri.store.size'], 10)
: 0,
};
});
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ jest.doMock('elasticsearch', () => {
});

export const MockOpenSearchClient = jest.fn();
jest.doMock('@elastic/elasticsearch', () => {
const real = jest.requireActual('@elastic/elasticsearch');
jest.doMock('@opensearch-project/opensearch', () => {
const real = jest.requireActual('@opensearch-project/opensearch');
return {
...real,
Client: MockOpenSearchClient,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import { errors as opensearchErrors } from 'elasticsearch';
import { LegacyOpenSearchErrorHelpers } from '../../opensearch/legacy';

import { opensearchClientMock } from '../../opensearch/client/mocks';
import { ResponseError } from '@elastic/elasticsearch/lib/errors';
import { ResponseError } from '@opensearch-project/opensearch/lib/errors';
import * as osdTestServer from '../../../test_helpers/osd_server';
import { InternalOpenSearchServiceStart } from '../../opensearch';

Expand Down
2 changes: 0 additions & 2 deletions src/core/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,6 @@ export {
SavedObjectsRepository,
SavedObjectsDeleteByNamespaceOptions,
SavedObjectsIncrementCounterOptions,
SavedObjectsComplexFieldMapping,
SavedObjectsCoreFieldMapping,
SavedObjectsFieldMapping,
SavedObjectsTypeMappingDefinition,
SavedObjectsMappingProperties,
Expand Down
2 changes: 1 addition & 1 deletion src/core/server/opensearch/client/client_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import { ConnectionOptions as TlsConnectionOptions } from 'tls';
import { URL } from 'url';
import { Duration } from 'moment';
import { ClientOptions, NodeOptions } from '@elastic/elasticsearch';
import { ClientOptions, NodeOptions } from '@opensearch-project/opensearch';
import { OpenSearchConfig } from '../opensearch_config';
import { DEFAULT_HEADERS } from '../default_headers';

Expand Down
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 '@elastic/elasticsearch';
import { Client } from '@opensearch-project/opensearch';
import { Logger } from '../../logging';
import { GetAuthHeaders, Headers, isOpenSearchDashboardsRequest, isRealRequest } from '../../http';
import { ensureRawRequest, filterHeaders } from '../../http/router';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ jest.doMock('./client_config', () => ({
}));

export const ClientMock = jest.fn();
jest.doMock('@elastic/elasticsearch', () => {
const actual = jest.requireActual('@elastic/elasticsearch');
jest.doMock('@opensearch-project/opensearch', () => {
const actual = jest.requireActual('@opensearch-project/opensearch');
return {
...actual,
Client: ClientMock,
Expand Down
6 changes: 3 additions & 3 deletions src/core/server/opensearch/client/configure_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
import { Buffer } from 'buffer';
import { Readable } from 'stream';

import { RequestEvent, errors } from '@elastic/elasticsearch';
import { TransportRequestParams, RequestBody } from '@elastic/elasticsearch/lib/Transport';
import { RequestEvent, errors } from '@opensearch-project/opensearch';
import { TransportRequestParams, RequestBody } from '@opensearch-project/opensearch/lib/Transport';

import { parseClientOptionsMock, ClientMock } from './configure_client.test.mocks';
import { loggingSystemMock } from '../../logging/logging_system.mock';
Expand Down Expand Up @@ -131,7 +131,7 @@ describe('configureClient', () => {
});

describe('Client logging', () => {
it('logs error when the client emits an @elastic/elasticsearch error', () => {
it('logs error when the client emits an @opensearch-project/opensearch error', () => {
const client = configureClient(config, { logger, scoped: false });

const response = createApiResponse({ body: {} });
Expand Down
4 changes: 2 additions & 2 deletions src/core/server/opensearch/client/configure_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
*/
import { Buffer } from 'buffer';
import { stringify } from 'querystring';
import { Client } from '@elastic/elasticsearch';
import { RequestBody } from '@elastic/elasticsearch/lib/Transport';
import { Client } from '@opensearch-project/opensearch';
import { RequestBody } from '@opensearch-project/opensearch/lib/Transport';

import { Logger } from '../../logging';
import { parseClientOptions, OpenSearchClientConfig } from './client_config';
Expand Down
4 changes: 2 additions & 2 deletions src/core/server/opensearch/client/errors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ import {
ResponseError,
ConnectionError,
ConfigurationError,
} from '@elastic/elasticsearch/lib/errors';
import { ApiResponse } from '@elastic/elasticsearch';
} from '@opensearch-project/opensearch/lib/errors';
import { ApiResponse } from '@opensearch-project/opensearch';
import { isResponseError, isUnauthorizedError } from './errors';

const createApiResponseError = ({
Expand Down
2 changes: 1 addition & 1 deletion src/core/server/opensearch/client/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* GitHub history for details.
*/

import { ResponseError } from '@elastic/elasticsearch/lib/errors';
import { ResponseError } from '@opensearch-project/opensearch/lib/errors';

export type UnauthorizedError = ResponseError & {
statusCode: 401;
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
12 changes: 7 additions & 5 deletions src/core/server/opensearch/client/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/
import { Client, ApiResponse } from '@elastic/elasticsearch';
import { TransportRequestPromise } from '@elastic/elasticsearch/lib/Transport';
import { Client, ApiResponse } from '@opensearch-project/opensearch';
import { TransportRequestPromise } from '@opensearch-project/opensearch/lib/Transport';
import { OpenSearchClient } from './types';
import { ICustomClusterClient } from './cluster_client';

Expand Down Expand Up @@ -63,7 +63,7 @@ const createInternalClientMock = (): DeeplyMockedKeys<Client> => {
};

const mockify = (obj: Record<string, any>, omitted: string[] = []) => {
// the @elastic/elasticsearch::Client uses prototypical inheritance
// the @opensearch-project/opensearch::Client uses prototypical inheritance
// so we have to crawl up the prototype chain and get all descriptors
// to find everything that we should be mocking
const descriptors = getAllPropertyDescriptors(obj);
Expand Down Expand Up @@ -176,9 +176,11 @@ 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
Loading

0 comments on commit 46d160e

Please sign in to comment.