Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ingest API to delete index patterns #6457

Merged
merged 7 commits into from
Mar 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/plugins/kibana/public/settings/sections/indices/_edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import PluginsKibanaSettingsSectionsIndicesFieldTypesProvider from 'plugins/kiba
import uiRoutes from 'ui/routes';
import uiModules from 'ui/modules';
import editTemplate from 'plugins/kibana/settings/sections/indices/_edit.html';
import IngestProvider from 'ui/ingest';

uiRoutes
.when('/settings/indices/edit/:indexPatternId', {
Expand All @@ -26,6 +27,7 @@ uiModules.get('apps/settings')
const notify = new Notifier();
const $state = $scope.state = new AppState();
const refreshKibanaIndex = Private(PluginsKibanaSettingsSectionsIndicesRefreshKibanaIndexProvider);
const ingest = Private(IngestProvider);

$scope.kbnUrl = Private(UrlProvider);
$scope.indexPattern = $route.current.locals.indexPattern;
Expand Down Expand Up @@ -62,8 +64,8 @@ uiModules.get('apps/settings')
}
}

courier.indexPatterns.delete($scope.indexPattern)
.then(refreshKibanaIndex)
ingest.delete($scope.indexPattern.id)
.then($scope.indexPattern.destroy)
.then(function () {
$location.url('/settings/indices');
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ const testData = {
const testPipeline = [
{
grok: {
match_field: 'message',
match_pattern: 'foo'
field: 'message',
pattern: 'foo'
}
},
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,37 @@
// wrapper directive, which sets some global stuff up like the left nav
require('ui/modules').get('apps/settings')
.directive('kbnSettingsIndices', function ($route, config, kbnUrl) {
.directive('kbnSettingsIndices', function ($route, config, kbnUrl, indexPatterns, Private) {
return {
restrict: 'E',
transclude: true,
template: require('plugins/kibana/settings/sections/indices/directives/kbn_settings_indices.html'),
link: function ($scope) {
const refreshKibanaIndex = Private(require('plugins/kibana/settings/sections/indices/_refresh_kibana_index'));

$scope.showAddNew = !/^\/settings\/indices$/.test($route.current.$$route.originalPath);
$scope.editingId = $route.current.params.indexPatternId;
config.$bind($scope, 'defaultIndex');

$scope.$watch('defaultIndex', function () {
var ids = $route.current.locals.indexPatternIds;
$scope.indexPatternList = ids.map(function (id) {
return {
id: id,
url: kbnUrl.eval('#/settings/indices/edit/{{id}}', {id: id}),
class: 'sidebar-item-title ' + ($scope.editingId === id ? 'active' : ''),
default: $scope.defaultIndex === id
};
function refreshIndexPatternList() {
indexPatterns.getIds.clearCache();
indexPatterns.getIds()
.then((ids) => {
$scope.indexPatternIds = ids;
$scope.indexPatternList = ids.map(function (id) {
return {
id: id,
url: kbnUrl.eval('#/settings/indices/edit/{{id}}', {id: id}),
class: 'sidebar-item-title ' + ($scope.editingId === id ? 'active' : ''),
default: $scope.defaultIndex === id
};
});
});
}

$scope.$watch('defaultIndex', refreshIndexPatternList);

$scope.$on('ingest:updated', () => {
refreshKibanaIndex().then(refreshIndexPatternList);
});

$scope.$emit('application.load');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ <h1>Tail a File</h1>
<div class="nav-buttons">
<button ng-click="wizard.prevStep()">Prev</button>
<button ng-disabled="!wizard.stepResults.indexPattern || !wizard.stepResults.indexPattern.id"
ng-click="wizard.nextStep()">
ng-click="wizard.save()">
Save
</button>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var modules = require('ui/modules');
var template = require('plugins/kibana/settings/sections/indices/filebeat/directives/filebeat_wizard.html');
import modules from 'ui/modules';
import template from 'plugins/kibana/settings/sections/indices/filebeat/directives/filebeat_wizard.html';
import IngestProvider from 'ui/ingest';

require('plugins/kibana/settings/sections/indices/add_data_steps/pattern_review_step');
require('plugins/kibana/settings/sections/indices/add_data_steps/paste_samples_step');
Expand All @@ -15,8 +16,14 @@ modules.get('apps/settings')
scope: {},
bindToController: true,
controllerAs: 'wizard',
controller: function ($scope, AppState, safeConfirm, kbnUrl) {
var $state = this.state = new AppState();
controller: function ($scope, AppState, safeConfirm, kbnUrl, $http, Notifier, $window, config, Private) {
const ingest = Private(IngestProvider);
const $state = this.state = new AppState();

var notify = new Notifier({
location: 'Add Data'
});

var totalSteps = 4;
this.stepResults = {};

Expand All @@ -36,12 +43,26 @@ modules.get('apps/settings')
kbnUrl.change('/discover');
}
};

this.prevStep = () => {
if ($state.currentStep > 0) {
this.setCurrentStep($state.currentStep - 1);
}
};

this.save = () => {
return ingest.save(this.stepResults.indexPattern, this.stepResults.pipeline)
.then(
() => {
this.nextStep();
},
(err) => {
notify.error(err);
$window.scrollTo(0,0);
}
);
};

$scope.$watch('wizard.state.currentStep', (newValue, oldValue) => {
if (this.complete) {
$state.currentStep = totalSteps - 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ module.exports = function registerDelete(server) {
callWithRequest(req, 'indices.deleteTemplate', {name: patternToIngest(req.params.id), ignore: [404]}),
callWithRequest(req, 'transport.request', {
path: `_ingest/pipeline/${patternToIngest(req.params.id)}`,
method: 'DELETE'
method: 'DELETE',
ignore: [404]
})
])
.then(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import handleESError from '../../../lib/handle_es_error';
import createMappingsFromPatternFields from '../../../lib/create_mappings_from_pattern_fields';
import initDefaultFieldProps from '../../../lib/init_default_field_props';
import {ingestToPattern, patternToIngest} from '../../../lib/convert_pattern_and_ingest_name';
import { keysToCamelCaseShallow } from '../../../lib/case_conversion';
import { keysToCamelCaseShallow } from '../../../../common/lib/case_conversion';

function patternRollback(rootError, indexPatternId, boundCallWithRequest) {
const deleteParams = {
Expand Down
116 changes: 116 additions & 0 deletions src/ui/public/ingest/__tests__/ingest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import ngMock from 'ngMock';
import expect from 'expect.js';
import IngestProvider from '../ingest';
import sinon from 'auto-release-sinon';

describe('Ingest Service', function () {
let $httpBackend;
let ingest;
let config;
let $rootScope;

beforeEach(ngMock.module('kibana'));
beforeEach(ngMock.inject(function ($injector, Private) {
ingest = Private(IngestProvider);
config = $injector.get('config');
$httpBackend = $injector.get('$httpBackend');

$rootScope = $injector.get('$rootScope');
sinon.spy($rootScope, '$broadcast');
}));

describe('save', function () {
it('Throws an error if an index pattern is not provided', function () {
expect(ingest.save).to.throwException(/index pattern is required/);
});

it('Sets the default index if there isn\'t one already', function () {
$httpBackend
.when('POST', '../api/kibana/ingest')
.respond('ok');

expect(config.get('defaultIndex')).to.be(null);
ingest.save({id: 'foo'});
ingest.save({id: 'bar'});
$httpBackend.flush();
expect(config.get('defaultIndex')).to.be('foo');
});

it('Returns error from ingest API if there is one', function (done) {
$httpBackend
.expectPOST('../api/kibana/ingest')
.respond(400);

ingest.save({id: 'foo'})
.then(
() => {
throw new Error('expected an error response');
},
(error) => {
expect(error.status).to.be(400);
done();
}
);

$httpBackend.flush();
});

it('Broadcasts an ingest:updated event on the rootScope upon succesful save', function () {
$httpBackend
.when('POST', '../api/kibana/ingest')
.respond('ok');

ingest.save({id: 'foo'});
$httpBackend.flush();

expect($rootScope.$broadcast.calledOnce);
expect($rootScope.$broadcast.calledWith('ingest:updated')).to.be.ok();
});
});

describe('delete', function () {
it('throws an error if no ingest id is provided', function () {
expect(ingest.delete).to.throwException(/ingest id is required/);
});

it('Calls the DELETE endpoint of the ingest API with the given id', function () {
$httpBackend
.expectDELETE('../api/kibana/ingest/foo')
.respond('ok');

ingest.delete('foo');
$httpBackend.flush();
});

it('Returns error from ingest API if there is one', function (done) {
$httpBackend
.expectDELETE('../api/kibana/ingest/foo')
.respond(404);

ingest.delete('foo')
.then(
() => {
throw new Error('expected an error response');
},
(error) => {
expect(error.status).to.be(404);
done();
}
);

$httpBackend.flush();
});

it('Broadcasts an ingest:updated event on the rootScope upon succesful save', function () {
$httpBackend
.when('DELETE', '../api/kibana/ingest/foo')
.respond('ok');

ingest.delete('foo');
$httpBackend.flush();

expect($rootScope.$broadcast.calledOnce);
expect($rootScope.$broadcast.calledWith('ingest:updated')).to.be.ok();
});
});
});
41 changes: 41 additions & 0 deletions src/ui/public/ingest/ingest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { keysToSnakeCaseShallow } from '../../../plugins/kibana/common/lib/case_conversion';
import _ from 'lodash';

export default function IngestProvider($rootScope, $http, config) {

const ingestAPIPrefix = '../api/kibana/ingest';

this.save = function (indexPattern, pipeline) {
if (_.isEmpty(indexPattern)) {
throw new Error('index pattern is required');
}

const payload = {
index_pattern: keysToSnakeCaseShallow(indexPattern)
};
if (!_.isEmpty(pipeline)) {
payload.pipeline = pipeline;
}

return $http.post(`${ingestAPIPrefix}`, payload)
.then(() => {
if (!config.get('defaultIndex')) {
config.set('defaultIndex', indexPattern.id);
}

$rootScope.$broadcast('ingest:updated');
});
};

this.delete = function (ingestId) {
if (_.isEmpty(ingestId)) {
throw new Error('ingest id is required');
}

return $http.delete(`${ingestAPIPrefix}/${ingestId}`)
.then(() => {
$rootScope.$broadcast('ingest:updated');
});
};

}