Skip to content

Commit

Permalink
Add kibana version to esNames
Browse files Browse the repository at this point in the history
  • Loading branch information
mikecote committed Feb 21, 2020
1 parent 4d3d4a7 commit 0d5bde1
Show file tree
Hide file tree
Showing 12 changed files with 22 additions and 217 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ const createClusterClientMock = () => {
createIndexTemplate: jest.fn(),
doesAliasExist: jest.fn(),
createIndex: jest.fn(),
getIndexTemplate: jest.fn(),
updateIndexTemplate: jest.fn(),
rolloverIndex: jest.fn(),
};
return mock;
};
Expand Down
53 changes: 0 additions & 53 deletions x-pack/plugins/event_log/server/es/cluster_client_adapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,43 +140,6 @@ describe('createIndexTemplate', () => {
});
});

describe('updateIndexTemplate', () => {
test('should call cluster with given template', async () => {
await clusterClientAdapter.updateIndexTemplate('foo', { args: true });
expect(clusterClient.callAsInternalUser).toHaveBeenCalledWith('indices.putTemplate', {
name: 'foo',
body: { args: true },
});
});

test('should throw error when call cluster throws', async () => {
clusterClient.callAsInternalUser.mockRejectedValue(new Error('Fail'));
await expect(
clusterClientAdapter.updateIndexTemplate('foo', { args: true })
).rejects.toThrowErrorMatchingInlineSnapshot(`"Fail"`);
});
});

describe('getIndexTemplate', () => {
beforeEach(() => {
clusterClient.callAsInternalUser.mockResolvedValue({});
});

test('should call cluster with given name', async () => {
await clusterClientAdapter.getIndexTemplate('foo');
expect(clusterClient.callAsInternalUser).toHaveBeenCalledWith('indices.getTemplate', {
name: 'foo',
});
});

test('should throw error if call cluster trows', async () => {
clusterClient.callAsInternalUser.mockRejectedValueOnce(new Error('Fail'));
await expect(
clusterClientAdapter.getIndexTemplate('foo')
).rejects.toThrowErrorMatchingInlineSnapshot(`"Fail"`);
});
});

describe('doesAliasExist', () => {
test('should call cluster with proper arguments', async () => {
await clusterClientAdapter.doesAliasExist('foo');
Expand Down Expand Up @@ -232,19 +195,3 @@ describe('createIndex', () => {
await clusterClientAdapter.createIndex('foo');
});
});

describe('rolloverIndex', () => {
test('should call cluster with given body', async () => {
await clusterClientAdapter.rolloverIndex({ args: true });
expect(clusterClient.callAsInternalUser).toHaveBeenCalledWith('indices.rollover', {
args: true,
});
});

test('should throw error when call cluster throws', async () => {
clusterClient.callAsInternalUser.mockRejectedValue(new Error('Fail'));
await expect(
clusterClientAdapter.rolloverIndex({ args: true })
).rejects.toThrowErrorMatchingInlineSnapshot(`"Fail"`);
});
});
17 changes: 0 additions & 17 deletions x-pack/plugins/event_log/server/es/cluster_client_adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,6 @@ export class ClusterClientAdapter {
}
}

public async updateIndexTemplate(name: string, template: any): Promise<void> {
const updateTemplateParams = {
name,
body: template,
};
await this.callEs('indices.putTemplate', updateTemplateParams);
}

public async getIndexTemplate(name: string): Promise<any> {
const response = await this.callEs('indices.getTemplate', { name });
return response[name];
}

public async doesAliasExist(name: string): Promise<boolean> {
let result;
try {
Expand All @@ -120,10 +107,6 @@ export class ClusterClientAdapter {
}
}

public async rolloverIndex(body: any): Promise<void> {
await this.callEs('indices.rollover', body);
}

private async callEs(operation: string, body?: any): Promise<any> {
try {
this.debug(`callEs(${operation}) calls:`, body);
Expand Down
1 change: 0 additions & 1 deletion x-pack/plugins/event_log/server/es/documents.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,5 @@ describe('getIndexTemplate()', () => {
expect(indexTemplate.settings['index.lifecycle.name']).toBe(esNames.ilmPolicy);
expect(indexTemplate.settings['index.lifecycle.rollover_alias']).toBe(esNames.alias);
expect(indexTemplate.mappings).toMatchObject({});
expect(indexTemplate.version).toBeGreaterThanOrEqual(0);
});
});
2 changes: 0 additions & 2 deletions x-pack/plugins/event_log/server/es/documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import { EsNames } from './names';
import mappings from '../../generated/mappings.json';
import { getCurrentVersionAsInteger } from '../lib/get_current_version_as_integer';

// returns the body of an index template used in an ES indices.putTemplate call
export function getIndexTemplate(esNames: EsNames) {
Expand All @@ -19,7 +18,6 @@ export function getIndexTemplate(esNames: EsNames) {
'index.lifecycle.rollover_alias': esNames.alias,
},
mappings,
version: getCurrentVersionAsInteger(),
};

return indexTemplateBody;
Expand Down
37 changes: 0 additions & 37 deletions x-pack/plugins/event_log/server/es/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
import { contextMock } from './context.mock';
import { initializeEs } from './init';

jest.mock('../lib/get_current_version_as_integer', () => ({
getCurrentVersionAsInteger: () => 1,
}));

describe('initializeEs', () => {
let esContext = contextMock.create();

Expand Down Expand Up @@ -65,37 +61,4 @@ describe('initializeEs', () => {
expect(esContext.esAdapter.doesAliasExist).toHaveBeenCalled();
expect(esContext.esAdapter.createIndex).not.toHaveBeenCalled();
});

test('should migrate index template when kibana version is more recent', async () => {
esContext.esAdapter.getIndexTemplate.mockResolvedValue({
version: 0,
});

await initializeEs(esContext);
expect(esContext.esAdapter.getIndexTemplate).toHaveBeenCalled();
expect(esContext.esAdapter.updateIndexTemplate).toHaveBeenCalled();
expect(esContext.esAdapter.rolloverIndex).toHaveBeenCalled();
});

test(`shouldn't migrate index template when kibana version is the same`, async () => {
esContext.esAdapter.getIndexTemplate.mockResolvedValue({
version: 1,
});

await initializeEs(esContext);
expect(esContext.esAdapter.getIndexTemplate).toHaveBeenCalled();
expect(esContext.esAdapter.updateIndexTemplate).not.toHaveBeenCalled();
expect(esContext.esAdapter.rolloverIndex).not.toHaveBeenCalled();
});

test('should log error if template version in Elasticsearch is more recent', async () => {
esContext.esAdapter.getIndexTemplate.mockResolvedValue({
version: 2,
});

await initializeEs(esContext);
expect(esContext.logger.error).toHaveBeenLastCalledWith(
'error initializing elasticsearch resources: Index template belongs to a more recent version of Kibana (2)'
);
});
});
27 changes: 0 additions & 27 deletions x-pack/plugins/event_log/server/es/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ async function initializeEsResources(esContext: EsContext) {
await steps.createIlmPolicyIfNotExists();
await steps.createIndexTemplateIfNotExists();
await steps.createInitialIndexIfNotExists();
await steps.migrateIndexTemplate();
}

class EsInitializationSteps {
Expand Down Expand Up @@ -72,30 +71,4 @@ class EsInitializationSteps {
});
}
}

async migrateIndexTemplate() {
const updatedTemplateBody = getIndexTemplate(this.esContext.esNames);
const existingTemplate = await this.esContext.esAdapter.getIndexTemplate(
this.esContext.esNames.indexTemplate
);

if (updatedTemplateBody.version > existingTemplate.version) {
await this.esContext.esAdapter.updateIndexTemplate(
this.esContext.esNames.indexTemplate,
updatedTemplateBody
);
await this.esContext.esAdapter.rolloverIndex({
alias: this.esContext.esNames.alias,
body: {
conditions: {
max_age: '0d',
},
},
});
} else if (updatedTemplateBody.version < existingTemplate.version) {
throw new Error(
`Index template belongs to a more recent version of Kibana (${existingTemplate.version})`
);
}
}
}
8 changes: 4 additions & 4 deletions x-pack/plugins/event_log/server/es/names.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import { EsNames } from './names';
const createNamesMock = () => {
const mock: jest.Mocked<EsNames> = {
base: '.kibana',
alias: '.kibana-event-log',
ilmPolicy: '.kibana-event-log-policy',
alias: '.kibana-event-log-8.0.0',
ilmPolicy: '.kibana-event-log-8.0.0-policy',
indexPattern: '.kibana-event-log-*',
initialIndex: '.kibana-event-log-000001',
indexTemplate: '.kibana-event-log-template',
initialIndex: '.kibana-event-log-8.0.0-000001',
indexTemplate: '.kibana-event-log-8.0.0-template',
};
return mock;
};
Expand Down
13 changes: 9 additions & 4 deletions x-pack/plugins/event_log/server/es/names.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,20 @@

import { getEsNames } from './names';

jest.mock('../lib/../../../../package.json', () => ({
version: '1.2.3',
}));

describe('getEsNames()', () => {
test('works as expected', () => {
const base = 'XYZ';
const version = '1.2.3';
const esNames = getEsNames(base);
expect(esNames.base).toEqual(base);
expect(esNames.alias).toEqual(`${base}-event-log`);
expect(esNames.ilmPolicy).toEqual(`${base}-event-log-policy`);
expect(esNames.alias).toEqual(`${base}-event-log-${version}`);
expect(esNames.ilmPolicy).toEqual(`${base}-event-log-${version}-policy`);
expect(esNames.indexPattern).toEqual(`${base}-event-log-*`);
expect(esNames.initialIndex).toEqual(`${base}-event-log-000001`);
expect(esNames.indexTemplate).toEqual(`${base}-event-log-template`);
expect(esNames.initialIndex).toEqual(`${base}-event-log-${version}-000001`);
expect(esNames.indexTemplate).toEqual(`${base}-event-log-${version}-template`);
});
});
14 changes: 9 additions & 5 deletions x-pack/plugins/event_log/server/es/names.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
* you may not use this file except in compliance with the Elastic License.
*/

const EVENT_LOG_NAME_SUFFIX = '-event-log';
import xPackage from '../../../../package.json';

const EVENT_LOG_NAME_SUFFIX = `-event-log`;
const EVENT_LOG_VERSION_SUFFIX = `-${xPackage.version}`;

export interface EsNames {
base: string;
Expand All @@ -17,12 +20,13 @@ export interface EsNames {

export function getEsNames(baseName: string): EsNames {
const eventLogName = `${baseName}${EVENT_LOG_NAME_SUFFIX}`;
const eventLogNameWithVersion = `${eventLogName}${EVENT_LOG_VERSION_SUFFIX}`;
return {
base: baseName,
alias: eventLogName,
ilmPolicy: `${eventLogName}-policy`,
alias: eventLogNameWithVersion,
ilmPolicy: `${eventLogNameWithVersion}-policy`,
indexPattern: `${eventLogName}-*`,
initialIndex: `${eventLogName}-000001`,
indexTemplate: `${eventLogName}-template`,
initialIndex: `${eventLogNameWithVersion}-000001`,
indexTemplate: `${eventLogNameWithVersion}-template`,
};
}

This file was deleted.

This file was deleted.

0 comments on commit 0d5bde1

Please sign in to comment.