Skip to content

Commit

Permalink
[Agent Packages] Extend 'contains' helper to work on strings (#102786) (
Browse files Browse the repository at this point in the history
#102943)

* Extend 'contains' helper to work on strings

* remove stray import

Co-authored-by: Andrew Stucki <andrew.stucki@elastic.co>
  • Loading branch information
kibanamachine and Andrew Stucki committed Jun 22, 2021
1 parent 9070345 commit 119a7a0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
29 changes: 29 additions & 0 deletions x-pack/plugins/fleet/server/services/epm/agent/agent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ processors:
password: {{password}}
{{#if password}}
hidden_password: {{password}}
{{/if}}
`;
const streamTemplateWithString = `
{{#if (contains ".pcap" file)}}
pcap: true
{{else}}
pcap: false
{{/if}}
`;

Expand Down Expand Up @@ -168,6 +175,28 @@ hidden_password: {{password}}
tags: ['foo', 'bar'],
});
});

it('should support strings', () => {
const vars = {
file: { value: 'foo.pcap' },
};

const output = compileTemplate(vars, streamTemplateWithString);
expect(output).toEqual({
pcap: true,
});
});

it('should support strings with no match', () => {
const vars = {
file: { value: 'file' },
};

const output = compileTemplate(vars, streamTemplateWithString);
expect(output).toEqual({
pcap: false,
});
});
});

it('should support optional yaml values at root level', () => {
Expand Down
5 changes: 3 additions & 2 deletions x-pack/plugins/fleet/server/services/epm/agent/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,12 @@ function buildTemplateVariables(variables: PackagePolicyConfigRecord, templateSt
return { vars, yamlValues };
}

function containsHelper(this: any, item: string, list: string[], options: any) {
if (Array.isArray(list) && list.includes(item)) {
function containsHelper(this: any, item: string, check: string | string[], options: any) {
if ((Array.isArray(check) || typeof check === 'string') && check.includes(item)) {
if (options && options.fn) {
return options.fn(this);
}
return true;
}
return '';
}
Expand Down

0 comments on commit 119a7a0

Please sign in to comment.