[Synthetics] Delete monitor API via id param !! (#190210)

## Summary

Allow deletion of monitor via id param !!

User can now delete monitor via passing id as url param

`DELETE <kibana host>:<port>/api/synthetics/monitors/<config_id>`

Previous bulk delete via list of ids via API body still works as well !!

Docs are updated !!
This commit is contained in:
Shahzad 2024-08-12 21:13:33 +02:00 committed by GitHub
parent 8dbc2a08c3
commit 69f6687af9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 83 additions and 12 deletions

View file

@ -8,9 +8,9 @@ Deletes one or more monitors from the Synthetics app.
=== {api-request-title}
`DELETE <kibana host>:<port>/api/synthetics/monitors`
`DELETE <kibana host>:<port>/api/synthetics/monitors/<config_id>`
`DELETE <kibana host>:<port>/s/<space_id>/api/synthetics/monitors`
`DELETE <kibana host>:<port>/s/<space_id>/api/synthetics/monitors/<config_id>`
=== {api-prereq-title}
@ -20,6 +20,26 @@ You must have `all` privileges for the *Synthetics* feature in the *{observabili
You must have `all` privileges for the *Synthetics* feature in the *{observability}* section of the
<<kibana-feature-privileges,{kib} feature privileges>>.
[[delete-monitor-api-path-params]]
=== {api-path-parms-title}
`config_id`::
(Required, string) The ID of the monitor that you want to delete.
Here is an example of a DELETE request to delete a monitor by ID:
[source,sh]
--------------------------------------------------
DELETE /api/synthetics/monitors/monitor1-id
--------------------------------------------------
==== Bulk Delete Monitors
You can delete multiple monitors by sending a list of config ids to a DELETE request to the `/api/synthetics/monitors` endpoint.
[[monitors-delete-request-body]]
==== Request Body

View file

@ -200,7 +200,7 @@ export function useMonitorListColumns({
},
{
'data-test-subj': 'syntheticsMonitorCopyAction',
isPrimary: true,
isPrimary: false,
name: (fields) => (
<NoPermissionsTooltip
canEditSynthetics={canEditSynthetics}

View file

@ -29,30 +29,48 @@ import { formatSecrets, normalizeSecrets } from '../../synthetics_service/utils/
export const deleteSyntheticsMonitorRoute: SyntheticsRestApiRouteFactory<
DeleteParamsResponse[],
Record<string, any>,
Record<string, any>,
Record<string, string>,
Record<string, string>,
{ ids: string[] }
> = () => ({
method: 'DELETE',
path: SYNTHETICS_API_URLS.SYNTHETICS_MONITORS,
path: SYNTHETICS_API_URLS.SYNTHETICS_MONITORS + '/{id?}',
validate: {},
validation: {
request: {
body: schema.object({
ids: schema.arrayOf(schema.string(), {
minSize: 1,
}),
body: schema.nullable(
schema.object({
ids: schema.arrayOf(schema.string(), {
minSize: 1,
}),
})
),
params: schema.object({
id: schema.maybe(schema.string()),
}),
},
},
handler: async (routeContext): Promise<any> => {
const { request, response } = routeContext;
const { ids } = request.body;
const { ids } = request.body || {};
const { id: queryId } = request.params;
if (ids && queryId) {
return response.badRequest({
body: { message: 'id must be provided either via param or body.' },
});
}
const result: Array<{ id: string; deleted: boolean; error?: string }> = [];
const idsToDelete = [...(ids ?? []), ...(queryId ? [queryId] : [])];
if (idsToDelete.length === 0) {
return response.badRequest({
body: { message: 'id must be provided via param or body.' },
});
}
await pMap(ids, async (id) => {
await pMap(idsToDelete, async (id) => {
try {
const { errors, res } = await deleteMonitor({
routeContext,

View file

@ -79,6 +79,26 @@ export default function ({ getService }: FtrProviderContext) {
// Hit get endpoint and expect 404 as well
await supertest.get(SYNTHETICS_API_URLS.SYNTHETICS_MONITORS + '/' + monitorId).expect(404);
});
it('deletes monitor by param id', async () => {
const { id: monitorId } = await saveMonitor(httpMonitorJson as MonitorFields);
const deleteResponse = await monitorTestService.deleteMonitorByIdParam(monitorId, 200);
expect(deleteResponse.body).eql([{ id: monitorId, deleted: true }]);
// Hit get endpoint and expect 404 as well
await supertest.get(SYNTHETICS_API_URLS.SYNTHETICS_MONITORS + '/' + monitorId).expect(404);
});
it('throws error if both body and param are missing', async () => {
const deleteResponse = await supertest
.delete(SYNTHETICS_API_URLS.SYNTHETICS_MONITORS)
.send()
.set('kbn-xsrf', 'true');
expect(deleteResponse.status).to.eql(400);
});
it('deletes multiple monitors by id', async () => {
const { id: monitorId } = await saveMonitor(httpMonitorJson as MonitorFields);
const { id: monitorId2 } = await saveMonitor({

View file

@ -173,4 +173,17 @@ export class SyntheticsMonitorTestService {
expect(deleteResponse.status).to.eql(statusCode);
return deleteResponse;
}
async deleteMonitorByIdParam(monitorId?: string, statusCode = 200, spaceId?: string) {
const deleteResponse = await this.supertest
.delete(
spaceId
? `/s/${spaceId}${SYNTHETICS_API_URLS.SYNTHETICS_MONITORS}/${monitorId}`
: SYNTHETICS_API_URLS.SYNTHETICS_MONITORS + '/' + monitorId
)
.send()
.set('kbn-xsrf', 'true');
expect(deleteResponse.status).to.eql(statusCode);
return deleteResponse;
}
}