diff --git a/x-pack/plugins/observability_solution/synthetics/server/routes/monitor_cruds/add_monitor/utils.test.ts b/x-pack/plugins/observability_solution/synthetics/server/routes/monitor_cruds/add_monitor/utils.test.ts index c6bd1d330bc86..d9d1a23196055 100644 --- a/x-pack/plugins/observability_solution/synthetics/server/routes/monitor_cruds/add_monitor/utils.test.ts +++ b/x-pack/plugins/observability_solution/synthetics/server/routes/monitor_cruds/add_monitor/utils.test.ts @@ -33,6 +33,11 @@ describe('parseMonitorLocations', () => { id: 'local', isServiceManaged: true, }; + const localLoc2 = { + label: 'Local 2', + id: 'local2', + isServiceManaged: true, + }; it('should return expected', function () { const result = parseMonitorLocations({ @@ -81,6 +86,20 @@ describe('parseMonitorLocations', () => { }); }); + it('should handle editing location', function () { + const result = parseMonitorLocations( + { + locations: ['local'], + } as any, + [localLoc, localLoc2] + ); + + expect(result).toEqual({ + locations: ['local'], + privateLocations: [], + }); + }); + it('should add private locations to existing', function () { const result = parseMonitorLocations( { @@ -91,7 +110,7 @@ describe('parseMonitorLocations', () => { expect(result).toEqual({ locations: ['local'], - privateLocations: ['test-private-location-2', 'test-private-location'], + privateLocations: ['test-private-location-2'], }); }); @@ -185,4 +204,19 @@ describe('parseMonitorLocations', () => { privateLocations: [], }); }); + + it('should handle private location objects', function () { + const result = parseMonitorLocations( + { + locations: [pvtLoc1], + } as any, + [localLoc, pvtLoc1], + true + ); + + expect(result).toEqual({ + locations: [], + privateLocations: ['test-private-location'], + }); + }); }); diff --git a/x-pack/plugins/observability_solution/synthetics/server/routes/monitor_cruds/add_monitor/utils.ts b/x-pack/plugins/observability_solution/synthetics/server/routes/monitor_cruds/add_monitor/utils.ts index 0d096ee21745f..2c58e868d827f 100644 --- a/x-pack/plugins/observability_solution/synthetics/server/routes/monitor_cruds/add_monitor/utils.ts +++ b/x-pack/plugins/observability_solution/synthetics/server/routes/monitor_cruds/add_monitor/utils.ts @@ -25,7 +25,7 @@ export const getPrivateLocationsForMonitor = async ( export const parseMonitorLocations = ( monitorPayload: CreateMonitorPayLoad, prevLocations?: MonitorFields['locations'], - internal: boolean = false + internal = false ) => { const { locations, private_locations: privateLocations } = monitorPayload; @@ -42,41 +42,33 @@ export const parseMonitorLocations = ( pvtLocs = prevLocations.filter((loc) => !loc.isServiceManaged).map((loc) => loc.id); } else { if (prevLocations && !internal) { + const prevPublicLocs = prevLocations + .filter((loc) => loc.isServiceManaged) + .map((loc) => loc.id); + const prevPrivateLocs = prevLocations + .filter((loc) => !loc.isServiceManaged) + .map((loc) => loc.id); + if (!locations && !privateLocations) { - locs = prevLocations.filter((loc) => loc.isServiceManaged).map((loc) => loc.id); - pvtLocs = prevLocations.filter((loc) => !loc.isServiceManaged).map((loc) => loc.id); + locs = prevPublicLocs; + pvtLocs = prevPrivateLocs; } else { if (!privateLocations) { - pvtLocs = [ - ...(pvtLocs ?? []), - ...prevLocations.filter((loc) => !loc.isServiceManaged).map((loc) => loc.id), - ]; + pvtLocs = [...(pvtLocs ?? []), ...prevPrivateLocs]; if (locations?.length === 0) { locs = []; - } else { - locs = [ - ...(locs ?? []), - ...prevLocations.filter((loc) => loc.isServiceManaged).map((loc) => loc.id), - ]; } } if (!locations) { - locs = [ - ...(locs ?? []), - ...prevLocations.filter((loc) => loc.isServiceManaged).map((loc) => loc.id), - ]; + locs = [...(locs ?? []), ...prevPublicLocs]; if (privateLocations?.length === 0) { pvtLocs = []; - } else { - pvtLocs = [ - ...(pvtLocs ?? []), - ...prevLocations.filter((loc) => !loc.isServiceManaged).map((loc) => loc.id), - ]; } } } } } + return { locations: Array.from(new Set(locs)), privateLocations: Array.from(new Set(pvtLocs)), diff --git a/x-pack/test/api_integration/apis/synthetics/edit_monitor_public_api.ts b/x-pack/test/api_integration/apis/synthetics/edit_monitor_public_api.ts index e52d83ce9b263..e8112627397c4 100644 --- a/x-pack/test/api_integration/apis/synthetics/edit_monitor_public_api.ts +++ b/x-pack/test/api_integration/apis/synthetics/edit_monitor_public_api.ts @@ -270,14 +270,15 @@ export default function ({ getService }: FtrProviderContext) { ...updates, revision: 4, url: 'https://www.google.com', - locations: [localLoc, pvtLoc], + locations: [pvtLoc], }) ); }); it('can remove private location from existing monitor', async () => { - const monitor = { - private_locations: [], + let monitor: any = { + locations: [localLoc.id], + private_locations: [pvtLoc.id], }; const { body: result } = await editMonitorAPI(monitorId, monitor); @@ -288,6 +289,22 @@ export default function ({ getService }: FtrProviderContext) { ...updates, revision: 5, url: 'https://www.google.com', + locations: [localLoc, pvtLoc], + }) + ); + + monitor = { + private_locations: [], + }; + + const { body: result1 } = await editMonitorAPI(monitorId, monitor); + + expect(result1).eql( + omitMonitorKeys({ + ...defaultFields, + ...updates, + revision: 6, + url: 'https://www.google.com', locations: [localLoc], }) );