Skip to content

Commit

Permalink
init data source service
Browse files Browse the repository at this point in the history
Signed-off-by: Zhongnan Su <szhongna@amazon.com>
  • Loading branch information
zhongnansu committed Aug 11, 2022
1 parent 4a9d4b0 commit 025dcca
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 12 deletions.
21 changes: 15 additions & 6 deletions src/plugins/data_source/server/client/data_source_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

import { Logger, OpenSearchClient, SavedObjectsClientContract } from 'src/core/server';
import { DataSourceService } from '../data_source_service';

/**
* Represents an OpenSearch cluster API client created by the platform.
Expand Down Expand Up @@ -31,16 +32,24 @@ export interface ICustomDataSourceClient extends IDataSourceClient {
close: () => Promise<void>;
}

interface DataSourceClientCtorParams {
dataSourceService: DataSourceService;
logger: Logger;
scopedSavedObjectsClient: SavedObjectsClientContract;
}
// TODO: This needs further implementation. See https://github.com/opensearch-project/OpenSearch-Dashboards/issues/1981
export class DataSourceClient implements ICustomDataSourceClient {
private scopedSavedObjectsClient?: SavedObjectsClientContract;

constructor(logger: Logger) {}
asDataSource!: (dataSourceId: string) => Promise<OpenSearchClient>;
private dataSourceService: DataSourceService;
private log: Logger;
private scopedSavedObjectClient;

public attachScopedSavedObjectsClient(scopedSavedObjectsClient: SavedObjectsClientContract) {
this.scopedSavedObjectsClient = scopedSavedObjectsClient;
constructor(ctorParams: DataSourceClientCtorParams) {
this.dataSourceService = ctorParams.dataSourceService;
this.log = ctorParams.logger;
this.scopedSavedObjectClient = ctorParams.scopedSavedObjectsClient;
}

asDataSource!: (dataSourceId: string) => Promise<OpenSearchClient>;

public async close() {}
}
35 changes: 35 additions & 0 deletions src/plugins/data_source/server/data_source_service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { Logger, OpenSearchClient, SavedObjectsClientContract } from 'src/core/server';
import { DataSourceClient } from './client';
import { IDataSourceService } from './types';

export class DataSourceService implements IDataSourceService {
private openSearchClientsPool: Map<string, OpenSearchClient>;
constructor() {
this.openSearchClientsPool = new Map<string, OpenSearchClient>();
}
// TODO: placeholders, need implement when adding global config
isEnabled(): boolean {
throw new Error('Method not implemented.');
}

getDataSourceClient(logger: Logger, savedObjectClient: SavedObjectsClientContract) {
return new DataSourceClient({
logger,
dataSourceService: this,
scopedSavedObjectsClient: savedObjectClient,
});
}
// TODO: placeholders, need implement client pooling strategy
addOpenSearchClient() {}
getOpenSearchClient(): OpenSearchClient {
throw new Error('Method not implemented.');
}

// TODO: close all data source clients in the clients pool
stop() {}
}
15 changes: 10 additions & 5 deletions src/plugins/data_source/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ import {
} from '../../../../src/core/server';
import { DataSourceClient } from './client/data_source_client';
import { DataSourceRouteHandlerContext } from './data_source_route_handler_context';
import { DataSourceService } from './data_source_service';
import { dataSource, credential } from './saved_objects';

import { DataSourcePluginSetup, DataSourcePluginStart } from './types';

export class DataSourcePlugin implements Plugin<DataSourcePluginSetup, DataSourcePluginStart> {
private readonly logger: Logger;
private readonly dataSourceClient: DataSourceClient;
private dataSourceService?: DataSourceService;

constructor(initializerContext: PluginInitializerContext) {
this.logger = initializerContext.logger.get();
this.dataSourceClient = new DataSourceClient(this.logger);
}

public setup(core: CoreSetup) {
Expand All @@ -36,6 +36,8 @@ export class DataSourcePlugin implements Plugin<DataSourcePluginSetup, DataSourc
// Register data source saved object type
core.savedObjects.registerType(dataSource);

this.dataSourceService = new DataSourceService();

// Register plugin context to route handler context
core.http.registerRouteHandlerContext('data_source', this.createRouteHandlerContext(core));

Expand All @@ -48,16 +50,19 @@ export class DataSourcePlugin implements Plugin<DataSourcePluginSetup, DataSourc
}

public stop() {
this.dataSourceClient.close();
this.dataSourceService!.stop();
}

private createRouteHandlerContext = (
core: CoreSetup
): IContextProvider<RequestHandler<unknown, unknown, unknown>, 'data_source'> => {
return async (context, req) => {
const [{ savedObjects }] = await core.getStartServices();
this.dataSourceClient.attachScopedSavedObjectsClient(savedObjects.getScopedClient(req));
return new DataSourceRouteHandlerContext(this.dataSourceClient, this.logger);
const dataSourceClient = this.dataSourceService!.getDataSourceClient(
this.logger,
savedObjects.getScopedClient(req)
);
return new DataSourceRouteHandlerContext(dataSourceClient, this.logger);
};
};
}
13 changes: 12 additions & 1 deletion src/plugins/data_source/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,19 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { OpenSearchClient } from 'src/core/server';
import { Logger, OpenSearchClient, SavedObjectsClientContract } from 'src/core/server';
import { DataSourceClient } from './client';

export interface IDataSourceService {
isEnabled(): boolean;
getDataSourceClient(
logger: Logger,
savedObjectClient: SavedObjectsClientContract
): DataSourceClient;
addOpenSearchClient(): void;
getOpenSearchClient(): OpenSearchClient;
stop(): void;
}
export interface DataSourcePluginRequestContext {
opensearch: {
getClient: (dataSourceId: string) => Promise<OpenSearchClient>;
Expand Down

0 comments on commit 025dcca

Please sign in to comment.