Update DELETE to use template id included in pattern resource instead of relying on a naming convention

This commit is contained in:
Matthew Bargar 2015-12-11 18:26:03 -05:00
parent 0397a2f659
commit 6b28312e69
8 changed files with 54 additions and 81 deletions

View file

@ -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/);
});
});
});

View file

@ -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()}`;
}
};

View file

@ -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));
}
);
}
});
};

View file

@ -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');

View file

@ -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);
});

View file

@ -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')
]);
});

View file

@ -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-*'
});
});
});

View file

@ -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 () {