From bcfb02af8bc9fd77d0fb8e6bbac6f563c575e92f Mon Sep 17 00:00:00 2001 From: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Date: Thu, 1 Dec 2022 13:21:53 -0500 Subject: [PATCH] [8.6] [Synthetics] Fixes monitors search by location/hosts (#146529) (#146800) # Backport This will backport the following commits from `main` to `8.6`: - [[Synthetics] Fixes monitors search by location/hosts (#146529)](https://github.com/elastic/kibana/pull/146529) ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) Co-authored-by: Shahzad --- .../migrations/check_registered_types.test.ts | 2 +- .../common/utils/location_formatter.ts | 17 +++++++++++++ .../monitor_add_edit/form/field_config.tsx | 6 ++--- .../monitor_config/locations.tsx | 5 ++-- .../lib/saved_objects/synthetics_monitor.ts | 3 +++ .../synthetics/server/routes/common.ts | 3 ++- .../routes/monitor_cruds/get_monitor.ts | 25 +++++++++++-------- .../normalizers/browser_monitor.test.ts | 12 --------- .../normalizers/common_fields.ts | 7 +++--- .../normalizers/icmp_monitor.test.ts | 6 ----- .../normalizers/tcp_monitor.test.ts | 6 ----- .../project_monitor_formatter.test.ts | 5 ++-- .../project_monitor_formatter_legacy.test.ts | 5 ++-- .../apis/synthetics/add_monitor_project.ts | 21 ---------------- .../synthetics/add_monitor_project_legacy.ts | 21 ---------------- 15 files changed, 51 insertions(+), 93 deletions(-) create mode 100644 x-pack/plugins/synthetics/common/utils/location_formatter.ts diff --git a/src/core/server/integration_tests/saved_objects/migrations/check_registered_types.test.ts b/src/core/server/integration_tests/saved_objects/migrations/check_registered_types.test.ts index 716dde9b770b0..f02bf2597343b 100644 --- a/src/core/server/integration_tests/saved_objects/migrations/check_registered_types.test.ts +++ b/src/core/server/integration_tests/saved_objects/migrations/check_registered_types.test.ts @@ -131,7 +131,7 @@ describe('checking migration metadata changes on all registered SO types', () => "siem-ui-timeline-pinned-event": "e2697b38751506c7fce6e8b7207a830483dc4283", "space": "c4a0acce1bd4b9cce85154f2a350624a53111c59", "spaces-usage-stats": "922d3235bbf519e3fb3b260e27248b1df8249b79", - "synthetics-monitor": "d784b64a3def47d3f3d1f367df71ae41ef33cb3c", + "synthetics-monitor": "7c1e5a78fb3b88cc03b441d3bf3714d9967ab214", "synthetics-privates-locations": "dd00385f4a27ef062c3e57312eeb3799872fa4af", "tag": "39413f4578cc2128c9a0fda97d0acd1c8862c47a", "task": "ef53d0f070bd54957b8fe22fae3b1ff208913f76", diff --git a/x-pack/plugins/synthetics/common/utils/location_formatter.ts b/x-pack/plugins/synthetics/common/utils/location_formatter.ts new file mode 100644 index 0000000000000..6eb441fa8ad23 --- /dev/null +++ b/x-pack/plugins/synthetics/common/utils/location_formatter.ts @@ -0,0 +1,17 @@ +/* + * 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 { PrivateLocation, ServiceLocation } from '../runtime_types'; + +export const formatLocation = (location: ServiceLocation | PrivateLocation) => { + return { + id: location.id, + label: location.label, + geo: location.geo, + isServiceManaged: location.isServiceManaged, + }; +}; diff --git a/x-pack/plugins/synthetics/public/apps/synthetics/components/monitor_add_edit/form/field_config.tsx b/x-pack/plugins/synthetics/public/apps/synthetics/components/monitor_add_edit/form/field_config.tsx index 644901aadd5c2..c55891d7871ef 100644 --- a/x-pack/plugins/synthetics/public/apps/synthetics/components/monitor_add_edit/form/field_config.tsx +++ b/x-pack/plugins/synthetics/public/apps/synthetics/components/monitor_add_edit/form/field_config.tsx @@ -29,6 +29,7 @@ import { EuiLink, EuiTextArea, } from '@elastic/eui'; +import { formatLocation } from '../../../../../../common/utils/location_formatter'; import { getDocLinks } from '../../../../../kibana_services'; import { useMonitorName } from '../hooks/use_monitor_name'; import { MonitorTypeRadioGroup } from '../fields/monitor_type_radio_group'; @@ -407,10 +408,7 @@ export const FIELD: Record = { onChange: (updatedValues: ServiceLocations) => { setValue( ConfigKey.LOCATIONS, - updatedValues.map((location) => ({ - id: location.id, - isServiceManaged: location.isServiceManaged, - })) as MonitorServiceLocations, + updatedValues.map((location) => formatLocation(location)) as MonitorServiceLocations, { shouldValidate: Boolean(formState.submitCount > 0) } ); }, diff --git a/x-pack/plugins/synthetics/public/legacy_uptime/components/monitor_management/monitor_config/locations.tsx b/x-pack/plugins/synthetics/public/legacy_uptime/components/monitor_management/monitor_config/locations.tsx index d92d70147378d..11e2588e909b7 100644 --- a/x-pack/plugins/synthetics/public/legacy_uptime/components/monitor_management/monitor_config/locations.tsx +++ b/x-pack/plugins/synthetics/public/legacy_uptime/components/monitor_management/monitor_config/locations.tsx @@ -11,6 +11,7 @@ import { i18n } from '@kbn/i18n'; import { EuiCheckboxGroup, EuiFormRow, EuiText, EuiBadge, EuiIconTip } from '@elastic/eui'; import { useKibana } from '@kbn/kibana-react-plugin/public'; import { useRouteMatch } from 'react-router-dom'; +import { formatLocation } from '../../../../../common/utils/location_formatter'; import { monitorManagementListSelector } from '../../../state/selectors'; import { MonitorServiceLocations, LocationStatus } from '../../../../../common/runtime_types'; import { ClientPluginsStart } from '../../../../plugin'; @@ -44,9 +45,7 @@ export const ServiceLocations = ({ const location = locations.find((loc) => loc.id === optionId); if (isSelected) { setLocations((prevLocations) => - location - ? [...prevLocations, { id: location.id, isServiceManaged: location.isServiceManaged }] - : prevLocations + location ? [...prevLocations, formatLocation(location)] : prevLocations ); } else { setLocations((prevLocations) => [...prevLocations].filter((loc) => loc.id !== optionId)); diff --git a/x-pack/plugins/synthetics/server/legacy_uptime/lib/saved_objects/synthetics_monitor.ts b/x-pack/plugins/synthetics/server/legacy_uptime/lib/saved_objects/synthetics_monitor.ts index a228e4047e4e7..cb12f045a2311 100644 --- a/x-pack/plugins/synthetics/server/legacy_uptime/lib/saved_objects/synthetics_monitor.ts +++ b/x-pack/plugins/synthetics/server/legacy_uptime/lib/saved_objects/synthetics_monitor.ts @@ -104,6 +104,9 @@ export const getSyntheticsMonitorSavedObjectType = ( }, }, }, + label: { + type: 'text', + }, }, }, custom_heartbeat_id: { diff --git a/x-pack/plugins/synthetics/server/routes/common.ts b/x-pack/plugins/synthetics/server/routes/common.ts index 5c49308469b27..9b3a50b7befdc 100644 --- a/x-pack/plugins/synthetics/server/routes/common.ts +++ b/x-pack/plugins/synthetics/server/routes/common.ts @@ -33,6 +33,7 @@ export const SEARCH_FIELDS = [ 'name', 'tags.text', 'locations.id.text', + 'locations.label', 'urls', 'hosts', 'project_id.text', @@ -74,7 +75,7 @@ export const getMonitors = ( page, sortField: sortField === 'schedule.keyword' ? 'schedule.number' : sortField, sortOrder, - searchFields: ['name', 'tags.text', 'locations.id.text', 'urls', 'project_id.text'], + searchFields: SEARCH_FIELDS, search: query ? `${query}*` : undefined, filter: filterStr, fields, 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 72c24a7886ca2..11c39a8dbcb9b 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 @@ -53,27 +53,30 @@ export const getAllSyntheticsMonitorRoute: SyntheticsRestApiRouteFactory = () => query: QuerySchema, }, handler: async ({ request, savedObjectsClient, syntheticsMonitorClient }): Promise => { - const queryResult = await getMonitors( - request.query, - syntheticsMonitorClient.syntheticsService, - savedObjectsClient - ); - - const countResult = isMonitorsQueryFiltered(request.query) - ? await savedObjectsClient.find({ + const totalCountQuery = async () => { + if (isMonitorsQueryFiltered(request.query)) { + return savedObjectsClient.find({ type: syntheticsMonitorType, perPage: 0, page: 1, - }) - : queryResult; + }); + } + }; + + const [queryResult, totalCount] = await Promise.all([ + getMonitors(request.query, syntheticsMonitorClient.syntheticsService, savedObjectsClient), + totalCountQuery(), + ]); + + const absoluteTotal = totalCount?.total ?? queryResult.total; const { saved_objects: monitors, per_page: perPageT, ...rest } = queryResult; return { ...rest, monitors, + absoluteTotal, perPage: perPageT, - absoluteTotal: countResult.total, syncErrors: syntheticsMonitorClient.syntheticsService.syncErrors, }; }, diff --git a/x-pack/plugins/synthetics/server/synthetics_service/project_monitor/normalizers/browser_monitor.test.ts b/x-pack/plugins/synthetics/server/synthetics_service/project_monitor/normalizers/browser_monitor.test.ts index a11a990abaa56..f41f47311dcad 100644 --- a/x-pack/plugins/synthetics/server/synthetics_service/project_monitor/normalizers/browser_monitor.test.ts +++ b/x-pack/plugins/synthetics/server/synthetics_service/project_monitor/normalizers/browser_monitor.test.ts @@ -136,8 +136,6 @@ describe('browser normalizers', () => { id: 'us_central', isServiceManaged: true, label: 'Test Location', - url: 'test-url', - status: 'ga', }, ], name: 'test-name-1', @@ -182,8 +180,6 @@ describe('browser normalizers', () => { id: 'us_central', isServiceManaged: true, label: 'Test Location', - url: 'test-url', - status: 'ga', }, { geo: { @@ -193,8 +189,6 @@ describe('browser normalizers', () => { id: 'us_east', isServiceManaged: true, label: 'Test Location', - url: 'test-url', - status: 'ga', }, ], name: 'test-name-2', @@ -240,8 +234,6 @@ describe('browser normalizers', () => { id: 'us_central', isServiceManaged: true, label: 'Test Location', - url: 'test-url', - status: 'ga', }, { geo: { @@ -251,15 +243,11 @@ describe('browser normalizers', () => { id: 'us_east', isServiceManaged: true, label: 'Test Location', - url: 'test-url', - status: 'ga', }, { id: 'germany', isServiceManaged: false, label: 'Germany', - agentPolicyId: 'germany', - concurrentMonitors: 1, }, ], name: 'test-name-3', diff --git a/x-pack/plugins/synthetics/server/synthetics_service/project_monitor/normalizers/common_fields.ts b/x-pack/plugins/synthetics/server/synthetics_service/project_monitor/normalizers/common_fields.ts index 564de1c1c92d6..128c268254c1e 100644 --- a/x-pack/plugins/synthetics/server/synthetics_service/project_monitor/normalizers/common_fields.ts +++ b/x-pack/plugins/synthetics/server/synthetics_service/project_monitor/normalizers/common_fields.ts @@ -6,6 +6,7 @@ */ import { omit } from 'lodash'; +import { formatLocation } from '../../../../common/utils/location_formatter'; import { formatKibanaNamespace } from '../../../../common/formatters'; import { BrowserFields, @@ -108,9 +109,9 @@ export const getMonitorLocations = ({ ); }) || []; - return [...publicLocs, ...privateLocs].filter( - (location) => location !== undefined - ) as BrowserFields[ConfigKey.LOCATIONS]; + return [...publicLocs, ...privateLocs] + .filter((location) => location !== undefined) + .map((loc) => formatLocation(loc!)) as BrowserFields[ConfigKey.LOCATIONS]; }; export const getUnsupportedKeysError = ( diff --git a/x-pack/plugins/synthetics/server/synthetics_service/project_monitor/normalizers/icmp_monitor.test.ts b/x-pack/plugins/synthetics/server/synthetics_service/project_monitor/normalizers/icmp_monitor.test.ts index 3c1d9209ff1bb..b8d64473cae42 100644 --- a/x-pack/plugins/synthetics/server/synthetics_service/project_monitor/normalizers/icmp_monitor.test.ts +++ b/x-pack/plugins/synthetics/server/synthetics_service/project_monitor/normalizers/icmp_monitor.test.ts @@ -120,8 +120,6 @@ describe('icmp normalizers', () => { id: 'us_central', isServiceManaged: true, label: 'Test Location', - status: 'ga', - url: 'test-url', }, ], name: 'Cloudflare DNS', @@ -162,8 +160,6 @@ describe('icmp normalizers', () => { id: 'us_central', isServiceManaged: true, label: 'Test Location', - status: 'ga', - url: 'test-url', }, ], name: 'Cloudflare DNS 2', @@ -217,8 +213,6 @@ describe('icmp normalizers', () => { id: 'us_central', isServiceManaged: true, label: 'Test Location', - status: 'ga', - url: 'test-url', }, ], name: 'Cloudflare DNS 3', diff --git a/x-pack/plugins/synthetics/server/synthetics_service/project_monitor/normalizers/tcp_monitor.test.ts b/x-pack/plugins/synthetics/server/synthetics_service/project_monitor/normalizers/tcp_monitor.test.ts index 79ea67964fd28..66560d132ae96 100644 --- a/x-pack/plugins/synthetics/server/synthetics_service/project_monitor/normalizers/tcp_monitor.test.ts +++ b/x-pack/plugins/synthetics/server/synthetics_service/project_monitor/normalizers/tcp_monitor.test.ts @@ -128,8 +128,6 @@ describe('tcp normalizers', () => { id: 'us_central', isServiceManaged: true, label: 'Test Location', - status: 'ga', - url: 'test-url', }, ], name: 'GMail SMTP', @@ -184,8 +182,6 @@ describe('tcp normalizers', () => { id: 'us_central', isServiceManaged: true, label: 'Test Location', - status: 'ga', - url: 'test-url', }, ], name: 'Always Down', @@ -253,8 +249,6 @@ describe('tcp normalizers', () => { id: 'us_central', isServiceManaged: true, label: 'Test Location', - status: 'ga', - url: 'test-url', }, ], name: 'Always Down', diff --git a/x-pack/plugins/synthetics/server/synthetics_service/project_monitor/project_monitor_formatter.test.ts b/x-pack/plugins/synthetics/server/synthetics_service/project_monitor/project_monitor_formatter.test.ts index 28de75ed511d3..14bc64e1df236 100644 --- a/x-pack/plugins/synthetics/server/synthetics_service/project_monitor/project_monitor_formatter.test.ts +++ b/x-pack/plugins/synthetics/server/synthetics_service/project_monitor/project_monitor_formatter.test.ts @@ -21,6 +21,7 @@ import { httpServerMock } from '@kbn/core-http-server-mocks'; import { formatSecrets } from '../utils'; import * as telemetryHooks from '../../routes/telemetry/monitor_upgrade_sender'; +import { formatLocation } from '../../../common/utils/location_formatter'; const testMonitors = [ { @@ -453,7 +454,7 @@ const payloadData = [ form_monitor_type: 'multistep', ignore_https_errors: false, journey_id: 'check if title is present 10 0', - locations: privateLocations, + locations: privateLocations.map((l) => formatLocation(l)), name: 'check if title is present 10 0', namespace: 'default_space', origin: 'project', @@ -515,7 +516,7 @@ const payloadData = [ form_monitor_type: 'multistep', ignore_https_errors: false, journey_id: 'check if title is present 10 1', - locations: privateLocations, + locations: privateLocations.map((l) => formatLocation(l)), name: 'check if title is present 10 1', namespace: 'default_space', origin: 'project', diff --git a/x-pack/plugins/synthetics/server/synthetics_service/project_monitor/project_monitor_formatter_legacy.test.ts b/x-pack/plugins/synthetics/server/synthetics_service/project_monitor/project_monitor_formatter_legacy.test.ts index 14df60edfc4e2..cd74b2df88552 100644 --- a/x-pack/plugins/synthetics/server/synthetics_service/project_monitor/project_monitor_formatter_legacy.test.ts +++ b/x-pack/plugins/synthetics/server/synthetics_service/project_monitor/project_monitor_formatter_legacy.test.ts @@ -22,6 +22,7 @@ import { Subject } from 'rxjs'; import { formatSecrets } from '../utils'; import * as telemetryHooks from '../../routes/telemetry/monitor_upgrade_sender'; +import { formatLocation } from '../../../common/utils/location_formatter'; const testMonitors = [ { @@ -525,7 +526,7 @@ const payloadData = [ form_monitor_type: 'multistep', ignore_https_errors: false, journey_id: 'check if title is present 10 0', - locations: privateLocations, + locations: privateLocations.map((l) => formatLocation(l)), name: 'check if title is present 10 0', namespace: 'default_space', origin: 'project', @@ -587,7 +588,7 @@ const payloadData = [ form_monitor_type: 'multistep', ignore_https_errors: false, journey_id: 'check if title is present 10 1', - locations: privateLocations, + locations: privateLocations.map((l) => formatLocation(l)), name: 'check if title is present 10 1', namespace: 'default_space', origin: 'project', 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 7911beea80a6e..168da6d75bb63 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 @@ -152,11 +152,8 @@ export default function ({ getService }: FtrProviderContext) { lon: 0, }, id: 'localhost', - isInvalid: false, isServiceManaged: true, label: 'Local Synthetics Service', - status: 'experimental', - url: 'mockDevUrl', }, ], name: 'check if title is present', @@ -282,11 +279,8 @@ export default function ({ getService }: FtrProviderContext) { lon: 0, }, id: 'localhost', - isInvalid: false, isServiceManaged: true, label: 'Local Synthetics Service', - status: 'experimental', - url: 'mockDevUrl', }, ], max_redirects: '0', @@ -390,11 +384,8 @@ export default function ({ getService }: FtrProviderContext) { lon: 0, }, id: 'localhost', - isInvalid: false, isServiceManaged: true, label: 'Local Synthetics Service', - status: 'experimental', - url: 'mockDevUrl', }, ], name: monitor.name, @@ -489,21 +480,15 @@ export default function ({ getService }: FtrProviderContext) { lon: 0, }, id: 'localhost', - isInvalid: false, isServiceManaged: true, label: 'Local Synthetics Service', - status: 'experimental', - url: 'mockDevUrl', }, { - agentPolicyId: testPolicyId, - concurrentMonitors: 1, geo: { lat: '', lon: '', }, id: testPolicyId, - isInvalid: false, isServiceManaged: false, label: 'Test private location 0', }, @@ -1317,22 +1302,16 @@ export default function ({ getService }: FtrProviderContext) { id: 'localhost', label: 'Local Synthetics Service', geo: { lat: 0, lon: 0 }, - url: 'mockDevUrl', isServiceManaged: true, - status: 'experimental', - isInvalid: false, }, { label: 'Test private location 0', isServiceManaged: false, - isInvalid: false, - agentPolicyId: testPolicyId, id: testPolicyId, geo: { lat: '', lon: '', }, - concurrentMonitors: 1, }, ]); }); diff --git a/x-pack/test/api_integration/apis/synthetics/add_monitor_project_legacy.ts b/x-pack/test/api_integration/apis/synthetics/add_monitor_project_legacy.ts index 9d5e4b45f4e7f..fcaee57dd8166 100644 --- a/x-pack/test/api_integration/apis/synthetics/add_monitor_project_legacy.ts +++ b/x-pack/test/api_integration/apis/synthetics/add_monitor_project_legacy.ts @@ -152,11 +152,8 @@ export default function ({ getService }: FtrProviderContext) { lon: 0, }, id: 'localhost', - isInvalid: false, isServiceManaged: true, label: 'Local Synthetics Service', - status: 'experimental', - url: 'mockDevUrl', }, ], name: 'check if title is present', @@ -279,11 +276,8 @@ export default function ({ getService }: FtrProviderContext) { lon: 0, }, id: 'localhost', - isInvalid: false, isServiceManaged: true, label: 'Local Synthetics Service', - status: 'experimental', - url: 'mockDevUrl', }, ], max_redirects: '0', @@ -384,11 +378,8 @@ export default function ({ getService }: FtrProviderContext) { lon: 0, }, id: 'localhost', - isInvalid: false, isServiceManaged: true, label: 'Local Synthetics Service', - status: 'experimental', - url: 'mockDevUrl', }, ], name: monitor.name, @@ -481,21 +472,15 @@ export default function ({ getService }: FtrProviderContext) { lon: 0, }, id: 'localhost', - isInvalid: false, isServiceManaged: true, label: 'Local Synthetics Service', - status: 'experimental', - url: 'mockDevUrl', }, { - agentPolicyId: testPolicyId, - concurrentMonitors: 1, geo: { lat: '', lon: '', }, id: testPolicyId, - isInvalid: false, isServiceManaged: false, label: 'Test private location 0', }, @@ -2012,22 +1997,16 @@ export default function ({ getService }: FtrProviderContext) { id: 'localhost', label: 'Local Synthetics Service', geo: { lat: 0, lon: 0 }, - url: 'mockDevUrl', isServiceManaged: true, - status: 'experimental', - isInvalid: false, }, { label: 'Test private location 0', isServiceManaged: false, - isInvalid: false, - agentPolicyId: testPolicyId, id: testPolicyId, geo: { lat: '', lon: '', }, - concurrentMonitors: 1, }, ]); });