Skip to content

Commit

Permalink
[Management] Allows for imports to select existing index (#14137) (#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisronline authored and tylersmalley committed Oct 5, 2017
1 parent cbe69ab commit 0a0ca43
Show file tree
Hide file tree
Showing 14 changed files with 762 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ <h1 class="kuiTitle">
<div class="kuiBarSection">
<button
class="kuiButton kuiButton--basic kuiButton--iconText"
data-test-subj="exportAllObjects"
ng-click="exportAll()"
>
<span class="kuiButton__inner">
Expand Down Expand Up @@ -49,6 +50,7 @@ <h1 class="kuiTitle">
ng-class="{ 'kuiTab-isSelected': state.tab === service.title }"
ng-repeat="service in services"
ng-click="changeTab(service)"
data-test-subj="objectsTab-{{ service.title }}"
>
{{ service.title }}
<small aria-label="{{:: service.data.length + ' of ' + service.total + ' ' + service.title }}">
Expand Down Expand Up @@ -126,7 +128,7 @@ <h1 class="kuiTitle">
</div>

<!-- Table -->
<table class="kuiTable" ng-if="service.data.length">
<table class="kuiTable" ng-if="service.data.length" data-test-subj="objectsTable-{{service.title}}">
<thead>
<tr>
<th class="kuiTableHeaderCell kuiTableHeaderCell--checkBox">
Expand All @@ -148,6 +150,7 @@ <h1 class="kuiTitle">
<tr
ng-repeat="item in service.data | orderBy:'title'"
class="kuiTableRow"
data-test-subj="objectsTableRow"
>
<td class="kuiTableRowCell kuiTableRowCell--checkBox">
<div class="kuiTableRowCell__liner">
Expand Down
146 changes: 96 additions & 50 deletions src/core_plugins/kibana/public/management/sections/objects/_objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,25 @@ import 'ui/directives/file_upload';
import uiRoutes from 'ui/routes';
import { SavedObjectsClientProvider } from 'ui/saved_objects';
import { uiModules } from 'ui/modules';
import { showChangeIndexModal } from './show_change_index_modal';
import { SavedObjectNotFound } from 'ui/errors';

const indexPatternsResolutions = {
indexPatterns: function (Private) {
const savedObjectsClient = Private(SavedObjectsClientProvider);

return savedObjectsClient.find({
type: 'index-pattern',
fields: ['title'],
perPage: 10000
}).then(response => response.savedObjects);
}
};

uiRoutes
.when('/management/kibana/objects', {
template: objectIndexHTML
template: objectIndexHTML,
resolve: indexPatternsResolutions
});

uiRoutes
Expand All @@ -19,7 +34,7 @@ uiRoutes
});

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

return {
Expand Down Expand Up @@ -188,61 +203,92 @@ uiModules.get('apps/management')
}
);
})
.then((overwriteAll) => {
function importDocument(doc) {
const { service } = find($scope.services, { type: doc._type }) || {};
.then((overwriteAll) => {
const conflictedIndexPatterns = [];

if (!service) {
const msg = `Skipped import of "${doc._source.title}" (${doc._id})`;
const reason = `Invalid type: "${doc._type}"`;
function importDocument(doc) {
const { service } = find($scope.services, { type: doc._type }) || {};

notify.warning(`${msg}, ${reason}`, {
lifetime: 0,
});

return;
}
if (!service) {
const msg = `Skipped import of "${doc._source.title}" (${doc._id})`;
const reason = `Invalid type: "${doc._type}"`;

return service.get()
.then(function (obj) {
obj.id = doc._id;
return obj.applyESResp(doc)
.then(() => {
return obj.save({ confirmOverwrite : !overwriteAll });
})
.catch((err) => {
// swallow errors here so that the remaining promise chain executes
err.message = `Importing ${obj.title} (${obj.id}) failed: ${err.message}`;
notify.error(err);
});
});
}

function groupByType(docs) {
const defaultDocTypes = {
searches: [],
other: [],
};
notify.warning(`${msg}, ${reason}`, {
lifetime: 0,
});

return docs.reduce((types, doc) => {
switch (doc._type) {
case 'search':
types.searches.push(doc);
break;
default:
types.other.push(doc);
}
return types;
}, defaultDocTypes);
return;
}

const docTypes = groupByType(docs);
return service.get()
.then(function (obj) {
obj.id = doc._id;
return obj.applyESResp(doc)
.then(() => {
return obj.save({ confirmOverwrite : !overwriteAll });
})
.catch((err) => {
if (err instanceof SavedObjectNotFound && err.savedObjectType === 'index-pattern') {
conflictedIndexPatterns.push({ obj, doc });
return;
}

// swallow errors here so that the remaining promise chain executes
err.message = `Importing ${obj.title} (${obj.id}) failed: ${err.message}`;
notify.error(err);
});
});
}

function groupByType(docs) {
const defaultDocTypes = {
searches: [],
other: [],
};

return Promise.map(docTypes.searches, importDocument)
.then(() => Promise.map(docTypes.other, importDocument))
.then(refreshData)
.catch(notify.error);
});
return docs.reduce((types, doc) => {
switch (doc._type) {
case 'search':
types.searches.push(doc);
break;
default:
types.other.push(doc);
}
return types;
}, defaultDocTypes);
}

const docTypes = groupByType(docs);

return Promise.map(docTypes.searches, importDocument)
.then(() => Promise.map(docTypes.other, importDocument))
.then(() => {
if (conflictedIndexPatterns.length) {
showChangeIndexModal(
(objs) => {
return Promise.map(
conflictedIndexPatterns,
({ obj }) => {
const oldIndexId = obj.searchSource.getOwn('index');
const newIndexId = objs.find(({ oldId }) => oldId === oldIndexId).newId;
if (newIndexId === oldIndexId) {
// Skip
return;
}
return obj.hydrateIndexPattern(newIndexId)
.then(() => obj.save({ confirmOverwrite : !overwriteAll }));
}
).then(refreshData);
},
conflictedIndexPatterns,
$route.current.locals.indexPatterns,
);
} else {
return refreshData();
}
})
.catch(notify.error);
});
};

// TODO: Migrate all scope methods to the controller.
Expand Down
Loading

0 comments on commit 0a0ca43

Please sign in to comment.