Skip to content

Commit

Permalink
[Synthetics] Clarify location geo property type (#162371)
Browse files Browse the repository at this point in the history
  • Loading branch information
shahzad31 authored Jul 27, 2023
1 parent 5a2b80f commit 698ff71
Show file tree
Hide file tree
Showing 8 changed files with 221 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ describe('checking migration metadata changes on all registered SO types', () =>
"spaces-usage-stats": "3abca98713c52af8b30300e386c7779b3025a20e",
"synthetics-monitor": "33ddc4b8979f378edf58bcc7ba13c5c5b572f42d",
"synthetics-param": "3ebb744e5571de678b1312d5c418c8188002cf5e",
"synthetics-privates-locations": "9cfbd6d1d2e2c559bf96dd6fbc27ff0c47719dd3",
"synthetics-privates-locations": "f53d799d5c9bc8454aaa32c6abc99a899b025d5c",
"tag": "e2544392fe6563e215bb677abc8b01c2601ef2dc",
"task": "04f30bd7bae923f3a53c31ab3b9745a93872fc02",
"telemetry": "7b00bcf1c7b4f6db1192bb7405a6a63e78b699fd",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ export const PrivateLocationCodec = t.intersection([
isServiceManaged: t.boolean,
isInvalid: t.boolean,
tags: t.array(t.string),
/* Empty Lat lon was accidentally saved as an empty string instead of undefined or null
* Need a migration to fix */
geo: t.interface({
lat: t.union([t.string, t.number, t.null]),
lon: t.union([t.string, t.number, t.null]),
lat: t.number,
lon: t.number,
}),
}),
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const testLocations2 = {
label: 'BEEP',
agentPolicyId: 'e3134290-0f73-11ee-ba15-159f4f728dec',
id: 'e3134290-0f73-11ee-ba15-159f4f728dec',
geo: { lat: '-10', lon: '20' },
geo: { lat: -10, lon: 20 },
concurrentMonitors: 1,
isInvalid: true,
isServiceManaged: true,
Expand Down Expand Up @@ -113,8 +113,8 @@ describe('toClientContract', () => {
agentPolicyId: 'e3134290-0f73-11ee-ba15-159f4f728dec',
concurrentMonitors: 1,
geo: {
lat: '-10',
lon: '20',
lat: -10,
lon: 20,
},
id: 'e3134290-0f73-11ee-ba15-159f4f728dec',
isInvalid: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ export const toClientContract = (
isServiceManaged: false,
isInvalid: !Boolean(agentPolicies?.find((policy) => policy.id === location.agentPolicyId)),
tags: location.tags,
geo: {
lat: location.geo?.lat ?? null,
lon: location.geo?.lon ?? null,
},
geo: location.geo,
})),
};
};
Expand All @@ -41,9 +38,6 @@ export const toSavedObjectContract = (location: PrivateLocation): PrivateLocatio
concurrentMonitors: location.concurrentMonitors,
tags: location.tags,
isServiceManaged: false,
geo: {
lat: location.geo?.lat ?? null,
lon: location.geo?.lon ?? null,
},
geo: location.geo,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ export const PrivateLocationAttributesCodec = t.intersection([
t.partial({
tags: t.array(t.string),
geo: t.interface({
lat: t.union([t.null, t.number, t.string]),
lon: t.union([t.null, t.number, t.string]),
lat: t.number,
lon: t.number,
}),
}),
]);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
/*
* 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 { transformGeoProperty } from './model_version_1';
import { privateLocationsSavedObjectName } from '../../../../common/saved_objects/private_locations';

describe('model version 1 migration', () => {
const testLocation = {
label: 'us-east-1',
id: 'us-east-1',
geo: {
lat: '40.7128',
lon: '-74.0060',
},
agentPolicyId: 'agentPolicyId',
concurrentMonitors: 1,
};
const testObject = {
type: privateLocationsSavedObjectName,
id: 'synthetics-privates-locations-singleton',
attributes: {
locations: [testLocation],
},
};
it('should return expected result', function () {
const result = transformGeoProperty(testObject, {} as any);
expect(result.document).toEqual({
attributes: {
locations: [
{
agentPolicyId: 'agentPolicyId',
concurrentMonitors: 1,
geo: {
lat: 40.7128,
lon: -74.006,
},
id: 'us-east-1',
isServiceManaged: false,
label: 'us-east-1',
},
],
},
id: 'synthetics-privates-locations-singleton',
type: 'synthetics-privates-locations',
});
});

it('should return expected result for zero values', function () {
testLocation.geo.lat = '0';
testLocation.geo.lon = '0';
const result = transformGeoProperty(testObject, {} as any);
expect(result.document).toEqual({
attributes: {
locations: [
{
agentPolicyId: 'agentPolicyId',
concurrentMonitors: 1,
geo: {
lat: 0,
lon: 0,
},
id: 'us-east-1',
isServiceManaged: false,
label: 'us-east-1',
},
],
},
id: 'synthetics-privates-locations-singleton',
type: 'synthetics-privates-locations',
});
});

it('should return expected result for zero integers', function () {
// @ts-ignore
testLocation.geo.lat = 0;
// @ts-ignore
testLocation.geo.lon = 0;
const result = transformGeoProperty(testObject, {} as any);
expect(result.document).toEqual({
attributes: {
locations: [
{
agentPolicyId: 'agentPolicyId',
concurrentMonitors: 1,
geo: {
lat: 0,
lon: 0,
},
id: 'us-east-1',
isServiceManaged: false,
label: 'us-east-1',
},
],
},
id: 'synthetics-privates-locations-singleton',
type: 'synthetics-privates-locations',
});
});

it('should return expected result for empty values', function () {
// @ts-ignore
testLocation.geo.lat = '';
// @ts-ignore
testLocation.geo.lon = '';
const result = transformGeoProperty(testObject, {} as any);
expect(result.document).toEqual({
attributes: {
locations: [
{
agentPolicyId: 'agentPolicyId',
concurrentMonitors: 1,
geo: {
lat: 0,
lon: 0,
},
id: 'us-east-1',
isServiceManaged: false,
label: 'us-east-1',
},
],
},
id: 'synthetics-privates-locations-singleton',
type: 'synthetics-privates-locations',
});
});
it('should return expected result for null values', function () {
// @ts-ignore
testLocation.geo.lat = null;
// @ts-ignore
testLocation.geo.lon = null;
const result = transformGeoProperty(testObject, {} as any);
expect(result.document).toEqual({
attributes: {
locations: [
{
agentPolicyId: 'agentPolicyId',
concurrentMonitors: 1,
geo: {
lat: 0,
lon: 0,
},
id: 'us-east-1',
isServiceManaged: false,
label: 'us-east-1',
},
],
},
id: 'synthetics-privates-locations-singleton',
type: 'synthetics-privates-locations',
});
});

it('should return expected result for undefined values', function () {
// @ts-ignore
testLocation.geo = undefined;
const result = transformGeoProperty(testObject, {} as any);
expect(result.document).toEqual({
attributes: {
locations: [
{
agentPolicyId: 'agentPolicyId',
concurrentMonitors: 1,
geo: {
lat: 0,
lon: 0,
},
id: 'us-east-1',
isServiceManaged: false,
label: 'us-east-1',
},
],
},
id: 'synthetics-privates-locations-singleton',
type: 'synthetics-privates-locations',
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
* 2.0.
*/
import * as t from 'io-ts';
import type { SavedObjectMigrationFn } from '@kbn/core/server';
import type { SyntheticsPrivateLocationsAttributes } from '../../../../../runtime_types/private_locations';
import {
SavedObjectModelTransformationFn,
SavedObjectsModelVersion,
} from '@kbn/core-saved-objects-server';
import { SyntheticsPrivateLocationsAttributes } from '../../../runtime_types/private_locations';

export const PrivateLocationAttributesCodecLegacy = t.intersection([
t.interface({
Expand All @@ -29,22 +32,33 @@ export type SyntheticsPrivateLocationsAttributesLegacy = t.TypeOf<
typeof SyntheticsPrivateLocationsAttributesCodecLegacy
>;

export const migration8100: SavedObjectMigrationFn<
export const transformGeoProperty: SavedObjectModelTransformationFn<
SyntheticsPrivateLocationsAttributesLegacy,
SyntheticsPrivateLocationsAttributes
> = (privateLocationDoc) => {
const { locations } = privateLocationDoc.attributes;
return {
...privateLocationDoc,
attributes: {
locations: locations.map((location) => ({
...location,
geo: {
lat: location.geo?.lat ? Number(location.geo?.lat) : null,
lon: location.geo?.lon ? Number(location.geo.lon) : null,
},
isServiceManaged: false,
})),
document: {
...privateLocationDoc,
attributes: {
locations: locations.map((location) => ({
...location,
geo: {
lat: Number(location.geo?.lat ?? 0),
lon: Number(location.geo?.lon ?? 0),
},
isServiceManaged: false,
})),
},
},
};
};

export const modelVersion1: SavedObjectsModelVersion = {
changes: [
{
type: 'unsafe_transform',
transformFn: transformGeoProperty,
},
],
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import { SavedObjectsType } from '@kbn/core/server';
import { modelVersion1 } from './migrations/private_locations/model_version_1';
import { privateLocationsSavedObjectName } from '../../common/saved_objects/private_locations';
export const privateLocationsSavedObjectId = 'synthetics-privates-locations-singleton';

Expand All @@ -25,4 +26,7 @@ export const PRIVATE_LOCATIONS_SAVED_OBJECT_TYPE: SavedObjectsType = {
management: {
importableAndExportable: true,
},
modelVersions: {
1: modelVersion1,
},
};

0 comments on commit 698ff71

Please sign in to comment.