From a95fa8990f1e1866ad9aa24bdb40fa5c2d309ab8 Mon Sep 17 00:00:00 2001 From: Nicolas Chaulet Date: Mon, 5 Dec 2022 11:06:42 -0500 Subject: [PATCH] [Fleet] Honor source_mode defined in a package datastream when installing it --- .../elasticsearch/template/install.test.ts | 34 +++++++++++++++++++ .../epm/elasticsearch/template/install.ts | 12 ++++++- 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/x-pack/plugins/fleet/server/services/epm/elasticsearch/template/install.test.ts b/x-pack/plugins/fleet/server/services/epm/elasticsearch/template/install.test.ts index 3478da69bf721..d8cc0e93c9ea0 100644 --- a/x-pack/plugins/fleet/server/services/epm/elasticsearch/template/install.test.ts +++ b/x-pack/plugins/fleet/server/services/epm/elasticsearch/template/install.test.ts @@ -88,4 +88,38 @@ describe('EPM index template install', () => { expect(indexTemplate.priority).toBe(templatePriorityDatasetIsPrefixTrue); expect(indexTemplate.index_patterns).toEqual([templateIndexPatternDatasetIsPrefixTrue]); }); + + it('tests prepareTemplate to set source mode to synthetics if specified', async () => { + const dataStreamDatasetIsPrefixTrue = { + type: 'metrics', + dataset: 'package.dataset', + title: 'test data stream', + release: 'experimental', + package: 'package', + path: 'path', + ingest_pipeline: 'default', + dataset_is_prefix: true, + elasticsearch: { + source_mode: 'synthetic', + }, + } as RegistryDataStream; + const pkg = { + name: 'package', + version: '0.0.1', + }; + + const { componentTemplates } = prepareTemplate({ + pkg, + dataStream: dataStreamDatasetIsPrefixTrue, + }); + + const packageTemplate = componentTemplates['metrics-package.dataset@package'].template; + + if (!('mappings' in packageTemplate)) { + throw new Error('no mappings on package template'); + } + + expect(packageTemplate.mappings).toHaveProperty('_source'); + expect(packageTemplate.mappings._source).toEqual({ mode: 'synthetic' }); + }); }); diff --git a/x-pack/plugins/fleet/server/services/epm/elasticsearch/template/install.ts b/x-pack/plugins/fleet/server/services/epm/elasticsearch/template/install.ts index ca03d272405f4..5d30433e80d4b 100644 --- a/x-pack/plugins/fleet/server/services/epm/elasticsearch/template/install.ts +++ b/x-pack/plugins/fleet/server/services/epm/elasticsearch/template/install.ts @@ -290,7 +290,17 @@ export function buildComponentTemplates(params: { mappings: { properties: mappingsProperties, dynamic_templates: mappingsDynamicTemplates.length ? mappingsDynamicTemplates : undefined, - ...omit(indexTemplateMappings, 'properties', 'dynamic_templates'), + ...omit(indexTemplateMappings, 'properties', 'dynamic_templates', '_source'), + ...(indexTemplateMappings?._source || registryElasticsearch?.source_mode + ? { + _source: { + ...indexTemplateMappings?._source, + ...(registryElasticsearch?.source_mode === 'synthetic' + ? { mode: 'synthetic' } + : {}), + }, + } + : {}), }, }, _meta,