Skip to content

Commit

Permalink
[Synthetics] Fix location remove via public API !! (#199170)
Browse files Browse the repository at this point in the history
## Summary

Fixes #199108

Fix location remove via public API !!

### Testing

Create a Monitor that is configured in two locations:
```
POST kbn:/api/synthetics/monitors
{
  "type": "http",
  "name": "healthcheck",
  "url": "https://www.elastic.co",
  "locations": ["united_kingdom", "germany"]
}
```
Extract from the response the config_id
Update the monitor to remove one location
```
PUT kbn:/api/synthetics/monitors/<config-id>
{
  "type": "http",
  "name": "healthcheck",
  "url": "https://www.elastic.co",
  "locations": ["united_kingdom"]
}
```

This should remove Germany location from monitor !!

(cherry picked from commit 4a34105)
  • Loading branch information
shahzad31 committed Nov 7, 2024
1 parent e29cc5f commit d4d902d
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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(
{
Expand All @@ -91,7 +110,7 @@ describe('parseMonitorLocations', () => {

expect(result).toEqual({
locations: ['local'],
privateLocations: ['test-private-location-2', 'test-private-location'],
privateLocations: ['test-private-location-2'],
});
});

Expand Down Expand Up @@ -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'],
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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],
})
);
Expand Down

0 comments on commit d4d902d

Please sign in to comment.