Skip to content

Commit

Permalink
Revert touching routes code
Browse files Browse the repository at this point in the history
  • Loading branch information
tsullivan committed May 18, 2020
1 parent 6e7725e commit d4fd2a3
Show file tree
Hide file tree
Showing 13 changed files with 69 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ import { Legacy } from 'kibana';
import rison from 'rison-node';
import { ReportingCore } from '../';
import { API_BASE_URL } from '../../common/constants';
import { LevelLogger } from '../lib';
import { HandlerErrorFunction, HandlerFunction, ReportingSetupDeps, ServerFacade } from '../types';
import { LevelLogger as Logger } from '../lib';
import { ReportingSetupDeps, ServerFacade } from '../types';
import { makeRequestFacade } from './lib/make_request_facade';
import {
GetRouteConfigFactoryFn,
getRouteConfigFactoryReportingPre,
RouteConfigFactory,
} from './lib/route_config_factories';
import { HandlerErrorFunction, HandlerFunction, ReportingResponseToolkit } from './types';

const BASE_GENERATE = `${API_BASE_URL}/generate`;

Expand All @@ -27,7 +28,7 @@ export function registerGenerateFromJobParams(
plugins: ReportingSetupDeps,
handler: HandlerFunction,
handleError: HandlerErrorFunction,
logger: LevelLogger
logger: Logger
) {
const config = reporting.getConfig();
const getRouteConfig = () => {
Expand Down Expand Up @@ -63,7 +64,7 @@ export function registerGenerateFromJobParams(
path: `${BASE_GENERATE}/{exportType}`,
method: 'POST',
options: getRouteConfig(),
handler: async (legacyRequest: Legacy.Request, h: Legacy.ResponseToolkit) => {
handler: async (legacyRequest: Legacy.Request, h: ReportingResponseToolkit) => {
const request = makeRequestFacade(legacyRequest);
let jobParamsRison: string | null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ import { get } from 'lodash';
import { ReportingCore } from '../';
import { API_BASE_GENERATE_V1, CSV_FROM_SAVEDOBJECT_JOB_TYPE } from '../../common/constants';
import { getJobParamsFromRequest } from '../../export_types/csv_from_savedobject/server/lib/get_job_params_from_request';
import { LevelLogger } from '../lib';
import { LevelLogger as Logger } from '../lib';
import { ReportingSetupDeps, ServerFacade } from '../types';
import { makeRequestFacade } from './lib/make_request_facade';
import { getRouteOptionsCsv } from './lib/route_config_factories';
import {
HandlerErrorFunction,
HandlerFunction,
QueuedJobPayload,
ReportingSetupDeps,
ServerFacade,
} from '../types';
import { makeRequestFacade } from './lib/make_request_facade';
import { getRouteOptionsCsv } from './lib/route_config_factories';
ReportingResponseToolkit,
} from './types';

/*
* This function registers API Endpoints for queuing Reporting jobs. The API inputs are:
Expand All @@ -35,7 +35,7 @@ export function registerGenerateCsvFromSavedObject(
plugins: ReportingSetupDeps,
handleRoute: HandlerFunction,
handleRouteError: HandlerErrorFunction,
logger: LevelLogger
logger: Logger
) {
const config = reporting.getConfig();
const routeOptions = getRouteOptionsCsv(config, plugins, logger);
Expand All @@ -44,7 +44,7 @@ export function registerGenerateCsvFromSavedObject(
path: `${API_BASE_GENERATE_V1}/csv/saved-object/{savedObjectType}:{savedObjectId}`,
method: 'POST',
options: routeOptions,
handler: async (legacyRequest: Legacy.Request, h: Legacy.ResponseToolkit) => {
handler: async (legacyRequest: Legacy.Request, h: ReportingResponseToolkit) => {
const requestFacade = makeRequestFacade(legacyRequest);

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import { API_BASE_GENERATE_V1 } from '../../common/constants';
import { createJobFactory, executeJobFactory } from '../../export_types/csv_from_savedobject';
import { getJobParamsFromRequest } from '../../export_types/csv_from_savedobject/server/lib/get_job_params_from_request';
import { JobDocPayloadPanelCsv } from '../../export_types/csv_from_savedobject/types';
import { LevelLogger } from '../lib';
import { LevelLogger as Logger } from '../lib';
import { JobDocOutput, ReportingSetupDeps, ServerFacade } from '../types';
import { makeRequestFacade } from './lib/make_request_facade';
import { getRouteOptionsCsv } from './lib/route_config_factories';
import { ReportingResponseToolkit } from './types';

type ResponseFacade = ResponseObject & {
isBoom: boolean;
Expand All @@ -33,7 +34,7 @@ export function registerGenerateCsvFromSavedObjectImmediate(
reporting: ReportingCore,
server: ServerFacade,
plugins: ReportingSetupDeps,
parentLogger: LevelLogger
parentLogger: Logger
) {
const config = reporting.getConfig();
const routeOptions = getRouteOptionsCsv(config, plugins, parentLogger);
Expand All @@ -47,7 +48,7 @@ export function registerGenerateCsvFromSavedObjectImmediate(
path: `${API_BASE_GENERATE_V1}/immediate/csv/saved-object/{savedObjectType}:{savedObjectId}`,
method: 'POST',
options: routeOptions,
handler: async (legacyRequest: Legacy.Request, h: Legacy.ResponseToolkit) => {
handler: async (legacyRequest: Legacy.Request, h: ReportingResponseToolkit) => {
const request = makeRequestFacade(legacyRequest);
const logger = parentLogger.clone(['savedobject-csv']);
const jobParams = getJobParamsFromRequest(request, { isImmediate: true });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import Hapi from 'hapi';
import { ReportingConfig, ReportingCore } from '../';
import { createMockReportingCore } from '../../test_helpers';
import { LevelLogger } from '../lib';
import { LevelLogger as Logger } from '../lib';
import { ReportingSetupDeps, ServerFacade } from '../types';
import { registerJobGenerationRoutes } from './generation';

Expand All @@ -27,7 +27,7 @@ let mockReportingConfig: ReportingConfig;
const mockLogger = ({
error: jest.fn(),
debug: jest.fn(),
} as unknown) as LevelLogger;
} as unknown) as Logger;

beforeEach(async () => {
mockServer = new Hapi.Server({
Expand Down
7 changes: 4 additions & 3 deletions x-pack/legacy/plugins/reporting/server/routes/generation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,21 @@ import { errors as elasticsearchErrors } from 'elasticsearch';
import { Legacy } from 'kibana';
import { ReportingCore } from '../';
import { API_BASE_URL } from '../../common/constants';
import { LevelLogger } from '../lib';
import { LevelLogger as Logger } from '../lib';
import { ReportingSetupDeps, ServerFacade } from '../types';
import { registerGenerateFromJobParams } from './generate_from_jobparams';
import { registerGenerateCsvFromSavedObject } from './generate_from_savedobject';
import { registerGenerateCsvFromSavedObjectImmediate } from './generate_from_savedobject_immediate';
import { makeRequestFacade } from './lib/make_request_facade';
import { ReportingResponseToolkit } from './types';

const esErrors = elasticsearchErrors as Record<string, any>;

export function registerJobGenerationRoutes(
reporting: ReportingCore,
server: ServerFacade,
plugins: ReportingSetupDeps,
logger: LevelLogger
logger: Logger
) {
const config = reporting.getConfig();
const downloadBaseUrl =
Expand All @@ -35,7 +36,7 @@ export function registerJobGenerationRoutes(
exportTypeId: string,
jobParams: object,
legacyRequest: Legacy.Request,
h: Legacy.ResponseToolkit
h: ReportingResponseToolkit
) {
const request = makeRequestFacade(legacyRequest);
const user = request.pre.user;
Expand Down
4 changes: 2 additions & 2 deletions x-pack/legacy/plugins/reporting/server/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { ReportingCore } from '../';
import { LevelLogger } from '../lib';
import { LevelLogger as Logger } from '../lib';
import { ReportingSetupDeps, ServerFacade } from '../types';
import { registerJobGenerationRoutes } from './generation';
import { registerJobInfoRoutes } from './jobs';
Expand All @@ -14,7 +14,7 @@ export function registerRoutes(
reporting: ReportingCore,
server: ServerFacade,
plugins: ReportingSetupDeps,
logger: LevelLogger
logger: Logger
) {
registerJobGenerationRoutes(reporting, server, plugins, logger);
registerJobInfoRoutes(reporting, server, plugins, logger);
Expand Down
9 changes: 5 additions & 4 deletions x-pack/legacy/plugins/reporting/server/routes/jobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { ResponseObject } from 'hapi';
import { Legacy } from 'kibana';
import { ReportingCore } from '../';
import { API_BASE_URL } from '../../common/constants';
import { LevelLogger } from '../lib';
import { LevelLogger as Logger } from '../lib';
import { jobsQueryFactory } from '../lib/jobs_query';
import { JobDocOutput, JobSource, ReportingSetupDeps, ServerFacade } from '../types';
import {
Expand All @@ -22,6 +22,7 @@ import {
getRouteConfigFactoryDownloadPre,
getRouteConfigFactoryManagementPre,
} from './lib/route_config_factories';
import { ReportingResponseToolkit } from './types';

interface ListQuery {
page: string;
Expand All @@ -38,7 +39,7 @@ export function registerJobInfoRoutes(
reporting: ReportingCore,
server: ServerFacade,
plugins: ReportingSetupDeps,
logger: LevelLogger
logger: Logger
) {
const config = reporting.getConfig();
const { elasticsearch } = plugins;
Expand Down Expand Up @@ -147,7 +148,7 @@ export function registerJobInfoRoutes(
path: `${MAIN_ENTRY}/download/{docId}`,
method: 'GET',
options: getRouteConfigDownload(),
handler: async (legacyRequest: Legacy.Request, h: Legacy.ResponseToolkit) => {
handler: async (legacyRequest: Legacy.Request, h: ReportingResponseToolkit) => {
const request = makeRequestFacade(legacyRequest);
const { docId } = request.params;

Expand Down Expand Up @@ -187,7 +188,7 @@ export function registerJobInfoRoutes(
path: `${MAIN_ENTRY}/delete/{docId}`,
method: 'DELETE',
options: getRouteConfigDelete(),
handler: async (legacyRequest: Legacy.Request, h: Legacy.ResponseToolkit) => {
handler: async (legacyRequest: Legacy.Request, h: ReportingResponseToolkit) => {
const request = makeRequestFacade(legacyRequest);
const { docId } = request.params;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Boom from 'boom';
import { Legacy } from 'kibana';
import { AuthenticatedUser } from '../../../../../../plugins/security/server';
import { ReportingConfig } from '../../../server';
import { LevelLogger } from '../../../server/lib';
import { LevelLogger as Logger } from '../../../server/lib';
import { ReportingSetupDeps } from '../../../server/types';
import { getUserFactory } from '../../lib/get_user';

Expand All @@ -21,7 +21,7 @@ export type PreRoutingFunction = (
export const authorizedUserPreRoutingFactory = function authorizedUserPreRoutingFn(
config: ReportingConfig,
plugins: ReportingSetupDeps,
logger: LevelLogger
logger: Logger
) {
const getUser = getUserFactory(plugins.security, logger);
const { info: xpackInfo } = plugins.__LEGACY.plugins.xpack_main;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import { RequestQuery } from 'hapi';
import { Legacy } from 'kibana';
import {
RequestFacade,
ReportingRequestPayload,
ReportingRequestPre,
ReportingRequestQuery,
RequestFacade,
} from '../../../server/types';

export function makeRequestFacade(request: Legacy.Request): RequestFacade {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
import Boom from 'boom';
import { Legacy } from 'kibana';
import { ReportingConfig } from '../../';
import { LevelLogger } from '../../lib';
import { LevelLogger as Logger } from '../../lib';
import { ReportingSetupDeps } from '../../types';

export type GetReportingFeatureIdFn = (request: Legacy.Request) => string;

export const reportingFeaturePreRoutingFactory = function reportingFeaturePreRoutingFn(
config: ReportingConfig,
plugins: ReportingSetupDeps,
logger: LevelLogger
logger: Logger
) {
const xpackMainPlugin = plugins.__LEGACY.plugins.xpack_main;
const pluginId = 'reporting';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import Joi from 'joi';
import { ReportingConfig } from '../../';
import { LevelLogger } from '../../lib';
import { LevelLogger as Logger } from '../../lib';
import { CSV_FROM_SAVEDOBJECT_JOB_TYPE } from '../../../common/constants';
import { ReportingSetupDeps } from '../../types';
import { authorizedUserPreRoutingFactory } from './authorized_user_pre_routing';
Expand All @@ -32,7 +32,7 @@ export type GetRouteConfigFactoryFn = (
export function getRouteConfigFactoryReportingPre(
config: ReportingConfig,
plugins: ReportingSetupDeps,
logger: LevelLogger
logger: Logger
): GetRouteConfigFactoryFn {
const authorizedUserPreRouting = authorizedUserPreRoutingFactory(config, plugins, logger);
const reportingFeaturePreRouting = reportingFeaturePreRoutingFactory(config, plugins, logger);
Expand All @@ -53,7 +53,7 @@ export function getRouteConfigFactoryReportingPre(
export function getRouteOptionsCsv(
config: ReportingConfig,
plugins: ReportingSetupDeps,
logger: LevelLogger
logger: Logger
) {
const getRouteConfig = getRouteConfigFactoryReportingPre(config, plugins, logger);
return {
Expand All @@ -78,7 +78,7 @@ export function getRouteOptionsCsv(
export function getRouteConfigFactoryManagementPre(
config: ReportingConfig,
plugins: ReportingSetupDeps,
logger: LevelLogger
logger: Logger
): GetRouteConfigFactoryFn {
const authorizedUserPreRouting = authorizedUserPreRoutingFactory(config, plugins, logger);
const reportingFeaturePreRouting = reportingFeaturePreRoutingFactory(config, plugins, logger);
Expand All @@ -102,7 +102,7 @@ export function getRouteConfigFactoryManagementPre(
export function getRouteConfigFactoryDownloadPre(
config: ReportingConfig,
plugins: ReportingSetupDeps,
logger: LevelLogger
logger: Logger
): GetRouteConfigFactoryFn {
const getManagementRouteConfig = getRouteConfigFactoryManagementPre(config, plugins, logger);
return (): RouteConfigFactory => ({
Expand All @@ -117,7 +117,7 @@ export function getRouteConfigFactoryDownloadPre(
export function getRouteConfigFactoryDeletePre(
config: ReportingConfig,
plugins: ReportingSetupDeps,
logger: LevelLogger
logger: Logger
): GetRouteConfigFactoryFn {
const getManagementRouteConfig = getRouteConfigFactoryManagementPre(config, plugins, logger);
return (): RouteConfigFactory => ({
Expand Down
28 changes: 28 additions & 0 deletions x-pack/legacy/plugins/reporting/server/routes/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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 { Legacy } from 'kibana';
import { JobDocPayload } from '../types';

export type HandlerFunction = (
exportType: string,
jobParams: object,
request: Legacy.Request,
h: ReportingResponseToolkit
) => any;

export type HandlerErrorFunction = (exportType: string, err: Error) => any;

export interface QueuedJobPayload<JobParamsType> {
error?: boolean;
source: {
job: {
payload: JobDocPayload<JobParamsType>;
};
};
}

export type ReportingResponseToolkit = Legacy.ResponseToolkit;
18 changes: 0 additions & 18 deletions x-pack/legacy/plugins/reporting/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,6 @@ export interface GenerateExportTypePayload {

export type ReportingRequestPayload = GenerateExportTypePayload | JobParamPostPayload;

export type HandlerFunction = (
exportType: string,
jobParams: object,
request: Legacy.Request,
h: Legacy.ResponseToolkit
) => any;

export interface TimeRangeParams {
timezone: string;
min: Date | string | number;
Expand All @@ -67,24 +60,13 @@ export interface JobParamPostPayload {
timerange: TimeRangeParams;
}

export type HandlerErrorFunction = (exportType: string, err: Error) => any;

export interface JobDocPayload<JobParamsType> {
headers?: string; // serialized encrypted headers
jobParams: JobParamsType;
title: string;
type: string | null;
}

export interface QueuedJobPayload<JobParamsType> {
error?: boolean;
source: {
job: {
payload: JobDocPayload<JobParamsType>;
};
};
}

export interface JobSource<JobParamsType> {
_id: string;
_index: string;
Expand Down

0 comments on commit d4fd2a3

Please sign in to comment.