Skip to content
Merged
12 changes: 9 additions & 3 deletions src/core_plugins/kibana/public/dashboard/__tests__/panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Promise from 'bluebird';
import sinon from 'sinon';
import noDigestPromise from 'test_utils/no_digest_promises';
import mockUiState from 'fixtures/mock_ui_state';
import { SavedObjectsClientProvider } from 'ui/saved_objects';

describe('dashboard panel', function () {
let $scope;
Expand All @@ -14,8 +15,13 @@ describe('dashboard panel', function () {

function init(mockDocResponse) {
ngMock.module('kibana');
ngMock.inject(($rootScope, $compile, esAdmin) => {
sinon.stub(esAdmin, 'mget').returns(Promise.resolve({ docs: [ mockDocResponse ] }));
ngMock.inject(($rootScope, $compile, Private, esAdmin) => {
Private.swap(SavedObjectsClientProvider, () => {
return {
get: sinon.stub().returns(Promise.resolve(mockDocResponse))
};
});

sinon.stub(esAdmin.indices, 'getFieldMapping').returns(Promise.resolve({
'.kibana': {
mappings: {
Expand Down Expand Up @@ -70,7 +76,7 @@ describe('dashboard panel', function () {
});

it('should try to visualize the visualization if found', function () {
init({ found: true, _source: {} });
init({ id: 'foo1', type: 'visualization', _version: 2, attributes: {} });
return $scope.loadedPanel.then(() => {
expect($scope.error).not.to.be.ok();
parentScope.$digest();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export function loadSavedObject(loaders, panel) {
if (!loader) {
throw new Error(`No loader for object of type ${panel.type}`);
}
return loader.get(panel.id)
.then(savedObj => ({ savedObj, editUrl: loader.urlFor(panel.id) }));
return loader.get(panel.id).then(savedObj => {
return { savedObj, editUrl: loader.urlFor(panel.id) };
});
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { saveAs } from '@spalger/filesaver';
import { extend, find, flattenDeep, partialRight, pick, pluck, sortBy } from 'lodash';
import { extend, find, flattenDeep, pluck, sortBy } from 'lodash';
import angular from 'angular';
import { savedObjectManagementRegistry } from 'plugins/kibana/management/saved_object_registry';
import objectIndexHTML from 'plugins/kibana/management/sections/objects/_objects.html';
import 'ui/directives/file_upload';
import uiRoutes from 'ui/routes';
import { SavedObjectsClientProvider } from 'ui/saved_objects';
import { uiModules } from 'ui/modules';

uiRoutes
Expand All @@ -19,10 +20,12 @@ uiRoutes

uiModules.get('apps/management')
.directive('kbnManagementObjects', function (kbnIndex, Notifier, Private, kbnUrl, Promise, confirmModal) {
const savedObjectsClient = Private(SavedObjectsClientProvider);

return {
restrict: 'E',
controllerAs: 'managementObjectsController',
controller: function ($scope, $injector, $q, AppState, esAdmin) {
controller: function ($scope, $injector, $q, AppState) {
const notify = new Notifier({ location: 'Saved Objects' });

// TODO: Migrate all scope variables to the controller.
Expand Down Expand Up @@ -123,7 +126,10 @@ uiModules.get('apps/management')

// TODO: Migrate all scope methods to the controller.
$scope.bulkExport = function () {
const objs = $scope.selectedItems.map(partialRight(extend, { type: $scope.currentTab.type }));
const objs = $scope.selectedItems.map(item => {
return { type: $scope.currentTab.type, id: item.id };
});

retrieveAndExportDocs(objs);
};

Expand All @@ -138,18 +144,17 @@ uiModules.get('apps/management')

function retrieveAndExportDocs(objs) {
if (!objs.length) return notify.error('No saved objects to export.');
esAdmin.mget({
index: kbnIndex,
body: { docs: objs.map(transformToMget) }
})
.then(function (response) {
saveToFile(response.docs.map(partialRight(pick, '_id', '_type', '_source')));
});
}

// Takes an object and returns the associated data needed for an mget API request
function transformToMget(obj) {
return { _id: obj.id, _type: obj.type };
savedObjectsClient.bulkGet(objs)
.then(function (response) {
saveToFile(response.savedObjects.map(obj => {
return {
_id: obj.id,
_type: obj.type,
_source: obj.attributes
};
}));
});
}

function saveToFile(results) {
Expand Down Expand Up @@ -235,18 +240,11 @@ uiModules.get('apps/management')

return Promise.map(docTypes.searches, importDocument)
.then(() => Promise.map(docTypes.other, importDocument))
.then(refreshIndex)
.then(refreshData)
.catch(notify.error);
});
};

function refreshIndex() {
return esAdmin.indices.refresh({
index: kbnIndex
});
}

// TODO: Migrate all scope methods to the controller.
$scope.changeTab = function (tab) {
$scope.currentTab = tab;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ describe('importDashboards(req)', () => {
{ create: { _type: 'visualization', _id: 'panel-01' } },
{ visState: '{}' }
],
index: '.kibana'
index: '.kibana',
refresh: 'wait_for'
});
});
});
Expand All @@ -69,7 +70,8 @@ describe('importDashboards(req)', () => {
{ index: { _type: 'visualization', _id: 'panel-01' } },
{ visState: '{}' }
],
index: '.kibana'
index: '.kibana',
refresh: 'wait_for'
});
});
});
Expand All @@ -84,7 +86,8 @@ describe('importDashboards(req)', () => {
{ create: { _type: 'dashboard', _id: 'dashboard-01' } },
{ panelJSON: '{}' }
],
index: '.kibana'
index: '.kibana',
refresh: 'wait_for'
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { SavedObjectsClient } from '../../../../../server/saved_objects';
export async function importDashboards(req) {
const { payload } = req;
const config = req.server.config();
const force = 'force' in req.query && req.query.force !== false;
const overwrite = 'force' in req.query && req.query.force !== false;
const exclude = flatten([req.query.exclude]);

const { callWithRequest } = req.server.plugins.elasticsearch.getCluster('admin');
Expand All @@ -15,6 +15,6 @@ export async function importDashboards(req) {
const docs = payload.objects
.filter(item => !exclude.includes(item.type));

const objects = await savedObjectsClient.bulkCreate(docs, { force });
const objects = await savedObjectsClient.bulkCreate(docs, { overwrite });
return { objects };
}
35 changes: 32 additions & 3 deletions src/server/saved_objects/client/__tests__/saved_objects_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe('SavedObjectsClient', () => {
});
});

it('should use ES create action', async () => {
it('should use ES index action', async () => {
callAdminCluster.returns({ _type: 'index-pattern', _id: 'logstash-*', _version: 2 });

await savedObjectsClient.create('index-pattern', {
Expand All @@ -82,6 +82,35 @@ describe('SavedObjectsClient', () => {
const args = callAdminCluster.getCall(0).args;
expect(args[0]).to.be('index');
});

it('should use create action if ID defined and overwrite=false', async () => {
callAdminCluster.returns({ _type: 'index-pattern', _id: 'logstash-*', _version: 2 });

await savedObjectsClient.create('index-pattern', {
title: 'Logstash'
}, {
id: 'logstash-*',
});

expect(callAdminCluster.calledOnce).to.be(true);

const args = callAdminCluster.getCall(0).args;
expect(args[0]).to.be('create');
});

it('allows for id to be provided', async () => {
callAdminCluster.returns({ _type: 'index-pattern', _id: 'logstash-*', _version: 2 });

await savedObjectsClient.create('index-pattern', {
id: 'logstash-*',
title: 'Logstash'
}, { id: 'myId' });

expect(callAdminCluster.calledOnce).to.be(true);

const args = callAdminCluster.getCall(0).args;
expect(args[1].id).to.be('myId');
});
});

describe('#bulkCreate', () => {
Expand All @@ -104,11 +133,11 @@ describe('SavedObjectsClient', () => {
]);
});

it('should overwrite objects if force is truthy', async () => {
it('should overwrite objects if overwrite is truthy', async () => {
await savedObjectsClient.bulkCreate([
{ type: 'config', id: 'one', attributes: { title: 'Test One' } },
{ type: 'index-pattern', id: 'two', attributes: { title: 'Test Two' } }
], { force: true });
], { overwrite: true });

expect(callAdminCluster.calledOnce).to.be(true);

Expand Down
Loading