Skip to content

Commit

Permalink
Merge pull request #4509 from lukasolson/import-saved-objects
Browse files Browse the repository at this point in the history
Change importer to use saved object class, fix broken select all
  • Loading branch information
jbudz committed Jul 29, 2015
2 parents cfc8a9c + 796e64b commit 05ca74b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 61 deletions.
73 changes: 37 additions & 36 deletions src/kibana/components/courier/saved_object/saved_object.js
Expand Up @@ -92,54 +92,55 @@ define(function (require) {

// fetch the object from ES
return docSource.fetch()
.then(function applyESResp(resp) {
.then(self.applyESResp);
})
.then(function () {
return customInit.call(self);
})
.then(function () {
// return our obj as the result of init()
return self;
});
});

self._source = _.cloneDeep(resp._source);
self.applyESResp = function (resp) {
self._source = _.cloneDeep(resp._source);

if (!resp.found) throw new errors.SavedObjectNotFound(type, self.id);
if (resp.found != null && !resp.found) throw new errors.SavedObjectNotFound(type, self.id);

var meta = resp._source.kibanaSavedObjectMeta || {};
delete resp._source.kibanaSavedObjectMeta;
var meta = resp._source.kibanaSavedObjectMeta || {};
delete resp._source.kibanaSavedObjectMeta;

if (!config.indexPattern && self._source.indexPattern) {
config.indexPattern = self._source.indexPattern;
delete self._source.indexPattern;
}
if (!config.indexPattern && self._source.indexPattern) {
config.indexPattern = self._source.indexPattern;
delete self._source.indexPattern;
}

// assign the defaults to the response
_.defaults(self._source, defaults);
// assign the defaults to the response
_.defaults(self._source, defaults);

// transform the source using _deserializers
_.forOwn(mapping, function ittr(fieldMapping, fieldName) {
if (fieldMapping._deserialize) {
self._source[fieldName] = fieldMapping._deserialize(self._source[fieldName], resp, fieldName, fieldMapping);
}
});
// transform the source using _deserializers
_.forOwn(mapping, function ittr(fieldMapping, fieldName) {
if (fieldMapping._deserialize) {
self._source[fieldName] = fieldMapping._deserialize(self._source[fieldName], resp, fieldName, fieldMapping);
}
});

// Give obj all of the values in _source.fields
_.assign(self, self._source);

return Promise.try(function () {
parseSearchSource(meta.searchSourceJSON);
})
.then(hydrateIndexPattern)
.then(function () {
return Promise.cast(afterESResp.call(self, resp));
})
.then(function () {
// Any time obj is updated, re-call applyESResp
docSource.onUpdate().then(applyESResp, notify.fatal);
});
});
// Give obj all of the values in _source.fields
_.assign(self, self._source);

return Promise.try(function () {
parseSearchSource(meta.searchSourceJSON);
})
.then(hydrateIndexPattern)
.then(function () {
return customInit.call(self);
return Promise.cast(afterESResp.call(self, resp));
})
.then(function () {
// return our obj as the result of init()
return self;
// Any time obj is updated, re-call applyESResp
docSource.onUpdate().then(self.applyESResp, notify.fatal);
});
});
};

function parseSearchSource(searchSourceJson) {
if (!self.searchSource) return;
Expand Down
1 change: 1 addition & 0 deletions src/kibana/directives/file_upload.js
Expand Up @@ -24,6 +24,7 @@ define(function (require) {
});

$elem.on('click', function (e) {
$fileInput.val(null);
$fileInput.trigger('click');
});
}
Expand Down
37 changes: 12 additions & 25 deletions src/kibana/plugins/settings/sections/objects/_objects.js
Expand Up @@ -13,7 +13,7 @@ define(function (require) {
});

require('modules').get('apps/settings')
.directive('kbnSettingsObjects', function (config, Notifier, Private, kbnUrl) {
.directive('kbnSettingsObjects', function (config, Notifier, Private, kbnUrl, Promise) {
return {
restrict: 'E',
controller: function ($scope, $injector, $q, AppState, es) {
Expand Down Expand Up @@ -41,7 +41,7 @@ define(function (require) {
$q.all(services).then(function (data) {
$scope.services = _.sortBy(data, 'title');
var tab = $scope.services[0];
if ($state.tab) tab = _.find($scope.services, {title: $state.tab});
if ($state.tab) $scope.currentTab = tab = _.find($scope.services, {title: $state.tab});

$scope.$watch('state.tab', function (tab) {
if (!tab) $scope.changeTab($scope.services[0]);
Expand Down Expand Up @@ -124,32 +124,19 @@ define(function (require) {
notify.error('The file could not be processed.');
}

return es.mget({
index: config.file.kibana_index,
body: {docs: docs.map(_.partialRight(_.pick, '_id', '_type'))}
return Promise.map(docs, function (doc) {
var service = _.find($scope.services, {type: doc._type}).service;
return service.get().then(function (obj) {
obj.id = doc._id;
return obj.applyESResp(doc).then(function () {
return obj.save();
});
});
})
.then(function (response) {
var existingDocs = _.where(response.docs, {found: true});
var confirmMessage = 'The following objects will be overwritten:\n\n';
if (existingDocs.length === 0 || window.confirm(confirmMessage + _.pluck(existingDocs, '_id').join('\n'))) {
return es.bulk({
index: config.file.kibana_index,
body: _.flattenDeep(docs.map(transformToBulk))
})
.then(refreshIndex)
.then(refreshData, notify.error);
}
});
.then(refreshIndex)
.then(refreshData, notify.error);
};

// Takes a doc and returns the associated two entries for an index bulk API request
function transformToBulk(doc) {
return [
{index: _.pick(doc, '_id', '_type')},
doc._source
];
}

function refreshIndex() {
return es.indices.refresh({
index: config.file.kibana_index
Expand Down

0 comments on commit 05ca74b

Please sign in to comment.