Skip to content

Commit

Permalink
add delete route
Browse files Browse the repository at this point in the history
  • Loading branch information
alisonelizabeth committed Apr 15, 2020
1 parent cf8b36b commit 5572566
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 1 deletion.
49 changes: 49 additions & 0 deletions x-pack/plugins/ingest_pipelines/server/routes/api/delete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { schema } from '@kbn/config-schema';

import { API_BASE_PATH } from '../../../common/constants';
import { RouteDependencies } from '../../types';

const paramsSchema = schema.object({
nameOrNames: schema.string(),
});

export const registerDeleteRoute = ({ router, license }: RouteDependencies): void => {
router.delete(
{
path: `${API_BASE_PATH}/{nameOrNames}`,
validate: {
params: paramsSchema,
},
},
license.guardApiRoute(async (ctx, req, res) => {
const { callAsCurrentUser } = ctx.core.elasticsearch.dataClient;
const { nameOrNames } = req.params;
const pipelineNames = nameOrNames.split(',');

const response: { itemsDeleted: string[]; errors: any[] } = {
itemsDeleted: [],
errors: [],
};

await Promise.all(
pipelineNames.map(pipelineName => {
return callAsCurrentUser('ingest.deletePipeline', { id: pipelineName })
.then(() => response.itemsDeleted.push(pipelineName))
.catch(e =>
response.errors.push({
name: pipelineName,
error: e,
})
);
})
);

return res.ok({ body: response });
})
);
};
2 changes: 2 additions & 0 deletions x-pack/plugins/ingest_pipelines/server/routes/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ export { registerGetRoutes } from './get';
export { registerCreateRoute } from './create';

export { registerUpdateRoute } from './update';

export { registerDeleteRoute } from './delete';
8 changes: 7 additions & 1 deletion x-pack/plugins/ingest_pipelines/server/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@

import { RouteDependencies } from '../types';

import { registerGetRoutes, registerCreateRoute, registerUpdateRoute } from './api';
import {
registerGetRoutes,
registerCreateRoute,
registerUpdateRoute,
registerDeleteRoute,
} from './api';

export class ApiRoutes {
setup(dependencies: RouteDependencies) {
registerGetRoutes(dependencies);
registerCreateRoute(dependencies);
registerUpdateRoute(dependencies);
registerDeleteRoute(dependencies);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -185,5 +185,100 @@ export default function({ getService }: FtrProviderContext) {
});
});
});

describe('Delete', () => {
const PIPELINE = {
description: 'test pipeline description',
processors: [
{
script: {
source: 'ctx._type = null',
},
},
],
version: 1,
};

it('should delete a pipeline', async () => {
// Create pipeline to be deleted
const PIPELINE_ID = 'test_delete_pipeline';
createPipeline({ body: PIPELINE, id: PIPELINE_ID });

const uri = `${API_BASE_PATH}/${PIPELINE_ID}`;

const { body } = await supertest
.delete(uri)
.set('kbn-xsrf', 'xxx')
.expect(200);

expect(body).to.eql({
itemsDeleted: [PIPELINE_ID],
errors: [],
});
});

it('should delete multiple pipelines', async () => {
// Create pipelines to be deleted
const PIPELINE_ONE_ID = 'test_delete_pipeline_1';
const PIPELINE_TWO_ID = 'test_delete_pipeline_2';
createPipeline({ body: PIPELINE, id: PIPELINE_ONE_ID });
createPipeline({ body: PIPELINE, id: PIPELINE_TWO_ID });

const uri = `${API_BASE_PATH}/${PIPELINE_ONE_ID},${PIPELINE_TWO_ID}`;

const { body } = await supertest
.delete(uri)
.set('kbn-xsrf', 'xxx')
.expect(200);

expect(body).to.eql({
itemsDeleted: [PIPELINE_ONE_ID, PIPELINE_TWO_ID],
errors: [],
});
});

it('should return an error for any pipelines not sucessfully deleted', async () => {
const PIPELINE_DOES_NOT_EXIST = 'pipeline_does_not_exist';

// Create pipeline to be deleted
const PIPELINE_ONE_ID = 'test_delete_pipeline_1';
createPipeline({ body: PIPELINE, id: PIPELINE_ONE_ID });

const uri = `${API_BASE_PATH}/${PIPELINE_ONE_ID},${PIPELINE_DOES_NOT_EXIST}`;

const { body } = await supertest
.delete(uri)
.set('kbn-xsrf', 'xxx')
.expect(200);

expect(body).to.eql({
itemsDeleted: [PIPELINE_ONE_ID],
errors: [
{
name: PIPELINE_DOES_NOT_EXIST,
error: {
msg: '[resource_not_found_exception] pipeline [pipeline_does_not_exist] is missing',
path: '/_ingest/pipeline/pipeline_does_not_exist',
query: {},
statusCode: 404,
response: JSON.stringify({
error: {
root_cause: [
{
type: 'resource_not_found_exception',
reason: 'pipeline [pipeline_does_not_exist] is missing',
},
],
type: 'resource_not_found_exception',
reason: 'pipeline [pipeline_does_not_exist] is missing',
},
status: 404,
}),
},
},
],
});
});
});
});
}

0 comments on commit 5572566

Please sign in to comment.