diff --git a/kibana-extra/castro/index.js b/kibana-extra/castro/index.js index d5ef87720565f..e9186bbe77e6b 100644 --- a/kibana-extra/castro/index.js +++ b/kibana-extra/castro/index.js @@ -1,4 +1,7 @@ import exampleRoute from './server/routes/example'; +import personRoute from './server/routes/person'; + +import mappings from './mappings'; export default function (kibana) { return new kibana.Plugin({ @@ -14,12 +17,16 @@ export default function (kibana) { hacks: [ 'plugins/castro/hack' - ] + ], + + mappings }, config(Joi) { return Joi.object({ enabled: Joi.boolean().default(true), + index: Joi.string().default('.castro'), + fromyaml: Joi.string().default('empty') }).default(); }, @@ -27,6 +34,7 @@ export default function (kibana) { init(server, options) { // Add server routes and initialize the plugin here exampleRoute(server); + personRoute(server); } diff --git a/kibana-extra/castro/mappings.json b/kibana-extra/castro/mappings.json new file mode 100644 index 0000000000000..86925ab6e0941 --- /dev/null +++ b/kibana-extra/castro/mappings.json @@ -0,0 +1,12 @@ +{ + "person": { + "properties": { + "firstName": { + "type": "text" + }, + "lastName": { + "type": "text" + } + } + } +} \ No newline at end of file diff --git a/kibana-extra/castro/proto/Example.proto b/kibana-extra/castro/proto/Example.proto index d77f2a28f7bac..651f978ca2dde 100644 --- a/kibana-extra/castro/proto/Example.proto +++ b/kibana-extra/castro/proto/Example.proto @@ -16,3 +16,8 @@ message Entry { string blob = 2; bool isBinary = 3; } + +message Person { + string firstName = 1; + string lastName = 2; +} diff --git a/kibana-extra/castro/server/routes/example.ts b/kibana-extra/castro/server/routes/example.ts index a10043c3a94d6..c38fc4afb42ff 100644 --- a/kibana-extra/castro/server/routes/example.ts +++ b/kibana-extra/castro/server/routes/example.ts @@ -43,6 +43,5 @@ export default function (server: Hapi.Server) { handler(req: Hapi.Request, reply: any) { getHeadCommit().then((result: Commit) => reply(result)) } - }) - + }); } diff --git a/kibana-extra/castro/server/routes/person.ts b/kibana-extra/castro/server/routes/person.ts new file mode 100644 index 0000000000000..168f854322a59 --- /dev/null +++ b/kibana-extra/castro/server/routes/person.ts @@ -0,0 +1,106 @@ +import * as Hapi from "hapi"; +import { Person } from '../../common/proto'; + +export default function (server: Hapi.Server) { + // Create + server.route({ + path: '/api/castro/person/{id}', + method: 'POST', + handler: async function (req: Hapi.Request, reply: any) { + const id = req.params.id; + const person = Person.fromObject(req.payload); + + const es = req.server.plugins.elasticsearch.getCluster("data"); + // const client = req.getSavedObjectsClient(); + const {callWithRequest} = es; + + const config = req.server.config(); + const index = config.get('castro.index'); + + callWithRequest(req, 'create', { + index, + type: 'person', + id, + body: person.toJSON() + }).then(function (response: any) { + reply(response); + }); + } + }); + + // Read + server.route({ + path: '/api/castro/person/{id}', + method: 'GET', + handler: async function (req: Hapi.Request, reply: any) { + + const config = req.server.config(); + const index = config.get('castro.index'); + console.log("## castro config: "); + console.log(config.get('castro.index')); + console.log(config.get('castro.fromyaml')); + + const id = req.params.id; + const es = req.server.plugins.elasticsearch.getCluster("data"); + // const client = req.getSavedObjectsClient(); + const {callWithRequest} = es; + callWithRequest(req, 'get', { + index, + type: 'person', + id + }).then(function (response) { + reply(response) + }); + } + }); + + // Update + server.route({ + path: '/api/castro/person/{id}', + method: 'PUT', + handler: async function (req: Hapi.Request, reply: any) { + const id = req.params.id; + const body = req.payload; + + const es = req.server.plugins.elasticsearch.getCluster("data"); + // const client = req.getSavedObjectsClient(); + const {callWithRequest} = es; + + const config = req.server.config(); + const index = config.get('castro.index'); + + callWithRequest(req, 'update', { + index, + type: 'person', + id, + body + }).then(function (response) { + reply(response) + }); + + } + }); + + // Delete + server.route({ + path: '/api/castro/person/{id}', + method: 'DELETE', + handler: async function (req: Hapi.Request, reply: any) { + const id = req.params.id; + const es = req.server.plugins.elasticsearch.getCluster("data"); + // const client = req.getSavedObjectsClient(); + const {callWithRequest} = es; + + const config = req.server.config(); + const index = config.get('castro.index'); + + callWithRequest(req, 'delete', { + index, + type: 'person', + id + }).then(function (response) { + reply(response) + }); + } + }); +} \ No newline at end of file