-
Notifications
You must be signed in to change notification settings - Fork 892
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
register data source route handler context
Signed-off-by: Zhongnan Su <szhongna@amazon.com>
- Loading branch information
1 parent
566e71b
commit b236a55
Showing
5 changed files
with
135 additions
and
3 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
src/plugins/data_source/server/client/data_source_client.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
39 changes: 39 additions & 0 deletions
39
src/plugins/data_source/server/data_source_route_handler_context.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters