Skip to content
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

[File upload] Update remaining File Upload dependencies for NP migration #58128

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 7 additions & 15 deletions x-pack/legacy/plugins/file_upload/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import { mappings } from './mappings';

export const fileUpload = kibana => {
return new kibana.Plugin({
require: ['elasticsearch', 'xpack_main'],
require: ['elasticsearch'],
name: 'file_upload',
id: 'file_upload',
// TODO: uiExports and savedObjectSchemas to be removed on migration
uiExports: {
mappings,
},
Expand All @@ -22,23 +23,14 @@ export const fileUpload = kibana => {

init(server) {
const coreSetup = server.newPlatform.setup.core;
const coreStart = server.newPlatform.start.core;
const { usageCollection } = server.newPlatform.setup.plugins;
const pluginsSetup = {
const pluginsStart = {
usageCollection,
};

// legacy dependencies
const __LEGACY = {
route: server.route.bind(server),
plugins: {
elasticsearch: server.plugins.elasticsearch,
},
savedObjects: {
getSavedObjectsRepository: server.savedObjects.getSavedObjectsRepository,
},
};

new FileUploadPlugin().setup(coreSetup, pluginsSetup, __LEGACY);
const fileUploadPlugin = new FileUploadPlugin();
fileUploadPlugin.setup(coreSetup);
fileUploadPlugin.start(coreStart, pluginsStart);
},
});
};

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
*/

import { once } from 'lodash';
import { getDataClient } from '../kibana_server_services';

const callWithRequest = once(elasticsearchPlugin => {
const cluster = elasticsearchPlugin.getCluster('data');
return cluster.callWithRequest;
});
const callWithRequest = once(() => getDataClient());

export const callWithRequestFactory = (elasticsearchPlugin, request) => {
export const callWithRequestFactory = request => {
return (...args) => {
return callWithRequest(elasticsearchPlugin)(request, ...args);
return (
callWithRequest()
.asScoped(request)
// @ts-ignore
.callAsCurrentUser(...args)
);
};
};
18 changes: 18 additions & 0 deletions x-pack/legacy/plugins/file_upload/server/kibana_server_services.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* 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.
*/

let dataClient;

export const setElasticsearchClientServices = elasticsearch => {
({ dataClient } = elasticsearch);
};
export const getDataClient = () => dataClient;

let internalRepository;
export const setInternalRepository = createInternalRepository => {
internalRepository = createInternalRepository();
};
export const getInternalRepository = () => internalRepository;
kindsun marked this conversation as resolved.
Show resolved Hide resolved
22 changes: 13 additions & 9 deletions x-pack/legacy/plugins/file_upload/server/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,23 @@
*/

import { initRoutes } from './routes/file_upload';
import { setElasticsearchClientServices, setInternalRepository } from './kibana_server_services';
import { registerFileUploadUsageCollector } from './telemetry';

export class FileUploadPlugin {
setup(core, plugins, __LEGACY) {
const elasticsearchPlugin = __LEGACY.plugins.elasticsearch;
const getSavedObjectsRepository = __LEGACY.savedObjects.getSavedObjectsRepository;
const router = core.http.createRouter();
constructor() {
this.router = null;
}

setup(core) {
setElasticsearchClientServices(core.elasticsearch);
this.router = core.http.createRouter();
}

initRoutes(router, elasticsearchPlugin, getSavedObjectsRepository);
start(core, plugins) {
initRoutes(this.router, core.savedObjects.getSavedObjectsRepository);
setInternalRepository(core.savedObjects.createInternalRepository);

registerFileUploadUsageCollector(plugins.usageCollection, {
elasticsearchPlugin,
getSavedObjectsRepository,
});
registerFileUploadUsageCollector(plugins.usageCollection);
}
}
10 changes: 5 additions & 5 deletions x-pack/legacy/plugins/file_upload/server/routes/file_upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export const idConditionalValidation = (body, boolHasId) =>
)
.validate(body);

const finishValidationAndProcessReq = (elasticsearchPlugin, getSavedObjectsRepository) => {
const finishValidationAndProcessReq = () => {
return async (con, req, { ok, badRequest }) => {
const {
query: { id },
Expand All @@ -86,7 +86,7 @@ const finishValidationAndProcessReq = (elasticsearchPlugin, getSavedObjectsRepos
let resp;
try {
const validIdReqData = idConditionalValidation(body, boolHasId);
const callWithRequest = callWithRequestFactory(elasticsearchPlugin, req);
const callWithRequest = callWithRequestFactory(req);
const { importData: importDataFunc } = importDataProvider(callWithRequest);

const { index, settings, mappings, ingestPipeline, data } = validIdReqData;
Expand All @@ -103,7 +103,7 @@ const finishValidationAndProcessReq = (elasticsearchPlugin, getSavedObjectsRepos
resp = ok({ body: processedReq });
// If no id's been established then this is a new index, update telemetry
if (!boolHasId) {
await updateTelemetry({ elasticsearchPlugin, getSavedObjectsRepository });
await updateTelemetry();
}
} else {
resp = badRequest(`Error processing request 1: ${processedReq.error.message}`, ['body']);
Expand All @@ -115,7 +115,7 @@ const finishValidationAndProcessReq = (elasticsearchPlugin, getSavedObjectsRepos
};
};

export const initRoutes = (router, esPlugin, getSavedObjectsRepository) => {
export const initRoutes = router => {
router.post(
{
path: `${IMPORT_ROUTE}{id?}`,
Expand All @@ -125,6 +125,6 @@ export const initRoutes = (router, esPlugin, getSavedObjectsRepository) => {
},
options,
},
finishValidationAndProcessReq(esPlugin, getSavedObjectsRepository)
finishValidationAndProcessReq()
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,11 @@ import { getTelemetry, initTelemetry } from './telemetry';

const TELEMETRY_TYPE = 'fileUploadTelemetry';

export function registerFileUploadUsageCollector(
usageCollection: UsageCollectionSetup,
deps: {
elasticsearchPlugin: any;
getSavedObjectsRepository: any;
}
): void {
const { elasticsearchPlugin, getSavedObjectsRepository } = deps;
export function registerFileUploadUsageCollector(usageCollection: UsageCollectionSetup): void {
const fileUploadUsageCollector = usageCollection.makeUsageCollector({
type: TELEMETRY_TYPE,
isReady: () => true,
fetch: async () =>
(await getTelemetry(elasticsearchPlugin, getSavedObjectsRepository)) || initTelemetry(),
fetch: async () => (await getTelemetry()) || initTelemetry(),
});

usageCollection.registerCollector(fileUploadUsageCollector);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

import { getTelemetry, updateTelemetry } from './telemetry';

const elasticsearchPlugin: any = null;
const getSavedObjectsRepository: any = null;
const internalRepository = () => ({
get: jest.fn(() => null),
create: jest.fn(() => ({ attributes: 'test' })),
Expand All @@ -25,7 +23,7 @@ describe('file upload plugin telemetry', () => {
describe('getTelemetry', () => {
it('should get existing telemetry', async () => {
const internalRepo = mockInit();
await getTelemetry(elasticsearchPlugin, getSavedObjectsRepository, internalRepo);
await getTelemetry(internalRepo);
expect(internalRepo.update.mock.calls.length).toBe(0);
expect(internalRepo.get.mock.calls.length).toBe(1);
expect(internalRepo.create.mock.calls.length).toBe(0);
Expand All @@ -40,11 +38,7 @@ describe('file upload plugin telemetry', () => {
},
});

await updateTelemetry({
elasticsearchPlugin,
getSavedObjectsRepository,
internalRepo,
});
await updateTelemetry(internalRepo);
expect(internalRepo.update.mock.calls.length).toBe(1);
expect(internalRepo.get.mock.calls.length).toBe(1);
expect(internalRepo.create.mock.calls.length).toBe(0);
Expand Down
39 changes: 7 additions & 32 deletions x-pack/legacy/plugins/file_upload/server/telemetry/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
*/

import _ from 'lodash';
import { callWithInternalUserFactory } from '../client/call_with_internal_user_factory';
// @ts-ignore
import { getInternalRepository } from '../kibana_server_services';

export const TELEMETRY_DOC_ID = 'file-upload-telemetry';

Expand All @@ -17,27 +18,14 @@ export interface TelemetrySavedObject {
attributes: Telemetry;
}

export function getInternalRepository(
elasticsearchPlugin: any,
getSavedObjectsRepository: any
): any {
const callWithInternalUser = callWithInternalUserFactory(elasticsearchPlugin);
return getSavedObjectsRepository(callWithInternalUser);
}

export function initTelemetry(): Telemetry {
return {
filesUploadedTotalCount: 0,
};
}

export async function getTelemetry(
elasticsearchPlugin: any,
getSavedObjectsRepository: any,
internalRepo?: object
): Promise<Telemetry> {
const internalRepository =
internalRepo || getInternalRepository(elasticsearchPlugin, getSavedObjectsRepository);
export async function getTelemetry(internalRepo?: object): Promise<Telemetry> {
const internalRepository = internalRepo || getInternalRepository();
let telemetrySavedObject;

try {
Expand All @@ -49,22 +37,9 @@ export async function getTelemetry(
return telemetrySavedObject ? telemetrySavedObject.attributes : null;
}

export async function updateTelemetry({
elasticsearchPlugin,
getSavedObjectsRepository,
internalRepo,
}: {
elasticsearchPlugin: any;
getSavedObjectsRepository: any;
internalRepo?: any;
}) {
const internalRepository =
internalRepo || getInternalRepository(elasticsearchPlugin, getSavedObjectsRepository);
let telemetry = await getTelemetry(
elasticsearchPlugin,
getSavedObjectsRepository,
internalRepository
);
export async function updateTelemetry(internalRepo?: any) {
const internalRepository = internalRepo || getInternalRepository();
let telemetry = await getTelemetry(internalRepository);
// Create if doesn't exist
if (!telemetry || _.isEmpty(telemetry)) {
const newTelemetrySavedObject = await internalRepository.create(
Expand Down