-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Reporting] refactor routes files and helpers (#30111)
* remove unused file * refactor routes files and helpers * default empty array for conflictedTypesFields * minor prettier * remove some unrelated diff * some typescript conversions * more typescripts * more typscript * jobtype is a string * revert some logic change * set payload.headers to undefined + not mutate * fix jest import
- Loading branch information
Showing
20 changed files
with
455 additions
and
462 deletions.
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.