From cc381adf1a69ae1f141dc5a27aa29b7b8b828239 Mon Sep 17 00:00:00 2001 From: Tyler Ohlsen Date: Mon, 26 Feb 2024 15:11:54 -0800 Subject: [PATCH] Get core APIs working Signed-off-by: Tyler Ohlsen --- public/pages/workflow_detail/workflow_detail.tsx | 14 +------------- public/route_service.ts | 4 ++-- public/store/reducers/opensearch_reducer.ts | 15 ++++++++++++--- server/routes/opensearch_routes_service.ts | 12 +++++------- 4 files changed, 20 insertions(+), 25 deletions(-) diff --git a/public/pages/workflow_detail/workflow_detail.tsx b/public/pages/workflow_detail/workflow_detail.tsx index 2d6f4c6f..085c7192 100644 --- a/public/pages/workflow_detail/workflow_detail.tsx +++ b/public/pages/workflow_detail/workflow_detail.tsx @@ -12,7 +12,7 @@ import { EuiPage, EuiPageBody } from '@elastic/eui'; import { BREADCRUMBS } from '../../utils'; import { getCore } from '../../services'; import { WorkflowDetailHeader } from './components'; -import { AppState, fetchIndices } from '../../store'; +import { AppState } from '../../store'; import { ResizableWorkspace } from './workspace'; import { Launches } from './launches'; import { Prototype } from './prototype'; @@ -48,13 +48,6 @@ function replaceActiveTab(activeTab: string, props: WorkflowDetailProps) { */ export function WorkflowDetail(props: WorkflowDetailProps) { const { workflows } = useSelector((state: AppState) => state.workflows); - const { indices, loading } = useSelector( - (state: AppState) => state.opensearch - ); - - console.log('indices fetched: ', indices); - console.log('workflows fetched: ', workflows); - console.log('loading: ', loading); const workflow = workflows.find( (wf) => wf.id === props.match?.params?.workflowId @@ -79,11 +72,6 @@ export function WorkflowDetail(props: WorkflowDetailProps) { } }, []); - useEffect(() => { - console.log('fetching indices...'); - fetchIndices('opensearch_dashboards_*'); - }, []); - useEffect(() => { getCore().chrome.setBreadcrumbs([ BREADCRUMBS.FLOW_FRAMEWORK, diff --git a/public/route_service.ts b/public/route_service.ts index 54b2a76e..82e04363 100644 --- a/public/route_service.ts +++ b/public/route_service.ts @@ -15,7 +15,7 @@ export function configureRoutes(core: CoreStart): RouteService { return { searchIndex: async (indexName: string, body: {}) => { try { - const response = await core.http.post<{ respString: string }>( + const response = await core.http.get<{ respString: string }>( `${SEARCH_INDICES_PATH}/${indexName}`, { body: JSON.stringify(body), @@ -28,7 +28,7 @@ export function configureRoutes(core: CoreStart): RouteService { }, fetchIndices: async (pattern: string) => { try { - const response = await core.http.post<{ respString: string }>( + const response = await core.http.get<{ respString: string }>( `${FETCH_INDICES_PATH}/${pattern}` ); return response; diff --git a/public/store/reducers/opensearch_reducer.ts b/public/store/reducers/opensearch_reducer.ts index 783ed400..9d2945fa 100644 --- a/public/store/reducers/opensearch_reducer.ts +++ b/public/store/reducers/opensearch_reducer.ts @@ -6,6 +6,7 @@ import { createAsyncThunk, createSlice } from '@reduxjs/toolkit'; import { getRouteService } from '../../services'; import { Index } from '../../../common'; +import { HttpFetchError } from '../../../../../src/core/public'; const initialState = { loading: false, @@ -18,11 +19,19 @@ const FETCH_INDICES_ACTION = `${OPENSEARCH_PREFIX}/fetchIndices`; export const fetchIndices = createAsyncThunk( FETCH_INDICES_ACTION, - async (pattern?: string) => { + async (pattern: string, { rejectWithValue }) => { // defaulting to fetch everything except system indices (starting with '.') const patternString = pattern || '*,-.*'; - const response = getRouteService().fetchIndices(patternString); - return response; + const response: any | HttpFetchError = await getRouteService().fetchIndices( + patternString + ); + if (response instanceof HttpFetchError) { + return rejectWithValue( + 'Error fetching indices: ' + response.body.message + ); + } else { + return response; + } } ); diff --git a/server/routes/opensearch_routes_service.ts b/server/routes/opensearch_routes_service.ts index b375be60..49fcc954 100644 --- a/server/routes/opensearch_routes_service.ts +++ b/server/routes/opensearch_routes_service.ts @@ -19,7 +19,7 @@ export function registerOpenSearchRoutes( router: IRouter, opensearchRoutesService: OpenSearchRoutesService ): void { - router.post( + router.get( { path: `${SEARCH_INDICES_PATH}/{index_name}`, validate: { @@ -32,7 +32,7 @@ export function registerOpenSearchRoutes( opensearchRoutesService.searchIndex ); - router.post( + router.get( { path: `${FETCH_INDICES_PATH}/{pattern}`, validate: { @@ -69,7 +69,7 @@ export class OpenSearchRoutesService { try { const response = await this.client .asScoped(req) - .callAsCurrentUser.search(params); + .callAsCurrentUser('search', params); return res.ok({ body: response }); } catch (err: any) { return generateCustomError(res, err); @@ -82,19 +82,17 @@ export class OpenSearchRoutesService { res: OpenSearchDashboardsResponseFactory ): Promise> => { const { pattern } = req.params; - console.log('in fetchIndices()'); - console.log('pattern: ', pattern); try { const response = await this.client .asScoped(req) - .callAsCurrentUser.cat.indices({ + .callAsCurrentUser('cat.indices', { index: pattern, format: 'json', h: 'health,index', }); // re-formatting the index results to match Index - const cleanedIndices = response.body.map((index) => ({ + const cleanedIndices = response.map((index: any) => ({ name: index.index, health: index.health, })) as Index[];