forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Maps] Add drawing index data endpoint (elastic#94728)
- Loading branch information
Aaron Caldwell
authored
Mar 26, 2021
1 parent
6872231
commit d89ede9
Showing
8 changed files
with
265 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { i18n } from '@kbn/i18n'; | ||
import { ElasticsearchClient } from 'kibana/server'; | ||
import { WriteSettings } from '../../common'; | ||
|
||
export async function writeDataToIndex( | ||
index: string, | ||
data: object, | ||
asCurrentUser: ElasticsearchClient | ||
) { | ||
try { | ||
const { body: indexExists } = await asCurrentUser.indices.exists({ index }); | ||
if (!indexExists) { | ||
throw new Error( | ||
i18n.translate('xpack.maps.indexData.indexExists', { | ||
defaultMessage: `Index: '{index}' not found. A valid index must be provided`, | ||
values: { | ||
index, | ||
}, | ||
}) | ||
); | ||
} | ||
const settings: WriteSettings = { index, body: data }; | ||
const { body: resp } = await asCurrentUser.index(settings); | ||
if (resp.result === 'Error') { | ||
throw resp; | ||
} else { | ||
return { | ||
success: true, | ||
data, | ||
}; | ||
} | ||
} catch (error) { | ||
return { | ||
success: false, | ||
error, | ||
}; | ||
} | ||
} |
104 changes: 104 additions & 0 deletions
104
x-pack/plugins/maps/server/data_indexing/indexing_routes.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { schema } from '@kbn/config-schema'; | ||
import { Logger } from 'src/core/server'; | ||
import { IRouter } from 'src/core/server'; | ||
import type { DataRequestHandlerContext } from 'src/plugins/data/server'; | ||
import { | ||
INDEX_SOURCE_API_PATH, | ||
GIS_API_PATH, | ||
MAX_DRAWING_SIZE_BYTES, | ||
} from '../../common/constants'; | ||
import { createDocSource } from './create_doc_source'; | ||
import { writeDataToIndex } from './index_data'; | ||
import { PluginStart as DataPluginStart } from '../../../../../src/plugins/data/server'; | ||
|
||
export function initIndexingRoutes({ | ||
router, | ||
logger, | ||
dataPlugin, | ||
}: { | ||
router: IRouter<DataRequestHandlerContext>; | ||
logger: Logger; | ||
dataPlugin: DataPluginStart; | ||
}) { | ||
router.post( | ||
{ | ||
path: `/${INDEX_SOURCE_API_PATH}`, | ||
validate: { | ||
body: schema.object({ | ||
index: schema.string(), | ||
mappings: schema.any(), | ||
}), | ||
}, | ||
options: { | ||
body: { | ||
accepts: ['application/json'], | ||
}, | ||
}, | ||
}, | ||
async (context, request, response) => { | ||
const { index, mappings } = request.body; | ||
const indexPatternsService = await dataPlugin.indexPatterns.indexPatternsServiceFactory( | ||
context.core.savedObjects.client, | ||
context.core.elasticsearch.client.asCurrentUser | ||
); | ||
const result = await createDocSource( | ||
index, | ||
mappings, | ||
context.core.elasticsearch.client, | ||
indexPatternsService | ||
); | ||
if (result.success) { | ||
return response.ok({ body: result }); | ||
} else { | ||
if (result.error) { | ||
logger.error(result.error); | ||
} | ||
return response.custom({ | ||
body: result?.error?.message, | ||
statusCode: 500, | ||
}); | ||
} | ||
} | ||
); | ||
|
||
router.post( | ||
{ | ||
path: `/${GIS_API_PATH}/feature`, | ||
validate: { | ||
body: schema.object({ | ||
index: schema.string(), | ||
data: schema.any(), | ||
}), | ||
}, | ||
options: { | ||
body: { | ||
accepts: ['application/json'], | ||
maxBytes: MAX_DRAWING_SIZE_BYTES, | ||
}, | ||
}, | ||
}, | ||
async (context, request, response) => { | ||
const result = await writeDataToIndex( | ||
request.body.index, | ||
request.body.data, | ||
context.core.elasticsearch.client.asCurrentUser | ||
); | ||
if (result.success) { | ||
return response.ok({ body: result }); | ||
} else { | ||
logger.error(result.error); | ||
return response.custom({ | ||
body: result.error.message, | ||
statusCode: 500, | ||
}); | ||
} | ||
} | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import expect from '@kbn/expect'; | ||
|
||
export default function ({ getService }) { | ||
const supertest = getService('supertest'); | ||
|
||
describe('index feature data', () => { | ||
it('should add point data to an existing index', async () => { | ||
await supertest | ||
.post(`/api/maps/docSource`) | ||
.set('kbn-xsrf', 'kibana') | ||
.send({ | ||
index: 'new-point-feature-index', | ||
mappings: { properties: { coordinates: { type: 'geo_point' } } }, | ||
}); | ||
|
||
const resp = await supertest | ||
.post(`/api/maps/feature`) | ||
.set('kbn-xsrf', 'kibana') | ||
.send({ | ||
index: 'new-point-feature-index', | ||
data: { coordinates: [125.6, 10.1], name: 'Dinagat Islands' }, | ||
}) | ||
.expect(200); | ||
|
||
expect(resp.body.success).to.be(true); | ||
}); | ||
|
||
it('should add shape data to an existing index', async () => { | ||
await supertest | ||
.post(`/api/maps/docSource`) | ||
.set('kbn-xsrf', 'kibana') | ||
.send({ | ||
index: 'new-shape-feature-index', | ||
mappings: { properties: { coordinates: { type: 'geo_shape' } } }, | ||
}); | ||
|
||
const resp = await supertest | ||
.post(`/api/maps/feature`) | ||
.set('kbn-xsrf', 'kibana') | ||
.send({ | ||
index: 'new-shape-feature-index', | ||
data: { | ||
coordinates: { | ||
type: 'Polygon', | ||
coordinates: [ | ||
[ | ||
[-20.91796875, 25.64152637306577], | ||
[-13.0517578125, 25.64152637306577], | ||
[-13.0517578125, 31.203404950917395], | ||
[-20.91796875, 31.203404950917395], | ||
[-20.91796875, 25.64152637306577], | ||
], | ||
], | ||
}, | ||
}, | ||
}) | ||
.expect(200); | ||
|
||
expect(resp.body.success).to.be(true); | ||
}); | ||
|
||
it('should fail if data is invalid', async () => { | ||
await supertest | ||
.post(`/api/maps/docSource`) | ||
.set('kbn-xsrf', 'kibana') | ||
.send({ | ||
index: 'new-feature-index2', | ||
mappings: { properties: { coordinates: { type: 'geo_point' } } }, | ||
}); | ||
await supertest | ||
.post(`/api/maps/feature`) | ||
.set('kbn-xsrf', 'kibana') | ||
.send({ | ||
index: 'new-feature-index2', | ||
data: { coordinates: [600, 800], name: 'Never Gonna Happen Islands' }, | ||
}) | ||
.expect(500); | ||
}); | ||
|
||
it('should fail if index does not exist', async () => { | ||
await supertest | ||
.post(`/api/maps/feature`) | ||
.set('kbn-xsrf', 'kibana') | ||
.send({ | ||
index: 'not-an-index', | ||
data: { coordinates: [125.6, 10.1], name: 'Dinagat Islands' }, | ||
}) | ||
.expect(500); | ||
}); | ||
}); | ||
} |