Skip to content

Commit

Permalink
[Ingest Manager] add _meta field to index templates (#70319)
Browse files Browse the repository at this point in the history
* add _meta field to index templates

* fix typescript issue

* make package an object

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
  • Loading branch information
neptunian and elasticmachine authored Jul 1, 2020
1 parent 2212beb commit 275fb97
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 14 deletions.
1 change: 1 addition & 0 deletions x-pack/plugins/ingest_manager/common/types/models/epm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ export interface IndexTemplate {
data_stream: {
timestamp_field: string;
};
_meta: object;
}

export interface TemplateRef {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ export async function installTemplateForDataset({
fields,
dataset,
packageVersion: pkg.version,
packageName: pkg.name,
});
}

Expand All @@ -161,11 +162,13 @@ export async function installTemplate({
fields,
dataset,
packageVersion,
packageName,
}: {
callCluster: CallESAsCurrentUser;
fields: Field[];
dataset: Dataset;
packageVersion: string;
packageName: string;
}): Promise<TemplateRef> {
const mappings = generateMappings(processFields(fields));
const templateName = generateTemplateName(dataset);
Expand All @@ -177,7 +180,13 @@ export async function installTemplate({
packageVersion,
});
}
const template = getTemplate(dataset.type, templateName, mappings, pipelineName);
const template = getTemplate({
type: dataset.type,
templateName,
mappings,
pipelineName,
packageName,
});
// TODO: Check return values for errors
const callClusterParams: {
method: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ expect.addSnapshotSerializer({
test('get template', () => {
const templateName = 'logs-nginx-access-abcd';

const template = getTemplate('logs', templateName, { properties: {} });
const template = getTemplate({
type: 'logs',
templateName,
packageName: 'nginx',
mappings: { properties: {} },
});
expect(template.index_patterns).toStrictEqual([`${templateName}-*`]);
});

Expand All @@ -35,7 +40,12 @@ test('tests loading base.yml', () => {

const processedFields = processFields(fields);
const mappings = generateMappings(processedFields);
const template = getTemplate('logs', 'foo', mappings);
const template = getTemplate({
type: 'logs',
templateName: 'foo',
packageName: 'nginx',
mappings,
});

expect(template).toMatchSnapshot(path.basename(ymlPath));
});
Expand All @@ -47,7 +57,12 @@ test('tests loading coredns.logs.yml', () => {

const processedFields = processFields(fields);
const mappings = generateMappings(processedFields);
const template = getTemplate('logs', 'foo', mappings);
const template = getTemplate({
type: 'logs',
templateName: 'foo',
packageName: 'coredns',
mappings,
});

expect(template).toMatchSnapshot(path.basename(ymlPath));
});
Expand All @@ -59,7 +74,12 @@ test('tests loading system.yml', () => {

const processedFields = processFields(fields);
const mappings = generateMappings(processedFields);
const template = getTemplate('metrics', 'whatsthis', mappings);
const template = getTemplate({
type: 'metrics',
templateName: 'whatsthis',
packageName: 'system',
mappings,
});

expect(template).toMatchSnapshot(path.basename(ymlPath));
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,20 @@ const DEFAULT_IGNORE_ABOVE = 1024;
*
* @param indexPattern String with the index pattern
*/
export function getTemplate(
type: string,
templateName: string,
mappings: IndexTemplateMappings,
pipelineName?: string | undefined
): IndexTemplate {
const template = getBaseTemplate(type, templateName, mappings);
export function getTemplate({
type,
templateName,
mappings,
pipelineName,
packageName,
}: {
type: string;
templateName: string;
mappings: IndexTemplateMappings;
pipelineName?: string | undefined;
packageName: string;
}): IndexTemplate {
const template = getBaseTemplate(type, templateName, mappings, packageName);
if (pipelineName) {
template.template.settings.index.default_pipeline = pipelineName;
}
Expand Down Expand Up @@ -236,7 +243,8 @@ export function generateESIndexPatterns(datasets: Dataset[] | undefined): Record
function getBaseTemplate(
type: string,
templateName: string,
mappings: IndexTemplateMappings
mappings: IndexTemplateMappings,
packageName: string
): IndexTemplate {
return {
// This takes precedence over all index templates installed with the 'base' package
Expand Down Expand Up @@ -297,6 +305,12 @@ function getBaseTemplate(
data_stream: {
timestamp_field: '@timestamp',
},
_meta: {
package: {
name: packageName,
},
managed_by: 'ingest-manager',
},
};
}

Expand Down
7 changes: 6 additions & 1 deletion x-pack/test/ingest_manager_api_integration/apis/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ export default function ({ getService }: FtrProviderContext) {
// This test was inspired by https://github.com/elastic/kibana/blob/master/x-pack/test/api_integration/apis/monitoring/common/mappings_exist.js
describe('template', async () => {
it('can be loaded', async () => {
const template = getTemplate('logs', indexPattern, mappings);
const template = getTemplate({
type: 'logs',
templateName,
mappings,
packageName: 'system',
});

// This test is not an API integration test with Kibana
// We want to test here if the template is valid and for this we need a running ES instance.
Expand Down

0 comments on commit 275fb97

Please sign in to comment.