Skip to content

Commit d3933c0

Browse files
committed
Make createStream function more readable
1 parent 792bf79 commit d3933c0

File tree

1 file changed

+10
-4
lines changed
  • x-pack/plugins/ingest_manager/server/services/epm/agent

1 file changed

+10
-4
lines changed

x-pack/plugins/ingest_manager/server/services/epm/agent/agent.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ import { safeLoad } from 'js-yaml';
99
import { DatasourceConfigRecord } from '../../../../common';
1010

1111
function replaceVariablesInYaml(yamlVariables: { [k: string]: any }, yaml: any) {
12-
const yamlKeys = Object.keys(yamlVariables);
13-
if (yamlKeys.length === 0) {
12+
if (Object.keys(yamlVariables).length === 0) {
1413
return yaml;
1514
}
1615

@@ -19,15 +18,15 @@ function replaceVariablesInYaml(yamlVariables: { [k: string]: any }, yaml: any)
1918
yaml[key] = replaceVariablesInYaml(yamlVariables, value);
2019
}
2120

22-
if (typeof value === 'string' && yamlKeys.indexOf(value) >= 0) {
21+
if (typeof value === 'string' && value in yamlVariables) {
2322
yaml[key] = yamlVariables[value];
2423
}
2524
});
2625

2726
return yaml;
2827
}
2928

30-
export function createStream(variables: DatasourceConfigRecord, streamTemplate: string) {
29+
function buildTemplateVariables(variables: DatasourceConfigRecord) {
3130
const yamlValues: { [k: string]: any } = {};
3231
const vars = Object.entries(variables).reduce((acc, [key, recordEntry]) => {
3332
// support variables with . like key.patterns
@@ -56,8 +55,15 @@ export function createStream(variables: DatasourceConfigRecord, streamTemplate:
5655
return acc;
5756
}, {} as { [k: string]: any });
5857

58+
return { vars, yamlValues };
59+
}
60+
61+
export function createStream(variables: DatasourceConfigRecord, streamTemplate: string) {
62+
const { vars, yamlValues } = buildTemplateVariables(variables);
63+
5964
const template = Handlebars.compile(streamTemplate, { noEscape: true });
6065
const stream = template(vars);
66+
6167
const yamlFromStream = safeLoad(stream, {});
6268

6369
return replaceVariablesInYaml(yamlValues, yamlFromStream);

0 commit comments

Comments
 (0)