diff --git a/src/plugins/kibana/server/routes/api/index_patterns/register_delete.js b/src/plugins/kibana/server/routes/api/index_patterns/register_delete.js index cf7b04026bee..38c737bb13ea 100644 --- a/src/plugins/kibana/server/routes/api/index_patterns/register_delete.js +++ b/src/plugins/kibana/server/routes/api/index_patterns/register_delete.js @@ -1,6 +1,7 @@ const _ = require('lodash'); const handleESError = require('../../../lib/handle_es_error'); const getIndexPattern = require('./get_index_pattern'); +const Boom = require('boom'); module.exports = function registerDelete(server) { server.route({ @@ -21,9 +22,14 @@ module.exports = function registerDelete(server) { if (shouldIncludeTemplate) { result = getIndexPattern(patternId, boundCallWithRequest) .then((patternResource) => { + const templateId = _.get(patternResource, 'data.relationships.template.data.id'); + if (!templateId) { + throw Boom.badRequest('The specified index pattern has no related template to delete'); + } + return boundCallWithRequest( 'indices.deleteTemplate', - {name: _.get(patternResource, 'data.relationships.template.data.id')} + {name: templateId} ) .catch((error) => { if (!error.status || error.status !== 404) { diff --git a/test/unit/api/index_patterns/_del.js b/test/unit/api/index_patterns/_del.js index 2b3f532e6efa..92ed195945ab 100644 --- a/test/unit/api/index_patterns/_del.js +++ b/test/unit/api/index_patterns/_del.js @@ -19,7 +19,15 @@ define(function (require) { }); bdd.afterEach(function () { - return request.del('/kibana/index_patterns/logstash-*?include=template'); + return request.del('/kibana/index_patterns/logstash-*') + .then(function () { + return scenarioManager.client.indices.deleteTemplate({name: 'kibana-logstash-*'}) + .catch(function (err) { + if (err.status !== 404) { + throw err; + } + }); + }); }); bdd.it('should return 200 for successful deletion of pattern and template', function () { @@ -36,6 +44,37 @@ define(function (require) { }); }); + bdd.it('should not delete the template if the include parameter is not sent', function () { + return request.del('/kibana/index_patterns/logstash-*') + .expect(200) + .then(function () { + return request.get('/kibana/index_patterns/logstash-*').expect(404); + }) + .then(function () { + return scenarioManager.client.indices.getTemplate({name: 'kibana-logstash-*'}) + .then(function (res) { + expect(res['kibana-logstash-*']).to.be.ok(); + }); + }); + }); + + bdd.it('should return a 400 if template deletion is requsted for a pattern with no related template', function () { + var templatelessPattern = createTestData().indexPatternWithTemplate; + delete templatelessPattern.included; + delete templatelessPattern.data.relationships; + + return request.del('/kibana/index_patterns/logstash-*?include=template') + .then(function () { + return request.post('/kibana/index_patterns') + .send(templatelessPattern) + .expect(201); + }) + .then(function () { + return request.del('/kibana/index_patterns/logstash-*?include=template') + .expect(400); + }); + }); + bdd.it('should return 404 for a non-existent id', function () { return request.del('/kibana/index_patterns/doesnotexist?include=template') .expect(404);