diff --git a/x-pack/plugins/synthetics/server/legacy_uptime/routes/types.ts b/x-pack/plugins/synthetics/server/legacy_uptime/routes/types.ts index f586a434acdb5..7d93077c7ef16 100644 --- a/x-pack/plugins/synthetics/server/legacy_uptime/routes/types.ts +++ b/x-pack/plugins/synthetics/server/legacy_uptime/routes/types.ts @@ -111,10 +111,10 @@ export type UMRouteHandler = ({ subject, }: UptimeRouteContext) => IKibanaResponse | Promise>; -export interface RouteContext { +export interface RouteContext> { uptimeEsClient: UptimeEsClient; context: UptimeRequestHandlerContext; - request: SyntheticsRequest; + request: KibanaRequest, Query, Record>; response: KibanaResponseFactory; savedObjectsClient: SavedObjectsClientContract; server: UptimeServerSetup; diff --git a/x-pack/plugins/synthetics/server/routes/common.ts b/x-pack/plugins/synthetics/server/routes/common.ts index c17b4a133fd2e..6cfac7e22a36a 100644 --- a/x-pack/plugins/synthetics/server/routes/common.ts +++ b/x-pack/plugins/synthetics/server/routes/common.ts @@ -12,6 +12,10 @@ import { EncryptedSyntheticsMonitor, ServiceLocations } from '../../common/runti import { monitorAttributes, syntheticsMonitorType } from '../../common/types/saved_objects'; import { RouteContext } from '../legacy_uptime/routes'; +const StringOrArraySchema = schema.maybe( + schema.oneOf([schema.string(), schema.arrayOf(schema.string())]) +); + export const QuerySchema = schema.object({ page: schema.maybe(schema.number()), perPage: schema.maybe(schema.number()), @@ -19,13 +23,12 @@ export const QuerySchema = schema.object({ sortOrder: schema.maybe(schema.oneOf([schema.literal('desc'), schema.literal('asc')])), query: schema.maybe(schema.string()), filter: schema.maybe(schema.string()), - tags: schema.maybe(schema.oneOf([schema.string(), schema.arrayOf(schema.string())])), - monitorTypes: schema.maybe(schema.oneOf([schema.string(), schema.arrayOf(schema.string())])), - locations: schema.maybe(schema.oneOf([schema.string(), schema.arrayOf(schema.string())])), - projects: schema.maybe(schema.oneOf([schema.string(), schema.arrayOf(schema.string())])), - schedules: schema.maybe(schema.oneOf([schema.string(), schema.arrayOf(schema.string())])), - status: schema.maybe(schema.oneOf([schema.string(), schema.arrayOf(schema.string())])), - fields: schema.maybe(schema.arrayOf(schema.string())), + tags: StringOrArraySchema, + monitorTypes: StringOrArraySchema, + locations: StringOrArraySchema, + projects: StringOrArraySchema, + schedules: StringOrArraySchema, + status: StringOrArraySchema, searchAfter: schema.maybe(schema.arrayOf(schema.string())), }); @@ -34,12 +37,12 @@ export type MonitorsQuery = TypeOf; export const OverviewStatusSchema = schema.object({ query: schema.maybe(schema.string()), filter: schema.maybe(schema.string()), - tags: schema.maybe(schema.oneOf([schema.string(), schema.arrayOf(schema.string())])), - monitorTypes: schema.maybe(schema.oneOf([schema.string(), schema.arrayOf(schema.string())])), - locations: schema.maybe(schema.oneOf([schema.string(), schema.arrayOf(schema.string())])), - projects: schema.maybe(schema.oneOf([schema.string(), schema.arrayOf(schema.string())])), - schedules: schema.maybe(schema.oneOf([schema.string(), schema.arrayOf(schema.string())])), - status: schema.maybe(schema.oneOf([schema.string(), schema.arrayOf(schema.string())])), + tags: StringOrArraySchema, + monitorTypes: StringOrArraySchema, + locations: StringOrArraySchema, + projects: StringOrArraySchema, + schedules: StringOrArraySchema, + status: StringOrArraySchema, scopeStatusByLocation: schema.maybe(schema.boolean()), }); @@ -56,7 +59,8 @@ export const SEARCH_FIELDS = [ ]; export const getMonitors = async ( - context: RouteContext + context: RouteContext, + { fields }: { fields?: string[] } = {} ): Promise> => { const { perPage = 50, @@ -68,11 +72,10 @@ export const getMonitors = async ( monitorTypes, locations, filter = '', - fields, searchAfter, projects, schedules, - } = context.request.query as MonitorsQuery; + } = context.request.query; const filterStr = await getMonitorFilters({ filter, @@ -93,8 +96,8 @@ export const getMonitors = async ( searchFields: SEARCH_FIELDS, search: query ? `${query}*` : undefined, filter: filterStr, - fields, searchAfter, + fields, }); }; diff --git a/x-pack/plugins/synthetics/server/routes/index.ts b/x-pack/plugins/synthetics/server/routes/index.ts index 5892f4e3be641..70e5145b7d8f0 100644 --- a/x-pack/plugins/synthetics/server/routes/index.ts +++ b/x-pack/plugins/synthetics/server/routes/index.ts @@ -23,7 +23,6 @@ import { getSyntheticsEnablementRoute, } from './synthetics_service/enablement'; import { - getAllSyntheticsMonitorRoute, getSyntheticsMonitorOverviewRoute, getSyntheticsMonitorRoute, } from './monitor_cruds/get_monitor'; @@ -53,6 +52,7 @@ import { addPrivateLocationRoute } from './settings/private_locations/add_privat import { deletePrivateLocationRoute } from './settings/private_locations/delete_private_location'; import { getPrivateLocationsRoute } from './settings/private_locations/get_private_locations'; import { getSyntheticsFilters } from './filters/filters'; +import { getAllSyntheticsMonitorRoute } from './monitor_cruds/get_monitors_list'; import { getLocationMonitors } from './settings/private_locations/get_location_monitors'; export const syntheticsAppRestApiRoutes: SyntheticsRestApiRouteFactory[] = [ diff --git a/x-pack/plugins/synthetics/server/routes/monitor_cruds/delete_monitor_project.ts b/x-pack/plugins/synthetics/server/routes/monitor_cruds/delete_monitor_project.ts index 49cfda80011d5..83c6bd26ee275 100644 --- a/x-pack/plugins/synthetics/server/routes/monitor_cruds/delete_monitor_project.ts +++ b/x-pack/plugins/synthetics/server/routes/monitor_cruds/delete_monitor_project.ts @@ -45,13 +45,16 @@ export const deleteSyntheticsMonitorProjectRoute: SyntheticsRestApiRouteFactory values: monitorsToDelete.map((id: string) => `${id}`), })}`; - const { saved_objects: monitors } = await getMonitors({ - ...routeContext, - request: { - ...request, - query: { ...request.query, filter: deleteFilter, fields: [], perPage: 500 }, + const { saved_objects: monitors } = await getMonitors( + { + ...routeContext, + request: { + ...request, + query: { ...request.query, filter: deleteFilter, perPage: 500 }, + }, }, - }); + { fields: [] } + ); const { integrations: { writeIntegrationPolicies }, diff --git a/x-pack/plugins/synthetics/server/routes/monitor_cruds/get_monitor.ts b/x-pack/plugins/synthetics/server/routes/monitor_cruds/get_monitor.ts index 5709c03fa2a39..1fec6218d4515 100644 --- a/x-pack/plugins/synthetics/server/routes/monitor_cruds/get_monitor.ts +++ b/x-pack/plugins/synthetics/server/routes/monitor_cruds/get_monitor.ts @@ -18,14 +18,7 @@ import { UMServerLibs } from '../../legacy_uptime/lib/lib'; import { SyntheticsRestApiRouteFactory } from '../../legacy_uptime/routes/types'; import { API_URLS, SYNTHETICS_API_URLS } from '../../../common/constants'; import { getMonitorNotFoundResponse } from '../synthetics_service/service_errors'; -import { - getMonitorFilters, - getMonitors, - isMonitorsQueryFiltered, - MonitorsQuery, - QuerySchema, - SEARCH_FIELDS, -} from '../common'; +import { getMonitorFilters, MonitorsQuery, QuerySchema, SEARCH_FIELDS } from '../common'; export const getSyntheticsMonitorRoute: SyntheticsRestApiRouteFactory = (libs: UMServerLibs) => ({ method: 'GET', @@ -78,43 +71,6 @@ export const getSyntheticsMonitorRoute: SyntheticsRestApiRouteFactory = (libs: U }, }); -export const getAllSyntheticsMonitorRoute: SyntheticsRestApiRouteFactory = () => ({ - method: 'GET', - path: API_URLS.SYNTHETICS_MONITORS, - validate: { - query: QuerySchema, - }, - handler: async (routeContext): Promise => { - const { request, savedObjectsClient, syntheticsMonitorClient } = routeContext; - const totalCountQuery = async () => { - if (isMonitorsQueryFiltered(request.query)) { - return savedObjectsClient.find({ - type: syntheticsMonitorType, - perPage: 0, - page: 1, - }); - } - }; - - const [queryResult, totalCount] = await Promise.all([ - getMonitors(routeContext), - totalCountQuery(), - ]); - - const absoluteTotal = totalCount?.total ?? queryResult.total; - - const { saved_objects: monitors, per_page: perPageT, ...rest } = queryResult; - - return { - ...rest, - monitors, - absoluteTotal, - perPage: perPageT, - syncErrors: syntheticsMonitorClient.syntheticsService.syncErrors, - }; - }, -}); - export const getSyntheticsMonitorOverviewRoute: SyntheticsRestApiRouteFactory = () => ({ method: 'GET', path: SYNTHETICS_API_URLS.SYNTHETICS_OVERVIEW, diff --git a/x-pack/plugins/synthetics/server/routes/monitor_cruds/get_monitor_project.ts b/x-pack/plugins/synthetics/server/routes/monitor_cruds/get_monitor_project.ts index 66ee960add4f8..eff7e40c86b20 100644 --- a/x-pack/plugins/synthetics/server/routes/monitor_cruds/get_monitor_project.ts +++ b/x-pack/plugins/synthetics/server/routes/monitor_cruds/get_monitor_project.ts @@ -37,21 +37,25 @@ export const getSyntheticsProjectMonitorsRoute: SyntheticsRestApiRouteFactory = const decodedSearchAfter = searchAfter ? decodeURI(searchAfter) : undefined; try { - const { saved_objects: monitors, total } = await getMonitors({ - ...routeContext, - request: { - ...request, - query: { - ...request.query, - filter: `${syntheticsMonitorType}.attributes.${ConfigKey.PROJECT_ID}: "${decodedProjectName}"`, - fields: [ConfigKey.JOURNEY_ID, ConfigKey.CONFIG_HASH], - perPage, - sortField: ConfigKey.JOURNEY_ID, - sortOrder: 'asc', - searchAfter: decodedSearchAfter ? [...decodedSearchAfter.split(',')] : undefined, + const { saved_objects: monitors, total } = await getMonitors( + { + ...routeContext, + request: { + ...request, + query: { + ...request.query, + filter: `${syntheticsMonitorType}.attributes.${ConfigKey.PROJECT_ID}: "${decodedProjectName}"`, + perPage, + sortField: ConfigKey.JOURNEY_ID, + sortOrder: 'asc', + searchAfter: decodedSearchAfter ? [...decodedSearchAfter.split(',')] : undefined, + }, }, }, - }); + { + fields: [ConfigKey.JOURNEY_ID, ConfigKey.CONFIG_HASH], + } + ); const projectMonitors = monitors.map((monitor) => ({ journey_id: monitor.attributes[ConfigKey.JOURNEY_ID], hash: monitor.attributes[ConfigKey.CONFIG_HASH] || '', diff --git a/x-pack/plugins/synthetics/server/routes/monitor_cruds/get_monitors_list.ts b/x-pack/plugins/synthetics/server/routes/monitor_cruds/get_monitors_list.ts new file mode 100644 index 0000000000000..d407134a8f024 --- /dev/null +++ b/x-pack/plugins/synthetics/server/routes/monitor_cruds/get_monitors_list.ts @@ -0,0 +1,47 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ +import { SyntheticsRestApiRouteFactory } from '../../legacy_uptime/routes'; +import { API_URLS } from '../../../common/constants'; +import { getMonitors, isMonitorsQueryFiltered, QuerySchema } from '../common'; +import { syntheticsMonitorType } from '../../../common/types/saved_objects'; + +export const getAllSyntheticsMonitorRoute: SyntheticsRestApiRouteFactory = () => ({ + method: 'GET', + path: API_URLS.SYNTHETICS_MONITORS, + validate: { + query: QuerySchema, + }, + handler: async (routeContext): Promise => { + const { request, savedObjectsClient, syntheticsMonitorClient } = routeContext; + const totalCountQuery = async () => { + if (isMonitorsQueryFiltered(request.query)) { + return savedObjectsClient.find({ + type: syntheticsMonitorType, + perPage: 0, + page: 1, + }); + } + }; + + const [queryResult, totalCount] = await Promise.all([ + getMonitors(routeContext), + totalCountQuery(), + ]); + + const absoluteTotal = totalCount?.total ?? queryResult.total; + + const { saved_objects: monitors, per_page: perPageT, ...rest } = queryResult; + + return { + ...rest, + monitors, + absoluteTotal, + perPage: perPageT, + syncErrors: syntheticsMonitorClient.syntheticsService.syncErrors, + }; + }, +});