From e18606ce619e337cc1c15832a539360067c1ee5c Mon Sep 17 00:00:00 2001 From: Larry Gregory Date: Fri, 6 Sep 2019 10:22:49 -0400 Subject: [PATCH] Export saved objects based on search criteria (#44723) * export all saved objects returned from search, not all saved objects that exist * remove unused mock --- .../__jest__/objects_table.test.js | 12 +++---- .../__jest__/relationships.test.js | 4 --- .../components/objects_table/objects_table.js | 17 +++------- .../objects/lib/fetch_export_by_type.js | 31 ------------------- .../management/sections/objects/lib/index.js | 1 - 5 files changed, 9 insertions(+), 56 deletions(-) delete mode 100644 src/legacy/core_plugins/kibana/public/management/sections/objects/lib/fetch_export_by_type.js diff --git a/src/legacy/core_plugins/kibana/public/management/sections/objects/components/objects_table/__jest__/objects_table.test.js b/src/legacy/core_plugins/kibana/public/management/sections/objects/components/objects_table/__jest__/objects_table.test.js index 51f33f7568369..0be6a98260554 100644 --- a/src/legacy/core_plugins/kibana/public/management/sections/objects/components/objects_table/__jest__/objects_table.test.js +++ b/src/legacy/core_plugins/kibana/public/management/sections/objects/components/objects_table/__jest__/objects_table.test.js @@ -20,7 +20,7 @@ import React from 'react'; import { shallowWithIntl } from 'test_utils/enzyme_helpers'; -import { ObjectsTable, POSSIBLE_TYPES } from '../objects_table'; +import { ObjectsTable } from '../objects_table'; import { Flyout } from '../components/flyout/'; import { Relationships } from '../components/relationships/'; import { findObjects } from '../../../lib'; @@ -57,10 +57,6 @@ jest.mock('../../../lib/fetch_export_objects', () => ({ fetchExportObjects: jest.fn(), })); -jest.mock('../../../lib/fetch_export_by_type', () => ({ - fetchExportByType: jest.fn(), -})); - jest.mock('../../../lib/get_saved_object_counts', () => ({ getSavedObjectCounts: jest.fn().mockImplementation(() => { return { @@ -318,7 +314,7 @@ describe('ObjectsTable', () => { }); it('should export all', async () => { - const { fetchExportByType } = require('../../../lib/fetch_export_by_type'); + const { fetchExportObjects } = require('../../../lib/fetch_export_objects'); const { saveAs } = require('@elastic/filesaver'); const component = shallowWithIntl( { // Set up mocks const blob = new Blob([JSON.stringify(allSavedObjects)], { type: 'application/ndjson' }); - fetchExportByType.mockImplementation(() => blob); + fetchExportObjects.mockImplementation(() => blob); await component.instance().onExportAll(); - expect(fetchExportByType).toHaveBeenCalledWith(POSSIBLE_TYPES, true); + expect(fetchExportObjects).toHaveBeenCalledWith(allSavedObjects.map(so => ({ type: so.type, id: so.id })), true); expect(saveAs).toHaveBeenCalledWith(blob, 'export.ndjson'); expect(addSuccessMock).toHaveBeenCalledWith({ title: 'Your file is downloading in the background' }); }); diff --git a/src/legacy/core_plugins/kibana/public/management/sections/objects/components/objects_table/components/relationships/__jest__/relationships.test.js b/src/legacy/core_plugins/kibana/public/management/sections/objects/components/objects_table/components/relationships/__jest__/relationships.test.js index 9ad6190bc30d5..abb4df74275b2 100644 --- a/src/legacy/core_plugins/kibana/public/management/sections/objects/components/objects_table/components/relationships/__jest__/relationships.test.js +++ b/src/legacy/core_plugins/kibana/public/management/sections/objects/components/objects_table/components/relationships/__jest__/relationships.test.js @@ -39,10 +39,6 @@ jest.mock('ui/chrome', () => ({ addBasePath: () => '' })); -jest.mock('../../../../../lib/fetch_export_by_type', () => ({ - fetchExportByType: jest.fn(), -})); - jest.mock('../../../../../lib/fetch_export_objects', () => ({ fetchExportObjects: jest.fn(), })); diff --git a/src/legacy/core_plugins/kibana/public/management/sections/objects/components/objects_table/objects_table.js b/src/legacy/core_plugins/kibana/public/management/sections/objects/components/objects_table/objects_table.js index 263b23859c8a5..e1cda703e64b1 100644 --- a/src/legacy/core_plugins/kibana/public/management/sections/objects/components/objects_table/objects_table.js +++ b/src/legacy/core_plugins/kibana/public/management/sections/objects/components/objects_table/objects_table.js @@ -58,7 +58,6 @@ import { getRelationships, getSavedObjectLabel, fetchExportObjects, - fetchExportByType, findObjects, } from '../../lib'; import { FormattedMessage, injectI18n } from '@kbn/i18n/react'; @@ -303,20 +302,14 @@ class ObjectsTableUI extends Component { onExportAll = async () => { const { intl } = this.props; - const { exportAllSelectedOptions, isIncludeReferencesDeepChecked } = this.state; - const exportTypes = Object.entries(exportAllSelectedOptions).reduce( - (accum, [id, selected]) => { - if (selected) { - accum.push(id); - } - return accum; - }, - [] - ); + + const { exportAllSelectedOptions, isIncludeReferencesDeepChecked, savedObjects } = this.state; + + const exportObjects = savedObjects.filter(so => exportAllSelectedOptions[so.type]).map(so => ({ type: so.type, id: so.id })); let blob; try { - blob = await fetchExportByType(exportTypes, isIncludeReferencesDeepChecked); + blob = await fetchExportObjects(exportObjects, isIncludeReferencesDeepChecked); } catch (e) { toastNotifications.addDanger({ title: intl.formatMessage({ diff --git a/src/legacy/core_plugins/kibana/public/management/sections/objects/lib/fetch_export_by_type.js b/src/legacy/core_plugins/kibana/public/management/sections/objects/lib/fetch_export_by_type.js deleted file mode 100644 index 71c022b9d3998..0000000000000 --- a/src/legacy/core_plugins/kibana/public/management/sections/objects/lib/fetch_export_by_type.js +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import { kfetch } from 'ui/kfetch'; - -export async function fetchExportByType(types, includeReferencesDeep = false) { - return await kfetch({ - method: 'POST', - pathname: '/api/saved_objects/_export', - body: JSON.stringify({ - type: types, - includeReferencesDeep, - }), - }); -} diff --git a/src/legacy/core_plugins/kibana/public/management/sections/objects/lib/index.js b/src/legacy/core_plugins/kibana/public/management/sections/objects/lib/index.js index 2818f0d8a6cb4..d79467f8d6dfa 100644 --- a/src/legacy/core_plugins/kibana/public/management/sections/objects/lib/index.js +++ b/src/legacy/core_plugins/kibana/public/management/sections/objects/lib/index.js @@ -17,7 +17,6 @@ * under the License. */ -export * from './fetch_export_by_type'; export * from './fetch_export_objects'; export * from './in_app_url'; export * from './get_relationships';