From c54b96e5c688966909a00823e4c1a15639a0600e Mon Sep 17 00:00:00 2001 From: Ajay Gupta Date: Mon, 10 Apr 2023 12:13:32 +0530 Subject: [PATCH] pit table --- config/opensearch_dashboards.yml | 13 +- package.json | 2 +- .../opensearch_dashboards.json | 16 +- .../public/components/breadcrumbs.ts | 17 ++ .../__snapshots__/empty_state.test.tsx.snap | 5 + .../public/components/index.ts | 2 +- .../__snapshots__/empty_state.test.tsx.snap | 32 +++ .../empty_state/empty_state.test.tsx | 16 ++ .../pit_table/empty_state/empty_state.tsx | 29 +++ .../components/pit_table/empty_state/index.ts | 6 + .../public/components/pit_table/index.ts | 6 + .../__snapshots__/page_header.test.tsx.snap | 46 ++++ .../components/pit_table/page_header/index.ts | 6 + .../page_header/page_header.test.tsx | 16 ++ .../pit_table/page_header/page_header.tsx | 39 +++ .../public/components/pit_table/pit_table.tsx | 230 ++++++++++++++++++ .../public/components/utils.ts | 30 +++ .../mount_management_section.tsx | 17 +- .../point_in_time_management/public/plugin.ts | 8 +- .../public/services.ts | 27 ++ .../point_in_time_management/public/types.ts | 8 +- .../point_in_time_management/server/plugin.ts | 4 +- .../server/routes/index.ts | 18 +- .../server/routes/opensearch.ts | 121 +++++++++ yarn.lock | 16 ++ 25 files changed, 695 insertions(+), 35 deletions(-) create mode 100644 src/plugins/point_in_time_management/public/components/breadcrumbs.ts create mode 100644 src/plugins/point_in_time_management/public/components/pit_table/empty_state/__snapshots__/empty_state.test.tsx.snap create mode 100644 src/plugins/point_in_time_management/public/components/pit_table/empty_state/empty_state.test.tsx create mode 100644 src/plugins/point_in_time_management/public/components/pit_table/empty_state/empty_state.tsx create mode 100644 src/plugins/point_in_time_management/public/components/pit_table/empty_state/index.ts create mode 100644 src/plugins/point_in_time_management/public/components/pit_table/index.ts create mode 100644 src/plugins/point_in_time_management/public/components/pit_table/page_header/__snapshots__/page_header.test.tsx.snap create mode 100644 src/plugins/point_in_time_management/public/components/pit_table/page_header/index.ts create mode 100644 src/plugins/point_in_time_management/public/components/pit_table/page_header/page_header.test.tsx create mode 100644 src/plugins/point_in_time_management/public/components/pit_table/page_header/page_header.tsx create mode 100644 src/plugins/point_in_time_management/public/components/pit_table/pit_table.tsx create mode 100644 src/plugins/point_in_time_management/public/components/utils.ts create mode 100644 src/plugins/point_in_time_management/public/services.ts create mode 100644 src/plugins/point_in_time_management/server/routes/opensearch.ts diff --git a/config/opensearch_dashboards.yml b/config/opensearch_dashboards.yml index 6bb4f63bbbde..7a7667a6d43f 100644 --- a/config/opensearch_dashboards.yml +++ b/config/opensearch_dashboards.yml @@ -231,9 +231,14 @@ # Set the value of this setting to true to enable the experimental multiple data source # support feature. Use with caution. -#data_source.enabled: false +data_source.enabled: false # Set the value of these settings to customize crypto materials to encryption saved credentials # in data sources. -#data_source.encryption.wrappingKeyName: 'changeme' -#data_source.encryption.wrappingKeyNamespace: 'changeme' -#data_source.encryption.wrappingKey: [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] +data_source.encryption.wrappingKeyName: 'changeme' +data_source.encryption.wrappingKeyNamespace: 'changeme' +data_source.encryption.wrappingKey: [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] +pit.enabled: true +opensearch.hosts: ["http://localhost:9200"] +opensearch.username: "admin" # Default username on the docker image +opensearch.password: "admin" # Default password on the docker image +opensearch.ssl.verificationMode: none \ No newline at end of file diff --git a/package.json b/package.json index 1e96cc9bdda5..d74a34384a85 100644 --- a/package.json +++ b/package.json @@ -134,7 +134,7 @@ "@hapi/podium": "^4.1.3", "@hapi/vision": "^6.1.0", "@hapi/wreck": "^17.1.0", - "@opensearch-project/opensearch": "^2.1.0", + "@opensearch-project/opensearch": "^2.2.0", "@osd/ace": "1.0.0", "@osd/analytics": "1.0.0", "@osd/apm-config-loader": "1.0.0", diff --git a/src/plugins/point_in_time_management/opensearch_dashboards.json b/src/plugins/point_in_time_management/opensearch_dashboards.json index 1757278f19a6..c369a1595e58 100644 --- a/src/plugins/point_in_time_management/opensearch_dashboards.json +++ b/src/plugins/point_in_time_management/opensearch_dashboards.json @@ -4,7 +4,15 @@ "opensearchDashboardsVersion": "opensearchDashboards", "server": true, "ui": true, - "requiredPlugins": ["navigation", "management"], - "optionalPlugins": [], - "requiredBundles": ["opensearchDashboardsReact"] -} + "requiredPlugins": [ + "navigation", + "management", + "data" + ], + "optionalPlugins": [ + "dataSource" + ], + "requiredBundles": [ + "opensearchDashboardsReact" + ] +} \ No newline at end of file diff --git a/src/plugins/point_in_time_management/public/components/breadcrumbs.ts b/src/plugins/point_in_time_management/public/components/breadcrumbs.ts new file mode 100644 index 000000000000..c20af07652b9 --- /dev/null +++ b/src/plugins/point_in_time_management/public/components/breadcrumbs.ts @@ -0,0 +1,17 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +import { i18n } from '@osd/i18n'; + +export function getListBreadcrumbs() { + return [ + { + text: i18n.translate('pitManagement.listBreadcrumb', { + defaultMessage: 'Point in time', + }), + href: `/`, + }, + ]; +} diff --git a/src/plugins/point_in_time_management/public/components/empty_state/__snapshots__/empty_state.test.tsx.snap b/src/plugins/point_in_time_management/public/components/empty_state/__snapshots__/empty_state.test.tsx.snap index cc1ee347208f..43c1e58e1acc 100644 --- a/src/plugins/point_in_time_management/public/components/empty_state/__snapshots__/empty_state.test.tsx.snap +++ b/src/plugins/point_in_time_management/public/components/empty_state/__snapshots__/empty_state.test.tsx.snap @@ -1,3 +1,8 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`EmptyState should render normally 1`] = ` diff --git a/src/plugins/point_in_time_management/public/components/index.ts b/src/plugins/point_in_time_management/public/components/index.ts index c903066cdbb0..2cdabdd2c047 100644 --- a/src/plugins/point_in_time_management/public/components/index.ts +++ b/src/plugins/point_in_time_management/public/components/index.ts @@ -3,4 +3,4 @@ * SPDX-License-Identifier: Apache-2.0 */ -export { EmptyState } from './empty_state'; +export { PITTableWithRouter } from './pit_table'; diff --git a/src/plugins/point_in_time_management/public/components/pit_table/empty_state/__snapshots__/empty_state.test.tsx.snap b/src/plugins/point_in_time_management/public/components/pit_table/empty_state/__snapshots__/empty_state.test.tsx.snap new file mode 100644 index 000000000000..414686fcb7bc --- /dev/null +++ b/src/plugins/point_in_time_management/public/components/pit_table/empty_state/__snapshots__/empty_state.test.tsx.snap @@ -0,0 +1,32 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`EmptyState should render normally 1`] = ` + + + + } + body={ +

+ +

+ } +/> +`; diff --git a/src/plugins/point_in_time_management/public/components/pit_table/empty_state/empty_state.test.tsx b/src/plugins/point_in_time_management/public/components/pit_table/empty_state/empty_state.test.tsx new file mode 100644 index 000000000000..82393e830152 --- /dev/null +++ b/src/plugins/point_in_time_management/public/components/pit_table/empty_state/empty_state.test.tsx @@ -0,0 +1,16 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +import React from 'react'; +import { EmptyState } from './empty_state'; +import { shallow } from 'enzyme'; + +describe('EmptyState', () => { + it('should render normally', () => { + const component = shallow(); + + expect(component).toMatchSnapshot(); + }); +}); diff --git a/src/plugins/point_in_time_management/public/components/pit_table/empty_state/empty_state.tsx b/src/plugins/point_in_time_management/public/components/pit_table/empty_state/empty_state.tsx new file mode 100644 index 000000000000..e0babd5db8b5 --- /dev/null +++ b/src/plugins/point_in_time_management/public/components/pit_table/empty_state/empty_state.tsx @@ -0,0 +1,29 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +import { EuiButton, EuiEmptyPrompt } from '@elastic/eui'; +import { FormattedMessage } from '@osd/i18n/react'; +import React from 'react'; + +export const EmptyState = () => ( + + +

+ } + actions={ + + + + } + /> +); diff --git a/src/plugins/point_in_time_management/public/components/pit_table/empty_state/index.ts b/src/plugins/point_in_time_management/public/components/pit_table/empty_state/index.ts new file mode 100644 index 000000000000..3b18ca75b42e --- /dev/null +++ b/src/plugins/point_in_time_management/public/components/pit_table/empty_state/index.ts @@ -0,0 +1,6 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +export { EmptyState } from './empty_state'; diff --git a/src/plugins/point_in_time_management/public/components/pit_table/index.ts b/src/plugins/point_in_time_management/public/components/pit_table/index.ts new file mode 100644 index 000000000000..35a1f8acc63f --- /dev/null +++ b/src/plugins/point_in_time_management/public/components/pit_table/index.ts @@ -0,0 +1,6 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +export { PITTableWithRouter } from './pit_table'; diff --git a/src/plugins/point_in_time_management/public/components/pit_table/page_header/__snapshots__/page_header.test.tsx.snap b/src/plugins/point_in_time_management/public/components/pit_table/page_header/__snapshots__/page_header.test.tsx.snap new file mode 100644 index 000000000000..84f28b4592f6 --- /dev/null +++ b/src/plugins/point_in_time_management/public/components/pit_table/page_header/__snapshots__/page_header.test.tsx.snap @@ -0,0 +1,46 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`PageHeader should render normally 1`] = ` + + + + +

+ +

+
+
+ + + +
+ +

+ +

+
+
+`; diff --git a/src/plugins/point_in_time_management/public/components/pit_table/page_header/index.ts b/src/plugins/point_in_time_management/public/components/pit_table/page_header/index.ts new file mode 100644 index 000000000000..0328fce3768d --- /dev/null +++ b/src/plugins/point_in_time_management/public/components/pit_table/page_header/index.ts @@ -0,0 +1,6 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +export { PageHeader } from './page_header'; diff --git a/src/plugins/point_in_time_management/public/components/pit_table/page_header/page_header.test.tsx b/src/plugins/point_in_time_management/public/components/pit_table/page_header/page_header.test.tsx new file mode 100644 index 000000000000..ea595f4363e4 --- /dev/null +++ b/src/plugins/point_in_time_management/public/components/pit_table/page_header/page_header.test.tsx @@ -0,0 +1,16 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +import React from 'react'; +import { PageHeader } from './page_header'; +import { shallow } from 'enzyme'; + +describe('PageHeader', () => { + it('should render normally', () => { + const component = shallow(); + + expect(component).toMatchSnapshot(); + }); +}); diff --git a/src/plugins/point_in_time_management/public/components/pit_table/page_header/page_header.tsx b/src/plugins/point_in_time_management/public/components/pit_table/page_header/page_header.tsx new file mode 100644 index 000000000000..3c0c60e69ac9 --- /dev/null +++ b/src/plugins/point_in_time_management/public/components/pit_table/page_header/page_header.tsx @@ -0,0 +1,39 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +import { + EuiButton, + EuiPageContentHeader, + EuiPageContentHeaderSection, + EuiText, + EuiTitle, +} from '@elastic/eui'; +import { FormattedMessage } from '@osd/i18n/react'; +import React from 'react'; + +export const PageHeader = () => ( + <> + + + +

+ +

+
+
+ + + +
+ +

+ +

+
+ +); diff --git a/src/plugins/point_in_time_management/public/components/pit_table/pit_table.tsx b/src/plugins/point_in_time_management/public/components/pit_table/pit_table.tsx new file mode 100644 index 000000000000..2f8339547bc8 --- /dev/null +++ b/src/plugins/point_in_time_management/public/components/pit_table/pit_table.tsx @@ -0,0 +1,230 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +import React, { useEffect, useState } from 'react'; +import { RouteComponentProps, withRouter } from 'react-router-dom'; +import { useEffectOnce, useMount } from 'react-use'; +import { + EuiButton, + EuiInMemoryTable, + EuiPageContent, + EuiPageContentBody, + EuiSpacer, + EuiSearchBarProps, + Query, +} from '@elastic/eui'; +import { i18n } from '@osd/i18n'; +import { FormattedMessage } from '@osd/i18n/react'; +import { getListBreadcrumbs } from '../breadcrumbs'; +import { PointInTimeManagementContext } from '../../types'; +import { useOpenSearchDashboards } from '../../../../opensearch_dashboards_react/public'; +import { getDataSources } from '../utils'; +import { EmptyState } from './empty_state'; +import { PageHeader } from './page_header'; +import { getServices, Services } from '../../services'; +// import { dataSource } from 'src/plugins/data_source/server/saved_objects'; + +export interface DataSourceItem { + id: string; + title: string; + sort: string; +} + +export interface PitItem { + pit_id: string; + creation_time: number; + keep_alive: number; +} + +const PITTable = ({ history }: RouteComponentProps) => { + const { + setBreadcrumbs, + savedObjects, + notifications: { toasts }, + http, + } = useOpenSearchDashboards().services; + + useMount(() => { + setBreadcrumbs(getListBreadcrumbs()); + }); + + const services: Services = getServices(http); + + // TODO: update this for cases when some data source name is default + const defaultDataSource: DataSourceItem = { id: '', title: 'Default', sort: '0' }; + + // TODO: use APIs to fetch PITs and update the table and message + const [loading, setLoading] = useState(false); + const [pits, setPits] = useState([]); + const [message, setMessage] = useState(); + + const [dataSources, setDataSources] = useState([defaultDataSource]); + const [dataSource, setDataSource] = useState('default'); + + useEffectOnce(() => { + fetchDataSources(); + }); + + useEffect(() => { + getPits(dataSource); + }, [dataSource]); + + const fetchDataSources = () => { + getDataSources(savedObjects.client) + .then((fetchedDataSources) => { + if (fetchedDataSources?.length) { + setDataSources( + fetchedDataSources + .concat([defaultDataSource]) + .sort((a, b) => a.sort.localeCompare(b.sort)) + ); + } + }) + .catch(() => { + toasts.addDanger( + i18n.translate('pitManagement.pitTable.fetchDataSourceError', { + defaultMessage: 'Unable to find existing data sources', + }) + ); + }); + }; + + const getPits = (dataSourceId?: string) => { + // setMessage(<>Loading PITs...); + setLoading(true); + if (dataSourceId === 'default') { + dataSourceId = undefined; + } + services + .getAllPits(dataSourceId) + .then((fetchedPits) => { + setLoading(false); + setPits(fetchedPits?.resp?.pits); + }) + .catch(() => { + setLoading(false); + toasts.addDanger( + i18n.translate('pitManagement.pitTable.fetchDataSourceError', { + defaultMessage: 'Unable to fetch point in time objects.', + }) + ); + }); + }; + + const columns = [ + { + field: 'pit_id', + name: i18n.translate('pitManagement.pitTable.nameColumnName', { + defaultMessage: 'Name', + }), + }, + { + field: 'expires', + name: i18n.translate('pitManagement.pitTable.expiresColumnName', { + defaultMessage: 'Expires', + }), + sortable: true, + }, + { + field: 'dataSource', + name: i18n.translate('pitManagement.pitTable.dataSourceColumnName', { + defaultMessage: 'Data Source', + }), + }, + { + field: 'creation_time', + name: i18n.translate('pitManagement.pitTable.createdColumnName', { + defaultMessage: 'Created', + }), + sortable: true, + }, + { + field: 'actions', + name: i18n.translate('pitManagement.pitTable.actionsColumnName', { + defaultMessage: 'Actions', + }), + }, + ]; + + const renderToolsRight = () => { + return [ + + + , + ]; + }; + + function onQueryChange({ query }: { query: Query }) { + if (query.ast.getFieldClauses('dataSource')) { + const selectedDataSource = query.ast.getFieldClauses('dataSource')[0].value as string; + setDataSource(selectedDataSource); + } + } + + const search: EuiSearchBarProps = { + toolsRight: renderToolsRight(), + defaultQuery: 'dataSource:""', + onChange: onQueryChange, + box: { + incremental: true, + schema: true, + disabled: pits.length === 0, + }, + filters: [ + { + type: 'field_value_selection', + searchThreshold: 5, + field: 'dataSource', + name: i18n.translate('pitManagement.pitTable.dataSourceFilterName', { + defaultMessage: 'Data Source', + }), + multiSelect: false, + options: dataSources.map((source) => ({ + value: source.id, + name: source.title, + view: `${source.title}`, + })), + }, + ], + }; + + const pagination = { + initialPageSize: 10, + pageSizeOptions: [5, 10, 25, 50], + }; + + return ( + <> + + + + + + + + + ); +}; + +export const PITTableWithRouter = withRouter(PITTable); diff --git a/src/plugins/point_in_time_management/public/components/utils.ts b/src/plugins/point_in_time_management/public/components/utils.ts new file mode 100644 index 000000000000..c06490d2ec17 --- /dev/null +++ b/src/plugins/point_in_time_management/public/components/utils.ts @@ -0,0 +1,30 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +import { SavedObjectsClientContract } from 'src/core/public'; +import { DataSourceAttributes } from 'src/plugins/data_source/common/data_sources'; + +export async function getDataSources(savedObjectsClient: SavedObjectsClientContract) { + return savedObjectsClient + .find({ + type: 'data-source', + fields: ['title', 'id'], + perPage: 10000, + }) + .then((response) => { + return ( + response.savedObjects.map((dataSource) => { + const id = dataSource.id; + const title = dataSource.get('title'); + + return { + id, + title, + sort: `${title}`, + }; + }) || [] + ); + }); +} diff --git a/src/plugins/point_in_time_management/public/management_app/mount_management_section.tsx b/src/plugins/point_in_time_management/public/management_app/mount_management_section.tsx index 96c87fc73538..10aad5d5a7b7 100644 --- a/src/plugins/point_in_time_management/public/management_app/mount_management_section.tsx +++ b/src/plugins/point_in_time_management/public/management_app/mount_management_section.tsx @@ -6,26 +6,37 @@ import React from 'react'; import ReactDOM from 'react-dom'; import { I18nProvider } from '@osd/i18n/react'; +import { Route, Router, Switch } from 'react-router-dom'; import { ManagementAppMountParams } from '../../../management/public'; import { PointInTimeManagementStartDependencies } from '../plugin'; import { StartServicesAccessor } from '../../../../core/public'; import { PointInTimeManagementContext } from '../types'; import { OpenSearchDashboardsContextProvider } from '../../../opensearch_dashboards_react/public'; -import { EmptyState } from '../components'; +import { PITTableWithRouter } from '../components'; export async function mountManagementSection( getStartServices: StartServicesAccessor, params: ManagementAppMountParams ) { - const [{ chrome, application }] = await getStartServices(); + const [{ chrome, application, savedObjects, notifications, http }] = await getStartServices(); const deps: PointInTimeManagementContext = { chrome, application, + notifications, + savedObjects, + http, + setBreadcrumbs: params.setBreadcrumbs, }; ReactDOM.render( - + + + + + + + , params.element diff --git a/src/plugins/point_in_time_management/public/plugin.ts b/src/plugins/point_in_time_management/public/plugin.ts index 53a1421926e4..ce2bb4b3c8c1 100644 --- a/src/plugins/point_in_time_management/public/plugin.ts +++ b/src/plugins/point_in_time_management/public/plugin.ts @@ -4,6 +4,8 @@ */ import { i18n } from '@osd/i18n'; +import { DataPublicPluginStart } from 'src/plugins/data/public'; +import { DataSourcePluginStart } from 'src/plugins/data_source/public'; import { CoreSetup, CoreStart, Plugin } from '../../../core/public'; import { PointInTimeManagementPluginSetup, PointInTimeManagementPluginStart } from './types'; import { ManagementSetup } from '../../management/public'; @@ -12,8 +14,10 @@ export interface PointInTimeManagementSetupDependencies { management: ManagementSetup; } -// eslint-disable-next-line @typescript-eslint/no-empty-interface -export interface PointInTimeManagementStartDependencies {} +export interface PointInTimeManagementStartDependencies { + data: DataPublicPluginStart; + dataSource?: DataSourcePluginStart; +} const sectionsHeader = i18n.translate('pointInTimeManagement.pointInTime.sectionsHeader', { defaultMessage: 'Point In Time', diff --git a/src/plugins/point_in_time_management/public/services.ts b/src/plugins/point_in_time_management/public/services.ts new file mode 100644 index 000000000000..05bc96d30418 --- /dev/null +++ b/src/plugins/point_in_time_management/public/services.ts @@ -0,0 +1,27 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +import { CoreStart, HttpFetchError } from 'opensearch-dashboards/public'; + +export interface Services { + getAllPits: (dataSourceId?: string) => Promise; +} + +export function getServices(http: CoreStart['http']): Services { + return { + getAllPits: async (dataSourceId?: string) => { + try { + const response = await http.post('/api/pit/all', { + body: JSON.stringify({ + dataSourceId: dataSourceId ? dataSourceId : '', + }), + }); + return response; + } catch (e) { + return e; + } + }, + }; +} diff --git a/src/plugins/point_in_time_management/public/types.ts b/src/plugins/point_in_time_management/public/types.ts index fc0c5b7038c8..c4625e843080 100644 --- a/src/plugins/point_in_time_management/public/types.ts +++ b/src/plugins/point_in_time_management/public/types.ts @@ -3,13 +3,19 @@ * SPDX-License-Identifier: Apache-2.0 */ -import { ApplicationStart, ChromeStart } from 'opensearch-dashboards/public'; +import { ApplicationStart, ChromeStart, HttpSetup } from 'opensearch-dashboards/public'; +import { NotificationsStart, SavedObjectsStart } from 'src/core/public'; import { NavigationPublicPluginStart } from '../../navigation/public'; +import { ManagementAppMountParams } from '../../management/public'; import { ManagementSetup } from '../../management/public'; export interface PointInTimeManagementContext { chrome: ChromeStart; application: ApplicationStart; + notifications: NotificationsStart; + savedObjects: SavedObjectsStart; + http: HttpSetup; + setBreadcrumbs: ManagementAppMountParams['setBreadcrumbs']; } // eslint-disable-next-line @typescript-eslint/no-empty-interface diff --git a/src/plugins/point_in_time_management/server/plugin.ts b/src/plugins/point_in_time_management/server/plugin.ts index 876e4271d432..b4711ee6ab80 100644 --- a/src/plugins/point_in_time_management/server/plugin.ts +++ b/src/plugins/point_in_time_management/server/plugin.ts @@ -12,7 +12,7 @@ import { } from '../../../core/server'; import { PointInTimeManagementPluginSetup, PointInTimeManagementPluginStart } from './types'; -import { defineRoutes } from './routes'; +import { registerPitRoutes } from './routes'; export class PointInTimeManagementPlugin implements Plugin { @@ -26,7 +26,7 @@ export class PointInTimeManagementPlugin this.logger.debug('pointInTimeManagement: Setup'); const router = core.http.createRouter(); - defineRoutes(router); + registerPitRoutes(router); return {}; } diff --git a/src/plugins/point_in_time_management/server/routes/index.ts b/src/plugins/point_in_time_management/server/routes/index.ts index b56d1871d7dd..49517bb34813 100644 --- a/src/plugins/point_in_time_management/server/routes/index.ts +++ b/src/plugins/point_in_time_management/server/routes/index.ts @@ -3,20 +3,4 @@ * SPDX-License-Identifier: Apache-2.0 */ -import { IRouter } from '../../../../core/server'; - -export function defineRoutes(router: IRouter) { - router.get( - { - path: '/api/point_in_time_management/test', - validate: false, - }, - async (context, request, response) => { - return response.ok({ - body: { - time: new Date().toISOString(), - }, - }); - } - ); -} +export { registerPitRoutes } from './opensearch'; diff --git a/src/plugins/point_in_time_management/server/routes/opensearch.ts b/src/plugins/point_in_time_management/server/routes/opensearch.ts new file mode 100644 index 000000000000..05493741318a --- /dev/null +++ b/src/plugins/point_in_time_management/server/routes/opensearch.ts @@ -0,0 +1,121 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +import { schema } from '@osd/config-schema'; +import { + IRouter, + OpenSearchClient, + OpenSearchDashboardsRequest, + RequestHandlerContext, +} from 'opensearch-dashboards/server'; + +export function registerPitRoutes(router: IRouter) { + router.post( + { + path: '/api/pit/all', + validate: { + body: schema.object({ + dataSourceId: schema.string(), + }), + }, + }, + async (context, req, res) => { + const client: OpenSearchClient = await getClient(req, context); + try { + const pits = await client.getAllPits({}); + return res.ok({ + body: { + ok: true, + resp: pits.body, + }, + }); + } catch (err: any) { + return res.ok({ + body: { + ok: false, + resp: err.message, + }, + }); + } + } + ); + + // router.post( + // { + // path: '/api/geospatial/_search', + // validate: { + // body: schema.object({ + // index: schema.string(), + // }), + // }, + // }, + // async (context, req, res) => { + // const client = context.core.opensearch.client.asCurrentUser; + // try { + // const { index } = req.body; + // const params = { index, body: {} }; + // const results = await client.search(params); + // return res.ok({ + // body: { + // ok: true, + // resp: results.body, + // }, + // }); + // } catch (err: any) { + // return res.ok({ + // body: { + // ok: false, + // resp: err.message, + // }, + // }); + // } + // } + // ); + + // router.post( + // { + // path: '/api/geospatial/_mappings', + // validate: { + // body: schema.object({ + // index: schema.string(), + // }), + // }, + // }, + // async (context, req, res) => { + // const client = context.core.opensearch.client.asCurrentUser; + // try { + // const { index } = req.body; + // const mappings = await client.indices.getMapping({ index }); + // return res.ok({ + // body: { + // ok: true, + // resp: mappings.body, + // }, + // }); + // } catch (err: any) { + // return res.ok({ + // body: { + // ok: false, + // resp: err.message, + // }, + // }); + // } + // } + // ); +} + +async function getClient( + req: OpenSearchDashboardsRequest< + unknown, + unknown, + Readonly<{} & { dataSourceId: string }>, + 'post' + >, + context: RequestHandlerContext +): Promise { + return req.body.dataSourceId && req.body.dataSourceId !== 'default' && context.dataSource + ? await context.dataSource.opensearch.getClient(req.body.dataSourceId) + : context.core.opensearch.client.asCurrentUser; +} diff --git a/yarn.lock b/yarn.lock index 2728b79ee99e..1a037f5af282 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2349,6 +2349,17 @@ ms "^2.1.3" secure-json-parse "^2.4.0" +"@opensearch-project/opensearch@^2.2.0": + version "2.2.0" + resolved "https://registry.yarnpkg.com/@opensearch-project/opensearch/-/opensearch-2.2.0.tgz#2a31a67242bf3bacedc9644a0c68893eb5fb9420" + integrity sha512-E0f2Hooruz9y+17AF69oyyruikVMAEr1TxcQBNEQV4aQnI3o0+6CjqZS+t94SCZdWRTuN7HjIIVGbaYCJDpxgA== + dependencies: + aws4 "^1.11.0" + debug "^4.3.1" + hpagent "^1.2.0" + ms "^2.1.3" + secure-json-parse "^2.4.0" + "@percy/cli-app@1.10.4": version "1.10.4" resolved "https://registry.yarnpkg.com/@percy/cli-app/-/cli-app-1.10.4.tgz#3c27b71269d41ca3bd5af6d69ec5493cf5a16d1a" @@ -9635,6 +9646,11 @@ hpagent@^0.1.1: resolved "https://registry.yarnpkg.com/hpagent/-/hpagent-0.1.2.tgz#cab39c66d4df2d4377dbd212295d878deb9bdaa9" integrity sha512-ePqFXHtSQWAFXYmj+JtOTHr84iNrII4/QRlAAPPE+zqnKy4xJo7Ie1Y4kC7AdB+LxLxSTTzBMASsEcy0q8YyvQ== +hpagent@^1.2.0: + version "1.2.0" + resolved "https://registry.yarnpkg.com/hpagent/-/hpagent-1.2.0.tgz#0ae417895430eb3770c03443456b8d90ca464903" + integrity sha512-A91dYTeIB6NoXG+PxTQpCCDDnfHsW9kc06Lvpu1TEe9gnd6ZFeiBoRO9JvzEv6xK7EX97/dUE8g/vBMTqTS3CA== + html-element-map@^1.2.0: version "1.3.1" resolved "https://registry.yarnpkg.com/html-element-map/-/html-element-map-1.3.1.tgz#44b2cbcfa7be7aa4ff59779e47e51012e1c73c08"