diff --git a/public/pages/CreatePolicy/containers/CreatePolicy/CreatePolicy.tsx b/public/pages/CreatePolicy/containers/CreatePolicy/CreatePolicy.tsx index ffb17b759..fb50c7f04 100644 --- a/public/pages/CreatePolicy/containers/CreatePolicy/CreatePolicy.tsx +++ b/public/pages/CreatePolicy/containers/CreatePolicy/CreatePolicy.tsx @@ -3,7 +3,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -import React, { ChangeEvent, Component, Fragment } from "react"; +import React, { ChangeEvent, Component, Fragment, useContext } from "react"; import { EuiSpacer, EuiTitle, EuiFlexGroup, EuiFlexItem, EuiButton, EuiButtonEmpty, EuiCallOut, EuiLink, EuiIcon } from "@elastic/eui"; import queryString from "query-string"; import { RouteComponentProps } from "react-router-dom"; @@ -15,8 +15,10 @@ import { PolicyService } from "../../../../services"; import { BREADCRUMBS, DOCUMENTATION_URL, ROUTES } from "../../../../utils/constants"; import { getErrorMessage } from "../../../../utils/helpers"; import { CoreServicesContext } from "../../../../components/core_services"; +import { DataSourceMenuContext, DataSourceMenuProperties } from "../../../../services/DataSourceMenuContext"; +import { useUpdateUrlWithDataSourceProperties } from "../../../../components/MDSEnabledComponent"; -interface CreatePolicyProps extends RouteComponentProps { +interface CreatePolicyProps extends RouteComponentProps, DataSourceMenuProperties { isEdit: boolean; policyService: PolicyService; } @@ -32,7 +34,7 @@ interface CreatePolicyState { hasSubmitted: boolean; } -export default class CreatePolicy extends Component { +export class CreatePolicy extends Component { static contextType = CoreServicesContext; _isMount: boolean; constructor(props: CreatePolicyProps) { @@ -78,6 +80,22 @@ export default class CreatePolicy extends Component) { + if (prevProps.dataSourceId != this.props.dataSourceId) { + // reset the state, if dataSourceId changes, i.e., clear state + this.setState({ + policySeqNo: null, + policyPrimaryTerm: null, + policyId: "", + policyIdError: "", + submitError: "", + jsonString: DEFAULT_POLICY, + isSubmitting: false, + hasSubmitted: false, + }); + } + } + getPolicyToEdit = async (policyId: string): Promise => { try { const { policyService } = this.props; @@ -250,3 +268,9 @@ export default class CreatePolicy extends Component) { + const dataSourceMenuProperties = useContext(DataSourceMenuContext); + useUpdateUrlWithDataSourceProperties(); + return ; +} diff --git a/public/pages/Main/Main.tsx b/public/pages/Main/Main.tsx index 319567772..30727f3ae 100644 --- a/public/pages/Main/Main.tsx +++ b/public/pages/Main/Main.tsx @@ -141,6 +141,11 @@ const dataSourceEnabledPaths: string[] = [ ROUTES.SPLIT_INDEX, ROUTES.ROLLOVER, ROUTES.INDEX_DETAIL, + ROUTES.INDEX_POLICIES, + ROUTES.MANAGED_INDICES, + ROUTES.CREATE_POLICY, + ROUTES.EDIT_POLICY, + ROUTES.POLICY_DETAILS, ROUTES.INDICES, ROUTES.CREATE_INDEX, ROUTES.ALIASES, @@ -203,6 +208,9 @@ export default class Main extends Component { if (this.props.multiDataSourceEnabled && this.isDataSourceEnabledForPath(pathname)) { services.indexService = new IndexService(http, this.state.dataSourceId, this.props.multiDataSourceEnabled); services.commonService = new CommonService(http, this.state.dataSourceId, this.props.multiDataSourceEnabled); + services.managedIndexService = new ManagedIndexService(http, this.state.dataSourceId, this.props.multiDataSourceEnabled); + services.policyService = new PolicyService(http, this.state.dataSourceId, this.props.multiDataSourceEnabled); + services.notificationService = new NotificationService(http, this.state.dataSourceId, this.props.multiDataSourceEnabled); } return services; } @@ -340,6 +348,8 @@ export default class Main extends Component { ROUTES.SPLIT_INDEX, ROUTES.ROLLOVER, ROUTES.INDEX_DETAIL, + ROUTES.EDIT_POLICY, + ROUTES.POLICY_DETAILS, ROUTES.REINDEX, ]} render={(props) => ( @@ -374,6 +384,10 @@ export default class Main extends Component { ROUTES.CREATE_TEMPLATE, ROUTES.COMPOSABLE_TEMPLATES, ROUTES.CREATE_COMPOSABLE_TEMPLATE, + ROUTES.MANAGED_INDICES, + ROUTES.INDEX_POLICIES, + ROUTES.CREATE_POLICY, + ROUTES.TRANSFORMS, ]} render={() => ( { +export class ManagedIndices extends MDSEnabledComponent { static contextType = CoreServicesContext; columns: EuiTableFieldDataColumnType[]; @@ -76,6 +78,7 @@ export default class ManagedIndices extends Component { @@ -227,7 +247,7 @@ export default class ManagedIndices extends Component => { const { managedIndexService } = this.props; - const serverResponse = await managedIndexService.getDataStreams(); + const serverResponse = await managedIndexService.getDataStreams(undefined); if (!serverResponse.ok) { if (serverResponse.error.startsWith(SECURITY_EXCEPTION_PREFIX)) { this.context.notifications.toasts.addWarning(serverResponse.error); @@ -284,11 +304,12 @@ export default class ManagedIndices extends Component) { + const dataSourceMenuProps = useContext(DataSourceMenuContext); + return ; +} diff --git a/public/pages/Policies/containers/Policies/Policies.tsx b/public/pages/Policies/containers/Policies/Policies.tsx index d36d1f8ee..8e7e97353 100644 --- a/public/pages/Policies/containers/Policies/Policies.tsx +++ b/public/pages/Policies/containers/Policies/Policies.tsx @@ -3,7 +3,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -import React, { Component } from "react"; +import React, { Component, useContext } from "react"; import { RouteComponentProps } from "react-router-dom"; import queryString from "query-string"; import { @@ -34,12 +34,15 @@ import { PolicyService } from "../../../../services"; import { getErrorMessage } from "../../../../utils/helpers"; import ConfirmationModal from "../../../../components/ConfirmationModal"; import { CoreServicesContext } from "../../../../components/core_services"; +import { DataSourceMenuContext, DataSourceMenuProperties } from "../../../../services/DataSourceMenuContext"; +import MDSEnabledComponent from "../../../../components/MDSEnabledComponent"; +import { DataSource } from "src/plugins/data/public"; -interface PoliciesProps extends RouteComponentProps { +interface PoliciesProps extends RouteComponentProps, DataSourceMenuProperties { policyService: PolicyService; } -interface PoliciesState { +interface PoliciesState extends DataSourceMenuProperties { totalPolicies: number; from: number; size: number; @@ -51,7 +54,7 @@ interface PoliciesState { loadingPolicies: boolean; } -export default class Policies extends Component { +export class Policies extends MDSEnabledComponent { static contextType = CoreServicesContext; columns: EuiTableFieldDataColumnType[]; @@ -61,6 +64,7 @@ export default class Policies extends Component { const { from, size, search, sortField, sortDirection } = getURLQueryParams(this.props.location); this.state = { + ...this.state, totalPolicies: 0, from, size, @@ -121,8 +125,16 @@ export default class Policies extends Component { } } - static getQueryObjectFromState({ from, size, search, sortField, sortDirection }: PoliciesState): PoliciesQueryParams { - return { from, size, search, sortField, sortDirection }; + static getQueryObjectFromState({ + from, + size, + search, + sortField, + sortDirection, + dataSourceId, + multiDataSourceEnabled, + }: PoliciesState): PoliciesQueryParams { + return { from, size, search, sortField, sortDirection, ...(multiDataSourceEnabled ? { dataSourceId } : {}) }; } getPolicies = async (): Promise => { @@ -130,7 +142,7 @@ export default class Policies extends Component { try { const { policyService, history } = this.props; const queryObject = Policies.getQueryObjectFromState(this.state); - const queryParamsString = queryString.stringify(queryObject); + const queryParamsString = queryString.stringify({ ...queryObject, dataSourceLabel: this.state.dataSourceLabel }); history.replace({ ...this.props.location, search: queryParamsString }); const getPoliciesResponse = await policyService.getPolicies(queryObject); if (getPoliciesResponse.ok) { @@ -312,3 +324,8 @@ export default class Policies extends Component { ); } } + +export default function (props: Omit) { + const dataSourceMenuProps = useContext(DataSourceMenuContext); + return ; +} diff --git a/public/pages/PolicyDetails/containers/PolicyDetails/PolicyDetails.tsx b/public/pages/PolicyDetails/containers/PolicyDetails/PolicyDetails.tsx index 42bd11f1c..8db46ede5 100644 --- a/public/pages/PolicyDetails/containers/PolicyDetails/PolicyDetails.tsx +++ b/public/pages/PolicyDetails/containers/PolicyDetails/PolicyDetails.tsx @@ -3,7 +3,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -import React, { Component } from "react"; +import React, { Component, useContext } from "react"; import { EuiSpacer, EuiTitle, @@ -33,8 +33,10 @@ import { ContentPanel } from "../../../../components/ContentPanel"; import { convertTemplatesToArray } from "../../../VisualCreatePolicy/utils/helpers"; import CreatePolicyModal from "../../../../components/CreatePolicyModal"; import { ModalConsumer } from "../../../../components/Modal"; +import { DataSourceMenuContext, DataSourceMenuProperties } from "../../../../services/DataSourceMenuContext"; +import { useUpdateUrlWithDataSourceProperties } from "../../../../components/MDSEnabledComponent"; -interface PolicyDetailsProps extends RouteComponentProps { +interface PolicyDetailsProps extends RouteComponentProps, DataSourceMenuProperties { policyService: PolicyService; } @@ -48,7 +50,7 @@ interface PolicyDetailsState { showPerPageOptions: boolean; } -export default class PolicyDetails extends Component { +export class PolicyDetails extends Component { static contextType = CoreServicesContext; constructor(props: PolicyDetailsProps) { super(props); @@ -76,6 +78,19 @@ export default class PolicyDetails extends Component, prevState: Readonly, snapshot?: any): void { + if (prevProps.dataSourceId !== this.props.dataSourceId) { + this.setState({ + policyId: "", + policy: null, + isJSONModalOpen: false, + isDeleteModalVisible: false, + pageIndex: 0, + pageSize: 10, + showPerPageOptions: true, + }); + } + } getPolicy = async (policyId: string): Promise => { try { const { policyService } = this.props; @@ -244,3 +259,9 @@ export default class PolicyDetails extends Component) { + const dataSourceMenuProperties = useContext(DataSourceMenuContext); + useUpdateUrlWithDataSourceProperties(); + return ; +} diff --git a/public/pages/VisualCreatePolicy/containers/VisualCreatePolicy/VisualCreatePolicy.tsx b/public/pages/VisualCreatePolicy/containers/VisualCreatePolicy/VisualCreatePolicy.tsx index 9825dd288..52dccc716 100644 --- a/public/pages/VisualCreatePolicy/containers/VisualCreatePolicy/VisualCreatePolicy.tsx +++ b/public/pages/VisualCreatePolicy/containers/VisualCreatePolicy/VisualCreatePolicy.tsx @@ -3,7 +3,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -import React, { ChangeEvent, Component } from "react"; +import React, { ChangeEvent, Component, useContext } from "react"; import { EuiText, EuiSpacer, EuiTitle, EuiFlexGroup, EuiFlexItem, EuiButton, EuiLink, EuiIcon } from "@elastic/eui"; import { RouteComponentProps } from "react-router-dom"; import queryString from "query-string"; @@ -19,8 +19,11 @@ import CreateState from "../CreateState"; import { getErrorMessage } from "../../../../utils/helpers"; import { getUpdatedPolicy, getUpdatedStates } from "../../utils/helpers"; import ErrorNotification from "../ErrorNotification"; +import { DataSourceMenuContext, DataSourceMenuProperties } from "../../../../services/DataSourceMenuContext"; +import { useUpdateUrlWithDataSourceProperties } from "../../../../components/MDSEnabledComponent"; +import { Data } from "vega"; -interface VisualCreatePolicyProps extends RouteComponentProps { +interface VisualCreatePolicyProps extends RouteComponentProps, DataSourceMenuProperties { isEdit: boolean; policyService: PolicyService; notificationService: NotificationService; @@ -40,7 +43,7 @@ interface VisualCreatePolicyState { errorNotificationJsonString: string; } -export default class VisualCreatePolicy extends Component { +export class VisualCreatePolicy extends Component { static contextType = CoreServicesContext; constructor(props: VisualCreatePolicyProps) { super(props); @@ -301,6 +304,7 @@ export default class VisualCreatePolicy extends Component ) { + const dataSourceMenuProperties = useContext(DataSourceMenuContext); + useUpdateUrlWithDataSourceProperties(); + return ; +} diff --git a/public/services/IndexService.ts b/public/services/IndexService.ts index 49e8baae9..c700872da 100644 --- a/public/services/IndexService.ts +++ b/public/services/IndexService.ts @@ -86,7 +86,7 @@ export default class IndexService extends MDSEnabledClientService { }; }; - applyPolicy = async (indices: string[], policyId: string, queryObject: HttpFetchQuery): Promise> => { + applyPolicy = async (indices: string[], policyId: string, queryObject?: HttpFetchQuery): Promise> => { const body = { indices, policyId }; queryObject = this.patchQueryObjectWithDataSourceId(queryObject); const url = `..${NODE_API.APPLY_POLICY}`; @@ -96,7 +96,7 @@ export default class IndexService extends MDSEnabledClientService { })) as ServerResponse; }; - editRolloverAlias = async (index: string, alias: string, queryObject: HttpFetchQuery): Promise> => { + editRolloverAlias = async (index: string, alias: string, queryObject?: HttpFetchQuery): Promise> => { const body = { index, alias }; queryObject = this.patchQueryObjectWithDataSourceId(queryObject); const url = `..${NODE_API.EDIT_ROLLOVER_ALIAS}`; diff --git a/public/services/MDSEnabledClientService.ts b/public/services/MDSEnabledClientService.ts index 14a4f60f0..c5a2755c4 100644 --- a/public/services/MDSEnabledClientService.ts +++ b/public/services/MDSEnabledClientService.ts @@ -11,7 +11,7 @@ export abstract class MDSEnabledClientService { this.mdsEnabled = mdsEnabled; } - patchQueryObjectWithDataSourceId(queryObject?: HttpFetchQuery) { + patchQueryObjectWithDataSourceId(queryObject?: HttpFetchQuery): HttpFetchQuery | undefined { if (this.mdsEnabled) { queryObject = queryObject || {}; queryObject.dataSourceId = this.dataSourceId; diff --git a/public/services/ManagedIndexService.ts b/public/services/ManagedIndexService.ts index deedec9dc..d36c225bb 100644 --- a/public/services/ManagedIndexService.ts +++ b/public/services/ManagedIndexService.ts @@ -3,7 +3,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -import { HttpSetup } from "opensearch-dashboards/public"; +import { HttpFetchQuery, HttpSetup } from "opensearch-dashboards/public"; import { ChangePolicyResponse, GetDataStreamsResponse, @@ -13,43 +13,52 @@ import { } from "../../server/models/interfaces"; import { ServerResponse } from "../../server/models/types"; import { NODE_API } from "../../utils/constants"; +import { MDSEnabledClientService } from "./MDSEnabledClientService"; -export default class ManagedIndexService { - httpClient: HttpSetup; - - constructor(httpClient: HttpSetup) { - this.httpClient = httpClient; - } - - getManagedIndex = async (managedIndexUuid: string): Promise> => { - const response = (await this.httpClient.get(`..${NODE_API.MANAGED_INDICES}/${managedIndexUuid}`)) as ServerResponse; +export default class ManagedIndexService extends MDSEnabledClientService { + getManagedIndex = async (managedIndexUuid: string, queryObject: HttpFetchQuery | undefined): Promise> => { + let url = `..${NODE_API.MANAGED_INDICES}/${managedIndexUuid}`; + queryObject = this.patchQueryObjectWithDataSourceId(queryObject); + const response = (await this.httpClient.get(url, { query: queryObject })) as ServerResponse; return response; }; - getManagedIndices = async (queryObject: object): Promise> => { + getManagedIndices = async (queryObject: HttpFetchQuery | undefined): Promise> => { let url = `..${NODE_API.MANAGED_INDICES}`; + queryObject = this.patchQueryObjectWithDataSourceId(queryObject); + console.log("queryObject", queryObject); const response = (await this.httpClient.get(url, { query: queryObject })) as ServerResponse; return response; }; - getDataStreams = async (): Promise> => { - const url = `..${NODE_API._DATA_STREAMS}`; - return await this.httpClient.get(url); + getDataStreams = async (queryObject: HttpFetchQuery | undefined): Promise> => { + let url = `..${NODE_API._DATA_STREAMS}`; + queryObject = this.patchQueryObjectWithDataSourceId(queryObject); + const response = (await this.httpClient.get(url, { query: queryObject })) as ServerResponse; + return response; }; - retryManagedIndexPolicy = async (index: string[], state: string | null): Promise> => { + retryManagedIndexPolicy = async ( + index: string[], + state: string | null, + queryObject: HttpFetchQuery | undefined + ): Promise> => { const body = { index, state }; - const response = (await this.httpClient.post(`..${NODE_API.RETRY}`, { body: JSON.stringify(body) })) as ServerResponse< - RetryManagedIndexResponse - >; + queryObject = this.patchQueryObjectWithDataSourceId(queryObject); + const response = (await this.httpClient.post(`..${NODE_API.RETRY}`, { + body: JSON.stringify(body), + query: queryObject, + })) as ServerResponse; return response; }; - removePolicy = async (indices: string[]): Promise> => { + removePolicy = async (indices: string[], queryObject: HttpFetchQuery | undefined): Promise> => { const body = { indices }; - const response = (await this.httpClient.post(`..${NODE_API.REMOVE_POLICY}`, { body: JSON.stringify(body) })) as ServerResponse< - RemovePolicyResponse - >; + queryObject = this.patchQueryObjectWithDataSourceId(queryObject); + const response = (await this.httpClient.post(`..${NODE_API.REMOVE_POLICY}`, { + body: JSON.stringify(body), + query: queryObject, + })) as ServerResponse; return response; }; @@ -57,12 +66,15 @@ export default class ManagedIndexService { indices: string[], policyId: string, state: string | null, - include: object[] + include: object[], + queryObject: HttpFetchQuery | undefined ): Promise> => { const body = { indices, policyId, state, include }; - const response = (await this.httpClient.post(`..${NODE_API.CHANGE_POLICY}`, { body: JSON.stringify(body) })) as ServerResponse< - ChangePolicyResponse - >; + queryObject = this.patchQueryObjectWithDataSourceId(queryObject); + const response = (await this.httpClient.post(`..${NODE_API.CHANGE_POLICY}`, { + body: JSON.stringify(body), + query: queryObject, + })) as ServerResponse; return response; }; } diff --git a/public/services/NotificationService.ts b/public/services/NotificationService.ts index 751f2af44..33e0b50bd 100644 --- a/public/services/NotificationService.ts +++ b/public/services/NotificationService.ts @@ -7,23 +7,20 @@ import { HttpSetup } from "opensearch-dashboards/public"; import { GetChannelsResponse, GetNotificationConfigsResponse } from "../../server/models/interfaces"; import { ServerResponse } from "../../server/models/types"; import { NODE_API } from "../../utils/constants"; +import { MDSEnabledClientService } from "./MDSEnabledClientService"; -export default class NotificationService { - httpClient: HttpSetup; - - constructor(httpClient: HttpSetup) { - this.httpClient = httpClient; - } - +export default class NotificationService extends MDSEnabledClientService { getChannels = async (): Promise> => { let url = `..${NODE_API.CHANNELS}`; - const response = (await this.httpClient.get(url)) as ServerResponse; + const queryObject = this.patchQueryObjectWithDataSourceId(); + const response = (await this.httpClient.get(url, { query: queryObject })) as ServerResponse; return response; }; getChannel = async (channelId: string): Promise> => { let url = `..${NODE_API.CHANNELS}/${channelId}`; - const response = (await this.httpClient.get(url)) as ServerResponse; + const queryObject = this.patchQueryObjectWithDataSourceId(); + const response = (await this.httpClient.get(url, { query: queryObject })) as ServerResponse; return response; }; } diff --git a/public/services/PolicyService.ts b/public/services/PolicyService.ts index 2dae2f4d6..42fcccba1 100644 --- a/public/services/PolicyService.ts +++ b/public/services/PolicyService.ts @@ -3,21 +3,17 @@ * SPDX-License-Identifier: Apache-2.0 */ -import { HttpSetup } from "opensearch-dashboards/public"; +import { HttpFetchError, HttpFetchQuery, HttpSetup } from "opensearch-dashboards/public"; import { GetPoliciesResponse, PutPolicyResponse } from "../../server/models/interfaces"; import { ServerResponse } from "../../server/models/types"; import { NODE_API } from "../../utils/constants"; import { DocumentPolicy, Policy } from "../../models/interfaces"; +import { MDSEnabledClientService } from "./MDSEnabledClientService"; -export default class PolicyService { - httpClient: HttpSetup; - - constructor(httpClient: HttpSetup) { - this.httpClient = httpClient; - } - - getPolicies = async (queryObject: object): Promise> => { +export default class PolicyService extends MDSEnabledClientService { + getPolicies = async (queryObject: HttpFetchQuery | undefined): Promise> => { let url = `..${NODE_API.POLICIES}`; + queryObject = this.patchQueryObjectWithDataSourceId(queryObject); const response = (await this.httpClient.get(url, { query: queryObject })) as ServerResponse; return response; }; @@ -29,7 +25,10 @@ export default class PolicyService { primaryTerm?: number ): Promise> => { let url = `..${NODE_API.POLICIES}/${policyId}`; - const response = (await this.httpClient.put(url, { query: { seqNo, primaryTerm }, body: JSON.stringify(policy) })) as ServerResponse< + let queryObject: HttpFetchQuery | undefined = { seqNo, primaryTerm }; + queryObject = this.patchQueryObjectWithDataSourceId(queryObject); + console.log("PolicyService putPolicy queryObject: ", queryObject); + const response = (await this.httpClient.put(url, { query: queryObject, body: JSON.stringify(policy) })) as ServerResponse< PutPolicyResponse >; return response; @@ -37,13 +36,15 @@ export default class PolicyService { getPolicy = async (policyId: string): Promise> => { const url = `..${NODE_API.POLICIES}/${policyId}`; - const response = (await this.httpClient.get(url)) as ServerResponse; + const queryObject = this.patchQueryObjectWithDataSourceId(); + const response = (await this.httpClient.get(url, { query: queryObject })) as ServerResponse; return response; }; deletePolicy = async (policyId: string): Promise> => { const url = `..${NODE_API.POLICIES}/${policyId}`; - const response = (await this.httpClient.delete(url)) as ServerResponse; + const queryObject = this.patchQueryObjectWithDataSourceId(); + const response = (await this.httpClient.delete(url, { query: queryObject })) as ServerResponse; return response; }; } diff --git a/server/plugin.ts b/server/plugin.ts index f925e3be3..0e52f45df 100644 --- a/server/plugin.ts +++ b/server/plugin.ts @@ -32,6 +32,7 @@ import { import dataStreams from "./routes/dataStreams"; import { NodeServices } from "./models/interfaces"; import { DataSourcePluginSetup } from "../../../src/plugins/data_source/server"; +import { data } from "jquery"; export interface IndexManagementPluginDependencies { dataSource: DataSourcePluginSetup; @@ -49,11 +50,11 @@ export class IndexPatternManagementPlugin implements Plugin { +export async function getIndexToDataStreamMapping(callWithRequest: DataSourceClient): Promise { const [dataStreams] = await getDataStreams(callWithRequest); const mapping: { [indexName: string]: string } = {}; diff --git a/server/services/IndexService.ts b/server/services/IndexService.ts index 767a3374f..e5d0b5994 100644 --- a/server/services/IndexService.ts +++ b/server/services/IndexService.ts @@ -99,7 +99,7 @@ export default class IndexService extends MDSEnabledClientService { actions: "indices:data/write/reindex", }).catch(() => []), callWithRequest("cat.indices", params), - getIndexToDataStreamMapping({ callAsCurrentUser: callWithRequest }), + getIndexToDataStreamMapping(callWithRequest), ]); const formattedTasks: IReindexItem[] = tasks.map( @@ -248,10 +248,10 @@ export default class IndexService extends MDSEnabledClientService { ): Promise>> => { try { const { indices, policyId } = request.body as { indices: string[]; policyId: string }; - const { callAsCurrentUser: callWithRequest } = this.osDriver.asScoped(request); + const callWithRequest = this.getClientBasedOnDataSource(context, request); const params = { index: indices.join(","), body: { policy_id: policyId } }; - const addResponse: AddResponse = await callWithRequest("ism.add", params); + const addResponse: AddResponse = (await callWithRequest("ism.add", params)) as AddResponse; return response.custom({ statusCode: 200, body: { @@ -287,9 +287,9 @@ export default class IndexService extends MDSEnabledClientService { ): Promise>> => { try { const { alias, index } = request.body as { alias: string; index: string }; - const { callAsCurrentUser: callWithRequest } = this.osDriver.asScoped(request); + const callWithRequest = this.getClientBasedOnDataSource(context, request); const params = { index, body: { [Setting.RolloverAlias]: alias } }; - const rollOverResponse = await callWithRequest("indices.putSettings", params); + const rollOverResponse: AcknowledgedResponse = (await callWithRequest("indices.putSettings", params)) as AcknowledgedResponse; return response.custom({ statusCode: 200, body: { diff --git a/server/services/MDSEnabledClientService.ts b/server/services/MDSEnabledClientService.ts index c5bb3404c..e7e9aba9a 100644 --- a/server/services/MDSEnabledClientService.ts +++ b/server/services/MDSEnabledClientService.ts @@ -28,3 +28,5 @@ export abstract class MDSEnabledClientService { } } } + +export type DataSourceClient = ReturnType; diff --git a/server/services/ManagedIndexService.ts b/server/services/ManagedIndexService.ts index f74a85395..ed4335efb 100644 --- a/server/services/ManagedIndexService.ts +++ b/server/services/ManagedIndexService.ts @@ -10,7 +10,6 @@ import { OpenSearchDashboardsRequest, OpenSearchDashboardsResponseFactory, IOpenSearchDashboardsResponse, - ILegacyCustomClusterClient, } from "opensearch-dashboards/server"; import { INDEX } from "../utils/constants"; import { getSearchString, transformManagedIndexMetaData } from "../utils/helpers"; @@ -29,14 +28,9 @@ import { import { ManagedIndicesSort, ServerResponse } from "../models/types"; import { ManagedIndexItem } from "../../models/interfaces"; import { getIndexToDataStreamMapping } from "./DataStreamService"; +import { MDSEnabledClientService } from "./MDSEnabledClientService"; -export default class ManagedIndexService { - osDriver: ILegacyCustomClusterClient; - - constructor(osDriver: ILegacyCustomClusterClient) { - this.osDriver = osDriver; - } - +export default class ManagedIndexService extends MDSEnabledClientService { // TODO: Not finished, need UI page that uses this first getManagedIndex = async ( context: RequestHandlerContext, @@ -46,8 +40,8 @@ export default class ManagedIndexService { try { const { id } = request.params as { id: string }; const params: RequestParams.Get = { id, index: INDEX.OPENDISTRO_ISM_CONFIG }; - const { callAsCurrentUser: callWithRequest } = this.osDriver.asScoped(request); - const results: SearchResponse = await callWithRequest("search", params); + const callWithRequest = this.getClientBasedOnDataSource(context, request); + const results: SearchResponse = (await callWithRequest("search", params)) as SearchResponse; return response.custom({ statusCode: 200, body: { @@ -91,13 +85,13 @@ export default class ManagedIndexService { sortOrder: sortDirection, queryString: getSearchString(terms, indices, dataStreams, showDataStreams), from: from, - size: size + size: size, }; - const { callAsCurrentUser: callWithRequest } = this.osDriver.asScoped(request); + const callWithRequest = this.getClientBasedOnDataSource(context, request); const [explainAllResponse, indexToDataStreamMapping] = await Promise.all([ callWithRequest("ism.explainAll", explainParams) as Promise, - getIndexToDataStreamMapping({ callAsCurrentUser: callWithRequest }), + getIndexToDataStreamMapping(callWithRequest), ]); const managedIndices: ManagedIndexItem[] = []; for (const indexName in explainAllResponse) { @@ -138,7 +132,7 @@ export default class ManagedIndexService { statusCode: 200, body: { ok: true, - response: { managedIndices: managedIndices, totalManagedIndices: totalManagedIndices}, + response: { managedIndices: managedIndices, totalManagedIndices: totalManagedIndices }, }, }); } catch (err) { @@ -215,9 +209,9 @@ export default class ManagedIndexService { state: string | null; include: { state: string }[]; }; - const { callAsCurrentUser: callWithRequest } = this.osDriver.asScoped(request); + const callWithRequest = this.getClientBasedOnDataSource(context, request); const params = { index: indices.join(","), body: { policy_id: policyId, include, state } }; - const changeResponse: RemoveResponse = await callWithRequest("ism.change", params); + const changeResponse: RemoveResponse = (await callWithRequest("ism.change", params)) as RemoveResponse; return response.custom({ statusCode: 200, body: { @@ -252,9 +246,9 @@ export default class ManagedIndexService { ): Promise>> => { try { const { indices } = request.body as { indices: string[] }; - const { callAsCurrentUser: callWithRequest } = this.osDriver.asScoped(request); + const callWithRequest = this.getClientBasedOnDataSource(context, request); const params = { index: indices.join(",") }; - const addResponse: RemoveResponse = await callWithRequest("ism.remove", params); + const addResponse: RemoveResponse = (await callWithRequest("ism.remove", params)) as RemoveResponse; return response.custom({ statusCode: 200, body: { diff --git a/server/services/NotificationService.ts b/server/services/NotificationService.ts index b7d39a37a..7b6005dc2 100644 --- a/server/services/NotificationService.ts +++ b/server/services/NotificationService.ts @@ -13,22 +13,17 @@ import { } from "opensearch-dashboards/server"; import { ServerResponse } from "../models/types"; import { GetChannelsResponse, GetNotificationConfigsResponse } from "../models/interfaces"; +import { MDSEnabledClientService } from "./MDSEnabledClientService"; -export default class NotificationService { - osDriver: ILegacyCustomClusterClient; - - constructor(osDriver: ILegacyCustomClusterClient) { - this.osDriver = osDriver; - } - +export default class NotificationService extends MDSEnabledClientService { getChannels = async ( context: RequestHandlerContext, request: OpenSearchDashboardsRequest, response: OpenSearchDashboardsResponseFactory ): Promise | ResponseError>> => { try { - const { callAsCurrentUser: callWithRequest } = this.osDriver.asScoped(request); - const getChannelsResponse: GetChannelsResponse = await callWithRequest("ism.getChannels"); + const callWithRequest = this.getClientBasedOnDataSource(context, request); + const getChannelsResponse: GetChannelsResponse = (await callWithRequest("ism.getChannels", {})) as GetChannelsResponse; return response.custom({ statusCode: 200, @@ -59,10 +54,10 @@ export default class NotificationService { id: string; }; - const { callAsCurrentUser: callWithRequest } = this.osDriver.asScoped(request); - const getResponse: GetNotificationConfigsResponse = await callWithRequest("ism.getChannel", { + const callWithRequest = this.getClientBasedOnDataSource(context, request); + const getResponse: GetNotificationConfigsResponse = (await callWithRequest("ism.getChannel", { id, - }); + })) as GetNotificationConfigsResponse; return response.custom({ statusCode: 200, diff --git a/server/services/PolicyService.ts b/server/services/PolicyService.ts index 395df38dc..224e078ef 100644 --- a/server/services/PolicyService.ts +++ b/server/services/PolicyService.ts @@ -5,24 +5,18 @@ import _ from "lodash"; import { - ILegacyCustomClusterClient, + IOpenSearchDashboardsResponse, + LegacyCallAPIOptions, OpenSearchDashboardsRequest, OpenSearchDashboardsResponseFactory, - IOpenSearchDashboardsResponse, - ResponseError, RequestHandlerContext, -} from "opensearch-dashboards/server"; +} from "../../../../src/core/server"; import { DeletePolicyParams, DeletePolicyResponse, GetPoliciesResponse, PutPolicyParams, PutPolicyResponse } from "../models/interfaces"; import { PoliciesSort, ServerResponse } from "../models/types"; import { DocumentPolicy, Policy } from "../../models/interfaces"; +import { MDSEnabledClientService } from "./MDSEnabledClientService"; -export default class PolicyService { - osDriver: ILegacyCustomClusterClient; - - constructor(osDriver: ILegacyCustomClusterClient) { - this.osDriver = osDriver; - } - +export default class PolicyService extends MDSEnabledClientService { /** * Calls backend Put Policy API */ @@ -40,8 +34,8 @@ export default class PolicyService { method = "ism.createPolicy"; params = { policyId: id, body: JSON.stringify(request.body) }; } - const { callAsCurrentUser: callWithRequest } = this.osDriver.asScoped(request); - const putPolicyResponse: PutPolicyResponse = await callWithRequest(method, params); + const callWithRequest = this.getClientBasedOnDataSource(context, request); + const putPolicyResponse: PutPolicyResponse = (await callWithRequest(method, params)) as PutPolicyResponse; return response.custom({ statusCode: 200, body: { @@ -72,8 +66,8 @@ export default class PolicyService { try { const { id } = request.params as { id: string }; const params: DeletePolicyParams = { policyId: id }; - const { callAsCurrentUser: callWithRequest } = this.osDriver.asScoped(request); - const deletePolicyResponse: DeletePolicyResponse = await callWithRequest("ism.deletePolicy", params); + const callWithRequest = this.getClientBasedOnDataSource(context, request); + const deletePolicyResponse: DeletePolicyResponse = (await callWithRequest("ism.deletePolicy", params)) as DeletePolicyResponse; if (deletePolicyResponse.result !== "deleted") { return response.custom({ statusCode: 200, @@ -113,7 +107,7 @@ export default class PolicyService { try { const { id } = request.params as { id: string }; const params = { policyId: id }; - const { callAsCurrentUser: callWithRequest } = this.osDriver.asScoped(request); + const callWithRequest = this.getClientBasedOnDataSource(context, request); const getResponse = await callWithRequest("ism.getPolicy", params); const policy = _.get(getResponse, "policy", null); const seqNo = _.get(getResponse, "_seq_no"); @@ -147,9 +141,6 @@ export default class PolicyService { } }; - /** - * Calls backend Get Policy API - */ getPolicies = async ( context: RequestHandlerContext, request: OpenSearchDashboardsRequest, @@ -178,7 +169,7 @@ export default class PolicyService { queryString: search.trim() ? `*${search.trim().split(" ").join("* *")}*` : "*", }; - const { callAsCurrentUser: callWithRequest } = this.osDriver.asScoped(request); + const callWithRequest = this.getClientBasedOnDataSource(context, request); const getResponse = await callWithRequest("ism.getPolicies", params); const policies: DocumentPolicy[] = getResponse.policies.map((p) => ({