diff --git a/src/plugins/region_map/server/plugin.js b/src/plugins/region_map/server/plugin.js deleted file mode 100644 index 14aeb0801d23..000000000000 --- a/src/plugins/region_map/server/plugin.js +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright OpenSearch Contributors - * SPDX-License-Identifier: Apache-2.0 - */ - -import { OpensearchService } from './services'; -import { opensearch } from '../server/routes'; -import { getUiSettings } from './ui_settings'; - -export class RegionMapPlugin { - constructor(initializerContext) { - this.logger = initializerContext.logger.get(); - } - - async setup(core) { - const opensearchClient = core.opensearch.legacy.createClient('opensearch'); - - // Initialize services - const opensearchService = new OpensearchService(opensearchClient); - - // Register server side APIs - const router = core.http.createRouter(); - core.uiSettings.register(getUiSettings()); - opensearch(opensearchService, router); - - return {}; - } - - async start() { - return {}; - } - - async stop() {} -} diff --git a/src/plugins/region_map/server/plugin.ts b/src/plugins/region_map/server/plugin.ts new file mode 100644 index 000000000000..6fc6e8987bc0 --- /dev/null +++ b/src/plugins/region_map/server/plugin.ts @@ -0,0 +1,38 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +import { + PluginInitializerContext, + CoreSetup, + CoreStart, + Plugin, + Logger, +} from 'opensearch-dashboards/server'; +import { registerGeospatialRoutes } from '../server/routes'; +import { getUiSettings } from './ui_settings'; +import { RegionMapPluginSetup, RegionMapPluginStart } from './types'; + +export class RegionMapPlugin implements Plugin { + private readonly logger: Logger; + + constructor(initializerContext: PluginInitializerContext) { + this.logger = initializerContext.logger.get(); + } + + public setup(core: CoreSetup) { + this.logger.debug('RegionMap: Setup'); + const router = core.http.createRouter(); + core.uiSettings.register(getUiSettings()); + registerGeospatialRoutes(router); + return {}; + } + + public start(_core: CoreStart) { + this.logger.debug('RegionMap: Started'); + return {}; + } + + public stop() {} +} diff --git a/src/plugins/region_map/server/routes/index.ts b/src/plugins/region_map/server/routes/index.ts index bdbd7bdca88e..37d50c83f609 100644 --- a/src/plugins/region_map/server/routes/index.ts +++ b/src/plugins/region_map/server/routes/index.ts @@ -3,6 +3,4 @@ * SPDX-License-Identifier: Apache-2.0 */ -import opensearch from './opensearch'; - -export { opensearch }; +export { registerGeospatialRoutes } from './opensearch'; diff --git a/src/plugins/region_map/server/routes/opensearch.ts b/src/plugins/region_map/server/routes/opensearch.ts index 2d67bc9c4e32..5eebc9a0ffda 100644 --- a/src/plugins/region_map/server/routes/opensearch.ts +++ b/src/plugins/region_map/server/routes/opensearch.ts @@ -4,9 +4,9 @@ */ import { schema } from '@osd/config-schema'; +import { IRouter } from 'opensearch-dashboards/server'; -// eslint-disable-next-line import/no-default-export -export default function (services, router) { +export function registerGeospatialRoutes(router: IRouter) { router.post( { path: '/api/geospatial/_indices', @@ -16,7 +16,39 @@ export default function (services, router) { }), }, }, - services.getIndex + async (context, req, res) => { + const client = context.core.opensearch.client.asCurrentUser; + try { + const { index } = req.body; + const indices = await client.cat.indices({ + index, + format: 'json', + }); + return res.ok({ + body: { + ok: true, + resp: indices.body, + }, + }); + } catch (err: any) { + // Opensearch throws an index_not_found_exception which we'll treat as a success + if (err.statusCode === 404) { + return res.ok({ + body: { + ok: false, + resp: [], + }, + }); + } else { + return res.ok({ + body: { + ok: false, + resp: err.message, + }, + }); + } + } + } ); router.post( @@ -28,7 +60,27 @@ export default function (services, router) { }), }, }, - services.search + 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( @@ -40,6 +92,25 @@ export default function (services, router) { }), }, }, - services.getMappings + 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, + }, + }); + } + } ); } diff --git a/src/plugins/region_map/server/services/index.js b/src/plugins/region_map/server/services/index.js deleted file mode 100644 index 63a48fdfdd4d..000000000000 --- a/src/plugins/region_map/server/services/index.js +++ /dev/null @@ -1,8 +0,0 @@ -/* - * Copyright OpenSearch Contributors - * SPDX-License-Identifier: Apache-2.0 - */ - -import OpensearchService from './opensearch_service'; - -export { OpensearchService }; diff --git a/src/plugins/region_map/server/services/opensearch_service.js b/src/plugins/region_map/server/services/opensearch_service.js deleted file mode 100644 index e9be890b72f0..000000000000 --- a/src/plugins/region_map/server/services/opensearch_service.js +++ /dev/null @@ -1,88 +0,0 @@ -/* - * Copyright OpenSearch Contributors - * SPDX-License-Identifier: Apache-2.0 - */ - -export default class OpensearchService { - constructor(esDriver) { - this.esDriver = esDriver; - } - - getMappings = async (context, req, res) => { - try { - const { index } = req.body; - const { callAsCurrentUser } = this.esDriver.asScoped(req); - const mappings = await callAsCurrentUser('indices.getMapping', { index }); - return res.ok({ - body: { - ok: true, - resp: mappings, - }, - }); - } catch (err) { - return res.ok({ - body: { - ok: false, - resp: err.message, - }, - }); - } - }; - - search = async (context, req, res) => { - try { - const { query, index, size } = req.body; - const params = { index, size, body: query }; - const { callAsCurrentUser } = this.esDriver.asScoped(req); - const results = await callAsCurrentUser('search', params); - return res.ok({ - body: { - ok: true, - resp: results, - }, - }); - } catch (err) { - return res.ok({ - body: { - ok: false, - resp: err.message, - }, - }); - } - }; - - getIndex = async (context, req, res) => { - try { - const { index } = req.body; - const { callAsCurrentUser } = this.esDriver.asScoped(req); - const indices = await callAsCurrentUser('cat.indices', { - index, - format: 'json', - h: 'health,index,status', - }); - return res.ok({ - body: { - ok: true, - resp: indices, - }, - }); - } catch (err) { - // Opensearch throws an index_not_found_exception which we'll treat as a success - if (err.statusCode === 404) { - return res.ok({ - body: { - ok: false, - resp: [], - }, - }); - } else { - return res.ok({ - body: { - ok: false, - resp: err.message, - }, - }); - } - } - }; -}