From 6b28312e6939e8d8b636a9d5f52ae87cd193b8d8 Mon Sep 17 00:00:00 2001 From: Matthew Bargar Date: Fri, 11 Dec 2015 18:26:03 -0500 Subject: [PATCH] Update DELETE to use template id included in pattern resource instead of relying on a naming convention --- .../convert_pattern_and_template_name.js | 31 ----------- .../lib/convert_pattern_and_template_name.js | 22 -------- .../api/index_patterns/register_delete.js | 53 +++++++++++++------ .../api/index_patterns/register_post.js | 1 - test/unit/api/index_patterns/_del.js | 13 +++-- test/unit/api/index_patterns/_get.js | 6 +-- test/unit/api/index_patterns/_post.js | 7 ++- test/unit/api/index_patterns/_put.js | 2 +- 8 files changed, 54 insertions(+), 81 deletions(-) delete mode 100644 src/plugins/kibana/server/lib/__tests__/convert_pattern_and_template_name.js delete mode 100644 src/plugins/kibana/server/lib/convert_pattern_and_template_name.js diff --git a/src/plugins/kibana/server/lib/__tests__/convert_pattern_and_template_name.js b/src/plugins/kibana/server/lib/__tests__/convert_pattern_and_template_name.js deleted file mode 100644 index 71f7d97c0e85..000000000000 --- a/src/plugins/kibana/server/lib/__tests__/convert_pattern_and_template_name.js +++ /dev/null @@ -1,31 +0,0 @@ -const {templateToPattern, patternToTemplate} = require('../convert_pattern_and_template_name'); -const expect = require('expect.js'); - -describe('convertPatternAndTemplateName', function () { - - describe('templateToPattern', function () { - - it('should convert an index template\'s name to its matching index pattern\'s title', function () { - expect(templateToPattern('kibana-logstash-*')).to.be('logstash-*'); - }); - - it('should throw an error if the template name isn\'t a valid kibana namespaced name', function () { - expect(templateToPattern).withArgs('logstash-*').to.throwException('not a valid kibana namespaced template name'); - expect(templateToPattern).withArgs('').to.throwException(/not a valid kibana namespaced template name/); - }); - - }); - - describe('patternToTemplate', function () { - - it('should convert an index pattern\'s title to its matching index template\'s name', function () { - expect(patternToTemplate('logstash-*')).to.be('kibana-logstash-*'); - }); - - it('should throw an error if the pattern is empty', function () { - expect(patternToTemplate).withArgs('').to.throwException(/pattern must not be empty/); - }); - - }); - -}); diff --git a/src/plugins/kibana/server/lib/convert_pattern_and_template_name.js b/src/plugins/kibana/server/lib/convert_pattern_and_template_name.js deleted file mode 100644 index 2ed68f30ad4e..000000000000 --- a/src/plugins/kibana/server/lib/convert_pattern_and_template_name.js +++ /dev/null @@ -1,22 +0,0 @@ -// To avoid index template naming collisions the index pattern creation API -// namespaces template names by prepending 'kibana-' to the matching pattern's title. -// e.g. a pattern with title `logstash-*` will have a matching template named `kibana-logstash-*`. -// This module provides utility functions for easily converting between template and pattern names. - -module.exports = { - templateToPattern: (templateName) => { - if (templateName.indexOf('kibana-') === -1) { - throw new Error('not a valid kibana namespaced template name'); - } - - return templateName.slice(templateName.indexOf('-') + 1); - }, - - patternToTemplate: (patternName) => { - if (patternName === '') { - throw new Error('pattern must not be empty'); - } - - return `kibana-${patternName.toLowerCase()}`; - } -}; 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 e20e8655a545..cf7b04026bee 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,33 +1,52 @@ -const Promise = require('bluebird'); +const _ = require('lodash'); const handleESError = require('../../../lib/handle_es_error'); -const {templateToPattern, patternToTemplate} = require('../../../lib/convert_pattern_and_template_name'); +const getIndexPattern = require('./get_index_pattern'); module.exports = function registerDelete(server) { server.route({ path: '/api/kibana/index_patterns/{id}', method: 'DELETE', handler: function (req, reply) { - const callWithRequest = server.plugins.elasticsearch.callWithRequest; + const boundCallWithRequest = _.partial(server.plugins.elasticsearch.callWithRequest, req); + const shouldIncludeTemplate = req.query.include === 'template'; + const patternId = req.params.id; + const deletePatternParams = { index: '.kibana', type: 'index-pattern', - id: req.params.id + id: patternId }; - Promise.all([ - callWithRequest(req, 'delete', deletePatternParams), - callWithRequest(req, 'indices.deleteTemplate', {name: patternToTemplate(req.params.id)}) - .catch((error) => { - if (!error.status || error.status !== 404) { - throw error; - } + let result; + if (shouldIncludeTemplate) { + result = getIndexPattern(patternId, boundCallWithRequest) + .then((patternResource) => { + return boundCallWithRequest( + 'indices.deleteTemplate', + {name: _.get(patternResource, 'data.relationships.template.data.id')} + ) + .catch((error) => { + if (!error.status || error.status !== 404) { + throw error; + } + }); }) - ]) - .then(function (pattern) { - reply('success'); - }, function (error) { - reply(handleESError(error)); - }); + .then(() => { + return boundCallWithRequest('delete', deletePatternParams); + }); + } + else { + result = boundCallWithRequest('delete', deletePatternParams); + } + + result.then( + function () { + reply('success'); + }, + function (error) { + reply(handleESError(error)); + } + ); } }); }; diff --git a/src/plugins/kibana/server/routes/api/index_patterns/register_post.js b/src/plugins/kibana/server/routes/api/index_patterns/register_post.js index 9cf85696404b..2ebd511c7d9f 100644 --- a/src/plugins/kibana/server/routes/api/index_patterns/register_post.js +++ b/src/plugins/kibana/server/routes/api/index_patterns/register_post.js @@ -1,6 +1,5 @@ const Boom = require('boom'); const _ = require('lodash'); -const {templateToPattern, patternToTemplate} = require('../../../lib/convert_pattern_and_template_name'); const indexPatternSchema = require('../../../lib/schemas/resources/index_pattern_schema'); const handleESError = require('../../../lib/handle_es_error'); const addMappingInfoToPatternFields = require('../../../lib/add_mapping_info_to_pattern_fields'); diff --git a/test/unit/api/index_patterns/_del.js b/test/unit/api/index_patterns/_del.js index 6bd856566dd6..2b3f532e6efa 100644 --- a/test/unit/api/index_patterns/_del.js +++ b/test/unit/api/index_patterns/_del.js @@ -10,17 +10,20 @@ define(function (require) { bdd.describe('DELETE index_patterns', function deleteIndexPatterns() { bdd.beforeEach(function () { - return scenarioManager.reload('emptyKibana').then(function () { - return request.post('/kibana/index_patterns').send(createTestData().indexPatternWithTemplate); + return scenarioManager.reload('emptyKibana') + .then(function () { + return request.post('/kibana/index_patterns') + .send(createTestData().indexPatternWithTemplate) + .expect(201); }); }); bdd.afterEach(function () { - return request.del('/kibana/index_patterns/logstash-*'); + return request.del('/kibana/index_patterns/logstash-*?include=template'); }); bdd.it('should return 200 for successful deletion of pattern and template', function () { - return request.del('/kibana/index_patterns/logstash-*') + return request.del('/kibana/index_patterns/logstash-*?include=template') .expect(200) .then(function () { return request.get('/kibana/index_patterns/logstash-*').expect(404); @@ -34,7 +37,7 @@ define(function (require) { }); bdd.it('should return 404 for a non-existent id', function () { - return request.del('/kibana/index_patterns/doesnotexist') + return request.del('/kibana/index_patterns/doesnotexist?include=template') .expect(404); }); diff --git a/test/unit/api/index_patterns/_get.js b/test/unit/api/index_patterns/_get.js index f2de7262f07b..fc46d912be62 100644 --- a/test/unit/api/index_patterns/_get.js +++ b/test/unit/api/index_patterns/_get.js @@ -46,9 +46,9 @@ define(function (require) { bdd.after(function () { return Promise.all([ - request.del('/kibana/index_patterns/logstash-*'), - request.del('/kibana/index_patterns/foo'), - request.del('/kibana/index_patterns/bar*') + request.del('/kibana/index_patterns/logstash-*?include=template'), + request.del('/kibana/index_patterns/foo?include=template'), + request.del('/kibana/index_patterns/bar*?include=template') ]); }); diff --git a/test/unit/api/index_patterns/_post.js b/test/unit/api/index_patterns/_post.js index dcf55cbd3af3..03bfc44da756 100644 --- a/test/unit/api/index_patterns/_post.js +++ b/test/unit/api/index_patterns/_post.js @@ -12,7 +12,7 @@ define(function (require) { }); bdd.afterEach(function () { - return request.del('/kibana/index_patterns/logstash-*'); + return request.del('/kibana/index_patterns/logstash-*?include=template'); }); bdd.it('should return 400 for an invalid payload', function invalidPayload() { @@ -74,6 +74,11 @@ define(function (require) { return request.post('/kibana/index_patterns') .send(createTestData().indexPatternWithTemplate) .expect(409); + }) + .then(function () { + return scenarioManager.client.indices.deleteTemplate({ + name: 'kibana-logstash-*' + }); }); }); diff --git a/test/unit/api/index_patterns/_put.js b/test/unit/api/index_patterns/_put.js index be3f5281100a..6c2b315c4c5d 100644 --- a/test/unit/api/index_patterns/_put.js +++ b/test/unit/api/index_patterns/_put.js @@ -15,7 +15,7 @@ define(function (require) { }); bdd.afterEach(function () { - return request.del('/kibana/index_patterns/logstash-*'); + return request.del('/kibana/index_patterns/logstash-*?include=template'); }); bdd.it('should return 200 for a successful update', function () {