[Fleet] Fix updateCurrentWriteIndices to work with datastream with prefix (#121884) (#121898)

Co-authored-by: Nicolas Chaulet <nicolas.chaulet@elastic.co>
This commit is contained in:
Kibana Machine 2021-12-22 13:28:45 -05:00 committed by GitHub
parent 63bac2e2a6
commit 93aa2a13a9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 2 deletions

View file

@ -839,6 +839,33 @@ describe('EPM template', () => {
});
describe('updateCurrentWriteIndices', () => {
it('update all the index matching, index template index pattern', async () => {
const esClient = elasticsearchServiceMock.createElasticsearchClient();
esClient.indices.getDataStream.mockResolvedValue({
body: {
data_streams: [{ name: 'test.prefix1-default' }],
},
} as any);
const logger = loggerMock.create();
await updateCurrentWriteIndices(esClient, logger, [
{
templateName: 'test',
indexTemplate: {
index_patterns: ['test.*-*'],
template: {
settings: { index: {} },
mappings: { properties: {} },
},
} as any,
},
]);
expect(esClient.indices.getDataStream).toBeCalledWith({
name: 'test.*-*',
});
const putMappingsCall = esClient.indices.putMapping.mock.calls.map(([{ index }]) => index);
expect(putMappingsCall).toHaveLength(1);
expect(putMappingsCall[0]).toBe('test.prefix1-default');
});
it('update non replicated datastream', async () => {
const esClient = elasticsearchServiceMock.createElasticsearchClient();
esClient.indices.getDataStream.mockResolvedValue({
@ -854,6 +881,7 @@ describe('EPM template', () => {
{
templateName: 'test',
indexTemplate: {
index_patterns: ['test-*'],
template: {
settings: { index: {} },
mappings: { properties: {} },

View file

@ -468,8 +468,11 @@ const getDataStreams = async (
esClient: ElasticsearchClient,
template: IndexTemplateEntry
): Promise<CurrentDataStream[] | undefined> => {
const { templateName, indexTemplate } = template;
const { body } = await esClient.indices.getDataStream({ name: `${templateName}-*` });
const { indexTemplate } = template;
const { body } = await esClient.indices.getDataStream({
name: indexTemplate.index_patterns.join(','),
});
const dataStreams = body.data_streams;
if (!dataStreams.length) return;