Skip to content

Commit

Permalink
[EPM] Add basics for creating the ILM setup (#50474)
Browse files Browse the repository at this point in the history
This contains the basic objects to setup ILM

* Create index and alias with a write index
* Get the policy

The code does not contain any functional tests yet as it is still open on how to do it best. I suggest to get this in as a foundation and then iterate on top of it.
  • Loading branch information
ruflin authored Nov 28, 2019
1 parent ef9bc47 commit 74d984d
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 6 deletions.
15 changes: 15 additions & 0 deletions x-pack/legacy/plugins/epm/server/lib/ilm/ilm.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { getIndexWithWithAlias } from './ilm';

test('get index with alias', () => {
const aliasName = 'bar';

const data = getIndexWithWithAlias(aliasName);
// Verifies that the bar key exists and write index is set to true
expect(data.aliases.bar.is_write_index).toStrictEqual(true);
});
38 changes: 38 additions & 0 deletions x-pack/legacy/plugins/epm/server/lib/ilm/ilm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

export function getIndexWithWithAlias(aliasName: string) {
return {
aliases: {
[aliasName]: {
is_write_index: true,
},
},
};
}

/**
* Returns the current default policy used for Beats.
* This will later be replaced by the default policies.
*
* This policy will have to be pushed to PUT /_ilm/policy/{policy-name}
*/
export function getPolicy() {
return {
policy: {
phases: {
hot: {
actions: {
rollover: {
max_size: '50gb',
max_age: '30d',
},
},
},
},
},
};
}
64 changes: 64 additions & 0 deletions x-pack/test/epm_api_integration/apis/ilm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import expect from '@kbn/expect';
import { FtrProviderContext } from '../../api_integration/ftr_provider_context';
import { getPolicy, getIndexWithWithAlias } from '../../../legacy/plugins/epm/server/lib/ilm/ilm';

export default function({ getService }: FtrProviderContext) {
describe('ilm', () => {
it('setup policy', async () => {
const policyName = 'foo';
const es = getService('es');
const policy = getPolicy();

const data = await es.transport.request({
method: 'PUT',
path: '/_ilm/policy/' + policyName,
body: policy,
});

expect(data.body.acknowledged).to.eql(true);
expect(data.statusCode).to.eql(200);
});

it('setup index with alias', async () => {
const indexName = 'test-index-with-alias';
const aliasName = 'alias-to-index';
const es = getService('es');

// Delete index first if it exists as otherwise we get an error
const existsBody = await es.indices.exists({ index: indexName });
if (existsBody.statusCode === 200) {
const response = await es.indices.delete({ index: indexName });

// Sanity check to make sure removal work as expected
// If it didn't we already know where the problem lays in the test
expect(response.statusCode).to.eql(200);
}

// Calls the given esClient, creates and index and sets it as write index on the given alias.
//
// This should be moved later to the ilm lib but have it here for now as passing the client
// does not work.
const body = getIndexWithWithAlias(aliasName);
const data = await es.indices.create({
index: indexName,
body,
});

// Sanity checks to make sure ES confirmed the data we sent is sane
// and the index with the alias was created.
expect(data.body.acknowledged).to.eql(true);
expect(data.statusCode).to.eql(200);

// Retreiving the index information again to see if the is_write_index
// is set correctly for the alias.
const indexData = await es.indices.get({ index: indexName });
expect(indexData.body[indexName].aliases[aliasName].is_write_index).to.eql(true);
});
});
}
1 change: 1 addition & 0 deletions x-pack/test/epm_api_integration/apis/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ export default function ({ loadTestFile }) {
loadTestFile(require.resolve('./list'));
loadTestFile(require.resolve('./file'));
loadTestFile(require.resolve('./template'));
loadTestFile(require.resolve('./ilm'));
});
}
20 changes: 14 additions & 6 deletions x-pack/test/epm_api_integration/apis/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,23 @@ export default function({ getService }: FtrProviderContext) {
// This test was inspired by https://github.com/elastic/kibana/blob/master/x-pack/test/api_integration/apis/monitoring/common/mappings_exist.js
describe('template', async () => {
it('can be loaded', async () => {
const body = getTemplate(indexPattern);
const template = getTemplate(indexPattern);

const { body: response } = await es.indices.putTemplate({
// This test is not an API integration test with Kibana
// We want to test here if the template is valid and for this we need a running ES instance.
// If the ES instance takes the template, we assume it is a valid template.
const { body: response1 } = await es.indices.putTemplate({
name: templateName,
body,
body: template,
});
expect(response).to.eql({ acknowledged: true });
const { body: indexTemplate } = await es.indices.getTemplate({ name: templateName });
expect(indexTemplate[templateName].index_patterns).to.eql([indexPattern]);
// Checks if template loading worked as expected
expect(response1).to.eql({ acknowledged: true });

const { body: response2 } = await es.indices.getTemplate({ name: templateName });
// Checks if the content of the template that was loaded is as expected
// We already know based on the above test that the template was valid
// but we check here also if we wrote the index pattern inside the template as expected
expect(response2[templateName].index_patterns).to.eql([indexPattern]);
});
});
}

0 comments on commit 74d984d

Please sign in to comment.