-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Ingest] Support yaml variables in datasource #64459
Changes from 7 commits
41ba84b
e3ec291
792bf79
d3933c0
59ae793
2268f1d
8d33767
e10a43b
87a185c
4836c11
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,12 +5,73 @@ | |
*/ | ||
|
||
import Handlebars from 'handlebars'; | ||
import { safeLoad } from 'js-yaml'; | ||
import { DatasourceConfigRecord } from '../../../../common'; | ||
|
||
interface StreamVars { | ||
[k: string]: string | string[]; | ||
function isValidKey(key: string) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not a blocker for this PR but future improvement: I think these services belong in |
||
return key !== '__proto__' && key !== 'constructor' && key !== 'prototype'; | ||
} | ||
|
||
export function createStream(vars: StreamVars, streamTemplate: string) { | ||
function replaceVariablesInYaml(yamlVariables: { [k: string]: any }, yaml: any) { | ||
if (Object.keys(yamlVariables).length === 0 || !yaml) { | ||
return yaml; | ||
} | ||
|
||
Object.entries(yaml).forEach(([key, value]: [string, any]) => { | ||
if (typeof value === 'object') { | ||
yaml[key] = replaceVariablesInYaml(yamlVariables, value); | ||
} | ||
|
||
if (typeof value === 'string' && value in yamlVariables) { | ||
yaml[key] = yamlVariables[value]; | ||
} | ||
}); | ||
|
||
return yaml; | ||
} | ||
|
||
function buildTemplateVariables(variables: DatasourceConfigRecord) { | ||
const yamlValues: { [k: string]: any } = {}; | ||
const vars = Object.entries(variables).reduce((acc, [key, recordEntry]) => { | ||
// support variables with . like key.patterns | ||
const keyParts = key.split('.'); | ||
const lastKeyPart = keyParts.pop(); | ||
|
||
if (!lastKeyPart || !isValidKey(lastKeyPart)) { | ||
throw new Error('Invalid key'); | ||
} | ||
|
||
let varPart = acc; | ||
for (const keyPart of keyParts) { | ||
if (!isValidKey(keyPart)) { | ||
throw new Error('Invalid key'); | ||
} | ||
if (!varPart[keyPart]) { | ||
varPart[keyPart] = {}; | ||
} | ||
varPart = varPart[keyPart]; | ||
} | ||
|
||
if (recordEntry.type && recordEntry.type === 'yaml') { | ||
const yamlKeyPlaceholder = `##${key}##`; | ||
varPart[lastKeyPart] = `"${yamlKeyPlaceholder}"`; | ||
nchaulet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
yamlValues[yamlKeyPlaceholder] = recordEntry.value ? safeLoad(recordEntry.value) : null; | ||
} else { | ||
varPart[lastKeyPart] = recordEntry.value; | ||
} | ||
return acc; | ||
}, {} as { [k: string]: any }); | ||
|
||
return { vars, yamlValues }; | ||
} | ||
|
||
export function createStream(variables: DatasourceConfigRecord, streamTemplate: string) { | ||
const { vars, yamlValues } = buildTemplateVariables(variables); | ||
|
||
const template = Handlebars.compile(streamTemplate); | ||
return template(vars); | ||
const stream = template(vars); | ||
|
||
const yamlFromStream = safeLoad(stream, {}); | ||
|
||
return replaceVariablesInYaml(yamlValues, yamlFromStream); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we changed
createStream
to accept a map ofstring
->any
orunknown
(vs currentstring
->string
) I believe we could do this test likeThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could retranslate to a yaml after, but it's going to be stored as a JSON and send to the agent as a JSON, so if we can avoid one other round of conversion to yaml -> to json