[Management] Provide a way to fetch indices and template index patterns (#12200)

* Provide a way to fetch indices and template index patterns

* Simplify the data structure

* Change the api to expect a query for both fetches

* Add unit tests

* Clarifying comment

* More tests

* - Move logic into ui/public/indices
- Add test to handle duplication logic

* Feedback from PR

* Add toggle (which defaults to on) to suppress 404s

* Leverage existing error handling constructs
This commit is contained in:
Chris Roberson 2017-06-15 09:28:26 -04:00 committed by GitHub
parent 383be943ff
commit 6d8d1ae4ed
5 changed files with 155 additions and 0 deletions

View file

@ -0,0 +1,56 @@
import expect from 'expect.js';
import ngMock from 'ng_mock';
import { IndicesGetIndicesProvider } from 'ui/indices/get_indices';
describe('GetIndices', function () {
let response;
let getIndices;
beforeEach(ngMock.module('kibana', ($provide) => {
response = {
'.monitoring-es-2': {
aliases: {},
},
'.monitoring-es-3': {
aliases: {
'.monitoring-es-active' : { }
},
},
'.monitoring-es-4': {
aliases: {},
},
'.monitoring-es-5': {
aliases: {},
},
'.kibana': {
aliases: {},
}
};
$provide.service('esAdmin', function () {
return {
indices: {
getAlias: async function () {
return response;
}
}
};
});
}));
beforeEach(ngMock.inject((Private) => {
getIndices = Private(IndicesGetIndicesProvider);
}));
it('should be a function', function () {
expect(getIndices).to.be.a(Function);
});
it('should get all indices', async function () {
const indices = await getIndices();
Object.keys(response).forEach(index => {
expect(indices).to.contain(index);
});
expect(indices).to.contain('.monitoring-es-active');
});
});

View file

@ -0,0 +1,58 @@
import expect from 'expect.js';
import ngMock from 'ng_mock';
import { IndicesGetTemplateIndexPatternsProvider } from 'ui/indices/get_template_index_patterns';
describe('GetTemplateIndexPatterns', function () {
let response;
let getTemplateIndexPatterns;
beforeEach(ngMock.module('kibana', ($provide) => {
response = {
'.ml-state': {
'index_patterns': [
'.ml-state'
]
},
'.watches': {
'index_patterns': [
'.watches*'
]
},
'.watches2': {
'index_patterns': [
'.watches*'
]
},
};
$provide.service('esAdmin', function () {
return {
indices: {
getTemplate: async function () {
return response;
}
}
};
});
}));
beforeEach(ngMock.inject((Private) => {
getTemplateIndexPatterns = Private(IndicesGetTemplateIndexPatternsProvider);
}));
it('should be a function', function () {
expect(getTemplateIndexPatterns).to.be.a(Function);
});
it('should get all index patterns', async function () {
const indices = await getTemplateIndexPatterns();
expect(indices).to.contain('.ml-state');
expect(indices).to.contain('.watches*');
});
it('should prevent duplicates', async function () {
const indices = await getTemplateIndexPatterns();
expect(indices).to.contain('.watches*');
expect(indices.length).to.be(2);
});
});

View file

@ -0,0 +1,18 @@
import { reduce, size } from 'lodash';
const getIndicesFromResponse = json => {
return reduce(json, (list, { aliases }, indexName) => {
list.push(indexName);
if (size(aliases) > 0) {
list.push(...Object.keys(aliases));
}
return list;
}, []);
};
export function IndicesGetIndicesProvider(esAdmin) {
return async function getIndices(query) {
const aliasesJson = await esAdmin.indices.getAlias({ index: query, allowNoIndices: true });
return getIndicesFromResponse(aliasesJson);
};
}

View file

@ -0,0 +1,12 @@
import { flatten, pluck, uniq } from 'lodash';
const getIndexPatternsFromResponse = json => {
return uniq(flatten(pluck(json, 'index_patterns')));
};
export function IndicesGetTemplateIndexPatternsProvider(esAdmin) {
return async function getTemplateIndexPatterns(query) {
const templatesJson = await esAdmin.indices.getTemplate({ name: query, ignore: 404 });
return getIndexPatternsFromResponse(templatesJson);
};
}

View file

@ -0,0 +1,11 @@
import { IndicesGetIndicesProvider } from 'ui/indices/get_indices';
import { IndicesGetTemplateIndexPatternsProvider } from 'ui/indices/get_template_index_patterns';
import { uiModules } from 'ui/modules';
const module = uiModules.get('kibana/indices');
export function IndicesProvider(Private) {
this.getIndices = Private(IndicesGetIndicesProvider);
this.getTemplateIndexPatterns = Private(IndicesGetTemplateIndexPatternsProvider);
}
module.service('indices', Private => Private(IndicesProvider));