Skip to content

Commit

Permalink
update to backfill
Browse files Browse the repository at this point in the history
  • Loading branch information
shahzad31 committed Aug 2, 2023
1 parent a5d96ed commit 323a494
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 24 deletions.
4 changes: 2 additions & 2 deletions x-pack/plugins/fleet/server/saved_objects/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,8 @@ const getSavedObjectTypes = (): { [key: string]: SavedObjectsType } => ({
backfillFn: migratePackagePolicyToV8100,
},
{
type: 'unsafe_transform',
transformFn: migrateSyntheticsPackagePolicyToV8100,
type: 'data_backfill',
backfillFn: migrateSyntheticsPackagePolicyToV8100,
},
],
schemas: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@ 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 { document: actual } = migration(
const actual = migration(
getBrowserPolicy('false'),
{} as SavedObjectModelTransformationContext
);
expect(actual.attributes?.inputs[3]?.streams[0]?.vars?.processors?.value).toEqual(
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(
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 { document: actual } = migration(
const actual = migration(
getBrowserPolicy('', 'test-project'),
{} as SavedObjectModelTransformationContext
);
expect(actual.attributes?.inputs[3]?.streams[0]?.vars?.processors?.value).toEqual(
expect(actual.attributes?.inputs?.[3]?.streams[0]?.vars?.processors?.value).toEqual(
JSON.stringify([
{
add_fields: {
Expand All @@ -46,7 +46,7 @@ describe('8.10.0 Synthetics Package Policy migration', () => {
},
])
);
expect(actual.attributes?.inputs[3]?.streams[0]?.compiled_stream.processors).toEqual(
expect(actual.attributes?.inputs?.[3]?.streams[0]?.compiled_stream.processors).toEqual(
JSON.stringify([
{
add_fields: {
Expand All @@ -64,11 +64,11 @@ describe('8.10.0 Synthetics Package Policy migration', () => {
});

it('handles processors field for test now fields', () => {
const { document: actual } = migration(
const actual = migration(
getBrowserPolicy('', 'test-project', 'test-run-id', true),
{} as SavedObjectModelTransformationContext
);
expect(actual.attributes?.inputs[3]?.streams[0]?.vars?.processors?.value).toEqual(
expect(actual.attributes?.inputs?.[3]?.streams[0]?.vars?.processors?.value).toEqual(
JSON.stringify([
{
add_fields: {
Expand All @@ -85,7 +85,7 @@ describe('8.10.0 Synthetics Package Policy migration', () => {
},
])
);
expect(actual.attributes?.inputs[3]?.streams[0]?.compiled_stream.processors).toEqual(
expect(actual.attributes?.inputs?.[3]?.streams[0]?.compiled_stream.processors).toEqual(
JSON.stringify([
{
add_fields: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,29 @@
* 2.0.
*/

import type { SavedObjectUnsanitizedDoc } from '@kbn/core/server';

import type { SavedObjectModelUnsafeTransformFn } from '@kbn/core-saved-objects-server';
import type { SavedObjectModelDataBackfillFn } from '@kbn/core-saved-objects-server';

import type { PackagePolicy, PackagePolicyConfigRecord } from '../../../../common';

export const migrateSyntheticsPackagePolicyToV8100: SavedObjectModelUnsafeTransformFn<
export const migrateSyntheticsPackagePolicyToV8100: SavedObjectModelDataBackfillFn<
PackagePolicy,
PackagePolicy
> = (packagePolicyDoc) => {
if (
packagePolicyDoc.attributes.package?.name !== 'synthetics' ||
!packagePolicyDoc.attributes.is_managed
) {
return {
document: packagePolicyDoc,
};
return packagePolicyDoc;
}
const updatedPackagePolicyDoc: SavedObjectUnsanitizedDoc<PackagePolicy> = packagePolicyDoc;
const updatedAttributes = packagePolicyDoc.attributes;

const enabledInput = updatedPackagePolicyDoc.attributes.inputs.find(
(input) => input.enabled === true
);
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 {
document: updatedPackagePolicyDoc,
attributes: updatedAttributes,
};
}

Expand All @@ -44,7 +38,7 @@ export const migrateSyntheticsPackagePolicyToV8100: SavedObjectModelUnsafeTransf
}

return {
document: updatedPackagePolicyDoc,
attributes: updatedAttributes,
};
};

Expand Down

0 comments on commit 323a494

Please sign in to comment.