Skip to content

Commit

Permalink
Use new np router and fetch. Add placeholder schema validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Caldwell committed Jan 3, 2020
1 parent b67d203 commit 9cff803
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 35 deletions.
2 changes: 2 additions & 0 deletions x-pack/legacy/plugins/file_upload/public/kibana_services.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ export let savedObjectsClient;
export let basePath;
export let apiBasePath;
export let kbnVersion;
export let kbnFetch;

export const initServicesAndConstants = ({ savedObjects, http, injectedMetadata }) => {
savedObjectsClient = savedObjects.client;
basePath = http.basePath.basePath;
apiBasePath = http.basePath.prepend('/api');
kbnVersion = injectedMetadata.getKibanaVersion(DEFAULT_KBN_VERSION);
kbnFetch = http.fetch;
};
5 changes: 2 additions & 3 deletions x-pack/legacy/plugins/file_upload/public/util/http_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import { addSystemApiHeader } from 'ui/system_api';
import { i18n } from '@kbn/i18n';
import { kbnVersion } from '../kibana_services';
import { kbnVersion, kbnFetch } from '../kibana_services';

export async function http(options) {
if (!(options && options.url)) {
Expand Down Expand Up @@ -40,8 +40,7 @@ export async function http(options) {

async function doFetch(url, payload) {
try {
const resp = await fetch(url, payload);
return resp.json();
return await kbnFetch(url, payload);
} catch (err) {
return {
failures: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { http as httpService } from './http_service';
import { indexPatternService, apiBasePath, savedObjectsClient } from '../kibana_services';
import { indexPatternService, savedObjectsClient } from '../kibana_services';
import { getGeoJsonIndexingDetails } from './geo_processing';
import { sizeLimitedChunking } from './size_limited_chunking';
import { i18n } from '@kbn/i18n';
Expand Down Expand Up @@ -37,11 +37,9 @@ export async function indexData(parsedFile, transformDetails, indexName, dataTyp
data: [],
index: indexName,
});
let id;
const id = createdIndex && createdIndex.id;
try {
if (createdIndex && createdIndex.id) {
id = createdIndex.id;
} else {
if (!id) {
throw i18n.translate('xpack.fileUpload.indexingService.errorCreatingIndex', {
defaultMessage: 'Error creating index',
});
Expand Down Expand Up @@ -121,7 +119,7 @@ async function writeToIndex(indexingDetails) {
const { appName, index, data, settings, mappings, ingestPipeline } = indexingDetails;

return await httpService({
url: `${apiBasePath}/fileupload/import${paramString}`,
url: `/api/fileupload/import${paramString}`,
method: 'POST',
data: {
index,
Expand Down Expand Up @@ -227,7 +225,7 @@ async function getIndexPatternId(name) {

export const getExistingIndexNames = async () => {
const indexes = await httpService({
url: `${apiBasePath}/index_management/indices`,
url: `/api/index_management/indices`,
method: 'GET',
});
return indexes ? indexes.map(({ name }) => name) : [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function importDataProvider(callWithRequest) {
try {
const { id: pipelineId, pipeline } = ingestPipeline;

if (id === undefined) {
if (!id) {
// first chunk of data, create the index and id to return
id = uuid.v1();

Expand Down
2 changes: 0 additions & 2 deletions x-pack/legacy/plugins/file_upload/server/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import { initRoutes } from './routes/file_upload';
import { registerFileUploadUsageCollector } from './telemetry';

export const IMPORT_ROUTE = '/api/fileupload/import';

export class FileUploadPlugin {
setup(core, plugins, __LEGACY) {
const elasticsearchPlugin = __LEGACY.plugins.elasticsearch;
Expand Down
57 changes: 35 additions & 22 deletions x-pack/legacy/plugins/file_upload/server/routes/file_upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,49 +10,56 @@ import { importDataProvider } from '../models/import_data';
import { updateTelemetry } from '../telemetry/telemetry';
import { MAX_BYTES } from '../../common/constants/file_import';
import Joi from 'joi';
import { IMPORT_ROUTE } from '../plugin';
import { schema } from '@kbn/config-schema';
import { inspect } from 'util' // or directly;


export const IMPORT_ROUTE = '/api/fileupload/import';

function importData({ callWithRequest, id, index, settings, mappings, ingestPipeline, data }) {
const { importData: importDataFunc } = importDataProvider(callWithRequest);
return importDataFunc(id, index, settings, mappings, ingestPipeline, data);
}

export function getImportRouteHandler(elasticsearchPlugin, getSavedObjectsRepository) {
return async request => {
return async (con, req, res) => {
// console.log(JSON.stringify(Object.keys(req)));
const requestObj = {
query: request.query,
payload: request.payload,
params: request.params,
auth: request.auth,
headers: request.headers,
query: req.query,
body: req.body,
params: req.params,
headers: req.headers,
};

// `id` being `undefined` tells us that this is a new import due to create a new index.
// follow-up import calls to just add additional data will include the `id` of the created
// index, we'll ignore those and don't increment the counter.
const { id } = requestObj.query;
if (id === undefined) {
const { id } = requestObj.params;
if (!id) {
await updateTelemetry({ elasticsearchPlugin, getSavedObjectsRepository });
}

const requestContentWithDefaults = {
id,
callWithRequest: callWithRequestFactory(elasticsearchPlugin, requestObj),
...requestObj.payload,
...requestObj.body,
};
return importData(requestContentWithDefaults).catch(wrapError);
const importDataResult = await importData(requestContentWithDefaults).catch(wrapError);
return res.ok({
body: importDataResult
});
};
}

export const importRouteConfig = {
payload: {
maxBytes: MAX_BYTES,
},
// payload: {
// maxBytes: MAX_BYTES,
// },
validate: {
query: Joi.object().keys({
id: Joi.string(),
}),
payload: Joi.object({
body: Joi.object({
app: Joi.string(),
index: Joi.string()
.min(1)
Expand Down Expand Up @@ -81,11 +88,17 @@ export const importRouteConfig = {
},
};

export const initRoutes = (route, esPlugin, getSavedObjectsRepository) => {
route({
method: 'POST',
path: IMPORT_ROUTE,
handler: getImportRouteHandler(esPlugin, getSavedObjectsRepository),
config: importRouteConfig,
});
export const initRoutes = (router, esPlugin, getSavedObjectsRepository) => {
router.post({
path: `${IMPORT_ROUTE}{id?}`,
validate: {
params: schema.maybe(schema.any()),
query: schema.object({}, { allowUnknowns: true }),
body: schema.object({}, { allowUnknowns: true }),
},
//validate: importRouteConfig,
// config: importRouteConfig.validate,
},
getImportRouteHandler(esPlugin, getSavedObjectsRepository)
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* 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 { CANVAS_TYPE } from '../../../../../legacy/plugins/canvas/common/lib/constants';
import { initializeDeleteWorkpadRoute } from './delete';
import {
IRouter,
kibanaResponseFactory,
RequestHandlerContext,
RequestHandler,
} from 'src/core/server';
import {
savedObjectsClientMock,
httpServiceMock,
httpServerMock,
loggingServiceMock,
} from 'src/core/server/mocks';

const mockRouteContext = ({
core: {
savedObjects: {
client: savedObjectsClientMock.create(),
},
},
} as unknown) as RequestHandlerContext;

describe('POST file', () => {
let routeHandler: RequestHandler<any, any, any>;

beforeEach(() => {
const httpService = httpServiceMock.createSetupContract();
const router = httpService.createRouter('') as jest.Mocked<IRouter>;
initializeDeleteWorkpadRoute({
router,
logger: loggingServiceMock.create().get(),
});

routeHandler = router.delete.mock.calls[0][1];
});

it(`rejects file with no content`, async () => {
});
it(`accepts request with correct structure`, async () => {
});
it(`rejects request with incorrect structure`, async () => {
});
it(`rejects request over size limit`, async () => {
});
it(`accepts request within size limit`, async () => {
});
it(`requires mappings if no id present`, async () => {
});
it(`requires data & settings if id is present`, async () => {
});

it(`returns 200 ok when the workpad is deleted`, async () => {
const id = 'some-id';
const request = httpServerMock.createKibanaRequest({
method: 'delete',
path: `api/canvas/workpad/${id}`,
params: {
id,
},
});

const response = await routeHandler(mockRouteContext, request, kibanaResponseFactory);

expect(response.status).toBe(200);
expect(response.payload).toEqual({ ok: true });
expect(mockRouteContext.core.savedObjects.client.delete).toBeCalledWith(CANVAS_TYPE, id);
});

it(`returns bad request if delete is unsuccessful`, async () => {
const request = httpServerMock.createKibanaRequest({
method: 'delete',
path: `api/canvas/workpad/some-id`,
params: {
id: 'some-id',
},
});

(mockRouteContext.core.savedObjects.client.delete as jest.Mock).mockImplementationOnce(() => {
throw mockRouteContext.core.savedObjects.client.errors.createBadRequestError('bad request');
});

const response = await routeHandler(mockRouteContext, request, kibanaResponseFactory);

expect(response.status).toBe(400);
});
});

0 comments on commit 9cff803

Please sign in to comment.