mirror of
https://github.com/elastic/kibana.git
synced 2025-04-23 09:19:04 -04:00
update PUT endpoint for new resource schema
This commit is contained in:
parent
bf45998057
commit
b9cf052bc1
2 changed files with 28 additions and 48 deletions
|
@ -16,36 +16,28 @@ module.exports = function registerPut(server) {
|
|||
if (_.isEmpty(req.payload)) {
|
||||
return reply(Boom.badRequest('Payload required'));
|
||||
}
|
||||
if (req.payload.title && req.payload.title !== req.params.id) {
|
||||
return reply(Boom.badRequest('Updates to title not supported'));
|
||||
}
|
||||
|
||||
const callWithRequest = server.plugins.elasticsearch.callWithRequest;
|
||||
const indexPattern = _.cloneDeep(req.payload);
|
||||
const mappings = _(req.payload.fields)
|
||||
.indexBy('name')
|
||||
.mapValues(value => value.mapping)
|
||||
.omit(_.isUndefined)
|
||||
.value();
|
||||
const indexPatternResource = _.cloneDeep(req.payload);
|
||||
const indexPatternId = indexPatternResource.data.id;
|
||||
const indexPattern = indexPatternResource.data.attributes;
|
||||
const included = indexPatternResource.included;
|
||||
indexPattern.fields = JSON.stringify(indexPattern.fields);
|
||||
|
||||
indexPattern.fields = JSON.stringify(_.map(indexPattern.fields, (field) => {
|
||||
return _.omit(field, 'mapping');
|
||||
}));
|
||||
|
||||
if (!_.isEmpty(mappings)) {
|
||||
return reply(Boom.badRequest('Mappings cannot be updated'));
|
||||
if (!_.isEmpty(included)) {
|
||||
return reply(Boom.badRequest('PUT does not support included resource updates'));
|
||||
}
|
||||
|
||||
const params = {
|
||||
index: '.kibana',
|
||||
type: 'index-pattern',
|
||||
id: req.params.id,
|
||||
id: indexPatternId,
|
||||
body: {
|
||||
doc: indexPattern
|
||||
}
|
||||
};
|
||||
callWithRequest(req, 'update', params)
|
||||
.then(function (pattern) {
|
||||
.then(function () {
|
||||
return reply('success');
|
||||
}, function (error) {
|
||||
return reply(handleESError(error));
|
||||
|
|
|
@ -31,36 +31,20 @@ define(function (require) {
|
|||
return request.get('/kibana/index_patterns/logstash-*');
|
||||
})
|
||||
.then(function (res) {
|
||||
expect(res.body.time_field_name).to.be('foo');
|
||||
expect(res.body.fields[0].count).to.be(5);
|
||||
expect(res.body.data.attributes.time_field_name).to.be('foo');
|
||||
expect(res.body.data.attributes.fields[0].count).to.be(5);
|
||||
});
|
||||
});
|
||||
|
||||
bdd.it('should return 400 if you try to modify the title', function () {
|
||||
var pattern = createTestData().indexPatternWithTemplate;
|
||||
pattern.fields = _.map(pattern.fields, function (field) {
|
||||
return _.omit(field, 'mapping');
|
||||
});
|
||||
pattern.title = 'foo';
|
||||
|
||||
return request.put('/kibana/index_patterns/logstash-*')
|
||||
.send(pattern)
|
||||
.expect(400);
|
||||
});
|
||||
|
||||
bdd.it('should return 400 if you try to update mappings', function () {
|
||||
bdd.it('should return 400 if you try to update an included template', function () {
|
||||
return request.put('/kibana/index_patterns/logstash-*')
|
||||
.send(createTestData().indexPatternWithTemplate)
|
||||
.expect(400);
|
||||
});
|
||||
|
||||
bdd.it('should return 400 for an invalid payload', function () {
|
||||
function omitMappings(pattern) {
|
||||
pattern.fields = _.map(pattern.fields, function (field) {
|
||||
return _.omit(field, 'mapping');
|
||||
});
|
||||
return pattern;
|
||||
};
|
||||
const putTestData = createTestData().indexPatternWithTemplate;
|
||||
delete putTestData.included;
|
||||
|
||||
return Promise.all([
|
||||
request.put('/kibana/index_patterns/logstash-*').expect(400),
|
||||
|
@ -71,32 +55,36 @@ define(function (require) {
|
|||
|
||||
//fields must be an array
|
||||
request.put('/kibana/index_patterns/logstash-*')
|
||||
.send(_.assign(omitMappings(createTestData().indexPatternWithTemplate), {fields: {}}))
|
||||
.send(_.set(putTestData, 'data.attributes.fields', {}))
|
||||
.expect(400),
|
||||
|
||||
// field objects must have a name
|
||||
request.put('/kibana/index_patterns/logstash-*')
|
||||
.send(_.assign(omitMappings(createTestData().indexPatternWithTemplate), {fields: [{count: 0}]}))
|
||||
.send(_.set(putTestData, 'data.attributes.fields', [{count: 0}]))
|
||||
.expect(400)
|
||||
]);
|
||||
});
|
||||
|
||||
bdd.it('should return 404 for a non-existent id', function () {
|
||||
var pattern = createTestData().indexPatternWithTemplate;
|
||||
pattern.fields = _.map(pattern.fields, function (field) {
|
||||
return _.omit(field, 'mapping');
|
||||
});
|
||||
pattern.title = 'idonotexist';
|
||||
pattern.data.id = 'idonotexist';
|
||||
pattern.data.attributes.title = 'idonotexist';
|
||||
delete pattern.included;
|
||||
|
||||
return request.put('/kibana/index_patterns/idonotexist')
|
||||
.send(pattern)
|
||||
.expect(404);
|
||||
.send(pattern)
|
||||
.expect(404);
|
||||
});
|
||||
|
||||
bdd.it('should enforce snake_case in the request body', function () {
|
||||
var pattern = createTestData().indexPatternWithTemplate;
|
||||
pattern.data.attributes.timeFieldName = 'foo';
|
||||
delete pattern.data.attributes.time_field_name;
|
||||
delete pattern.included;
|
||||
|
||||
return request.put('/kibana/index_patterns/logstash-*')
|
||||
.send({timeFieldName: 'foo'})
|
||||
.expect(400);
|
||||
.send(pattern)
|
||||
.expect(400);
|
||||
});
|
||||
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue