From 8c1624b457c6baeab67d4745bd0796483d1c6d45 Mon Sep 17 00:00:00 2001 From: Dominique Clarke Date: Mon, 22 May 2023 07:44:46 -0400 Subject: [PATCH] [Synthetics] remove public SO usage from delete params flow (#158046) --- .../settings/global_params/delete_param.tsx | 8 ++--- .../synthetics/state/global_params/api.ts | 10 ++++++ .../synthetics/state/global_params/index.ts | 1 + .../public/utils/api_service/api_service.ts | 5 +-- .../plugins/synthetics/server/routes/index.ts | 2 ++ .../server/routes/settings/delete_param.ts | 11 +++--- .../apis/synthetics/sync_global_params.ts | 34 ++++++++++++++++--- 7 files changed, 53 insertions(+), 18 deletions(-) diff --git a/x-pack/plugins/synthetics/public/apps/synthetics/components/settings/global_params/delete_param.tsx b/x-pack/plugins/synthetics/public/apps/synthetics/components/settings/global_params/delete_param.tsx index c63d0ecdb8bd9..7e7d215a2dc48 100644 --- a/x-pack/plugins/synthetics/public/apps/synthetics/components/settings/global_params/delete_param.tsx +++ b/x-pack/plugins/synthetics/public/apps/synthetics/components/settings/global_params/delete_param.tsx @@ -12,10 +12,9 @@ import { toMountPoint, useKibana } from '@kbn/kibana-react-plugin/public'; import { i18n } from '@kbn/i18n'; import { useDispatch } from 'react-redux'; -import { getGlobalParamAction } from '../../../state/global_params'; +import { getGlobalParamAction, deleteGlobalParams } from '../../../state/global_params'; import { syncGlobalParamsAction } from '../../../state/settings'; import { kibanaService } from '../../../../../utils/kibana_service'; -import { syntheticsParamType } from '../../../../../../common/types/saved_objects'; import { NO_LABEL, YES_LABEL } from '../../monitors_page/management/monitor_list_table/labels'; import { ListParamItem } from './params_list'; @@ -38,10 +37,7 @@ export const DeleteParam = ({ const { status } = useFetcher(() => { if (isDeleting && savedObjects) { - return savedObjects.client.bulkDelete( - items.map(({ id }) => ({ type: syntheticsParamType, id })), - { force: true } - ); + return deleteGlobalParams({ ids: items.map(({ id }) => id) }); } }, [items, isDeleting]); diff --git a/x-pack/plugins/synthetics/public/apps/synthetics/state/global_params/api.ts b/x-pack/plugins/synthetics/public/apps/synthetics/state/global_params/api.ts index 757a87b16674c..3fd5e8678d94b 100644 --- a/x-pack/plugins/synthetics/public/apps/synthetics/state/global_params/api.ts +++ b/x-pack/plugins/synthetics/public/apps/synthetics/state/global_params/api.ts @@ -35,3 +35,13 @@ export const editGlobalParam = async ({ ...paramRequest, }); }; + +export const deleteGlobalParams = async ({ + ids, +}: { + ids: string[]; +}): Promise => { + return apiService.delete(SYNTHETICS_API_URLS.PARAMS, { + ids: JSON.stringify(ids), + }); +}; diff --git a/x-pack/plugins/synthetics/public/apps/synthetics/state/global_params/index.ts b/x-pack/plugins/synthetics/public/apps/synthetics/state/global_params/index.ts index 2bea20a8bd00b..6555518b6c505 100644 --- a/x-pack/plugins/synthetics/public/apps/synthetics/state/global_params/index.ts +++ b/x-pack/plugins/synthetics/public/apps/synthetics/state/global_params/index.ts @@ -69,3 +69,4 @@ export const globalParamsReducer = createReducer(initialState, (builder) => { export * from './actions'; export * from './effects'; export * from './selectors'; +export * from './api'; diff --git a/x-pack/plugins/synthetics/public/utils/api_service/api_service.ts b/x-pack/plugins/synthetics/public/utils/api_service/api_service.ts index 150fad62aec46..9df3339e1b795 100644 --- a/x-pack/plugins/synthetics/public/utils/api_service/api_service.ts +++ b/x-pack/plugins/synthetics/public/utils/api_service/api_service.ts @@ -115,8 +115,9 @@ class ApiService { return response; } - public async delete(apiUrl: string) { - const response = await this._http!.delete(apiUrl); + public async delete(apiUrl: string, params?: HttpFetchQuery) { + const response = await this._http!.delete({ path: apiUrl, query: params }); + if (response instanceof Error) { throw response; } diff --git a/x-pack/plugins/synthetics/server/routes/index.ts b/x-pack/plugins/synthetics/server/routes/index.ts index 836143d55f014..41ca6803d24cb 100644 --- a/x-pack/plugins/synthetics/server/routes/index.ts +++ b/x-pack/plugins/synthetics/server/routes/index.ts @@ -45,6 +45,7 @@ import { } from '../legacy_uptime/routes'; import { getHasIntegrationMonitorsRoute } from './fleet/get_has_integration_monitors'; import { addSyntheticsParamsRoute } from './settings/add_param'; +import { deleteSyntheticsParamsRoute } from './settings/delete_param'; import { enableDefaultAlertingRoute } from './default_alerts/enable_default_alert'; import { getDefaultAlertingRoute } from './default_alerts/get_default_alert'; import { createNetworkEventsRoute } from './network_events'; @@ -78,6 +79,7 @@ export const syntheticsAppRestApiRoutes: SyntheticsRestApiRouteFactory[] = [ getSyntheticsParamsRoute, editSyntheticsParamsRoute, addSyntheticsParamsRoute, + deleteSyntheticsParamsRoute, syncParamsSyntheticsParamsRoute, enableDefaultAlertingRoute, getDefaultAlertingRoute, diff --git a/x-pack/plugins/synthetics/server/routes/settings/delete_param.ts b/x-pack/plugins/synthetics/server/routes/settings/delete_param.ts index 63909772838a8..ab8b1fefbbd1b 100644 --- a/x-pack/plugins/synthetics/server/routes/settings/delete_param.ts +++ b/x-pack/plugins/synthetics/server/routes/settings/delete_param.ts @@ -14,16 +14,17 @@ export const deleteSyntheticsParamsRoute: SyntheticsRestApiRouteFactory = () => method: 'DELETE', path: SYNTHETICS_API_URLS.PARAMS, validate: { - body: schema.object({ - ids: schema.arrayOf(schema.string()), + query: schema.object({ + ids: schema.string(), }), }, writeAccess: true, - handler: async ({ savedObjectsClient, request, server }): Promise => { - const { ids } = request.body as { ids: string[] }; + handler: async ({ savedObjectsClient, request }): Promise => { + const { ids } = request.query as { ids: string }; + const parsedIds = JSON.parse(ids) as string[]; const result = await savedObjectsClient.bulkDelete( - ids.map((id) => ({ type: syntheticsParamType, id })), + parsedIds.map((id) => ({ type: syntheticsParamType, id })), { force: true } ); diff --git a/x-pack/test/api_integration/apis/synthetics/sync_global_params.ts b/x-pack/test/api_integration/apis/synthetics/sync_global_params.ts index 3a44de5cbcf02..09829ed68d438 100644 --- a/x-pack/test/api_integration/apis/synthetics/sync_global_params.ts +++ b/x-pack/test/api_integration/apis/synthetics/sync_global_params.ts @@ -244,13 +244,37 @@ export default function ({ getService }: FtrProviderContext) { }); it('delete all params and sync again', async () => { - await kServer.savedObjects.clean({ types: [syntheticsParamType] }); - const apiResponseK = await supertestAPI - .get(SYNTHETICS_API_URLS.SYNC_GLOBAL_PARAMS) + await supertestAPI + .post(SYNTHETICS_API_URLS.PARAMS) .set('kbn-xsrf', 'true') - .send({ key: 'test', value: 'test' }); + .send({ key: 'get', value: 'test' }); + const getResponse = await supertestAPI + .get(SYNTHETICS_API_URLS.PARAMS) + .set('kbn-xsrf', 'true') + .expect(200); + + expect(getResponse.body.data.length).eql(2); - expect(apiResponseK.status).eql(200); + const paramsResponse = getResponse.body.data || []; + const ids = paramsResponse.map((param: any) => param.id); + + await supertestAPI + .delete(SYNTHETICS_API_URLS.PARAMS) + .query({ ids: JSON.stringify(ids) }) + .set('kbn-xsrf', 'true') + .expect(200); + + const getResponseAfterDelete = await supertestAPI + .get(SYNTHETICS_API_URLS.PARAMS) + .set('kbn-xsrf', 'true') + .expect(200); + + expect(getResponseAfterDelete.body.data.length).eql(0); + + await supertestAPI + .get(SYNTHETICS_API_URLS.SYNC_GLOBAL_PARAMS) + .set('kbn-xsrf', 'true') + .expect(200); const apiResponse = await supertestAPI.get( '/api/fleet/package_policies?page=1&perPage=2000&kuery=ingest-package-policies.package.name%3A%20synthetics'