Skip to content

Commit

Permalink
[Fleet] Remove aliases from index_template when updating an existing …
Browse files Browse the repository at this point in the history
…template (#91200) (#91221)
  • Loading branch information
nchaulet authored Feb 11, 2021
1 parent 6643748 commit 348233f
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { RegistryDataStream } from '../../../../types';
import { Field } from '../../fields/field';

import { elasticsearchServiceMock } from 'src/core/server/mocks';
import { installTemplate } from './install';

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]);
});
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,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,
Expand Down

0 comments on commit 348233f

Please sign in to comment.