Skip to content

Commit 9bd3cd0

Browse files
author
Chris Wiechmann
committed
Create Index method now checks if a given Index-Template exists
1 parent 89989e6 commit 9bd3cd0

File tree

7 files changed

+148
-7
lines changed

7 files changed

+148
-7
lines changed

api-builder-plugin-fn-elasticsearch/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/)
55
and this project adheres to [Semantic Versioning](http://semver.org/).
66

7+
## [2.1.0] 2021-09-16
8+
### Added
9+
- Create Index method now checks if a given Index-Template exists
10+
711
## [2.0.0] 2021-09-14
812
### Changed
913
- Search method now provides missingIndex & noResult exists making it easier to control your flow

api-builder-plugin-fn-elasticsearch/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@axway-api-builder-ext/api-builder-plugin-fn-elasticsearch",
3-
"version": "2.0.0",
3+
"version": "2.1.0",
44
"description": "Integrate Elasticsearch into your API-Builder flow to combine search data for instance with other data available in your flow.",
55
"author": "Chris Wiechmann <cwiechmann@axway.com> (http://www.axway.com)",
66
"license": "Apache-2.0",

api-builder-plugin-fn-elasticsearch/src/actions/indices.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,27 +79,35 @@ async function indicesRollover(params, options) {
7979

8080
async function indicesCreate(params, options) {
8181
const elasticSearchConfig = options.pluginConfig.elastic;
82+
const { index, alias, indexTemplate } = params;
8283

8384
if (typeof elasticSearchConfig.node === 'undefined' && typeof elasticSearchConfig.nodes === 'undefined') {
8485
options.logger.error('Elasticsearch configuration is invalid: nodes or node is missing.');
8586
throw new Error('Elasticsearch configuration is invalid: nodes or node is missing.');
8687
}
87-
if (!params.index) {
88+
if (!index) {
8889
throw new Error('Missing required parameter: index');
8990
}
9091
try {
91-
var aliasName;
92-
if(params.alias) {
93-
aliasName = params.alias;
94-
delete params.alias;
92+
if(alias) {
9593
if(params.body) { // a body might be given
9694
if(params.body.aliases == undefined) {
9795
params.body.aliases = {};
9896
}
9997
} else {
10098
params.body = { aliases: { }};
10199
}
102-
params.body.aliases[aliasName] = {};
100+
params.body.aliases[alias] = {};
101+
delete params.alias;
102+
}
103+
// Check if the index template exists
104+
if(indexTemplate) {
105+
var client = new ElasticsearchClient(elasticSearchConfig).client;
106+
var result = await client.indices.existsTemplate( { name: indexTemplate }, { maxRetries: 3 });
107+
if(result.statusCode == 404) {
108+
throw new Error(`The index template: '${indexTemplate}' is missing. Index wont be created.`);
109+
}
110+
delete params.indexTemplate;
103111
}
104112
var client = new ElasticsearchClient(elasticSearchConfig).client;
105113
var result = await client.indices.create( params, { ignore: [404], maxRetries: 3 });

api-builder-plugin-fn-elasticsearch/src/flow-nodes.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -987,6 +987,13 @@ flow-nodes:
987987
initialType: string
988988
schema:
989989
type: string
990+
indexTemplate:
991+
name: Index template name
992+
description: If given, the index will only be created if the index-template already exists.
993+
required: false
994+
initialType: string
995+
schema:
996+
type: string
990997
body:
991998
name: Index configuration
992999
description: The configuration for the index (settings and mappings)

api-builder-plugin-fn-elasticsearch/test/indices/indices-Test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,30 @@ describe('Indices rollover tests', () => {
135135
expect(mockedIndicesCreate.lastCall.arg.body.aliases["existingAlias"]).to.be.a('object');
136136
expect(mockedIndicesCreate.lastCall.arg.body.settings["number_of_shards"]).to.equal(1);
137137
});
138+
139+
it.only('should fail if the given index-template does not exist', async () => {
140+
const mockedIndexTemplateExists = setupElasticsearchMock(client, 'indices.existsTemplate', './test/mock/indexTemplates/indexTemplateNotExistsResponse.json', false);
141+
142+
const inputParameter = { index: "my_index_to_be_created", indexTemplate: "this_template_is_missing" };
143+
const { value, output } = await flowNode.indicesCreate(inputParameter);
144+
145+
expect(value).to.be.instanceOf(Error)
146+
.and.to.have.property('message', "The index template: 'this_template_is_missing' is missing. Index wont be created.");
147+
expect(output).to.equal('error');
148+
expect(mockedIndexTemplateExists.callCount).to.equals(1);
149+
});
150+
151+
it.only('should pass when the index is created including a given index_template name', async () => {
152+
const mockedIndexTemplateExists = setupElasticsearchMock(client, 'indices.existsTemplate', './test/mock/indexTemplates/indexTemplateExistsResponse.json', false);
153+
const mockedIndicesCreate = setupElasticsearchMock(client, 'indices.create', './test/mock/indices/indexCreatedResponse.json', false);
154+
155+
const inputParameter = { index: "my_index_to_be_created", indexTemplate: "management-kpis" };
156+
const { value, output } = await flowNode.indicesCreate(inputParameter);
157+
158+
expect(output).to.equal('next');
159+
expect(mockedIndexTemplateExists.callCount).to.equals(1);
160+
expect(mockedIndicesCreate.callCount).to.equals(1);
161+
});
138162
});
139163

140164
describe('#Index exists tests', () => {
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
"body": true,
3+
"statusCode": 200,
4+
"headers": {
5+
"x-elastic-product": "Elasticsearch",
6+
"content-type": "application/json; charset=UTF-8",
7+
"content-length": "892"
8+
},
9+
"meta": {
10+
"context": null,
11+
"request": {
12+
"params": {
13+
"method": "HEAD",
14+
"path": "/_template/management-kpis",
15+
"body": null,
16+
"querystring": "",
17+
"headers": {
18+
"user-agent": "elasticsearch-js/7.14.1 (win32 10.0.19043-x64; Node.js v12.13.1)",
19+
"x-elastic-client-meta": "es=7.14.1,js=12.13.1,t=7.14.1,hc=12.13.1"
20+
},
21+
"timeout": 30000
22+
},
23+
"options": {
24+
"maxRetries": 3
25+
},
26+
"id": 1
27+
},
28+
"name": "elasticsearch-js",
29+
"connection": {
30+
"url": "https://api-env:9200/",
31+
"id": "https://api-env:9200/",
32+
"headers": {
33+
34+
},
35+
"deadCount": 0,
36+
"resurrectTimeout": 0,
37+
"_openRequests": 0,
38+
"status": "alive",
39+
"roles": {
40+
"master": true,
41+
"data": true,
42+
"ingest": true,
43+
"ml": false
44+
}
45+
},
46+
"attempts": 0,
47+
"aborted": false
48+
}
49+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
"body": false,
3+
"statusCode": 404,
4+
"headers": {
5+
"x-elastic-product": "Elasticsearch",
6+
"content-type": "application/json; charset=UTF-8",
7+
"content-length": "2"
8+
},
9+
"meta": {
10+
"context": null,
11+
"request": {
12+
"params": {
13+
"method": "HEAD",
14+
"path": "/_template/this_template_is_missing",
15+
"body": null,
16+
"querystring": "",
17+
"headers": {
18+
"user-agent": "elasticsearch-js/7.14.1 (win32 10.0.19043-x64; Node.js v12.13.1)",
19+
"x-elastic-client-meta": "es=7.14.1,js=12.13.1,t=7.14.1,hc=12.13.1"
20+
},
21+
"timeout": 30000
22+
},
23+
"options": {
24+
"maxRetries": 3
25+
},
26+
"id": 1
27+
},
28+
"name": "elasticsearch-js",
29+
"connection": {
30+
"url": "https://api-env:9200/",
31+
"id": "https://api-env:9200/",
32+
"headers": {
33+
34+
},
35+
"deadCount": 0,
36+
"resurrectTimeout": 0,
37+
"_openRequests": 0,
38+
"status": "alive",
39+
"roles": {
40+
"master": true,
41+
"data": true,
42+
"ingest": true,
43+
"ml": false
44+
}
45+
},
46+
"attempts": 0,
47+
"aborted": false
48+
}
49+
}

0 commit comments

Comments
 (0)