Skip to content

Commit

Permalink
[Synthetics] Delete monitor API via id param !! (elastic#190210)
Browse files Browse the repository at this point in the history
## 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 !!
  • Loading branch information
shahzad31 authored Aug 12, 2024
1 parent 8dbc2a0 commit 69f6687
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 12 deletions.
24 changes: 22 additions & 2 deletions docs/api/synthetics/monitors/delete-monitor-api.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ export function useMonitorListColumns({
},
{
'data-test-subj': 'syntheticsMonitorCopyAction',
isPrimary: true,
isPrimary: false,
name: (fields) => (
<NoPermissionsTooltip
canEditSynthetics={canEditSynthetics}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
20 changes: 20 additions & 0 deletions x-pack/test/api_integration/apis/synthetics/delete_monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 69f6687

Please sign in to comment.