Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] [Synthetics] Format locations for public API's (#195295) #195460

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading