-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Synthetics] Process processors in kibana as yaml (#162812)
- Loading branch information
Showing
13 changed files
with
342 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
x-pack/plugins/fleet/server/saved_objects/migrations/synthetics/to_v8_10_0.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import type { SavedObjectModelTransformationContext } from '@kbn/core-saved-objects-server'; | ||
|
||
import { getBrowserPolicy } from './fixtures/8.7.0'; | ||
|
||
import { migrateSyntheticsPackagePolicyToV8100 as migration } from './to_v8_10_0'; | ||
|
||
describe('8.10.0 Synthetics Package Policy migration', () => { | ||
describe('processors migration', () => { | ||
it('handles processors field for empty values', () => { | ||
const actual = migration( | ||
getBrowserPolicy('false'), | ||
{} as SavedObjectModelTransformationContext | ||
); | ||
expect(actual.attributes?.inputs?.[3]?.streams[0]?.vars?.processors?.value).toEqual( | ||
'[{"add_fields":{"fields":{"monitor.fleet_managed":true,"config_id":"420754e9-40f2-486c-bc2e-265bafd735c5"},"target":""}}]' | ||
); | ||
expect(actual.attributes?.inputs?.[3]?.streams[0]?.compiled_stream?.processors).toEqual( | ||
'[{"add_fields":{"fields":{"monitor.fleet_managed":true,"config_id":"420754e9-40f2-486c-bc2e-265bafd735c5"},"target":""}}]' | ||
); | ||
}); | ||
|
||
it('handles processors field for project monitor', () => { | ||
const actual = migration( | ||
getBrowserPolicy('', 'test-project'), | ||
{} as SavedObjectModelTransformationContext | ||
); | ||
expect(actual.attributes?.inputs?.[3]?.streams[0]?.vars?.processors?.value).toEqual( | ||
JSON.stringify([ | ||
{ | ||
add_fields: { | ||
fields: { | ||
'monitor.fleet_managed': true, | ||
config_id: '420754e9-40f2-486c-bc2e-265bafd735c5', | ||
'monitor.project.name': 'test-project', | ||
'monitor.project.id': 'test-project', | ||
}, | ||
target: '', | ||
}, | ||
}, | ||
]) | ||
); | ||
expect(actual.attributes?.inputs?.[3]?.streams[0]?.compiled_stream.processors).toEqual( | ||
JSON.stringify([ | ||
{ | ||
add_fields: { | ||
fields: { | ||
'monitor.fleet_managed': true, | ||
config_id: '420754e9-40f2-486c-bc2e-265bafd735c5', | ||
'monitor.project.name': 'test-project', | ||
'monitor.project.id': 'test-project', | ||
}, | ||
target: '', | ||
}, | ||
}, | ||
]) | ||
); | ||
}); | ||
|
||
it('handles processors field for test now fields', () => { | ||
const actual = migration( | ||
getBrowserPolicy('', 'test-project', 'test-run-id', true), | ||
{} as SavedObjectModelTransformationContext | ||
); | ||
expect(actual.attributes?.inputs?.[3]?.streams[0]?.vars?.processors?.value).toEqual( | ||
JSON.stringify([ | ||
{ | ||
add_fields: { | ||
fields: { | ||
'monitor.fleet_managed': true, | ||
test_run_id: 'test-run-id', | ||
run_once: true, | ||
config_id: '420754e9-40f2-486c-bc2e-265bafd735c5', | ||
'monitor.project.name': 'test-project', | ||
'monitor.project.id': 'test-project', | ||
}, | ||
target: '', | ||
}, | ||
}, | ||
]) | ||
); | ||
expect(actual.attributes?.inputs?.[3]?.streams[0]?.compiled_stream.processors).toEqual( | ||
JSON.stringify([ | ||
{ | ||
add_fields: { | ||
fields: { | ||
'monitor.fleet_managed': true, | ||
test_run_id: 'test-run-id', | ||
run_once: true, | ||
config_id: '420754e9-40f2-486c-bc2e-265bafd735c5', | ||
'monitor.project.name': 'test-project', | ||
'monitor.project.id': 'test-project', | ||
}, | ||
target: '', | ||
}, | ||
}, | ||
]) | ||
); | ||
}); | ||
}); | ||
}); |
89 changes: 89 additions & 0 deletions
89
x-pack/plugins/fleet/server/saved_objects/migrations/synthetics/to_v8_10_0.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import type { SavedObjectModelDataBackfillFn } from '@kbn/core-saved-objects-server'; | ||
|
||
import type { PackagePolicy, PackagePolicyConfigRecord } from '../../../../common'; | ||
|
||
export const migrateSyntheticsPackagePolicyToV8100: SavedObjectModelDataBackfillFn< | ||
PackagePolicy, | ||
PackagePolicy | ||
> = (packagePolicyDoc) => { | ||
if ( | ||
packagePolicyDoc.attributes.package?.name !== 'synthetics' || | ||
!packagePolicyDoc.attributes.is_managed | ||
) { | ||
return packagePolicyDoc; | ||
} | ||
const updatedAttributes = packagePolicyDoc.attributes; | ||
|
||
const enabledInput = updatedAttributes.inputs.find((input) => input.enabled === true); | ||
const enabledStream = enabledInput?.streams.find((stream) => { | ||
return ['browser', 'http', 'icmp', 'tcp'].includes(stream.data_stream.dataset); | ||
}); | ||
if (!enabledStream) { | ||
return { | ||
attributes: updatedAttributes, | ||
}; | ||
} | ||
|
||
if (enabledStream.vars) { | ||
const processors = processorsFormatter(enabledStream.vars); | ||
enabledStream.vars.processors = { value: processors, type: 'yaml' }; | ||
enabledStream.compiled_stream.processors = processors; | ||
} | ||
|
||
return { | ||
attributes: updatedAttributes, | ||
}; | ||
}; | ||
|
||
type Fields = Record<string, string | boolean>; | ||
|
||
interface FieldProcessor { | ||
add_fields: { | ||
target: string; | ||
fields: Fields; | ||
}; | ||
} | ||
|
||
export const processorsFormatter = (vars: PackagePolicyConfigRecord) => { | ||
const fields: Fields = { | ||
'monitor.fleet_managed': true, | ||
}; | ||
if (vars.test_run_id?.value) { | ||
fields.test_run_id = vars.test_run_id.value; | ||
} | ||
if (vars.run_once?.value) { | ||
fields.run_once = vars.run_once.value; | ||
} | ||
if (vars.config_id?.value) { | ||
fields.config_id = vars.config_id.value; | ||
} | ||
const projName = vars['monitor.project.name']?.value; | ||
if (projName) { | ||
fields['monitor.project.name'] = projName; | ||
} | ||
const projId = vars['monitor.project.id']?.value; | ||
if (projId) { | ||
fields['monitor.project.id'] = projId; | ||
} | ||
const monId = vars['monitor.id']?.value; | ||
if (monId) { | ||
fields['monitor.id'] = monId; | ||
} | ||
const processors: FieldProcessor[] = [ | ||
{ | ||
add_fields: { | ||
fields, | ||
target: '', | ||
}, | ||
}, | ||
]; | ||
|
||
return JSON.stringify(processors); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...ynthetics/server/synthetics_service/formatters/private_formatters/processors_formatter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { ProcessorFields } from './format_synthetics_policy'; | ||
import { MonitorFields } from '../../../../common/runtime_types'; | ||
|
||
type Fields = Record<string, string | boolean>; | ||
|
||
interface FieldProcessor { | ||
add_fields: { | ||
target: string; | ||
fields: Fields; | ||
}; | ||
} | ||
|
||
export const processorsFormatter = (config: Partial<MonitorFields & ProcessorFields>) => { | ||
const fields: Fields = { | ||
'monitor.fleet_managed': true, | ||
}; | ||
if (config.test_run_id) { | ||
fields.test_run_id = config.test_run_id; | ||
} | ||
if (config.run_once) { | ||
fields.run_once = config.run_once; | ||
} | ||
if (config.config_id) { | ||
fields.config_id = config.config_id; | ||
} | ||
if (config['monitor.project.name']) { | ||
fields['monitor.project.name'] = config['monitor.project.name']; | ||
} | ||
if (config['monitor.project.id']) { | ||
fields['monitor.project.id'] = config['monitor.project.id']; | ||
} | ||
if (config['monitor.id']) { | ||
fields['monitor.id'] = config['monitor.id']; | ||
} | ||
const processors: FieldProcessor[] = [ | ||
{ | ||
add_fields: { | ||
fields, | ||
target: '', | ||
}, | ||
}, | ||
]; | ||
|
||
return JSON.stringify(processors); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.