diff --git a/src/es_archiver/lib/indices/__tests__/create_index_stream.js b/src/es_archiver/lib/indices/__tests__/create_index_stream.js index d2a5f658a17a..836fdd10b27f 100644 --- a/src/es_archiver/lib/indices/__tests__/create_index_stream.js +++ b/src/es_archiver/lib/indices/__tests__/create_index_stream.js @@ -80,6 +80,29 @@ describe('esArchiver: createCreateIndexStream()', () => { ]); }); + it('creates aliases', async () => { + const client = createStubClient(); + const stats = createStubStats(); + await createPromiseFromStreams([ + createListStream([ + createStubIndexRecord('index', { foo: { } }), + createStubDocRecord('index', 1), + ]), + createCreateIndexStream({ client, stats }), + createConcatStream([]) + ]); + + sinon.assert.calledWith(client.indices.create, { + method: 'PUT', + index: 'index', + body: { + settings: undefined, + mappings: undefined, + aliases: { foo: {} }, + }, + }); + }); + it('passes through records with unknown types', async () => { const client = createStubClient(); const stats = createStubStats(); diff --git a/src/es_archiver/lib/indices/__tests__/generate_index_records_stream.js b/src/es_archiver/lib/indices/__tests__/generate_index_records_stream.js index 1bbff645e49c..05826e7c6d10 100644 --- a/src/es_archiver/lib/indices/__tests__/generate_index_records_stream.js +++ b/src/es_archiver/lib/indices/__tests__/generate_index_records_stream.js @@ -98,4 +98,25 @@ describe('esArchiver: createGenerateIndexRecordsStream()', () => { expect(indexRecords[2]).to.have.property('value'); expect(indexRecords[2].value).to.have.property('index', 'index3'); }); + + it('understands aliases', async () => { + const stats = createStubStats(); + const client = createStubClient(['index1'], { index1: { foo: {} } }); + + const indexRecords = await createPromiseFromStreams([ + createListStream(['index1']), + createGenerateIndexRecordsStream(client, stats), + createConcatStream([]), + ]); + + expect(indexRecords).to.eql([{ + type: 'index', + value: { + index: 'index1', + settings: {}, + mappings: {}, + aliases: { foo: {} }, + } + }]); + }); }); diff --git a/src/es_archiver/lib/indices/__tests__/stubs.js b/src/es_archiver/lib/indices/__tests__/stubs.js index cdef76721150..4fe7047cbf93 100644 --- a/src/es_archiver/lib/indices/__tests__/stubs.js +++ b/src/es_archiver/lib/indices/__tests__/stubs.js @@ -21,6 +21,7 @@ import sinon from 'sinon'; export const createStubStats = () => ({ createdIndex: sinon.stub(), + createdAliases: sinon.stub(), deletedIndex: sinon.stub(), skippedIndex: sinon.stub(), archivedIndex: sinon.stub(), @@ -35,9 +36,9 @@ export const createStubStats = () => ({ }, }); -export const createStubIndexRecord = (index) => ({ +export const createStubIndexRecord = (index, aliases = {}) => ({ type: 'index', - value: { index } + value: { index, aliases } }); export const createStubDocRecord = (index, id) => ({ @@ -55,7 +56,7 @@ const createEsClientError = (errorType) => { return err; }; -export const createStubClient = (existingIndices = []) => ({ +export const createStubClient = (existingIndices = [], aliases = {}) => ({ indices: { get: sinon.spy(async ({ index }) => { if (!existingIndices.includes(index)) { @@ -69,6 +70,19 @@ export const createStubClient = (existingIndices = []) => ({ } }; }), + getAlias: sinon.spy(({ index }) => { + return Promise.resolve({ [index]: { aliases: aliases[index] || {} } }); + }), + updateAliases: sinon.spy(async ({ body }) => { + body.actions.forEach(({ add: { index, alias } }) => { + if (!existingIndices.includes(index)) { + throw createEsClientError('index_not_found_exception'); + } + existingIndices.push({ index, alias }); + }); + + return { ok: true }; + }), create: sinon.spy(async ({ index }) => { if (existingIndices.includes(index)) { throw createEsClientError('resource_already_exists_exception'); diff --git a/src/es_archiver/lib/indices/create_index_stream.js b/src/es_archiver/lib/indices/create_index_stream.js index 8f5305fe3cfc..251fa6fddf34 100644 --- a/src/es_archiver/lib/indices/create_index_stream.js +++ b/src/es_archiver/lib/indices/create_index_stream.js @@ -35,14 +35,14 @@ export function createCreateIndexStream({ client, stats, skipExisting, log }) { } async function handleIndex(stream, record) { - const { index, settings, mappings } = record.value; + const { index, settings, mappings, aliases } = record.value; async function attemptToCreate(attemptNumber = 1) { try { await client.indices.create({ method: 'PUT', index, - body: { settings, mappings }, + body: { settings, mappings, aliases }, }); stats.createdIndex(index, { settings }); } catch (err) { diff --git a/src/es_archiver/lib/indices/generate_index_records_stream.js b/src/es_archiver/lib/indices/generate_index_records_stream.js index 7c2b7fb5d631..df74fba63566 100644 --- a/src/es_archiver/lib/indices/generate_index_records_stream.js +++ b/src/es_archiver/lib/indices/generate_index_records_stream.js @@ -38,7 +38,9 @@ export function createGenerateIndexRecordsStream(client, stats) { ] }); + const { [index]: { aliases } } = await client.indices.getAlias({ index }); const { settings, mappings } = resp[index]; + stats.archivedIndex(index, { settings, mappings }); callback(null, { type: 'index', @@ -46,6 +48,7 @@ export function createGenerateIndexRecordsStream(client, stats) { index, settings, mappings, + aliases, } }); } catch (err) {