Skip to content

Commit c22366e

Browse files
authored
[Fleet] Remove aliases from index_template when updating an existing template (#91142)
1 parent a42eab1 commit c22366e

2 files changed

Lines changed: 112 additions & 3 deletions

File tree

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

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ import { installTemplate } from './install';
1313

1414
test('tests installPackage to use correct priority and index_patterns for data stream with dataset_is_prefix not set', async () => {
1515
const callCluster = elasticsearchServiceMock.createLegacyScopedClusterClient().callAsCurrentUser;
16+
callCluster.mockImplementation((_, params) => {
17+
if (params.method === 'GET' && params.path === '/_index_template/metrics-package.dataset') {
18+
return { index_templates: [] };
19+
}
20+
});
21+
1622
const fields: Field[] = [];
1723
const dataStreamDatasetIsPrefixUnset = {
1824
type: 'metrics',
@@ -37,14 +43,20 @@ test('tests installPackage to use correct priority and index_patterns for data s
3743
packageName: pkg.name,
3844
});
3945
// @ts-ignore
40-
const sentTemplate = callCluster.mock.calls[0][1].body;
46+
const sentTemplate = callCluster.mock.calls[1][1].body;
4147
expect(sentTemplate).toBeDefined();
4248
expect(sentTemplate.priority).toBe(templatePriorityDatasetIsPrefixUnset);
4349
expect(sentTemplate.index_patterns).toEqual([templateIndexPatternDatasetIsPrefixUnset]);
4450
});
4551

4652
test('tests installPackage to use correct priority and index_patterns for data stream with dataset_is_prefix set to false', async () => {
4753
const callCluster = elasticsearchServiceMock.createLegacyScopedClusterClient().callAsCurrentUser;
54+
callCluster.mockImplementation((_, params) => {
55+
if (params.method === 'GET' && params.path === '/_index_template/metrics-package.dataset') {
56+
return { index_templates: [] };
57+
}
58+
});
59+
4860
const fields: Field[] = [];
4961
const dataStreamDatasetIsPrefixFalse = {
5062
type: 'metrics',
@@ -70,14 +82,20 @@ test('tests installPackage to use correct priority and index_patterns for data s
7082
packageName: pkg.name,
7183
});
7284
// @ts-ignore
73-
const sentTemplate = callCluster.mock.calls[0][1].body;
85+
const sentTemplate = callCluster.mock.calls[1][1].body;
7486
expect(sentTemplate).toBeDefined();
7587
expect(sentTemplate.priority).toBe(templatePriorityDatasetIsPrefixFalse);
7688
expect(sentTemplate.index_patterns).toEqual([templateIndexPatternDatasetIsPrefixFalse]);
7789
});
7890

7991
test('tests installPackage to use correct priority and index_patterns for data stream with dataset_is_prefix set to true', async () => {
8092
const callCluster = elasticsearchServiceMock.createLegacyScopedClusterClient().callAsCurrentUser;
93+
callCluster.mockImplementation((_, params) => {
94+
if (params.method === 'GET' && params.path === '/_index_template/metrics-package.dataset') {
95+
return { index_templates: [] };
96+
}
97+
});
98+
8199
const fields: Field[] = [];
82100
const dataStreamDatasetIsPrefixTrue = {
83101
type: 'metrics',
@@ -103,8 +121,60 @@ test('tests installPackage to use correct priority and index_patterns for data s
103121
packageName: pkg.name,
104122
});
105123
// @ts-ignore
106-
const sentTemplate = callCluster.mock.calls[0][1].body;
124+
const sentTemplate = callCluster.mock.calls[1][1].body;
107125
expect(sentTemplate).toBeDefined();
108126
expect(sentTemplate.priority).toBe(templatePriorityDatasetIsPrefixTrue);
109127
expect(sentTemplate.index_patterns).toEqual([templateIndexPatternDatasetIsPrefixTrue]);
110128
});
129+
130+
test('tests installPackage remove the aliases property if the property existed', async () => {
131+
const callCluster = elasticsearchServiceMock.createLegacyScopedClusterClient().callAsCurrentUser;
132+
callCluster.mockImplementation((_, params) => {
133+
if (params.method === 'GET' && params.path === '/_index_template/metrics-package.dataset') {
134+
return {
135+
index_templates: [
136+
{
137+
name: 'metrics-package.dataset',
138+
index_template: {
139+
index_patterns: ['metrics-package.dataset-*'],
140+
template: { aliases: {} },
141+
},
142+
},
143+
],
144+
};
145+
}
146+
});
147+
148+
const fields: Field[] = [];
149+
const dataStreamDatasetIsPrefixUnset = {
150+
type: 'metrics',
151+
dataset: 'package.dataset',
152+
title: 'test data stream',
153+
release: 'experimental',
154+
package: 'package',
155+
path: 'path',
156+
ingest_pipeline: 'default',
157+
} as RegistryDataStream;
158+
const pkg = {
159+
name: 'package',
160+
version: '0.0.1',
161+
};
162+
const templateIndexPatternDatasetIsPrefixUnset = 'metrics-package.dataset-*';
163+
const templatePriorityDatasetIsPrefixUnset = 200;
164+
await installTemplate({
165+
callCluster,
166+
fields,
167+
dataStream: dataStreamDatasetIsPrefixUnset,
168+
packageVersion: pkg.version,
169+
packageName: pkg.name,
170+
});
171+
172+
// @ts-ignore
173+
const removeAliases = callCluster.mock.calls[1][1].body;
174+
expect(removeAliases.template.aliases).not.toBeDefined();
175+
// @ts-ignore
176+
const sentTemplate = callCluster.mock.calls[2][1].body;
177+
expect(sentTemplate).toBeDefined();
178+
expect(sentTemplate.priority).toBe(templatePriorityDatasetIsPrefixUnset);
179+
expect(sentTemplate.index_patterns).toEqual([templateIndexPatternDatasetIsPrefixUnset]);
180+
});

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
@@ -311,6 +311,45 @@ export async function installTemplate({
311311
});
312312
}
313313

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

0 commit comments

Comments
 (0)