From d21495bbce07ed39fdcf077c9170d012adab74a4 Mon Sep 17 00:00:00 2001 From: Shahzad Date: Tue, 8 Oct 2024 17:52:33 +0200 Subject: [PATCH] [Synthetics] Format locations for public API's (#195295) ## Summary Fixes https://github.com/elastic/kibana/issues/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 !! --- .../runtime_types/monitor_management/state.ts | 1 + .../apps/synthetics/state/monitor_list/api.ts | 1 + .../synthetics/server/routes/common.ts | 5 ++ .../saved_object_to_monitor.test.ts | 53 ++++++++++++++----- .../formatters/saved_object_to_monitor.ts | 25 ++++++++- .../routes/monitor_cruds/get_monitors_list.ts | 1 + .../synthetics/synthetics_rule_helper.ts | 2 +- .../apis/synthetics/add_monitor_project.ts | 5 +- 8 files changed, 76 insertions(+), 17 deletions(-) diff --git a/x-pack/plugins/observability_solution/synthetics/common/runtime_types/monitor_management/state.ts b/x-pack/plugins/observability_solution/synthetics/common/runtime_types/monitor_management/state.ts index 0c546eb431dea..6bde68b526723 100644 --- a/x-pack/plugins/observability_solution/synthetics/common/runtime_types/monitor_management/state.ts +++ b/x-pack/plugins/observability_solution/synthetics/common/runtime_types/monitor_management/state.ts @@ -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< diff --git a/x-pack/plugins/observability_solution/synthetics/public/apps/synthetics/state/monitor_list/api.ts b/x-pack/plugins/observability_solution/synthetics/public/apps/synthetics/state/monitor_list/api.ts index bab1062a683a4..245c2be23f9c1 100644 --- a/x-pack/plugins/observability_solution/synthetics/public/apps/synthetics/state/monitor_list/api.ts +++ b/x-pack/plugins/observability_solution/synthetics/public/apps/synthetics/state/monitor_list/api.ts @@ -35,6 +35,7 @@ function toMonitorManagementListQueryArgs( schedules: pageState.schedules, monitorQueryIds: pageState.monitorQueryIds, searchFields: [], + internal: true, }; } diff --git a/x-pack/plugins/observability_solution/synthetics/server/routes/common.ts b/x-pack/plugins/observability_solution/synthetics/server/routes/common.ts index 2edb17a77a635..c4444afcc06e8 100644 --- a/x-pack/plugins/observability_solution/synthetics/server/routes/common.ts +++ b/x-pack/plugins/observability_solution/synthetics/server/routes/common.ts @@ -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; diff --git a/x-pack/plugins/observability_solution/synthetics/server/routes/monitor_cruds/formatters/saved_object_to_monitor.test.ts b/x-pack/plugins/observability_solution/synthetics/server/routes/monitor_cruds/formatters/saved_object_to_monitor.test.ts index 0ddfda0f77874..40aec3b468a37 100644 --- a/x-pack/plugins/observability_solution/synthetics/server/routes/monitor_cruds/formatters/saved_object_to_monitor.test.ts +++ b/x-pack/plugins/observability_solution/synthetics/server/routes/monitor_cruds/formatters/saved_object_to_monitor.test.ts @@ -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', @@ -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', @@ -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', diff --git a/x-pack/plugins/observability_solution/synthetics/server/routes/monitor_cruds/formatters/saved_object_to_monitor.ts b/x-pack/plugins/observability_solution/synthetics/server/routes/monitor_cruds/formatters/saved_object_to_monitor.ts index 4f5a3b194c896..06746fc235769 100644 --- a/x-pack/plugins/observability_solution/synthetics/server/routes/monitor_cruds/formatters/saved_object_to_monitor.ts +++ b/x-pack/plugins/observability_solution/synthetics/server/routes/monitor_cruds/formatters/saved_object_to_monitor.ts @@ -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, @@ -38,11 +39,14 @@ type Result = MonitorFieldsResult & { ssl: Record; response: Record; check: Record; + 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] }), @@ -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.') ); }; @@ -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 { diff --git a/x-pack/plugins/observability_solution/synthetics/server/routes/monitor_cruds/get_monitors_list.ts b/x-pack/plugins/observability_solution/synthetics/server/routes/monitor_cruds/get_monitors_list.ts index 62f60f6b5636c..ed99e2b1d64d0 100644 --- a/x-pack/plugins/observability_solution/synthetics/server/routes/monitor_cruds/get_monitors_list.ts +++ b/x-pack/plugins/observability_solution/synthetics/server/routes/monitor_cruds/get_monitors_list.ts @@ -45,6 +45,7 @@ export const getAllSyntheticsMonitorRoute: SyntheticsRestApiRouteFactory = () => monitors: savedObjects.map((monitor) => mapSavedObjectToMonitor({ monitor, + internal: request.query?.internal, }) ), absoluteTotal, diff --git a/x-pack/test/alerting_api_integration/observability/synthetics/synthetics_rule_helper.ts b/x-pack/test/alerting_api_integration/observability/synthetics/synthetics_rule_helper.ts index 9321bc0935a80..2915cb5ee5d3b 100644 --- a/x-pack/test/alerting_api_integration/observability/synthetics/synthetics_rule_helper.ts +++ b/x-pack/test/alerting_api_integration/observability/synthetics/synthetics_rule_helper.ts @@ -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); diff --git a/x-pack/test/api_integration/apis/synthetics/add_monitor_project.ts b/x-pack/test/api_integration/apis/synthetics/add_monitor_project.ts index 9654a2bc43404..ea8821901c9e9 100644 --- a/x-pack/test/api_integration/apis/synthetics/add_monitor_project.ts +++ b/x-pack/test/api_integration/apis/synthetics/add_monitor_project.ts @@ -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); })