Skip to content

Commit

Permalink
refactoring code for server & appending URL with datasourceId/datasou…
Browse files Browse the repository at this point in the history
…rcelabel

Signed-off-by: Ramakrishna Chilaka <[email protected]>
  • Loading branch information
RamakrishnaChilaka committed Mar 22, 2024
1 parent c208fa9 commit 2c37acf
Show file tree
Hide file tree
Showing 19 changed files with 110 additions and 91 deletions.
4 changes: 2 additions & 2 deletions opensearch_dashboards.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"version": "3.0.0.0",
"opensearchDashboardsVersion": "3.0.0",
"configPath": ["opensearch_index_management"],
"requiredPlugins": ["navigation", "opensearchDashboardsReact", "dataSourceManagement"],
"optionalPlugins": ["managementOverview", "dataSource"],
"requiredPlugins": ["navigation", "opensearchDashboardsReact"],
"optionalPlugins": ["managementOverview", "dataSource", "dataSourceManagement"],
"server": true,
"ui": true
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default class MDSEnabledComponent<
// static members cannot reference class type parameters
if (
nextProps.multiDataSourceEnabled &&
(nextProps.dataSourceId != prevState.dataSourceId || nextProps.dataSourceLabel != prevState.dataSourceLabel)
(nextProps.dataSourceId !== prevState.dataSourceId || nextProps.dataSourceLabel !== prevState.dataSourceLabel)
) {
return {
dataSourceId: nextProps.dataSourceId,
Expand All @@ -38,10 +38,13 @@ export function useUpdateUrlWithDataSourceProperties() {
const dataSourceMenuProps = useContext(DataSourceMenuContext);
const { dataSourceId, dataSourceLabel, multiDataSourceEnabled } = dataSourceMenuProps;
const history = useHistory();
const currentSearch = history.location.search;
const currentQuery = queryString.parse(currentSearch);
useEffect(() => {
if (multiDataSourceEnabled) {
history.replace({
search: queryString.stringify({
...currentQuery,
dataSourceId,
dataSourceLabel,
}),
Expand Down
5 changes: 4 additions & 1 deletion public/index_management_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ import Main from "./pages/Main";
import { CoreServicesContext } from "./components/core_services";
import "./app.scss";
import { AppPluginStartDependencies } from "./types";
import { DataSourceManagementPluginSetup } from "../../../src/plugins/data_source_management/public";

export function renderApp(
coreStart: CoreStart,
pluginStartDependencies: AppPluginStartDependencies,
params: AppMountParameters,
landingPage: string
landingPage: string,
dataSourceManagement: DataSourceManagementPluginSetup
) {
const isDarkMode = coreStart.uiSettings.get("theme:darkMode") || false;

Expand All @@ -32,6 +34,7 @@ export function renderApp(
landingPage={landingPage}
setActionMenu={params.setHeaderActionMenu}
multiDataSourceEnabled={!!pluginStartDependencies.dataSource}
dataSourceManagement={dataSourceManagement}
/>
</CoreServicesContext.Provider>
</DarkModeContext.Provider>
Expand Down
4 changes: 3 additions & 1 deletion public/pages/Main/Main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ import ForceMerge from "../ForceMerge";
import Notifications from "../Notifications";
import ComposableTemplates from "../ComposableTemplates";
import CreateComposableTemplate from "../CreateComposableTemplate";
import { DataSourceMenu } from "../../../../../src/plugins/data_source_management/public";
import { DataSourceMenuContext, DataSourceMenuProperties } from "../../services/DataSourceMenuContext";
import queryString from "query-string";
import { DataSourceManagementPluginSetup } from "../../../../../src/plugins/data_source_management/public";

enum Navigation {
IndexManagement = "Index Management",
Expand Down Expand Up @@ -128,6 +128,7 @@ interface MainProps extends RouteComponentProps {
landingPage: string;
setActionMenu: (menuMount: MountPoint | undefined) => void;
multiDataSourceEnabled: boolean;
dataSourceManagement: DataSourceManagementPluginSetup;
}

interface MainState extends Pick<DataSourceMenuProperties, "dataSourceId" | "dataSourceLabel"> {}
Expand Down Expand Up @@ -310,6 +311,7 @@ export default class Main extends Component<MainProps, MainState> {
const { landingPage } = this.props;

const ROUTE_STYLE = { padding: "25px 25px" };
const DataSourceMenu = this.props.dataSourceManagement.ui.DataSourceMenu;

return (
<CoreServicesConsumer>
Expand Down
2 changes: 1 addition & 1 deletion public/pages/Reindex/container/Reindex/Reindex.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ class Reindex extends Component<ReindexProps, ReindexState> {
}

export default function (props: ReindexProps) {
// in re-index we don't change the data source picker
// in re-index page, user can't change the data source i.e., its in read-only
useUpdateUrlWithDataSourceProperties();
return <Reindex {...props} />;
}
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ export class SplitIndex extends Component<SplitIndexProps> {
export default function SplitIndexWrapper(props: Omit<SplitIndexProps, "commonService" | "coreService">) {
const services = useContext(ServicesContext) as BrowserServices;
const coreService = useContext(CoreServicesContext) as CoreStart;
// in split-index we don't change the data source picker
// in split-index page, user can't change the data source i.e., its in read-only
useUpdateUrlWithDataSourceProperties();
return <SplitIndex {...props} commonService={services.commonService} coreService={coreService} />;
}
8 changes: 5 additions & 3 deletions public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,19 @@ import { actionRepoSingleton } from "./pages/VisualCreatePolicy/utils/helpers";
import { ROUTES } from "./utils/constants";
import { JobHandlerRegister } from "./JobHandler";
import { ManagementOverViewPluginSetup } from "../../../src/plugins/management_overview/public";
import { DataSourceManagementPluginSetup } from "../../../src/plugins/data_source_management/public";

interface IndexManagementSetupDeps {
managementOverview?: ManagementOverViewPluginSetup;
dataSourceManagement?: DataSourceManagementPluginSetup;
}

export class IndexManagementPlugin implements Plugin<IndexManagementPluginSetup, IndexManagementPluginStart, IndexManagementSetupDeps> {
constructor(private readonly initializerContext: PluginInitializerContext) {
// can retrieve config from initializerContext
}

public setup(core: CoreSetup, { managementOverview }: IndexManagementSetupDeps): IndexManagementPluginSetup {
public setup(core: CoreSetup, { managementOverview, dataSourceManagement }: IndexManagementSetupDeps): IndexManagementPluginSetup {
JobHandlerRegister(core);

if (managementOverview) {
Expand Down Expand Up @@ -58,7 +60,7 @@ export class IndexManagementPlugin implements Plugin<IndexManagementPluginSetup,
mount: async (params: AppMountParameters) => {
const { renderApp } = await import("./index_management_app");
const [coreStart, depsStart] = await core.getStartServices();
return renderApp(coreStart, depsStart, params, ROUTES.INDEX_POLICIES);
return renderApp(coreStart, depsStart, params, ROUTES.INDEX_POLICIES, dataSourceManagement);
},
});

Expand All @@ -70,7 +72,7 @@ export class IndexManagementPlugin implements Plugin<IndexManagementPluginSetup,
mount: async (params: AppMountParameters) => {
const { renderApp } = await import("./index_management_app");
const [coreStart, depsStart] = await core.getStartServices();
return renderApp(coreStart, depsStart, params, ROUTES.SNAPSHOT_POLICIES);
return renderApp(coreStart, depsStart, params, ROUTES.SNAPSHOT_POLICIES, dataSourceManagement);
},
});

Expand Down
18 changes: 6 additions & 12 deletions public/services/CommonService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,24 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { HttpFetchOptions, HttpSetup } from "opensearch-dashboards/public";
import { HttpFetchOptions, HttpFetchQuery } from "opensearch-dashboards/public";
import { ServerResponse } from "../../server/models/types";
import { NODE_API } from "../../utils/constants";
import { IAPICaller } from "../../models/interfaces";
import { MDSEnabledClientService } from "./MDSEnabledClientService";

export default class CommonService {
httpClient: HttpSetup;
dataSourceId: string;

constructor(httpClient: HttpSetup, dataSourceId: string = "") {
this.httpClient = httpClient;
this.dataSourceId = dataSourceId;
}

apiCaller = async <T>(params: IAPICaller): Promise<ServerResponse<T>> => {
export default class CommonService extends MDSEnabledClientService {
apiCaller = async <T>(params: IAPICaller, queryObject?: HttpFetchQuery): Promise<ServerResponse<T>> => {
let url = `${NODE_API.API_CALLER}`;
const payload: HttpFetchOptions = {};
queryObject = this.patchQueryObjectWithDataSourceId(queryObject || {});
payload.method = "POST";
params.data.dataSourceId = this.dataSourceId;
payload.body = JSON.stringify({
data: params.data,
endpoint: params.endpoint,
hideLog: params.hideLog,
});
payload.query = queryObject;
return (await this.httpClient.fetch(url, payload)) as ServerResponse<T>;
};
}
53 changes: 21 additions & 32 deletions public/services/IndexService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { HttpFetchQuery, HttpSetup } from "opensearch-dashboards/public";
import { HttpFetchQuery } from "opensearch-dashboards/public";
import {
AcknowledgedResponse,
ApplyPolicyResponse,
Expand All @@ -18,37 +18,24 @@ import { ServerResponse } from "../../server/models/types";
import { NODE_API } from "../../utils/constants";
import { IndexItem } from "../../models/interfaces";
import { SECURITY_EXCEPTION_PREFIX } from "../../server/utils/constants";
import { MDSEnabledClientService } from "./MDSEnabledClientService";

export default class IndexService {
httpClient: HttpSetup;
dataSourceId: string;

constructor(httpClient: HttpSetup, dataSourceId: string = "") {
this.httpClient = httpClient;
this.dataSourceId = dataSourceId;
}

patchQueryObjectWithObjectId(queryObject: HttpFetchQuery) {
queryObject.dataSourceId = this.dataSourceId;
return queryObject;
}

export default class IndexService extends MDSEnabledClientService {
getIndices = async (queryObject: HttpFetchQuery): Promise<ServerResponse<GetIndicesResponse>> => {
let url = `..${NODE_API._INDICES}`;
this.patchQueryObjectWithObjectId(queryObject);
const response = (await this.httpClient.get(url, { query: queryObject })) as ServerResponse<GetIndicesResponse>;
return response;
queryObject = this.patchQueryObjectWithDataSourceId(queryObject);
return (await this.httpClient.get(url, { query: queryObject })) as ServerResponse<GetIndicesResponse>;
};

getDataStreams = async (queryObject: HttpFetchQuery): Promise<ServerResponse<GetDataStreamsResponse>> => {
const url = `..${NODE_API._DATA_STREAMS}`;
this.patchQueryObjectWithObjectId(queryObject);
queryObject = this.patchQueryObjectWithDataSourceId(queryObject);
return await this.httpClient.get(url, { query: queryObject });
};

getAliases = async (queryObject: HttpFetchQuery): Promise<ServerResponse<GetAliasesResponse>> => {
const url = `..${NODE_API._ALIASES}`;
this.patchQueryObjectWithObjectId(queryObject);
queryObject = this.patchQueryObjectWithDataSourceId(queryObject);
return await this.httpClient.get(url, { query: queryObject });
};

Expand Down Expand Up @@ -99,28 +86,30 @@ export default class IndexService {
};
};

applyPolicy = async (indices: string[], policyId: string): Promise<ServerResponse<ApplyPolicyResponse>> => {
applyPolicy = async (indices: string[], policyId: string, queryObject: HttpFetchQuery): Promise<ServerResponse<ApplyPolicyResponse>> => {
const body = { indices, policyId };
this.patchQueryObjectWithObjectId(body);
queryObject = this.patchQueryObjectWithDataSourceId(body);
const url = `..${NODE_API.APPLY_POLICY}`;
const response = (await this.httpClient.post(url, { body: JSON.stringify(body) })) as ServerResponse<ApplyPolicyResponse>;
return response;
return (await this.httpClient.post(url, {
body: JSON.stringify(body),
query: queryObject,
})) as ServerResponse<ApplyPolicyResponse>;
};

editRolloverAlias = async (index: string, alias: string): Promise<ServerResponse<AcknowledgedResponse>> => {
editRolloverAlias = async (index: string, alias: string, queryObject: HttpFetchQuery): Promise<ServerResponse<AcknowledgedResponse>> => {
const body = { index, alias };
this.patchQueryObjectWithObjectId(body);
queryObject = this.patchQueryObjectWithDataSourceId(body);
const url = `..${NODE_API.EDIT_ROLLOVER_ALIAS}`;
const response = (await this.httpClient.post(url, { body: JSON.stringify(body) })) as ServerResponse<AcknowledgedResponse>;
return response;
return (await this.httpClient.post(url, {
body: JSON.stringify(body),
query: queryObject,
})) as ServerResponse<AcknowledgedResponse>;
};

searchPolicies = async (searchValue: string, source: boolean = false): Promise<ServerResponse<GetPoliciesResponse>> => {
const str = searchValue.trim();
const queryObject = { from: 0, size: 10, search: str, sortDirection: "desc", sortField: "id" };
this.patchQueryObjectWithObjectId(queryObject);
const queryObject = this.patchQueryObjectWithDataSourceId({ from: 0, size: 10, search: str, sortDirection: "desc", sortField: "id" });
const url = `..${NODE_API.POLICIES}`;
const response = (await this.httpClient.get(url, { query: queryObject })) as ServerResponse<GetPoliciesResponse>;
return response;
return (await this.httpClient.get(url, { query: queryObject })) as ServerResponse<GetPoliciesResponse>;
};
}
17 changes: 17 additions & 0 deletions public/services/MDSEnabledClientService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { HttpFetchQuery, HttpSetup } from "opensearch-dashboards/public";

export abstract class MDSEnabledClientService {
httpClient: HttpSetup;
dataSourceId: string;

constructor(httpClient: HttpSetup, dataSourceId: string = "") {
this.httpClient = httpClient;
this.dataSourceId = dataSourceId;
}

patchQueryObjectWithDataSourceId(queryObject: HttpFetchQuery) {
queryObject = queryObject || {};
queryObject.dataSourceId = this.dataSourceId;
return queryObject;
}
}
6 changes: 4 additions & 2 deletions server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ export class IndexPatternManagementPlugin implements Plugin<IndexManagementPlugi
aliasService,
};

dataSource.registerCustomApiSchema(ismPlugin);
if (dataSourceEnabled) {
dataSource.registerCustomApiSchema(ismPlugin);
}

// create router
const router = core.http.createRouter();
Expand All @@ -84,7 +86,7 @@ export class IndexPatternManagementPlugin implements Plugin<IndexManagementPlugi
transforms(services, router);
notifications(services, router);
snapshotManagement(services, router);
common(services, router);
common(services, router, dataSourceEnabled);
aliases(services, router, dataSourceEnabled);

return {};
Expand Down
12 changes: 10 additions & 2 deletions server/routes/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,16 @@ import { NodeServices } from "../models/interfaces";
import { NODE_API } from "../../utils/constants";
import { IRouter } from "../../../../src/core/server";

export default function (services: NodeServices, router: IRouter) {
export default function (services: NodeServices, router: IRouter, dataSourceEnabled: boolean = false) {
const { commonService } = services;
let query = schema.object({}, { unknowns: "allow" });

if (dataSourceEnabled) {
query = query.extends({
dataSourceId: schema.string(),
});
}

const payload = {
path: NODE_API.API_CALLER,
validate: {
Expand All @@ -20,7 +28,7 @@ export default function (services: NodeServices, router: IRouter) {
hideLog: schema.nullable(schema.boolean()),
})
),
query: schema.any(),
query: query,
},
};

Expand Down
4 changes: 2 additions & 2 deletions server/routes/dataStreams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { schema } from "@osd/config-schema";
import { schema, Type } from "@osd/config-schema";
import { NodeServices } from "../models/interfaces";
import { NODE_API } from "../../utils/constants";
import { IRouter } from "../../../../src/core/server";

export default function (services: NodeServices, router: IRouter, dataSourceEnabled: boolean = false) {
const { dataStreamService } = services;

let getDataStreamsQuerySchema: any = {
let getDataStreamsQuerySchema: { search: Type<string | undefined>; dataSourceId?: Type<string> } = {
search: schema.maybe(schema.string()),
};
if (dataSourceEnabled) {
Expand Down
Loading

0 comments on commit 2c37acf

Please sign in to comment.