Skip to content

Commit

Permalink
register data source route handler context
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 10, 2022
1 parent 566e71b commit b236a55
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 3 deletions.
47 changes: 47 additions & 0 deletions src/plugins/data_source/server/client/data_source_client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

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

/**
* Represents an OpenSearch cluster API client created by the platform.
* It allows to call API on behalf of user defined in "data source" saved object
*
* @public
**/
export interface IDataSourceClient {
/**
* Creates a {@link OpenSearchClient } bound to given data source
*/
asDataSource: (dataSourceId: string) => Promise<OpenSearchClient>;
}

/**
* See {@link IDataSourceClient}
*
* @public
*/
export interface ICustomDataSourceClient extends IDataSourceClient {
/**
* Closes the data source client. After that client cannot be used and one should
* create a new client instance to be able to interact with OpenSearch API.
*/
close: () => Promise<void>;
}

// TODO: This needs further implementation. See https://github.com/opensearch-project/OpenSearch-Dashboards/issues/1981
export class DataSourceClient implements ICustomDataSourceClient {
private savedObjectClient?: SavedObjectsClientContract;

constructor(logger: Logger) {}
asDataSource!: (dataSourceId: string) => Promise<OpenSearchClient>;
// asDataSource: (dataSourceId: string) => Promise<OpenSearchClient>;

public attachSavedObjectClient(savedObjectClient: SavedObjectsClientContract) {
this.savedObjectClient = savedObjectClient;
}

public async close() {}
}
6 changes: 6 additions & 0 deletions src/plugins/data_source/server/client/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 { IDataSourceClient, ICustomDataSourceClient, DataSourceClient } from './data_source_client';
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

// eslint-disable-next-line max-classes-per-file
import { Logger } from 'src/core/server';
import { IDataSourceClient } from './client/data_source_client';

class OpenSearchDataSourceRouteHandlerContext {
private logger: Logger;
constructor(private dataSourceClient: IDataSourceClient, logger: Logger) {
this.logger = logger;
}

public async getClient(dataSourceId: string) {
try {
const client = await this.dataSourceClient.asDataSource(dataSourceId);
return client;
} catch (error) {
// TODO: convert as audit log when integrate with osd auditing
this.logger.error(
`Fail to get data source client for dataSource id: [${dataSourceId}]. Detail: ${error.messages}`
);
throw error;
}
}
}

export class DataSourceRouteHandlerContext {
readonly opensearch: OpenSearchDataSourceRouteHandlerContext;

constructor(private readonly dataSourceClient: IDataSourceClient, logger: Logger) {
this.opensearch = new OpenSearchDataSourceRouteHandlerContext(
this.dataSourceClient,
logger.get('opensearch')
);
}
}
33 changes: 30 additions & 3 deletions src/plugins/data_source/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,31 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { PluginInitializerContext, CoreSetup, CoreStart, Plugin, Logger } from 'src/core/server';
import {
PluginInitializerContext,
CoreSetup,
CoreStart,
Plugin,
Logger,
IContextProvider,
RequestHandler,
} from '../../../../src/core/server';
import { DataSourceClient } from './client/data_source_client';
import { DataSourceRouteHandlerContext } from './data_source_route_handler_context';
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;

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

public setup(core: CoreSetup) {
public async setup(core: CoreSetup) {
this.logger.debug('data_source: Setup');

// Register credential saved object type
Expand All @@ -24,6 +36,9 @@ export class DataSourcePlugin implements Plugin<DataSourcePluginSetup, DataSourc
// Register data source saved object type
core.savedObjects.registerType(dataSource);

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

return {};
}

Expand All @@ -32,5 +47,17 @@ export class DataSourcePlugin implements Plugin<DataSourcePluginSetup, DataSourc
return {};
}

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

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

import { OpenSearchClient } from 'src/core/server';

export interface DataSourcePluginRequestContext {
opensearch: {
getClient: (dataSourceId: string) => Promise<OpenSearchClient>;
};
}
declare module 'src/core/server' {
interface RequestHandlerContext {
data_source: DataSourcePluginRequestContext;
}
}

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface DataSourcePluginSetup {}
// eslint-disable-next-line @typescript-eslint/no-empty-interface
Expand Down

0 comments on commit b236a55

Please sign in to comment.