Skip to content

Commit

Permalink
changes js code to ts in region_map (#2084) (#2103)
Browse files Browse the repository at this point in the history
Issue:
#2039

* changes js code to ts and use the updated client

Signed-off-by: Shivam Dhar <[email protected]>
(cherry picked from commit 45324d3)

Co-authored-by: Shivam Dhar <[email protected]>
  • Loading branch information
opensearch-trigger-bot[bot] and Shivamdhar authored Aug 9, 2022
1 parent 88f6c07 commit 06a5338
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 138 deletions.
34 changes: 0 additions & 34 deletions src/plugins/region_map/server/plugin.js

This file was deleted.

38 changes: 38 additions & 0 deletions src/plugins/region_map/server/plugin.ts
Original file line number Diff line number Diff line change
@@ -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<RegionMapPluginSetup, RegionMapPluginStart> {
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() {}
}
4 changes: 1 addition & 3 deletions src/plugins/region_map/server/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@
* SPDX-License-Identifier: Apache-2.0
*/

import opensearch from './opensearch';

export { opensearch };
export { registerGeospatialRoutes } from './opensearch';
81 changes: 76 additions & 5 deletions src/plugins/region_map/server/routes/opensearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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,
},
});
}
}
);
}
8 changes: 0 additions & 8 deletions src/plugins/region_map/server/services/index.js

This file was deleted.

88 changes: 0 additions & 88 deletions src/plugins/region_map/server/services/opensearch_service.js

This file was deleted.

0 comments on commit 06a5338

Please sign in to comment.