-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Reporting] refactor routes files and helpers #30111
Merged
tsullivan
merged 19 commits into
elastic:master
from
tsullivan:refactor/reporting-routes
Feb 12, 2019
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
733b26d
remove unused file
tsullivan a6c8662
refactor routes files and helpers
tsullivan a2a4a24
default empty array for conflictedTypesFields
tsullivan 27dbc8c
minor prettier
tsullivan 336512d
Merge branch 'master' into refactor/reporting-routes
tsullivan 267cd91
remove some unrelated diff
tsullivan 6bfce10
some typescript conversions
tsullivan 14da622
Merge branch 'master' into refactor/reporting-routes
tsullivan 027cdd8
more typescripts
tsullivan 47cdbe9
more typscript
tsullivan bbadf60
jobtype is a string
tsullivan 8c4edb2
revert some logic change
tsullivan 9d9af29
set payload.headers to undefined + not mutate
tsullivan d7f2277
fix jest import
tsullivan 9e265d3
Merge branch 'master' into refactor/reporting-routes
tsullivan 9775d23
Merge branch 'master' into refactor/reporting-routes
tsullivan 2f9c549
Merge branch 'master' into refactor/reporting-routes
tsullivan 5e91d4a
Merge branch 'master' into refactor/reporting-routes
tsullivan 31e6a26
Merge branch 'master' into refactor/reporting-routes
tsullivan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
129 changes: 0 additions & 129 deletions
129
x-pack/plugins/reporting/export_types/common/lib/__tests__/get_absolute_time.js
This file was deleted.
Oops, something went wrong.
25 changes: 0 additions & 25 deletions
25
x-pack/plugins/reporting/export_types/common/lib/get_absolute_time.js
This file was deleted.
Oops, something went wrong.
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,54 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import boom from 'boom'; | ||
import { Request, ResponseToolkit } from 'hapi'; | ||
import rison from 'rison-node'; | ||
import { API_BASE_URL } from '../../common/constants'; | ||
import { KbnServer } from '../../types'; | ||
import { getRouteConfigFactoryReportingPre } from './lib/route_config_factories'; | ||
import { HandlerErrorFunction, HandlerFunction } from './types'; | ||
|
||
const BASE_GENERATE = `${API_BASE_URL}/generate`; | ||
|
||
export function registerGenerate( | ||
server: KbnServer, | ||
handler: HandlerFunction, | ||
handleError: HandlerErrorFunction | ||
) { | ||
const getRouteConfig = getRouteConfigFactoryReportingPre(server); | ||
|
||
// generate report | ||
server.route({ | ||
path: `${BASE_GENERATE}/{exportType}`, | ||
method: 'POST', | ||
config: getRouteConfig(request => request.params.exportType), | ||
handler: async (request: Request, h: ResponseToolkit) => { | ||
const { exportType } = request.params; | ||
let response; | ||
try { | ||
// @ts-ignore | ||
const jobParams = rison.decode(request.query.jobParams); | ||
response = await handler(exportType, jobParams, request, h); | ||
} catch (err) { | ||
throw handleError(exportType, err); | ||
} | ||
return response; | ||
}, | ||
}); | ||
|
||
// show error about GET method to user | ||
server.route({ | ||
path: `${BASE_GENERATE}/{p*}`, | ||
method: 'GET', | ||
config: getRouteConfig(), | ||
handler: () => { | ||
const err = boom.methodNotAllowed('GET is not allowed'); | ||
err.output.headers.allow = 'POST'; | ||
return err; | ||
}, | ||
}); | ||
} |
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,57 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import boom from 'boom'; | ||
import { Request, ResponseToolkit } from 'hapi'; | ||
import { API_BASE_URL } from '../../common/constants'; | ||
import { KbnServer } from '../../types'; | ||
// @ts-ignore | ||
import { enqueueJobFactory } from '../lib/enqueue_job'; | ||
import { registerGenerate } from './generate'; | ||
import { registerJobs } from './jobs'; | ||
import { registerLegacy } from './legacy'; | ||
|
||
export function registerRoutes(server: KbnServer) { | ||
const config = server.config(); | ||
const DOWNLOAD_BASE_URL = config.get('server.basePath') + `${API_BASE_URL}/jobs/download`; | ||
const { errors: esErrors } = server.plugins.elasticsearch.getCluster('admin'); | ||
const enqueueJob = enqueueJobFactory(server); | ||
|
||
async function handler(exportTypeId: any, jobParams: any, request: Request, h: ResponseToolkit) { | ||
// @ts-ignore | ||
const user = request.pre.user; | ||
const headers = request.headers; | ||
|
||
const job = await enqueueJob(exportTypeId, jobParams, user, headers, request); | ||
|
||
// return the queue's job information | ||
const jobJson = job.toJSON(); | ||
|
||
return h | ||
.response({ | ||
path: `${DOWNLOAD_BASE_URL}/${jobJson.id}`, | ||
job: jobJson, | ||
}) | ||
.type('application/json'); | ||
} | ||
|
||
function handleError(exportType: any, err: Error) { | ||
if (err instanceof esErrors['401']) { | ||
return boom.unauthorized(`Sorry, you aren't authenticated`); | ||
} | ||
if (err instanceof esErrors['403']) { | ||
return boom.forbidden(`Sorry, you are not authorized to create ${exportType} reports`); | ||
} | ||
if (err instanceof esErrors['404']) { | ||
return boom.boomify(err, { statusCode: 404 }); | ||
} | ||
return err; | ||
} | ||
|
||
registerGenerate(server, handler, handleError); | ||
registerLegacy(server, handler, handleError); | ||
registerJobs(server); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice consolidation here