[Fleet] Remove aliases from index_template when updating an existing template (#91200) (#91220)

This commit is contained in:
Nicolas Chaulet 2021-02-11 18:40:26 -05:00 committed by GitHub
parent 2f1dcdbdf0
commit ac8f5bc23b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 128 additions and 3 deletions

View file

@ -13,6 +13,16 @@ import { installTemplate } from './install';
test('tests installPackage to use correct priority and index_patterns for data stream with dataset_is_prefix not set', async () => {
const callCluster = elasticsearchServiceMock.createLegacyScopedClusterClient().callAsCurrentUser;
callCluster.mockImplementation(async (_, params) => {
if (
params &&
params.method === 'GET' &&
params.path === '/_index_template/metrics-package.dataset'
) {
return { index_templates: [] };
}
});
const fields: Field[] = [];
const dataStreamDatasetIsPrefixUnset = {
type: 'metrics',
@ -37,7 +47,7 @@ test('tests installPackage to use correct priority and index_patterns for data s
packageName: pkg.name,
});
// @ts-ignore
const sentTemplate = callCluster.mock.calls[0][1].body;
const sentTemplate = callCluster.mock.calls[1][1].body;
expect(sentTemplate).toBeDefined();
expect(sentTemplate.priority).toBe(templatePriorityDatasetIsPrefixUnset);
expect(sentTemplate.index_patterns).toEqual([templateIndexPatternDatasetIsPrefixUnset]);
@ -45,6 +55,16 @@ test('tests installPackage to use correct priority and index_patterns for data s
test('tests installPackage to use correct priority and index_patterns for data stream with dataset_is_prefix set to false', async () => {
const callCluster = elasticsearchServiceMock.createLegacyScopedClusterClient().callAsCurrentUser;
callCluster.mockImplementation(async (_, params) => {
if (
params &&
params.method === 'GET' &&
params.path === '/_index_template/metrics-package.dataset'
) {
return { index_templates: [] };
}
});
const fields: Field[] = [];
const dataStreamDatasetIsPrefixFalse = {
type: 'metrics',
@ -70,7 +90,7 @@ test('tests installPackage to use correct priority and index_patterns for data s
packageName: pkg.name,
});
// @ts-ignore
const sentTemplate = callCluster.mock.calls[0][1].body;
const sentTemplate = callCluster.mock.calls[1][1].body;
expect(sentTemplate).toBeDefined();
expect(sentTemplate.priority).toBe(templatePriorityDatasetIsPrefixFalse);
expect(sentTemplate.index_patterns).toEqual([templateIndexPatternDatasetIsPrefixFalse]);
@ -78,6 +98,16 @@ test('tests installPackage to use correct priority and index_patterns for data s
test('tests installPackage to use correct priority and index_patterns for data stream with dataset_is_prefix set to true', async () => {
const callCluster = elasticsearchServiceMock.createLegacyScopedClusterClient().callAsCurrentUser;
callCluster.mockImplementation(async (_, params) => {
if (
params &&
params.method === 'GET' &&
params.path === '/_index_template/metrics-package.dataset'
) {
return { index_templates: [] };
}
});
const fields: Field[] = [];
const dataStreamDatasetIsPrefixTrue = {
type: 'metrics',
@ -103,8 +133,64 @@ test('tests installPackage to use correct priority and index_patterns for data s
packageName: pkg.name,
});
// @ts-ignore
const sentTemplate = callCluster.mock.calls[0][1].body;
const sentTemplate = callCluster.mock.calls[1][1].body;
expect(sentTemplate).toBeDefined();
expect(sentTemplate.priority).toBe(templatePriorityDatasetIsPrefixTrue);
expect(sentTemplate.index_patterns).toEqual([templateIndexPatternDatasetIsPrefixTrue]);
});
test('tests installPackage remove the aliases property if the property existed', async () => {
const callCluster = elasticsearchServiceMock.createLegacyScopedClusterClient().callAsCurrentUser;
callCluster.mockImplementation(async (_, params) => {
if (
params &&
params.method === 'GET' &&
params.path === '/_index_template/metrics-package.dataset'
) {
return {
index_templates: [
{
name: 'metrics-package.dataset',
index_template: {
index_patterns: ['metrics-package.dataset-*'],
template: { aliases: {} },
},
},
],
};
}
});
const fields: Field[] = [];
const dataStreamDatasetIsPrefixUnset = {
type: 'metrics',
dataset: 'package.dataset',
title: 'test data stream',
release: 'experimental',
package: 'package',
path: 'path',
ingest_pipeline: 'default',
} as RegistryDataStream;
const pkg = {
name: 'package',
version: '0.0.1',
};
const templateIndexPatternDatasetIsPrefixUnset = 'metrics-package.dataset-*';
const templatePriorityDatasetIsPrefixUnset = 200;
await installTemplate({
callCluster,
fields,
dataStream: dataStreamDatasetIsPrefixUnset,
packageVersion: pkg.version,
packageName: pkg.name,
});
// @ts-ignore
const removeAliases = callCluster.mock.calls[1][1].body;
expect(removeAliases.template.aliases).not.toBeDefined();
// @ts-ignore
const sentTemplate = callCluster.mock.calls[2][1].body;
expect(sentTemplate).toBeDefined();
expect(sentTemplate.priority).toBe(templatePriorityDatasetIsPrefixUnset);
expect(sentTemplate.index_patterns).toEqual([templateIndexPatternDatasetIsPrefixUnset]);
});

View file

@ -311,6 +311,45 @@ export async function installTemplate({
});
}
// Datastream now throw an error if the aliases field is present so ensure that we remove that field.
const getTemplateRes = await callCluster('transport.request', {
method: 'GET',
path: `/_index_template/${templateName}`,
ignore: [404],
});
const existingIndexTemplate = getTemplateRes?.index_templates?.[0];
if (
existingIndexTemplate &&
existingIndexTemplate.name === templateName &&
existingIndexTemplate?.index_template?.template?.aliases
) {
const updateIndexTemplateParams: {
method: string;
path: string;
ignore: number[];
body: any;
} = {
method: 'PUT',
path: `/_index_template/${templateName}`,
ignore: [404],
body: {
...existingIndexTemplate.index_template,
template: {
...existingIndexTemplate.index_template.template,
// Remove the aliases field
aliases: undefined,
},
},
};
// This uses the catch-all endpoint 'transport.request' because there is no
// convenience endpoint using the new _index_template API yet.
// The existing convenience endpoint `indices.putTemplate` only sends to _template,
// which does not support v2 templates.
// See src/core/server/elasticsearch/api_types.ts for available endpoints.
await callCluster('transport.request', updateIndexTemplateParams);
}
const composedOfTemplates = await installDataStreamComponentTemplates(
templateName,
dataStream.elasticsearch,