Skip to content

Commit 348233f

Browse files
authored
[Fleet] Remove aliases from index_template when updating an existing template (#91200) (#91221)
1 parent 6643748 commit 348233f

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
import { RegistryDataStream } from '../../../../types';
9+
import { Field } from '../../fields/field';
10+
11+
import { elasticsearchServiceMock } from 'src/core/server/mocks';
12+
import { installTemplate } from './install';
13+
14+
test('tests installPackage remove the aliases property if the property existed', async () => {
15+
const callCluster = elasticsearchServiceMock.createLegacyScopedClusterClient().callAsCurrentUser;
16+
callCluster.mockImplementation(async (_, params) => {
17+
if (
18+
params &&
19+
params.method === 'GET' &&
20+
params.path === '/_index_template/metrics-package.dataset'
21+
) {
22+
return {
23+
index_templates: [
24+
{
25+
name: 'metrics-package.dataset',
26+
index_template: {
27+
index_patterns: ['metrics-package.dataset-*'],
28+
template: { aliases: {} },
29+
},
30+
},
31+
],
32+
};
33+
}
34+
});
35+
36+
const fields: Field[] = [];
37+
const dataStreamDatasetIsPrefixUnset = {
38+
type: 'metrics',
39+
dataset: 'package.dataset',
40+
title: 'test data stream',
41+
release: 'experimental',
42+
package: 'package',
43+
path: 'path',
44+
ingest_pipeline: 'default',
45+
} as RegistryDataStream;
46+
const pkg = {
47+
name: 'package',
48+
version: '0.0.1',
49+
};
50+
const templateIndexPatternDatasetIsPrefixUnset = 'metrics-package.dataset-*';
51+
const templatePriorityDatasetIsPrefixUnset = 200;
52+
await installTemplate({
53+
callCluster,
54+
fields,
55+
dataStream: dataStreamDatasetIsPrefixUnset,
56+
packageVersion: pkg.version,
57+
packageName: pkg.name,
58+
});
59+
60+
// @ts-ignore
61+
const removeAliases = callCluster.mock.calls[1][1].body;
62+
expect(removeAliases.template.aliases).not.toBeDefined();
63+
// @ts-ignore
64+
const sentTemplate = callCluster.mock.calls[2][1].body;
65+
expect(sentTemplate).toBeDefined();
66+
expect(sentTemplate.priority).toBe(templatePriorityDatasetIsPrefixUnset);
67+
expect(sentTemplate.index_patterns).toEqual([templateIndexPatternDatasetIsPrefixUnset]);
68+
});

x-pack/plugins/fleet/server/services/epm/elasticsearch/template/install.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,45 @@ export async function installTemplate({
302302
});
303303
}
304304

305+
// Datastream now throw an error if the aliases field is present so ensure that we remove that field.
306+
const getTemplateRes = await callCluster('transport.request', {
307+
method: 'GET',
308+
path: `/_index_template/${templateName}`,
309+
ignore: [404],
310+
});
311+
312+
const existingIndexTemplate = getTemplateRes?.index_templates?.[0];
313+
if (
314+
existingIndexTemplate &&
315+
existingIndexTemplate.name === templateName &&
316+
existingIndexTemplate?.index_template?.template?.aliases
317+
) {
318+
const updateIndexTemplateParams: {
319+
method: string;
320+
path: string;
321+
ignore: number[];
322+
body: any;
323+
} = {
324+
method: 'PUT',
325+
path: `/_index_template/${templateName}`,
326+
ignore: [404],
327+
body: {
328+
...existingIndexTemplate.index_template,
329+
template: {
330+
...existingIndexTemplate.index_template.template,
331+
// Remove the aliases field
332+
aliases: undefined,
333+
},
334+
},
335+
};
336+
// This uses the catch-all endpoint 'transport.request' because there is no
337+
// convenience endpoint using the new _index_template API yet.
338+
// The existing convenience endpoint `indices.putTemplate` only sends to _template,
339+
// which does not support v2 templates.
340+
// See src/core/server/elasticsearch/api_types.ts for available endpoints.
341+
await callCluster('transport.request', updateIndexTemplateParams);
342+
}
343+
305344
const composedOfTemplates = await installDataStreamComponentTemplates(
306345
templateName,
307346
dataStream.elasticsearch,

0 commit comments

Comments
 (0)