From a98d5f542f308e08d75883ee794cf7d73ca3e6be Mon Sep 17 00:00:00 2001 From: Aaron Caldwell Date: Mon, 25 Nov 2019 11:50:26 -0700 Subject: [PATCH] [File upload][Maps] NP migration for server & client (#51045) (#51621) * Use np savedObjectsClient in indexing service * Export File Upload UI via start since it requires initialization * Pass services through top level react component props * Handle basePath ref and 'kbn-version' for requests * Bulk of logic for removing hapi server dependencies for server app * Use request obj subset of original request * Move startup logic over to server plugin file and call from index.js * Update server tests * Clean up * Remove old makeUsageCollector export statement * initServicesAndConstants in the start method instead of in the react component * Review feedback --- .../common/constants/file_import.ts | 2 + x-pack/legacy/plugins/file_upload/index.js | 29 ++++++++--- .../legacy/plugins/file_upload/mappings.json | 9 ---- x-pack/legacy/plugins/file_upload/mappings.ts | 15 ++++++ .../public/components/json_import_progress.js | 4 +- .../file_upload/public/{index.js => index.ts} | 6 ++- .../file_upload/public/kibana_services.js | 13 +++++ .../plugins/file_upload/public/legacy.ts | 12 +++++ .../plugins/file_upload/public/plugin.ts | 33 ++++++++++++ .../file_upload/public/util/http_service.js | 4 +- .../public/util/indexing_service.js | 24 ++++----- .../call_with_internal_user_factory.d.ts | 4 +- .../client/call_with_internal_user_factory.js | 9 ++-- .../call_with_internal_user_factory.test.ts | 22 +++----- .../client/call_with_request_factory.js | 8 +-- .../plugins/file_upload/server/plugin.js | 36 +++++++++++++ .../file_upload/server/routes/file_upload.js | 50 +++++++++++-------- .../file_upload/server/telemetry/index.ts | 1 - .../server/telemetry/make_usage_collector.ts | 27 ---------- .../server/telemetry/telemetry.test.ts | 22 ++++---- .../file_upload/server/telemetry/telemetry.ts | 33 ++++++++---- .../create_client_file_source_editor.js | 4 +- 22 files changed, 229 insertions(+), 138 deletions(-) delete mode 100644 x-pack/legacy/plugins/file_upload/mappings.json create mode 100644 x-pack/legacy/plugins/file_upload/mappings.ts rename x-pack/legacy/plugins/file_upload/public/{index.js => index.ts} (69%) create mode 100644 x-pack/legacy/plugins/file_upload/public/legacy.ts create mode 100644 x-pack/legacy/plugins/file_upload/public/plugin.ts create mode 100644 x-pack/legacy/plugins/file_upload/server/plugin.js delete mode 100644 x-pack/legacy/plugins/file_upload/server/telemetry/make_usage_collector.ts diff --git a/x-pack/legacy/plugins/file_upload/common/constants/file_import.ts b/x-pack/legacy/plugins/file_upload/common/constants/file_import.ts index 1c82c2b6237e..0770899af539 100644 --- a/x-pack/legacy/plugins/file_upload/common/constants/file_import.ts +++ b/x-pack/legacy/plugins/file_upload/common/constants/file_import.ts @@ -16,3 +16,5 @@ export const ES_GEO_FIELD_TYPE = { GEO_POINT: 'geo_point', GEO_SHAPE: 'geo_shape', }; + +export const DEFAULT_KBN_VERSION = 'kbnVersion'; diff --git a/x-pack/legacy/plugins/file_upload/index.js b/x-pack/legacy/plugins/file_upload/index.js index 24907082adb2..37d4ad80fa2c 100644 --- a/x-pack/legacy/plugins/file_upload/index.js +++ b/x-pack/legacy/plugins/file_upload/index.js @@ -3,10 +3,8 @@ * or more contributor license agreements. Licensed under the Elastic License; * you may not use this file except in compliance with the Elastic License. */ -import { mirrorPluginStatus } from '../../server/lib/mirror_plugin_status'; -import { fileUploadRoutes } from './server/routes/file_upload'; -import { makeUsageCollector } from './server/telemetry/'; -import mappings from './mappings'; +import { FileUploadPlugin } from './server/plugin'; +import { mappings } from './mappings'; export const fileUpload = kibana => { return new kibana.Plugin({ @@ -23,11 +21,26 @@ export const fileUpload = kibana => { }, init(server) { - const { xpack_main: xpackMainPlugin } = server.plugins; + const coreSetup = server.newPlatform.setup.core; + const pluginsSetup = {}; - mirrorPluginStatus(xpackMainPlugin, this); - fileUploadRoutes(server); - makeUsageCollector(server); + // legacy dependencies + const __LEGACY = { + route: server.route.bind(server), + plugins: { + elasticsearch: server.plugins.elasticsearch, + }, + savedObjects: { + getSavedObjectsRepository: server.savedObjects.getSavedObjectsRepository + }, + usage: { + collectorSet: { + makeUsageCollector: server.usage.collectorSet.makeUsageCollector + } + } + }; + + new FileUploadPlugin().setup(coreSetup, pluginsSetup, __LEGACY); } }); }; diff --git a/x-pack/legacy/plugins/file_upload/mappings.json b/x-pack/legacy/plugins/file_upload/mappings.json deleted file mode 100644 index addff6308d3f..000000000000 --- a/x-pack/legacy/plugins/file_upload/mappings.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "file-upload-telemetry": { - "properties": { - "filesUploadedTotalCount": { - "type": "long" - } - } - } -} diff --git a/x-pack/legacy/plugins/file_upload/mappings.ts b/x-pack/legacy/plugins/file_upload/mappings.ts new file mode 100644 index 000000000000..70229c708832 --- /dev/null +++ b/x-pack/legacy/plugins/file_upload/mappings.ts @@ -0,0 +1,15 @@ +/* + * 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. + */ + +export const mappings = { + 'file-upload-telemetry': { + properties: { + filesUploadedTotalCount: { + type: 'long', + }, + }, + }, +}; diff --git a/x-pack/legacy/plugins/file_upload/public/components/json_import_progress.js b/x-pack/legacy/plugins/file_upload/public/components/json_import_progress.js index 9e553a536845..9c6248049d9c 100644 --- a/x-pack/legacy/plugins/file_upload/public/components/json_import_progress.js +++ b/x-pack/legacy/plugins/file_upload/public/components/json_import_progress.js @@ -8,7 +8,7 @@ import React, { Fragment, Component } from 'react'; import { i18n } from '@kbn/i18n'; import { EuiCodeBlock, EuiSpacer, EuiText, EuiTitle, EuiProgress, EuiCallOut } from '@elastic/eui'; import { FormattedMessage } from '@kbn/i18n/react'; -import chrome from 'ui/chrome'; +import { basePath } from '../kibana_services'; export class JsonImportProgress extends Component { @@ -114,7 +114,7 @@ export class JsonImportProgress extends Component { diff --git a/x-pack/legacy/plugins/file_upload/public/index.js b/x-pack/legacy/plugins/file_upload/public/index.ts similarity index 69% rename from x-pack/legacy/plugins/file_upload/public/index.js rename to x-pack/legacy/plugins/file_upload/public/index.ts index a02b82170f70..205ceae37d6a 100644 --- a/x-pack/legacy/plugins/file_upload/public/index.js +++ b/x-pack/legacy/plugins/file_upload/public/index.ts @@ -4,4 +4,8 @@ * you may not use this file except in compliance with the Elastic License. */ -export { JsonUploadAndParse } from './components/json_upload_and_parse'; +import { FileUploadPlugin } from './plugin'; + +export function plugin() { + return new FileUploadPlugin(); +} diff --git a/x-pack/legacy/plugins/file_upload/public/kibana_services.js b/x-pack/legacy/plugins/file_upload/public/kibana_services.js index 10a6ae7179bc..3c00ab570966 100644 --- a/x-pack/legacy/plugins/file_upload/public/kibana_services.js +++ b/x-pack/legacy/plugins/file_upload/public/kibana_services.js @@ -5,5 +5,18 @@ */ import { start as data } from '../../../../../src/legacy/core_plugins/data/public/legacy'; +import { DEFAULT_KBN_VERSION } from '../common/constants/file_import'; export const indexPatternService = data.indexPatterns.indexPatterns; + +export let savedObjectsClient; +export let basePath; +export let apiBasePath; +export let kbnVersion; + +export const initServicesAndConstants = ({ savedObjects, http, injectedMetadata }) => { + savedObjectsClient = savedObjects.client; + basePath = http.basePath.basePath; + apiBasePath = http.basePath.prepend('/api'); + kbnVersion = injectedMetadata.getKibanaVersion(DEFAULT_KBN_VERSION); +}; diff --git a/x-pack/legacy/plugins/file_upload/public/legacy.ts b/x-pack/legacy/plugins/file_upload/public/legacy.ts new file mode 100644 index 000000000000..719599df3ccb --- /dev/null +++ b/x-pack/legacy/plugins/file_upload/public/legacy.ts @@ -0,0 +1,12 @@ +/* + * 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 { npStart } from 'ui/new_platform'; +import { plugin } from '.'; + +const pluginInstance = plugin(); + +export const start = pluginInstance.start(npStart.core); diff --git a/x-pack/legacy/plugins/file_upload/public/plugin.ts b/x-pack/legacy/plugins/file_upload/public/plugin.ts new file mode 100644 index 000000000000..cc9ebbfc15b3 --- /dev/null +++ b/x-pack/legacy/plugins/file_upload/public/plugin.ts @@ -0,0 +1,33 @@ +/* + * 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 { Plugin, CoreStart } from 'src/core/public'; +// @ts-ignore +import { initResources } from './util/indexing_service'; +// @ts-ignore +import { JsonUploadAndParse } from './components/json_upload_and_parse'; +// @ts-ignore +import { initServicesAndConstants } from './kibana_services'; + +/** + * These are the interfaces with your public contracts. You should export these + * for other plugins to use in _their_ `SetupDeps`/`StartDeps` interfaces. + * @public + */ +export type FileUploadPluginSetup = ReturnType; +export type FileUploadPluginStart = ReturnType; + +/** @internal */ +export class FileUploadPlugin implements Plugin { + public setup() {} + + public start(core: CoreStart) { + initServicesAndConstants(core); + return { + JsonUploadAndParse, + }; + } +} diff --git a/x-pack/legacy/plugins/file_upload/public/util/http_service.js b/x-pack/legacy/plugins/file_upload/public/util/http_service.js index 26d46cecb0e5..a744f0f07549 100644 --- a/x-pack/legacy/plugins/file_upload/public/util/http_service.js +++ b/x-pack/legacy/plugins/file_upload/public/util/http_service.js @@ -6,9 +6,9 @@ // service for interacting with the server -import chrome from 'ui/chrome'; import { addSystemApiHeader } from 'ui/system_api'; import { i18n } from '@kbn/i18n'; +import { kbnVersion } from '../kibana_services'; export async function http(options) { if(!(options && options.url)) { @@ -20,7 +20,7 @@ export async function http(options) { const url = options.url || ''; const headers = addSystemApiHeader({ 'Content-Type': 'application/json', - 'kbn-version': chrome.getXsrfToken(), + 'kbn-version': kbnVersion, ...options.headers }); diff --git a/x-pack/legacy/plugins/file_upload/public/util/indexing_service.js b/x-pack/legacy/plugins/file_upload/public/util/indexing_service.js index fd96bd95d4bb..b40659ec4b51 100644 --- a/x-pack/legacy/plugins/file_upload/public/util/indexing_service.js +++ b/x-pack/legacy/plugins/file_upload/public/util/indexing_service.js @@ -4,14 +4,16 @@ * you may not use this file except in compliance with the Elastic License. */ -import { http } from './http_service'; -import chrome from 'ui/chrome'; -import { i18n } from '@kbn/i18n'; -import { indexPatternService } from '../kibana_services'; +import { http as httpService } from './http_service'; +import { + indexPatternService, + apiBasePath, + savedObjectsClient +} from '../kibana_services'; import { getGeoJsonIndexingDetails } from './geo_processing'; import { sizeLimitedChunking } from './size_limited_chunking'; +import { i18n } from '@kbn/i18n'; -const basePath = chrome.addBasePath('/api/fileupload'); const fileType = 'json'; export async function indexData(parsedFile, transformDetails, indexName, dataType, appName) { @@ -19,7 +21,6 @@ export async function indexData(parsedFile, transformDetails, indexName, dataTyp throw(i18n.translate('xpack.fileUpload.indexingService.noFileImported', { defaultMessage: 'No file imported.' })); - return; } // Perform any processing required on file prior to indexing @@ -129,8 +130,8 @@ async function writeToIndex(indexingDetails) { ingestPipeline } = indexingDetails; - return await http({ - url: `${basePath}/import${paramString}`, + return await httpService({ + url: `${apiBasePath}/fileupload/import${paramString}`, method: 'POST', data: { index, @@ -223,7 +224,6 @@ export async function createIndexPattern(indexPatternName) { } async function getIndexPatternId(name) { - const savedObjectsClient = chrome.getSavedObjectsClient(); const savedObjectSearch = await savedObjectsClient.find({ type: 'index-pattern', perPage: 1000 }); const indexPatternSavedObjects = savedObjectSearch.savedObjects; @@ -237,9 +237,8 @@ async function getIndexPatternId(name) { } export const getExistingIndexNames = async () => { - const basePath = chrome.addBasePath('/api'); - const indexes = await http({ - url: `${basePath}/index_management/indices`, + const indexes = await httpService({ + url: `${apiBasePath}/index_management/indices`, method: 'GET', }); return indexes @@ -248,7 +247,6 @@ export const getExistingIndexNames = async () => { }; export const getExistingIndexPatternNames = async () => { - const savedObjectsClient = chrome.getSavedObjectsClient(); const indexPatterns = await savedObjectsClient.find({ type: 'index-pattern', fields: ['id', 'title', 'type', 'fields'], diff --git a/x-pack/legacy/plugins/file_upload/server/client/call_with_internal_user_factory.d.ts b/x-pack/legacy/plugins/file_upload/server/client/call_with_internal_user_factory.d.ts index 0b39c81cee6f..9c1000db8cb5 100644 --- a/x-pack/legacy/plugins/file_upload/server/client/call_with_internal_user_factory.d.ts +++ b/x-pack/legacy/plugins/file_upload/server/client/call_with_internal_user_factory.d.ts @@ -4,6 +4,4 @@ * you may not use this file except in compliance with the Elastic License. */ -import { Server } from 'hapi'; - -export function callWithInternalUserFactory(server: Server): any; +export function callWithInternalUserFactory(elasticsearchPlugin: any): any; diff --git a/x-pack/legacy/plugins/file_upload/server/client/call_with_internal_user_factory.js b/x-pack/legacy/plugins/file_upload/server/client/call_with_internal_user_factory.js index dc3131484e75..f42c3ffb99a5 100644 --- a/x-pack/legacy/plugins/file_upload/server/client/call_with_internal_user_factory.js +++ b/x-pack/legacy/plugins/file_upload/server/client/call_with_internal_user_factory.js @@ -5,16 +5,15 @@ */ - import { once } from 'lodash'; -const _callWithInternalUser = once((server) => { - const { callWithInternalUser } = server.plugins.elasticsearch.getCluster('admin'); +const _callWithInternalUser = once(elasticsearchPlugin => { + const { callWithInternalUser } = elasticsearchPlugin.getCluster('admin'); return callWithInternalUser; }); -export const callWithInternalUserFactory = (server) => { +export const callWithInternalUserFactory = elasticsearchPlugin => { return (...args) => { - return _callWithInternalUser(server)(...args); + return _callWithInternalUser(elasticsearchPlugin)(...args); }; }; diff --git a/x-pack/legacy/plugins/file_upload/server/client/call_with_internal_user_factory.test.ts b/x-pack/legacy/plugins/file_upload/server/client/call_with_internal_user_factory.test.ts index d77541e7d3d6..04c5013ed8e6 100644 --- a/x-pack/legacy/plugins/file_upload/server/client/call_with_internal_user_factory.test.ts +++ b/x-pack/legacy/plugins/file_upload/server/client/call_with_internal_user_factory.test.ts @@ -8,25 +8,15 @@ import { callWithInternalUserFactory } from './call_with_internal_user_factory'; describe('call_with_internal_user_factory', () => { describe('callWithInternalUserFactory', () => { - let server: any; - let callWithInternalUser: any; - - beforeEach(() => { - callWithInternalUser = jest.fn(); - server = { - plugins: { - elasticsearch: { - getCluster: jest.fn(() => ({ callWithInternalUser })), - }, - }, - }; - }); - it('should use internal user "admin"', () => { - const callWithInternalUserInstance = callWithInternalUserFactory(server); + const callWithInternalUser: any = jest.fn(); + const elasticsearchPlugin: any = { + getCluster: jest.fn(() => ({ callWithInternalUser })), + }; + const callWithInternalUserInstance = callWithInternalUserFactory(elasticsearchPlugin); callWithInternalUserInstance(); - expect(server.plugins.elasticsearch.getCluster).toHaveBeenCalledWith('admin'); + expect(elasticsearchPlugin.getCluster).toHaveBeenCalledWith('admin'); }); }); }); diff --git a/x-pack/legacy/plugins/file_upload/server/client/call_with_request_factory.js b/x-pack/legacy/plugins/file_upload/server/client/call_with_request_factory.js index 0040fcb6c802..885573c993b7 100644 --- a/x-pack/legacy/plugins/file_upload/server/client/call_with_request_factory.js +++ b/x-pack/legacy/plugins/file_upload/server/client/call_with_request_factory.js @@ -8,13 +8,13 @@ import { once } from 'lodash'; -const callWithRequest = once((server) => { - const cluster = server.plugins.elasticsearch.getCluster('data'); +const callWithRequest = once(elasticsearchPlugin => { + const cluster = elasticsearchPlugin.getCluster('data'); return cluster.callWithRequest; }); -export const callWithRequestFactory = (server, request) => { +export const callWithRequestFactory = (elasticsearchPlugin, request) => { return (...args) => { - return callWithRequest(server)(request, ...args); + return callWithRequest(elasticsearchPlugin)(request, ...args); }; }; diff --git a/x-pack/legacy/plugins/file_upload/server/plugin.js b/x-pack/legacy/plugins/file_upload/server/plugin.js new file mode 100644 index 000000000000..0baef6f8ffa4 --- /dev/null +++ b/x-pack/legacy/plugins/file_upload/server/plugin.js @@ -0,0 +1,36 @@ +/* + * 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 { getImportRouteHandler } from './routes/file_upload'; +import { getTelemetry, initTelemetry } from './telemetry/telemetry'; +import { MAX_BYTES } from '../common/constants/file_import'; + +const TELEMETRY_TYPE = 'fileUploadTelemetry'; + +export class FileUploadPlugin { + setup(core, plugins, __LEGACY) { + const elasticsearchPlugin = __LEGACY.plugins.elasticsearch; + const getSavedObjectsRepository = __LEGACY.savedObjects.getSavedObjectsRepository; + const makeUsageCollector = __LEGACY.usage.collectorSet.makeUsageCollector; + + // Set up route + __LEGACY.route({ + method: 'POST', + path: '/api/fileupload/import', + handler: getImportRouteHandler(elasticsearchPlugin, getSavedObjectsRepository), + config: { + payload: { maxBytes: MAX_BYTES }, + } + }); + + // Make usage collector + makeUsageCollector({ + type: TELEMETRY_TYPE, + isReady: () => true, + fetch: async () => (await getTelemetry(elasticsearchPlugin, getSavedObjectsRepository)) || initTelemetry() + }); + } +} diff --git a/x-pack/legacy/plugins/file_upload/server/routes/file_upload.js b/x-pack/legacy/plugins/file_upload/server/routes/file_upload.js index ac07d80962bd..1eeecdeb1525 100644 --- a/x-pack/legacy/plugins/file_upload/server/routes/file_upload.js +++ b/x-pack/legacy/plugins/file_upload/server/routes/file_upload.js @@ -7,7 +7,6 @@ import { callWithRequestFactory } from '../client/call_with_request_factory'; import { wrapError } from '../client/errors'; import { importDataProvider } from '../models/import_data'; -import { MAX_BYTES } from '../../common/constants/file_import'; import { updateTelemetry } from '../telemetry/telemetry'; @@ -18,28 +17,35 @@ function importData({ return importDataFunc(id, index, settings, mappings, ingestPipeline, data); } -export function fileUploadRoutes(server, commonRouteConfig) { +export function getImportRouteHandler(elasticsearchPlugin, getSavedObjectsRepository) { + return async request => { - server.route({ - method: 'POST', - path: '/api/fileupload/import', - async handler(request) { + const requestObj = { + query: request.query, + payload: request.payload, + params: request.payload, + auth: request.auth, + headers: request.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 } = request.query; - if (id === undefined) { - await updateTelemetry({ server, ...request.payload }); - } - - const callWithRequest = callWithRequestFactory(server, request); - return importData({ callWithRequest, id, ...request.payload }) - .catch(wrapError); - }, - config: { - ...commonRouteConfig, - payload: { maxBytes: MAX_BYTES }, + // `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) { + await updateTelemetry({ elasticsearchPlugin, getSavedObjectsRepository }); } - }); + + const requestContentWithDefaults = { + id, + callWithRequest: callWithRequestFactory(elasticsearchPlugin, requestObj), + index: undefined, + settings: {}, + mappings: {}, + ingestPipeline: {}, + data: [], + ...requestObj.payload + }; + return importData(requestContentWithDefaults).catch(wrapError); + }; } diff --git a/x-pack/legacy/plugins/file_upload/server/telemetry/index.ts b/x-pack/legacy/plugins/file_upload/server/telemetry/index.ts index d05f7cc63c89..46da040dc34f 100644 --- a/x-pack/legacy/plugins/file_upload/server/telemetry/index.ts +++ b/x-pack/legacy/plugins/file_upload/server/telemetry/index.ts @@ -5,4 +5,3 @@ */ export * from './telemetry'; -export { makeUsageCollector } from './make_usage_collector'; diff --git a/x-pack/legacy/plugins/file_upload/server/telemetry/make_usage_collector.ts b/x-pack/legacy/plugins/file_upload/server/telemetry/make_usage_collector.ts deleted file mode 100644 index f589280d8cf3..000000000000 --- a/x-pack/legacy/plugins/file_upload/server/telemetry/make_usage_collector.ts +++ /dev/null @@ -1,27 +0,0 @@ -/* - * 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 { Server } from 'hapi'; -import { getTelemetry, initTelemetry, Telemetry } from './telemetry'; - -// TODO this type should be defined by the platform -interface KibanaHapiServer extends Server { - usage: { - collectorSet: { - makeUsageCollector: any; - register: any; - }; - }; -} - -export function makeUsageCollector(server: KibanaHapiServer): void { - const fileUploadUsageCollector = server.usage.collectorSet.makeUsageCollector({ - type: 'fileUploadTelemetry', - isReady: () => true, - fetch: async (): Promise => (await getTelemetry(server)) || initTelemetry(), - }); - server.usage.collectorSet.register(fileUploadUsageCollector); -} diff --git a/x-pack/legacy/plugins/file_upload/server/telemetry/telemetry.test.ts b/x-pack/legacy/plugins/file_upload/server/telemetry/telemetry.test.ts index 5017c9cb41f0..1c785d8e7b61 100644 --- a/x-pack/legacy/plugins/file_upload/server/telemetry/telemetry.test.ts +++ b/x-pack/legacy/plugins/file_upload/server/telemetry/telemetry.test.ts @@ -6,22 +6,13 @@ import { getTelemetry, updateTelemetry } from './telemetry'; +const elasticsearchPlugin: any = null; +const getSavedObjectsRepository: any = null; const internalRepository = () => ({ get: jest.fn(() => null), create: jest.fn(() => ({ attributes: 'test' })), update: jest.fn(() => ({ attributes: 'test' })), }); -const server: any = { - savedObjects: { - getSavedObjectsRepository: jest.fn(() => internalRepository()), - }, - plugins: { - elasticsearch: { - getCluster: jest.fn(() => ({ callWithInternalUser })), - }, - }, -}; -const callWithInternalUser = jest.fn(); function mockInit(getVal: any = { attributes: {} }): any { return { @@ -34,7 +25,7 @@ describe('file upload plugin telemetry', () => { describe('getTelemetry', () => { it('should get existing telemetry', async () => { const internalRepo = mockInit(); - await getTelemetry(server, internalRepo); + await getTelemetry(elasticsearchPlugin, getSavedObjectsRepository, 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); @@ -48,7 +39,12 @@ describe('file upload plugin telemetry', () => { filesUploadedTotalCount: 2, }, }); - await updateTelemetry({ server, internalRepo }); + + await updateTelemetry({ + elasticsearchPlugin, + getSavedObjectsRepository, + 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); diff --git a/x-pack/legacy/plugins/file_upload/server/telemetry/telemetry.ts b/x-pack/legacy/plugins/file_upload/server/telemetry/telemetry.ts index b43e2a1b33a2..5ffa735f4c83 100644 --- a/x-pack/legacy/plugins/file_upload/server/telemetry/telemetry.ts +++ b/x-pack/legacy/plugins/file_upload/server/telemetry/telemetry.ts @@ -4,7 +4,6 @@ * you may not use this file except in compliance with the Elastic License. */ -import { Server } from 'hapi'; import _ from 'lodash'; import { callWithInternalUserFactory } from '../client/call_with_internal_user_factory'; @@ -18,9 +17,11 @@ export interface TelemetrySavedObject { attributes: Telemetry; } -export function getInternalRepository(server: Server): any { - const { getSavedObjectsRepository } = server.savedObjects; - const callWithInternalUser = callWithInternalUserFactory(server); +export function getInternalRepository( + elasticsearchPlugin: any, + getSavedObjectsRepository: any +): any { + const callWithInternalUser = callWithInternalUserFactory(elasticsearchPlugin); return getSavedObjectsRepository(callWithInternalUser); } @@ -30,8 +31,13 @@ export function initTelemetry(): Telemetry { }; } -export async function getTelemetry(server: Server, internalRepo?: object): Promise { - const internalRepository = internalRepo || getInternalRepository(server); +export async function getTelemetry( + elasticsearchPlugin: any, + getSavedObjectsRepository: any, + internalRepo?: object +): Promise { + const internalRepository = + internalRepo || getInternalRepository(elasticsearchPlugin, getSavedObjectsRepository); let telemetrySavedObject; try { @@ -44,14 +50,21 @@ export async function getTelemetry(server: Server, internalRepo?: object): Promi } export async function updateTelemetry({ - server, + elasticsearchPlugin, + getSavedObjectsRepository, internalRepo, }: { - server: any; + elasticsearchPlugin: any; + getSavedObjectsRepository: any; internalRepo?: any; }) { - const internalRepository = internalRepo || getInternalRepository(server); - let telemetry = await getTelemetry(server, internalRepository); + const internalRepository = + internalRepo || getInternalRepository(elasticsearchPlugin, getSavedObjectsRepository); + let telemetry = await getTelemetry( + elasticsearchPlugin, + getSavedObjectsRepository, + internalRepository + ); // Create if doesn't exist if (!telemetry || _.isEmpty(telemetry)) { const newTelemetrySavedObject = await internalRepository.create( diff --git a/x-pack/legacy/plugins/maps/public/layers/sources/client_file_source/create_client_file_source_editor.js b/x-pack/legacy/plugins/maps/public/layers/sources/client_file_source/create_client_file_source_editor.js index 7c56f4c7f431..6e931ebe2b95 100644 --- a/x-pack/legacy/plugins/maps/public/layers/sources/client_file_source/create_client_file_source_editor.js +++ b/x-pack/legacy/plugins/maps/public/layers/sources/client_file_source/create_client_file_source_editor.js @@ -6,7 +6,7 @@ import React from 'react'; -import { JsonUploadAndParse } from '../../../../../file_upload/public'; +import { start as fileUpload } from '../../../../../file_upload/public/legacy'; export function ClientFileCreateSourceEditor({ previewGeojsonFile, @@ -16,7 +16,7 @@ export function ClientFileCreateSourceEditor({ onIndexReady, }) { return ( -