Update DELETE to use template id included in pattern resource instead of relying on a naming convention
This commit is contained in:
parent
0397a2f659
commit
6b28312e69
8 changed files with 54 additions and 81 deletions
|
@ -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/);
|
|
||||||
});
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
});
|
|
|
@ -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()}`;
|
|
||||||
}
|
|
||||||
};
|
|
|
@ -1,33 +1,52 @@
|
||||||
const Promise = require('bluebird');
|
const _ = require('lodash');
|
||||||
const handleESError = require('../../../lib/handle_es_error');
|
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) {
|
module.exports = function registerDelete(server) {
|
||||||
server.route({
|
server.route({
|
||||||
path: '/api/kibana/index_patterns/{id}',
|
path: '/api/kibana/index_patterns/{id}',
|
||||||
method: 'DELETE',
|
method: 'DELETE',
|
||||||
handler: function (req, reply) {
|
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 = {
|
const deletePatternParams = {
|
||||||
index: '.kibana',
|
index: '.kibana',
|
||||||
type: 'index-pattern',
|
type: 'index-pattern',
|
||||||
id: req.params.id
|
id: patternId
|
||||||
};
|
};
|
||||||
|
|
||||||
Promise.all([
|
let result;
|
||||||
callWithRequest(req, 'delete', deletePatternParams),
|
if (shouldIncludeTemplate) {
|
||||||
callWithRequest(req, 'indices.deleteTemplate', {name: patternToTemplate(req.params.id)})
|
result = getIndexPattern(patternId, boundCallWithRequest)
|
||||||
.catch((error) => {
|
.then((patternResource) => {
|
||||||
if (!error.status || error.status !== 404) {
|
return boundCallWithRequest(
|
||||||
throw error;
|
'indices.deleteTemplate',
|
||||||
}
|
{name: _.get(patternResource, 'data.relationships.template.data.id')}
|
||||||
|
)
|
||||||
|
.catch((error) => {
|
||||||
|
if (!error.status || error.status !== 404) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
});
|
||||||
})
|
})
|
||||||
])
|
.then(() => {
|
||||||
.then(function (pattern) {
|
return boundCallWithRequest('delete', deletePatternParams);
|
||||||
reply('success');
|
});
|
||||||
}, function (error) {
|
}
|
||||||
reply(handleESError(error));
|
else {
|
||||||
});
|
result = boundCallWithRequest('delete', deletePatternParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
result.then(
|
||||||
|
function () {
|
||||||
|
reply('success');
|
||||||
|
},
|
||||||
|
function (error) {
|
||||||
|
reply(handleESError(error));
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
const Boom = require('boom');
|
const Boom = require('boom');
|
||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
const {templateToPattern, patternToTemplate} = require('../../../lib/convert_pattern_and_template_name');
|
|
||||||
const indexPatternSchema = require('../../../lib/schemas/resources/index_pattern_schema');
|
const indexPatternSchema = require('../../../lib/schemas/resources/index_pattern_schema');
|
||||||
const handleESError = require('../../../lib/handle_es_error');
|
const handleESError = require('../../../lib/handle_es_error');
|
||||||
const addMappingInfoToPatternFields = require('../../../lib/add_mapping_info_to_pattern_fields');
|
const addMappingInfoToPatternFields = require('../../../lib/add_mapping_info_to_pattern_fields');
|
||||||
|
|
|
@ -10,17 +10,20 @@ define(function (require) {
|
||||||
bdd.describe('DELETE index_patterns', function deleteIndexPatterns() {
|
bdd.describe('DELETE index_patterns', function deleteIndexPatterns() {
|
||||||
|
|
||||||
bdd.beforeEach(function () {
|
bdd.beforeEach(function () {
|
||||||
return scenarioManager.reload('emptyKibana').then(function () {
|
return scenarioManager.reload('emptyKibana')
|
||||||
return request.post('/kibana/index_patterns').send(createTestData().indexPatternWithTemplate);
|
.then(function () {
|
||||||
|
return request.post('/kibana/index_patterns')
|
||||||
|
.send(createTestData().indexPatternWithTemplate)
|
||||||
|
.expect(201);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
bdd.afterEach(function () {
|
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 () {
|
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)
|
.expect(200)
|
||||||
.then(function () {
|
.then(function () {
|
||||||
return request.get('/kibana/index_patterns/logstash-*').expect(404);
|
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 () {
|
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);
|
.expect(404);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -46,9 +46,9 @@ define(function (require) {
|
||||||
|
|
||||||
bdd.after(function () {
|
bdd.after(function () {
|
||||||
return Promise.all([
|
return Promise.all([
|
||||||
request.del('/kibana/index_patterns/logstash-*'),
|
request.del('/kibana/index_patterns/logstash-*?include=template'),
|
||||||
request.del('/kibana/index_patterns/foo'),
|
request.del('/kibana/index_patterns/foo?include=template'),
|
||||||
request.del('/kibana/index_patterns/bar*')
|
request.del('/kibana/index_patterns/bar*?include=template')
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,7 @@ define(function (require) {
|
||||||
});
|
});
|
||||||
|
|
||||||
bdd.afterEach(function () {
|
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() {
|
bdd.it('should return 400 for an invalid payload', function invalidPayload() {
|
||||||
|
@ -74,6 +74,11 @@ define(function (require) {
|
||||||
return request.post('/kibana/index_patterns')
|
return request.post('/kibana/index_patterns')
|
||||||
.send(createTestData().indexPatternWithTemplate)
|
.send(createTestData().indexPatternWithTemplate)
|
||||||
.expect(409);
|
.expect(409);
|
||||||
|
})
|
||||||
|
.then(function () {
|
||||||
|
return scenarioManager.client.indices.deleteTemplate({
|
||||||
|
name: 'kibana-logstash-*'
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -15,7 +15,7 @@ define(function (require) {
|
||||||
});
|
});
|
||||||
|
|
||||||
bdd.afterEach(function () {
|
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 () {
|
bdd.it('should return 200 for a successful update', function () {
|
||||||
|
|
Loading…
Reference in a new issue