From dde1fb6e6965d2b7232ff479e7c2f87e9011a8b7 Mon Sep 17 00:00:00 2001 From: Ryan Liang Date: Tue, 16 Jul 2024 18:14:54 -0700 Subject: [PATCH] [MDS] Add mds support for routers Signed-off-by: Ryan Liang --- .../data_connections_router.ts | 39 ++++++ server/routes/dsl.ts | 116 +++++++++++++++++- server/routes/index.ts | 2 +- 3 files changed, 155 insertions(+), 2 deletions(-) diff --git a/server/routes/data_connections/data_connections_router.ts b/server/routes/data_connections/data_connections_router.ts index 3ecd3d434..1626d5bf2 100644 --- a/server/routes/data_connections/data_connections_router.ts +++ b/server/routes/data_connections/data_connections_router.ts @@ -237,4 +237,43 @@ export function registerDataConnectionsRoute(router: IRouter, dataSourceEnabled: } } ); + + router.get( + { + path: `${DATACONNECTIONS_BASE}/{name}/dataSourceMDSId={dataSourceMDSId?}`, + validate: { + params: schema.object({ + name: schema.string(), + dataSourceMDSId: schema.maybe(schema.string({ defaultValue: '' })), + }), + }, + }, + async (context, request, response): Promise => { + const dataSourceMDSId = request.params.dataSourceMDSId; + try { + let dataConnectionsresponse; + if (dataSourceEnabled && dataSourceMDSId) { + const client = await context.dataSource.opensearch.legacy.getClient(dataSourceMDSId); + dataConnectionsresponse = await client.callAPI('ppl.getDataConnectionById', { + dataconnection: request.params.name, + }); + } else { + dataConnectionsresponse = await context.observability_plugin.observabilityClient + .asScoped(request) + .callAsCurrentUser('ppl.getDataConnectionById', { + dataconnection: request.params.name, + }); + } + return response.ok({ + body: dataConnectionsresponse, + }); + } catch (error: any) { + console.error('Issue in fetching data connection:', error); + return response.custom({ + statusCode: error.statusCode || 500, + body: error.message, + }); + } + } + ); } diff --git a/server/routes/dsl.ts b/server/routes/dsl.ts index 9bdc45342..0560ae049 100644 --- a/server/routes/dsl.ts +++ b/server/routes/dsl.ts @@ -15,7 +15,10 @@ import { DSL_SETTINGS, } from '../../common/constants/shared'; -export function registerDslRoute({ router }: { router: IRouter; facet: DSLFacet }) { +export function registerDslRoute( + { router }: { router: IRouter; facet: DSLFacet }, + dataSourceEnabled: boolean +) { router.post( { path: `${DSL_BASE}${DSL_SEARCH}`, @@ -122,4 +125,115 @@ export function registerDslRoute({ router }: { router: IRouter; facet: DSLFacet } } ); + + router.get( + { + path: `${DSL_BASE}${DSL_CAT}/dataSourceMDSId={dataSourceMDSId?}`, + validate: { + query: schema.object({ + format: schema.string(), + index: schema.maybe(schema.string()), + }), + params: schema.object({ + dataSourceMDSId: schema.maybe(schema.string({ defaultValue: '' })), + }), + }, + }, + async (context, request, response) => { + const dataSourceMDSId = request.params.dataSourceMDSId; + try { + let resp; + if (dataSourceEnabled && dataSourceMDSId) { + const client = await context.dataSource.opensearch.legacy.getClient(dataSourceMDSId); + resp = await client.callAPI('cat.indices', request.query); + } else { + resp = await context.core.opensearch.legacy.client.callAsCurrentUser( + 'cat.indices', + request.query + ); + } + return response.ok({ + body: resp, + }); + } catch (error) { + if (error.statusCode !== 404) console.error(error); + return response.custom({ + statusCode: error.statusCode || 500, + body: error.message, + }); + } + } + ); + + router.get( + { + path: `${DSL_BASE}${DSL_MAPPING}/dataSourceMDSId={dataSourceMDSId?}`, + validate: { + query: schema.any(), + params: schema.object({ + dataSourceMDSId: schema.maybe(schema.string({ defaultValue: '' })), + }), + }, + }, + async (context, request, response) => { + const dataSourceMDSId = request.params.dataSourceMDSId; + try { + let resp; + if (dataSourceEnabled && dataSourceMDSId) { + const client = await context.dataSource.opensearch.legacy.getClient(dataSourceMDSId); + resp = await client.callAPI('indices.getMapping', { index: request.query.index }); + } else { + resp = await context.core.opensearch.legacy.client.callAsCurrentUser( + 'indices.getMapping', + { index: request.query.index } + ); + } + return response.ok({ + body: resp, + }); + } catch (error) { + if (error.statusCode !== 404) console.error(error); + return response.custom({ + statusCode: error.statusCode || 500, + body: error.message, + }); + } + } + ); + + router.get( + { + path: `${DSL_BASE}${DSL_SETTINGS}/dataSourceMDSId={dataSourceMDSId?}`, + validate: { + query: schema.any(), + params: schema.object({ + dataSourceMDSId: schema.maybe(schema.string({ defaultValue: '' })), + }), + }, + }, + async (context, request, response) => { + const dataSourceMDSId = request.params.dataSourceMDSId; + try { + let resp; + if (dataSourceEnabled && dataSourceMDSId) { + const client = await context.dataSource.opensearch.legacy.getClient(dataSourceMDSId); + resp = await client.callAPI('indices.getSettings', { index: request.query.index }); + } else { + resp = await context.core.opensearch.legacy.client.callAsCurrentUser( + 'indices.getSettings', + { index: request.query.index } + ); + } + return response.ok({ + body: resp, + }); + } catch (error) { + if (error.statusCode !== 404) console.error(error); + return response.custom({ + statusCode: error.statusCode || 500, + body: error.message, + }); + } + } + ); } diff --git a/server/routes/index.ts b/server/routes/index.ts index 499fb2952..d8a695233 100644 --- a/server/routes/index.ts +++ b/server/routes/index.ts @@ -37,7 +37,7 @@ export function setupRoutes({ PanelsRouter(router); VisualizationsRouter(router); registerPplRoute({ router, facet: new PPLFacet(client) }); - registerDslRoute({ router, facet: new DSLFacet(client) }); + registerDslRoute({ router, facet: new DSLFacet(client) }, dataSourceEnabled); registerEventAnalyticsRouter({ router, savedObjectFacet: new SavedObjectFacet(client) }); registerAppAnalyticsRouter(router);