Skip to content

Commit

Permalink
[Synthetics] Format locations for public API's (elastic#195295)
Browse files Browse the repository at this point in the history
## Summary

Fixes elastic#194901 !!

Format locations for public API's !!

Public API has been refactored to return list of location ids just like
they are provided in request !!

(cherry picked from commit d21495b)
  • Loading branch information
shahzad31 committed Oct 8, 2024
1 parent f9c640d commit db5affd
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const FetchMonitorManagementListQueryArgsCodec = t.partial({
projects: t.array(t.string),
schedules: t.array(t.string),
monitorQueryIds: t.array(t.string),
internal: t.boolean,
});

export type FetchMonitorManagementListQueryArgs = t.TypeOf<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ function toMonitorManagementListQueryArgs(
schedules: pageState.schedules,
monitorQueryIds: pageState.monitorQueryIds,
searchFields: [],
internal: true,
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ export const QuerySchema = schema.object({
status: StringOrArraySchema,
searchAfter: schema.maybe(schema.arrayOf(schema.string())),
monitorQueryIds: StringOrArraySchema,
internal: schema.maybe(
schema.boolean({
defaultValue: false,
})
),
});

export type MonitorsQuery = TypeOf<typeof QuerySchema>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,8 @@ describe('mergeSourceMonitor', () => {
id: 'todos-lightweight-test-projects-default',
ipv4: true,
ipv6: true,
locations: [
{
geo: {
lat: 41.25,
lon: -95.86,
},
id: 'us_central',
isServiceManaged: true,
label: 'North America - US Central',
},
],
locations: ['us_central', 'us_east'],
private_locations: ['pvt_us_east'],
max_redirects: 0,
mode: 'any',
name: 'Todos Lightweight',
Expand Down Expand Up @@ -166,13 +157,31 @@ describe('mergeSourceMonitor', () => {
locations: [
{
geo: {
lat: 41.25,
lon: -95.86,
lat: 41.25,
},
id: 'us_central',
isServiceManaged: true,
id: 'us_central',
label: 'North America - US Central',
},
{
geo: {
lon: -95.86,
lat: 41.25,
},
isServiceManaged: true,
id: 'us-east4-a',
label: 'US East',
},
{
geo: {
lon: -95.86,
lat: 41.25,
},
isServiceManaged: false,
id: 'pvt_us_east',
label: 'US East (Private)',
},
],
max_redirects: '0',
mode: 'any',
Expand Down Expand Up @@ -240,6 +249,24 @@ const testMonitor = {
id: 'us_central',
label: 'North America - US Central',
},
{
geo: {
lon: -95.86,
lat: 41.25,
},
isServiceManaged: true,
id: 'us-east4-a',
label: 'US East',
},
{
geo: {
lon: -95.86,
lat: 41.25,
},
isServiceManaged: false,
id: 'pvt_us_east',
label: 'US East (Private)',
},
],
namespace: 'default',
origin: 'project',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import { SavedObject } from '@kbn/core/server';
import { mergeWith, omit, omitBy } from 'lodash';
import { LocationsMap } from '../../../synthetics_service/project_monitor/normalizers/common_fields';
import {
ConfigKey,
EncryptedSyntheticsMonitor,
Expand Down Expand Up @@ -38,11 +39,14 @@ type Result = MonitorFieldsResult & {
ssl: Record<string, any>;
response: Record<string, any>;
check: Record<string, any>;
locations: string[];
private_locations: string[];
};

export const transformPublicKeys = (result: Result) => {
let formattedResult = {
...result,
...formatLocations(result),
[ConfigKey.PARAMS]: formatParams(result),
retest_on_failure: (result[ConfigKey.MAX_ATTEMPTS] ?? 1) > 1,
...(result[ConfigKey.HOSTS] && { host: result[ConfigKey.HOSTS] }),
Expand All @@ -66,8 +70,7 @@ export const transformPublicKeys = (result: Result) => {

return omitBy(
res,
(value, key) =>
key.startsWith('response.') || key.startsWith('ssl.') || key.startsWith('check.')
(_, key) => key.startsWith('response.') || key.startsWith('ssl.') || key.startsWith('check.')
);
};

Expand Down Expand Up @@ -105,6 +108,24 @@ const customizer = (destVal: any, srcValue: any, key: string) => {
}
};

const formatLocations = (config: MonitorFields) => {
const locMap = Object.entries(LocationsMap);
const locations = config[ConfigKey.LOCATIONS]
?.filter((location) => location.isServiceManaged)
.map((location) => {
return locMap.find(([_key, value]) => value === location.id)?.[0] ?? location.id;
});

const privateLocations = config[ConfigKey.LOCATIONS]
?.filter((location) => !location.isServiceManaged)
.map((location) => location.id);

return {
...(locations && { locations }),
...(privateLocations && { private_locations: privateLocations }),
};
};

const formatParams = (config: MonitorFields) => {
if (config[ConfigKey.PARAMS]) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export const getAllSyntheticsMonitorRoute: SyntheticsRestApiRouteFactory = () =>
monitors: savedObjects.map((monitor) =>
mapSavedObjectToMonitor({
monitor,
internal: request.query?.internal,
})
),
absoluteTotal,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export class SyntheticsRuleHelper {
schedule: 1,
};
const res = await this.supertest
.post(SYNTHETICS_API_URLS.SYNTHETICS_MONITORS)
.post(SYNTHETICS_API_URLS.SYNTHETICS_MONITORS + '?internal=true')
.set('kbn-xsrf', 'true')
.send(testData);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1814,7 +1814,10 @@ export default function ({ getService }: FtrProviderContext) {
projectMonitors.monitors.map((monitor) => {
return supertest
.get(SYNTHETICS_API_URLS.SYNTHETICS_MONITORS)
.query({ filter: `${syntheticsMonitorType}.attributes.journey_id: ${monitor.id}` })
.query({
filter: `${syntheticsMonitorType}.attributes.journey_id: ${monitor.id}`,
internal: true,
})
.set('kbn-xsrf', 'true')
.expect(200);
})
Expand Down

0 comments on commit db5affd

Please sign in to comment.