Change the name of the index_pattern api to ingest and remove the JSON API payload wrapper

This commit is contained in:
Matthew Bargar 2016-01-06 15:48:12 -05:00
parent 2bea404c97
commit e9810328a9
14 changed files with 98 additions and 151 deletions

View file

@ -1,4 +1,4 @@
const indexPatterns = require('./server/routes/api/index_patterns'); const ingest = require('./server/routes/api/ingest');
module.exports = function (kibana) { module.exports = function (kibana) {
return new kibana.Plugin({ return new kibana.Plugin({
@ -48,7 +48,7 @@ module.exports = function (kibana) {
}, },
init: function (server, options) { init: function (server, options) {
indexPatterns(server); ingest(server);
} }
}); });

View file

@ -1,12 +0,0 @@
const Joi = require('joi');
module.exports = function (primary, included) {
const doc = {data: primary};
if (included) {
doc.included = included;
}
return Joi.object(doc);
};

View file

@ -1,15 +0,0 @@
const Joi = require('joi');
module.exports = function (attributes, relationships) {
const resource = {
type: Joi.string().required(),
id: Joi.string().required(),
attributes: attributes
};
if (relationships) {
resource.relationships = relationships;
}
return Joi.object(resource);
};

View file

@ -1,9 +0,0 @@
const Joi = require('joi');
module.exports = Joi.object({
data: Joi.object({
type: Joi.string().required(),
id: Joi.string().required()
})
});

View file

@ -1,36 +1,23 @@
const Joi = require('joi'); const Joi = require('joi');
const createApiDocumentSchema = require('../common/create_api_document_schema');
const createResourceObjectSchema = require('../common/create_resource_object_schema');
const relationshipObjectSchema = require('../common/relationship_object_schema');
const indexPatternResourceObject = createResourceObjectSchema( module.exports = Joi.object({
Joi.object({ id: Joi.string().required(),
title: Joi.string().required(), title: Joi.string().required(),
time_field_name: Joi.string(), time_field_name: Joi.string(),
interval_name: Joi.string(), interval_name: Joi.string(),
not_expandable: Joi.boolean(), not_expandable: Joi.boolean(),
fields: Joi.array().items( fields: Joi.array().items(
Joi.object({ Joi.object({
name: Joi.string().required(), name: Joi.string().required(),
type: Joi.string().required(), type: Joi.string().required(),
count: Joi.number().integer(), count: Joi.number().integer(),
scripted: Joi.boolean(), scripted: Joi.boolean(),
doc_values: Joi.boolean(), doc_values: Joi.boolean(),
analyzed: Joi.boolean(), analyzed: Joi.boolean(),
indexed: Joi.boolean(), indexed: Joi.boolean(),
script: Joi.string(), script: Joi.string(),
lang: Joi.string() lang: Joi.string()
}) })
).required(), ).required(),
field_format_map: Joi.object() field_format_map: Joi.object()
}) });
);
module.exports = {
post: createApiDocumentSchema(
Joi.alternatives().try(
indexPatternResourceObject,
Joi.array().items(indexPatternResourceObject)
).required()
)
};

View file

@ -4,7 +4,7 @@ const {templateToPattern, patternToTemplate} = require('../../../lib/convert_pat
module.exports = function registerDelete(server) { module.exports = function registerDelete(server) {
server.route({ server.route({
path: '/api/kibana/index_patterns/{id}', path: '/api/kibana/ingest/{id}',
method: 'DELETE', method: 'DELETE',
handler: function (req, reply) { handler: function (req, reply) {
const callWithRequest = server.plugins.elasticsearch.callWithRequest; const callWithRequest = server.plugins.elasticsearch.callWithRequest;

View file

@ -9,18 +9,19 @@ const initDefaultFieldProps = require('../../../lib/init_default_field_props');
module.exports = function registerPost(server) { module.exports = function registerPost(server) {
server.route({ server.route({
path: '/api/kibana/index_patterns', path: '/api/kibana/ingest',
method: 'POST', method: 'POST',
config: { config: {
validate: { validate: {
payload: indexPatternSchema.post payload: indexPatternSchema
} }
}, },
handler: function (req, reply) { handler: function (req, reply) {
const callWithRequest = server.plugins.elasticsearch.callWithRequest; const callWithRequest = server.plugins.elasticsearch.callWithRequest;
const requestDocument = _.cloneDeep(req.payload); const requestDocument = _.cloneDeep(req.payload);
const indexPatternId = requestDocument.data.id; const indexPatternId = requestDocument.id;
const indexPattern = convertToCamelCase(requestDocument.data.attributes); const indexPattern = convertToCamelCase(requestDocument);
delete indexPattern.id;
const mappings = createMappingsFromPatternFields(indexPattern.fields); const mappings = createMappingsFromPatternFields(indexPattern.fields);
indexPattern.fields = initDefaultFieldProps(indexPattern.fields); indexPattern.fields = initDefaultFieldProps(indexPattern.fields);

View file

@ -1,6 +1,6 @@
define({ define({
suites: [ suites: [
'test/unit/api/index_patterns/index' 'test/unit/api/ingest/index'
], ],
excludeInstrumentation: /(fixtures|node_modules)\//, excludeInstrumentation: /(fixtures|node_modules)\//,
loaderOptions: { loaderOptions: {

View file

@ -1,31 +0,0 @@
module.exports = function createTestData() {
return {
indexPattern: {
'data': {
'type': 'index_patterns',
'id': 'logstash-*',
'attributes': {
'title': 'logstash-*',
'time_field_name': '@timestamp',
'fields': [{
'name': 'ip',
'type': 'ip'
}, {
'name': '@timestamp',
'type': 'date'
}, {
'name': 'agent',
'type': 'string'
}, {
'name': 'bytes',
'type': 'number'
},
{
'name': 'geo.coordinates',
'type': 'geo_point'
}]
}
}
}
};
};

View file

@ -1,25 +1,25 @@
define(function (require) { define(function (require) {
var Promise = require('bluebird'); var Promise = require('bluebird');
var createTestData = require('intern/dojo/node!../../../unit/api/index_patterns/data'); var createTestData = require('intern/dojo/node!../../../unit/api/ingest/data');
var _ = require('intern/dojo/node!lodash'); var _ = require('intern/dojo/node!lodash');
var expect = require('intern/dojo/node!expect.js'); var expect = require('intern/dojo/node!expect.js');
return function (bdd, scenarioManager, request) { return function (bdd, scenarioManager, request) {
bdd.describe('DELETE index_patterns', function deleteIndexPatterns() { bdd.describe('DELETE ingest', function deleteIngestConfig() {
bdd.beforeEach(function () { bdd.beforeEach(function () {
return scenarioManager.reload('emptyKibana') return scenarioManager.reload('emptyKibana')
.then(function () { .then(function () {
return request.post('/kibana/index_patterns') return request.post('/kibana/ingest')
.send(createTestData().indexPattern) .send(createTestData())
.expect(201); .expect(201);
}); });
}); });
bdd.afterEach(function () { bdd.afterEach(function () {
return request.del('/kibana/index_patterns/logstash-*') return request.del('/kibana/ingest/logstash-*')
.then(function () { .then(function () {
return scenarioManager.client.indices.deleteTemplate({name: 'kibana-logstash-*'}) return scenarioManager.client.indices.deleteTemplate({name: 'kibana-logstash-*'})
.catch(function (err) { .catch(function (err) {
@ -31,10 +31,10 @@ define(function (require) {
}); });
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/ingest/logstash-*')
.expect(200) .expect(200)
.then(function () { .then(function () {
return request.get('/kibana/index_patterns/logstash-*').expect(404); return request.get('/kibana/ingest/logstash-*').expect(404);
}) })
.then(function () { .then(function () {
return scenarioManager.client.indices.getTemplate({name: 'kibana-logstash-*'}) return scenarioManager.client.indices.getTemplate({name: 'kibana-logstash-*'})
@ -45,7 +45,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/ingest/doesnotexist')
.expect(404); .expect(404);
}); });

View file

@ -1,52 +1,52 @@
define(function (require) { define(function (require) {
var Promise = require('bluebird'); var Promise = require('bluebird');
var createTestData = require('intern/dojo/node!../../../unit/api/index_patterns/data'); var createTestData = require('intern/dojo/node!../../../unit/api/ingest/data');
var _ = require('intern/dojo/node!lodash'); var _ = require('intern/dojo/node!lodash');
var expect = require('intern/dojo/node!expect.js'); var expect = require('intern/dojo/node!expect.js');
return function (bdd, scenarioManager, request) { return function (bdd, scenarioManager, request) {
bdd.describe('POST index_patterns', function postIndexPatterns() { bdd.describe('POST ingest', function postIngest() {
bdd.beforeEach(function () { bdd.beforeEach(function () {
return scenarioManager.reload('emptyKibana'); return scenarioManager.reload('emptyKibana');
}); });
bdd.afterEach(function () { bdd.afterEach(function () {
return request.del('/kibana/index_patterns/logstash-*'); return request.del('/kibana/ingest/logstash-*');
}); });
bdd.it('should return 400 for an invalid payload', function invalidPayload() { bdd.it('should return 400 for an invalid payload', function invalidPayload() {
return Promise.all([ return Promise.all([
request.post('/kibana/index_patterns').expect(400), request.post('/kibana/ingest').expect(400),
request.post('/kibana/index_patterns') request.post('/kibana/ingest')
.send({}) .send({})
.expect(400), .expect(400),
request.post('/kibana/index_patterns') request.post('/kibana/ingest')
.send(_.set(createTestData().indexPattern, 'data.attributes.title', false)) .send(_.set(createTestData(), 'title', false))
.expect(400), .expect(400),
request.post('/kibana/index_patterns') request.post('/kibana/ingest')
.send(_.set(createTestData().indexPattern, 'data.attributes.fields', {})) .send(_.set(createTestData(), 'fields', {}))
.expect(400), .expect(400),
// Fields must have a name and type // Fields must have a name and type
request.post('/kibana/index_patterns') request.post('/kibana/ingest')
.send(_.set(createTestData().indexPattern, 'data.attributes.fields', [{count: 0}])) .send(_.set(createTestData(), 'fields', [{count: 0}]))
.expect(400) .expect(400)
]); ]);
}); });
bdd.it('should return 201 when a pattern is successfully created', function createPattern() { bdd.it('should return 201 when an ingest config is successfully created', function createIngestConfig() {
return request.post('/kibana/index_patterns') return request.post('/kibana/ingest')
.send(createTestData().indexPattern) .send(createTestData())
.expect(201); .expect(201);
}); });
bdd.it('should create an index template if a fields array is included', function createTemplate() { bdd.it('should create an index template if a fields array is included', function createTemplate() {
return request.post('/kibana/index_patterns') return request.post('/kibana/ingest')
.send(createTestData().indexPattern) .send(createTestData())
.expect(201) .expect(201)
.then(function () { .then(function () {
return scenarioManager.client.indices.getTemplate({name: 'kibana-logstash-*'}); return scenarioManager.client.indices.getTemplate({name: 'kibana-logstash-*'});
@ -54,8 +54,8 @@ define(function (require) {
}); });
bdd.it('should provide defaults for field properties', function createTemplate() { bdd.it('should provide defaults for field properties', function createTemplate() {
return request.post('/kibana/index_patterns') return request.post('/kibana/ingest')
.send(createTestData().indexPattern) .send(createTestData())
.expect(201) .expect(201)
.then(function () { .then(function () {
return scenarioManager.client.get({ return scenarioManager.client.get({
@ -78,10 +78,10 @@ define(function (require) {
}); });
}); });
bdd.it('should create index template with _default_ mappings based on the info in the kibana index pattern', bdd.it('should create index template with _default_ mappings based on the info in the ingest config',
function createTemplate() { function createTemplate() {
return request.post('/kibana/index_patterns') return request.post('/kibana/ingest')
.send(createTestData().indexPattern) .send(createTestData())
.expect(201) .expect(201)
.then(function () { .then(function () {
return scenarioManager.client.indices.getTemplate({name: 'kibana-logstash-*'}) return scenarioManager.client.indices.getTemplate({name: 'kibana-logstash-*'})
@ -110,12 +110,12 @@ define(function (require) {
}); });
bdd.it('should return 409 conflict when a pattern with the given ID already exists', function patternConflict() { bdd.it('should return 409 conflict when a pattern with the given ID already exists', function patternConflict() {
return request.post('/kibana/index_patterns') return request.post('/kibana/ingest')
.send(createTestData().indexPattern) .send(createTestData())
.expect(201) .expect(201)
.then(function () { .then(function () {
return request.post('/kibana/index_patterns') return request.post('/kibana/ingest')
.send(createTestData().indexPattern) .send(createTestData())
.expect(409); .expect(409);
}); });
}); });
@ -127,8 +127,8 @@ define(function (require) {
template: 'logstash-*' template: 'logstash-*'
} }
}).then(function () { }).then(function () {
return request.post('/kibana/index_patterns') return request.post('/kibana/ingest')
.send(createTestData().indexPattern) .send(createTestData())
.expect(409); .expect(409);
}) })
.then(function () { .then(function () {
@ -140,21 +140,21 @@ define(function (require) {
bdd.it('should return 409 conflict when the pattern matches existing indices', bdd.it('should return 409 conflict when the pattern matches existing indices',
function existingIndicesConflict() { function existingIndicesConflict() {
var pattern = createTestData().indexPattern; var pattern = createTestData();
pattern.data.id = pattern.data.attributes.title = '.kib*'; pattern.id = pattern.title = '.kib*';
return request.post('/kibana/index_patterns') return request.post('/kibana/ingest')
.send(pattern) .send(pattern)
.expect(409); .expect(409);
}); });
bdd.it('should enforce snake_case in the request body', function () { bdd.it('should enforce snake_case in the request body', function () {
var pattern = createTestData().indexPattern; var pattern = createTestData();
pattern.data.attributes = _.mapKeys(pattern.data.attributes, function (value, key) { pattern = _.mapKeys(pattern, function (value, key) {
return _.camelCase(key); return _.camelCase(key);
}); });
return request.post('/kibana/index_patterns') return request.post('/kibana/ingest')
.send(pattern) .send(pattern)
.expect(400); .expect(400);
}); });

View file

@ -0,0 +1,26 @@
module.exports = function createTestData() {
return {
'id': 'logstash-*',
'title': 'logstash-*',
'time_field_name': '@timestamp',
'fields': [
{
'name': 'ip',
'type': 'ip'
}, {
'name': '@timestamp',
'type': 'date'
}, {
'name': 'agent',
'type': 'string'
}, {
'name': 'bytes',
'type': 'number'
},
{
'name': 'geo.coordinates',
'type': 'geo_point'
}
]
};
};

View file

@ -9,7 +9,7 @@ define(function (require) {
var post = require('./_post'); var post = require('./_post');
var del = require('./_del'); var del = require('./_del');
bdd.describe('index_patterns API', function () { bdd.describe('ingest API', function () {
var scenarioManager = new ScenarioManager(url.format(serverConfig.servers.elasticsearch)); var scenarioManager = new ScenarioManager(url.format(serverConfig.servers.elasticsearch));
request = request(url.format(serverConfig.servers.kibana) + '/api'); request = request(url.format(serverConfig.servers.kibana) + '/api');