Skip to content

Commit

Permalink
feat: send data_source_id from client
Browse files Browse the repository at this point in the history
Signed-off-by: suzhou <[email protected]>
  • Loading branch information
SuZhou-Joe committed May 9, 2023
1 parent b91ac6c commit 5a69d95
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
13 changes: 12 additions & 1 deletion public/index_management_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,18 @@ import {
import { DarkModeContext } from "./components/DarkMode";
import Main from "./pages/Main";
import { CoreServicesContext } from "./components/core_services";
import { PLUGIN_NAME } from "./utils/constants";
import { MDSIntercept } from "./utils/MDSIntercept";
import "./app.scss";

export function renderApp(coreStart: CoreStart, params: AppMountParameters, landingPage: string) {
const http = coreStart.http;
const mdsInterceptInstance = new MDSIntercept({
pluginId: PLUGIN_NAME,
http,
getDataSourceId: () => "3e2ff6a0-de64-11ed-b697-57f5dd34beb6",
});
mdsInterceptInstance.start();

const indexService = new IndexService(http);
const managedIndexService = new ManagedIndexService(http);
Expand Down Expand Up @@ -63,5 +71,8 @@ export function renderApp(coreStart: CoreStart, params: AppMountParameters, land
</Router>,
params.element
);
return () => ReactDOM.unmountComponentAtNode(params.element);
return () => {
mdsInterceptInstance.destroy();
ReactDOM.unmountComponentAtNode(params.element);
};
}
4 changes: 2 additions & 2 deletions public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { AppMountParameters, CoreSetup, CoreStart, Plugin, PluginInitializerCont
import { IndexManagementPluginSetup } from ".";
import { IndexManagementPluginStart } from ".";
import { actionRepoSingleton } from "./pages/VisualCreatePolicy/utils/helpers";
import { ROUTES } from "./utils/constants";
import { PLUGIN_NAME, ROUTES } from "./utils/constants";
import { JobHandlerRegister } from "./JobHandler";

export class IndexManagementPlugin implements Plugin<IndexManagementPluginSetup, IndexManagementPluginStart> {
Expand All @@ -18,7 +18,7 @@ export class IndexManagementPlugin implements Plugin<IndexManagementPluginSetup,
public setup(core: CoreSetup): IndexManagementPluginSetup {
JobHandlerRegister(core);
core.application.register({
id: "opensearch_index_management_dashboards",
id: PLUGIN_NAME,
title: "Index Management",
order: 7000,
category: {
Expand Down
26 changes: 26 additions & 0 deletions public/utils/MDSIntercept.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { set } from "lodash";
import { CoreStart, HttpFetchOptionsWithPath } from "opensearch-dashboards/public";

export class MDSIntercept {
private pluginId: string;
private http: CoreStart["http"];
private getDataSourceId: () => string;
private interceptDestroyHandler: (() => void) | undefined;
constructor(config: { pluginId: string; http: CoreStart["http"]; getDataSourceId: () => string }) {
this.pluginId = config.pluginId;
this.http = config.http;
this.getDataSourceId = config.getDataSourceId;
}
private interceptRequest(fetchOptions: HttpFetchOptionsWithPath) {
set(fetchOptions, `headers._${this.pluginId}_data_source_id_`, this.getDataSourceId());
return fetchOptions;
}
public start() {
this.interceptDestroyHandler = this.http.intercept({
request: this.interceptRequest.bind(this),
});
}
public destroy() {
this.interceptDestroyHandler?.();
}
}
7 changes: 4 additions & 3 deletions server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import dataStreams from "./routes/dataStreams";
import { NodeServices } from "./models/interfaces";
import { getClientSupportMDS } from "./client";
import { extendClient } from "./clusters/extend_client";
import { PLUGIN_NAME } from "../public/utils/constants";

export class IndexPatternManagementPlugin implements Plugin<IndexManagementPluginSetup, IndexManagementPluginStart> {
private readonly logger: Logger;
Expand All @@ -50,8 +51,8 @@ export class IndexPatternManagementPlugin implements Plugin<IndexManagementPlugi
const osDriverSupportMDS = getClientSupportMDS({
core,
client: legacyClient,
getDataSourceId() {
return "3e2ff6a0-de64-11ed-b697-57f5dd34beb6";
getDataSourceId(context, request) {
return request?.headers?.[`_${PLUGIN_NAME}_data_source_id_`] as string;
},
onExtendClient(client) {
const finalClient = (client as unknown) as OpenSearchDashboardsClient & { ism?: any };
Expand All @@ -74,7 +75,7 @@ export class IndexPatternManagementPlugin implements Plugin<IndexManagementPlugi
ism,
};
},
pluginId: "opensearch_index_management_dashboards",
pluginId: PLUGIN_NAME,
logger: this.logger,
});

Expand Down

0 comments on commit 5a69d95

Please sign in to comment.