Skip to content

Commit 6d5f9a6

Browse files
committed
feat: improved admin endpoints to create and delete ES indexes
Now endpoints support creating/deleting any indexes. Also, done some refactoring to reuse code and avoid code duplication which may lead to inconsistency issues.
1 parent 807be5d commit 6d5f9a6

File tree

8 files changed

+851
-1162
lines changed

8 files changed

+851
-1162
lines changed

migrations/elasticsearch_sync.js

Lines changed: 11 additions & 746 deletions
Large diffs are not rendered by default.

src/routes/admin/es-create-index.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Admin endpoint to create ES index.
3+
*
4+
* Waits until the operation is completed and returns result.
5+
*/
6+
import _ from 'lodash';
7+
import { middleware as tcMiddleware } from 'tc-core-library-js';
8+
import util from '../../util';
9+
import esUtils from '../../utils/es';
10+
import { INDEX_TO_DOC_TYPE } from '../../utils/es-config';
11+
12+
const permissions = tcMiddleware.permissions;
13+
14+
module.exports = [
15+
permissions('project.admin'),
16+
(req, res, next) => {
17+
try {
18+
const logger = req.log;
19+
logger.debug('Entered Admin#createIndex');
20+
21+
const indexName = _.get(req, 'body.indexName');
22+
if (!indexName) {
23+
const apiErr = new Error('"indexName" is required.');
24+
apiErr.status = 400;
25+
throw apiErr;
26+
}
27+
28+
const docType = _.get(req, 'body.param.docType', INDEX_TO_DOC_TYPE[indexName]);
29+
if (!docType) {
30+
const apiErr = new Error('Cannot find "docType" for the index.');
31+
apiErr.status = 500;
32+
throw apiErr;
33+
}
34+
35+
logger.debug('indexName', indexName);
36+
logger.debug('docType', docType);
37+
38+
39+
const esClient = util.getElasticSearchClient();
40+
esClient.indices.create(esUtils.buildCreateIndexRequest(indexName, docType))
41+
.then(() => {
42+
res.status(200).json({ message: 'Index successfully created.' });
43+
})
44+
.catch(next);
45+
} catch (err) {
46+
next(err);
47+
}
48+
},
49+
];

src/routes/admin/es-delete-index.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
/**
3+
* Admin endpoint to delete ES index.
4+
*
5+
* Waits until the operation is completed and returns result.
6+
*/
7+
import _ from 'lodash';
8+
import { middleware as tcMiddleware } from 'tc-core-library-js';
9+
import util from '../../util';
10+
11+
const permissions = tcMiddleware.permissions;
12+
13+
module.exports = [
14+
permissions('project.admin'),
15+
(req, res, next) => {
16+
try {
17+
const logger = req.log;
18+
logger.debug('Entered Admin#deleteIndex');
19+
20+
const indexName = _.get(req, 'body.indexName');
21+
logger.debug('indexName', indexName);
22+
23+
if (!indexName) {
24+
const apiErr = new Error('"indexName" is required.');
25+
apiErr.status = 400;
26+
throw apiErr;
27+
}
28+
29+
const esClient = util.getElasticSearchClient();
30+
esClient.indices.delete({ index: indexName })
31+
.then(() => {
32+
res.status(200).json({ message: 'Index successfully deleted.' });
33+
})
34+
.catch(next);
35+
} catch (err) {
36+
next(err);
37+
}
38+
},
39+
];

0 commit comments

Comments
 (0)