Skip to content

Commit

Permalink
[Ingest Manager] Add contains handlebar helper for conditional blocks…
Browse files Browse the repository at this point in the history
… in yaml (#72698) (#73131)

* Add contains handlebar helper for conditionally adding blocks in ingest manager yaml

* Split into two tests and sandbox handlebars runtime

* Make helper a little bit more robust and the any explicit

* Add this to function signature with explicit any type

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

Co-authored-by: Nicolas Chaulet <n.chaulet@gmail.com>

Co-authored-by: Nicolas Chaulet <n.chaulet@gmail.com>
Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>

Co-authored-by: Nicolas Chaulet <n.chaulet@gmail.com>
Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
  • Loading branch information
3 people authored Jul 24, 2020
1 parent 60a9283 commit a7233f5
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,67 @@ foo: bar
});
});

describe('contains blocks', () => {
const streamTemplate = `
input: log
paths:
{{#each paths}}
- {{this}}
{{/each}}
exclude_files: [".gz$"]
tags:
{{#each tags}}
- {{this}}
{{/each}}
{{#contains "forwarded" tags}}
publisher_pipeline.disable_host: true
{{/contains}}
processors:
- add_locale: ~
password: {{password}}
{{#if password}}
hidden_password: {{password}}
{{/if}}
`;

it('should support when a value is not contained in the array', () => {
const vars = {
paths: { value: ['/usr/local/var/log/nginx/access.log'] },
password: { type: 'password', value: '' },
tags: { value: ['foo', 'bar', 'forwarded'] },
};

const output = createStream(vars, streamTemplate);
expect(output).toEqual({
input: 'log',
paths: ['/usr/local/var/log/nginx/access.log'],
exclude_files: ['.gz$'],
processors: [{ add_locale: null }],
password: '',
'publisher_pipeline.disable_host': true,
tags: ['foo', 'bar', 'forwarded'],
});
});

it('should support when a value is contained in the array', () => {
const vars = {
paths: { value: ['/usr/local/var/log/nginx/access.log'] },
password: { type: 'password', value: '' },
tags: { value: ['foo', 'bar'] },
};

const output = createStream(vars, streamTemplate);
expect(output).toEqual({
input: 'log',
paths: ['/usr/local/var/log/nginx/access.log'],
exclude_files: ['.gz$'],
processors: [{ add_locale: null }],
password: '',
tags: ['foo', 'bar'],
});
});
});

it('should support optional yaml values at root level', () => {
const streamTemplate = `
input: logs
Expand Down
14 changes: 13 additions & 1 deletion x-pack/plugins/ingest_manager/server/services/epm/agent/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import Handlebars from 'handlebars';
import { safeLoad, safeDump } from 'js-yaml';
import { PackageConfigConfigRecord } from '../../../../common';

const handlebars = Handlebars.create();

export function createStream(variables: PackageConfigConfigRecord, streamTemplate: string) {
const { vars, yamlValues } = buildTemplateVariables(variables, streamTemplate);

const template = Handlebars.compile(streamTemplate, { noEscape: true });
const template = handlebars.compile(streamTemplate, { noEscape: true });
let stream = template(vars);
stream = replaceRootLevelYamlVariables(yamlValues, stream);

Expand Down Expand Up @@ -87,6 +89,16 @@ function buildTemplateVariables(variables: PackageConfigConfigRecord, streamTemp
return { vars, yamlValues };
}

function containsHelper(this: any, item: string, list: string[], options: any) {
if (Array.isArray(list) && list.includes(item)) {
if (options && options.fn) {
return options.fn(this);
}
}
return '';
}
handlebars.registerHelper('contains', containsHelper);

function replaceRootLevelYamlVariables(yamlVariables: { [k: string]: any }, yamlTemplate: string) {
if (Object.keys(yamlVariables).length === 0 || !yamlTemplate) {
return yamlTemplate;
Expand Down

0 comments on commit a7233f5

Please sign in to comment.