Skip to content

Commit

Permalink
[8.16] [Synthetics] Fix location remove via public API !! (elastic#19…
Browse files Browse the repository at this point in the history
…9170) (elastic#199326)

# Backport

This will backport the following commits from `main` to `8.16`:
- [[Synthetics] Fix location remove via public API !!
(elastic#199170)](elastic#199170)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT
[{"author":{"name":"Shahzad","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-11-07T14:37:55Z","message":"[Synthetics]
Fix location remove via public API !! (elastic#199170)\n\n##
Summary\r\n\r\nFixes
https://github.com/elastic/kibana/issues/199108\r\n\r\nFix location
remove via public API !!\r\n\r\n### Testing \r\n\r\nCreate a Monitor
that is configured in two locations:\r\n```\r\nPOST
kbn:/api/synthetics/monitors\r\n{\r\n \"type\": \"http\",\r\n \"name\":
\"healthcheck\",\r\n \"url\": \"https://www.elastic.co\",\r\n
\"locations\": [\"united_kingdom\", \"germany\"]\r\n}\r\n```\r\nExtract
from the response the config_id\r\nUpdate the monitor to remove one
location\r\n```\r\nPUT kbn:/api/synthetics/monitors/<config-id>\r\n{\r\n
\"type\": \"http\",\r\n \"name\": \"healthcheck\",\r\n \"url\":
\"https://www.elastic.co\",\r\n \"locations\":
[\"united_kingdom\"]\r\n}\r\n```\r\n\r\n\r\nThis should remove Germany
location from monitor
!!","sha":"4a341055cf374761f4b1b247d702f9e759b03d92","branchLabelMapping":{"^v9.0.0$":"main","^v8.17.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","v9.0.0","ci:project-deploy-observability","Team:obs-ux-management","v8.16.0","backport:version","v8.17.0"],"title":"[Synthetics]
Fix location remove via public API
!!","number":199170,"url":"https://github.com/elastic/kibana/pull/199170","mergeCommit":{"message":"[Synthetics]
Fix location remove via public API !! (elastic#199170)\n\n##
Summary\r\n\r\nFixes
https://github.com/elastic/kibana/issues/199108\r\n\r\nFix location
remove via public API !!\r\n\r\n### Testing \r\n\r\nCreate a Monitor
that is configured in two locations:\r\n```\r\nPOST
kbn:/api/synthetics/monitors\r\n{\r\n \"type\": \"http\",\r\n \"name\":
\"healthcheck\",\r\n \"url\": \"https://www.elastic.co\",\r\n
\"locations\": [\"united_kingdom\", \"germany\"]\r\n}\r\n```\r\nExtract
from the response the config_id\r\nUpdate the monitor to remove one
location\r\n```\r\nPUT kbn:/api/synthetics/monitors/<config-id>\r\n{\r\n
\"type\": \"http\",\r\n \"name\": \"healthcheck\",\r\n \"url\":
\"https://www.elastic.co\",\r\n \"locations\":
[\"united_kingdom\"]\r\n}\r\n```\r\n\r\n\r\nThis should remove Germany
location from monitor
!!","sha":"4a341055cf374761f4b1b247d702f9e759b03d92"}},"sourceBranch":"main","suggestedTargetBranches":["8.16","8.x"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/199170","number":199170,"mergeCommit":{"message":"[Synthetics]
Fix location remove via public API !! (elastic#199170)\n\n##
Summary\r\n\r\nFixes
https://github.com/elastic/kibana/issues/199108\r\n\r\nFix location
remove via public API !!\r\n\r\n### Testing \r\n\r\nCreate a Monitor
that is configured in two locations:\r\n```\r\nPOST
kbn:/api/synthetics/monitors\r\n{\r\n \"type\": \"http\",\r\n \"name\":
\"healthcheck\",\r\n \"url\": \"https://www.elastic.co\",\r\n
\"locations\": [\"united_kingdom\", \"germany\"]\r\n}\r\n```\r\nExtract
from the response the config_id\r\nUpdate the monitor to remove one
location\r\n```\r\nPUT kbn:/api/synthetics/monitors/<config-id>\r\n{\r\n
\"type\": \"http\",\r\n \"name\": \"healthcheck\",\r\n \"url\":
\"https://www.elastic.co\",\r\n \"locations\":
[\"united_kingdom\"]\r\n}\r\n```\r\n\r\n\r\nThis should remove Germany
location from monitor
!!","sha":"4a341055cf374761f4b1b247d702f9e759b03d92"}},{"branch":"8.16","label":"v8.16.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"8.x","label":"v8.17.0","branchLabelMappingKey":"^v8.17.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Shahzad <[email protected]>
  • Loading branch information
kibanamachine and shahzad31 authored Nov 7, 2024
1 parent 06fc368 commit 8fe43ee
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 8fe43ee

Please sign in to comment.