Skip to content

Commit

Permalink
fix route configs
Browse files Browse the repository at this point in the history
  • Loading branch information
tsullivan committed Oct 22, 2019
1 parent 89fe8c6 commit 6de1d4b
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import Joi from 'joi';
import rison from 'rison-node';
import { API_BASE_URL } from '../../common/constants';
import { ServerFacade, RequestFacade, ReportingResponseToolkit } from '../../types';
import { getRouteConfigFactoryReportingPre } from './lib/route_config_factories';
import {
getRouteConfigFactoryReportingPre,
GetRouteConfigFactoryFn,
RouteConfigFactory,
} from './lib/route_config_factories';
import { HandlerErrorFunction, HandlerFunction } from './types';

const BASE_GENERATE = `${API_BASE_URL}/generate`;
Expand All @@ -19,22 +23,31 @@ export function registerGenerateFromJobParams(
handler: HandlerFunction,
handleError: HandlerErrorFunction
) {
const getRouteConfig = () => ({
...getRouteConfigFactoryReportingPre(server),
validate: {
params: Joi.object({
exportType: Joi.string().required(),
}).required(),
payload: Joi.object({
jobParams: Joi.string()
.optional()
.default(null),
}).allow(null), // allow optional payload
query: Joi.object({
jobParams: Joi.string().default(null),
}).default(),
},
});
const getRouteConfig = () => {
const getOriginalRouteConfig: GetRouteConfigFactoryFn = getRouteConfigFactoryReportingPre(
server
);
const routeConfigFactory: RouteConfigFactory = getOriginalRouteConfig(
({ params: { exportType } }) => exportType
);

return {
...routeConfigFactory,
validate: {
params: Joi.object({
exportType: Joi.string().required(),
}).required(),
payload: Joi.object({
jobParams: Joi.string()
.optional()
.default(null),
}).allow(null), // allow optional payload
query: Joi.object({
jobParams: Joi.string().default(null),
}).default(),
},
};
};

// generate report
server.route({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,61 +4,13 @@
* you may not use this file except in compliance with the Elastic License.
*/

import Joi from 'joi';
import { get } from 'lodash';
import { API_BASE_GENERATE_V1, CSV_FROM_SAVEDOBJECT_JOB_TYPE } from '../../common/constants';
import { ServerFacade, RequestFacade, ReportingResponseToolkit } from '../../types';
import { getRouteConfigFactoryReportingPre } from './lib/route_config_factories';
import { HandlerErrorFunction, HandlerFunction, QueuedJobPayload } from './types';
import { getRouteOptionsCsv } from './lib/route_config_factories';
import { getJobParamsFromRequest } from '../../export_types/csv_from_savedobject/server/lib/get_job_params_from_request';

export function getRouteOptionsCsv(server: ServerFacade) {
const getRouteConfig = getRouteConfigFactoryReportingPre(server);
return {
...getRouteConfig(() => CSV_FROM_SAVEDOBJECT_JOB_TYPE),
validate: {
params: Joi.object({
savedObjectType: Joi.string().required(),
savedObjectId: Joi.string().required(),
}).required(),
payload: Joi.object({
state: Joi.object().default({}),
timerange: Joi.object({
timezone: Joi.string().default('UTC'),
min: Joi.date().required(),
max: Joi.date().required(),
}).optional(),
}),
},
};
}

/*
* 1. Build `jobParams` object: job data that execution will need to reference in various parts of the lifecycle
* 2. Pass the jobParams and other common params to `handleRoute`, a shared function to enqueue the job with the params
* 3. Ensure that details for a queued job were returned
*/
const getJobFromRouteHandler = async (
handleRoute: HandlerFunction,
handleRouteError: HandlerErrorFunction,
request: RequestFacade,
h: ReportingResponseToolkit
): Promise<QueuedJobPayload> => {
let result: QueuedJobPayload;
try {
const jobParams = getJobParamsFromRequest(request, { isImmediate: false });
result = await handleRoute(CSV_FROM_SAVEDOBJECT_JOB_TYPE, jobParams, request, h);
} catch (err) {
throw handleRouteError(CSV_FROM_SAVEDOBJECT_JOB_TYPE, err);
}

if (get(result, 'source.job') == null) {
throw new Error(`The Export handler is expected to return a result with job info! ${result}`);
}

return result;
};

/*
* This function registers API Endpoints for queuing Reporting jobs. The API inputs are:
* - saved object type and ID
Expand All @@ -80,7 +32,27 @@ export function registerGenerateCsvFromSavedObject(
method: 'POST',
options: routeOptions,
handler: async (request: RequestFacade, h: ReportingResponseToolkit) => {
return getJobFromRouteHandler(handleRoute, handleRouteError, request, h);
/*
* 1. Build `jobParams` object: job data that execution will need to reference in various parts of the lifecycle
* 2. Pass the jobParams and other common params to `handleRoute`, a shared function to enqueue the job with the params
* 3. Ensure that details for a queued job were returned
*/

let result: QueuedJobPayload;
try {
const jobParams = getJobParamsFromRequest(request, { isImmediate: false });
result = await handleRoute(CSV_FROM_SAVEDOBJECT_JOB_TYPE, jobParams, request, h);
} catch (err) {
throw handleRouteError(CSV_FROM_SAVEDOBJECT_JOB_TYPE, err);
}

if (get(result, 'source.job') == null) {
throw new Error(
`The Export handler is expected to return a result with job info! ${result}`
);
}

return result;
},
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
JobIDForImmediate,
JobDocOutputExecuted,
} from '../../types';
import { getRouteOptionsCsv } from './generate_from_savedobject';
import { getRouteOptionsCsv } from './lib/route_config_factories';
import { getJobParamsFromRequest } from '../../export_types/csv_from_savedobject/server/lib/get_job_params_from_request';

/*
Expand Down
2 changes: 1 addition & 1 deletion x-pack/legacy/plugins/reporting/server/routes/legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function registerLegacy(
handler: HandlerFunction,
handleError: HandlerErrorFunction
) {
const getRouteConfig: GetRouteConfigFactoryFn = getRouteConfigFactoryReportingPre(server);
const getRouteConfig = getRouteConfigFactoryReportingPre(server);

function createLegacyPdfRoute({ path, objectType }: { path: string; objectType: string }) {
const exportTypeId = 'printablePdf';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* you may not use this file except in compliance with the Elastic License.
*/

import Joi from 'joi';
import { CSV_FROM_SAVEDOBJECT_JOB_TYPE } from '../../../common/constants';
import { ServerFacade, RequestFacade } from '../../../types';
// @ts-ignore
import { authorizedUserPreRoutingFactory } from './authorized_user_pre_routing';
Expand All @@ -12,7 +14,7 @@ import { reportingFeaturePreRoutingFactory } from './reporting_feature_pre_routi

const API_TAG = 'api';

interface RouteConfigFactory {
export interface RouteConfigFactory {
tags?: string[];
pre: any[];
response?: {
Expand Down Expand Up @@ -44,6 +46,27 @@ export function getRouteConfigFactoryReportingPre(server: ServerFacade): GetRout
};
}

export function getRouteOptionsCsv(server: ServerFacade) {
const getRouteConfig = getRouteConfigFactoryReportingPre(server);
return {
...getRouteConfig(() => CSV_FROM_SAVEDOBJECT_JOB_TYPE),
validate: {
params: Joi.object({
savedObjectType: Joi.string().required(),
savedObjectId: Joi.string().required(),
}).required(),
payload: Joi.object({
state: Joi.object().default({}),
timerange: Joi.object({
timezone: Joi.string().default('UTC'),
min: Joi.date().required(),
max: Joi.date().required(),
}).optional(),
}),
},
};
}

export function getRouteConfigFactoryManagementPre(server: ServerFacade): GetRouteConfigFactoryFn {
const authorizedUserPreRouting = authorizedUserPreRoutingFactory(server);
const reportingFeaturePreRouting = reportingFeaturePreRoutingFactory(server);
Expand Down

0 comments on commit 6de1d4b

Please sign in to comment.