-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Co-authored-by: Elastic Machine <[email protected]>
- Loading branch information
1 parent
31292ed
commit 97f0353
Showing
37 changed files
with
1,436 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
x-pack/plugins/index_management/common/lib/component_template_serialization.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { deserializeComponentTemplate } from './component_template_serialization'; | ||
|
||
describe('deserializeComponentTemplate', () => { | ||
test('deserializes a component template', () => { | ||
expect( | ||
deserializeComponentTemplate( | ||
{ | ||
name: 'my_component_template', | ||
component_template: { | ||
version: 1, | ||
_meta: { | ||
serialization: { | ||
id: 10, | ||
class: 'MyComponentTemplate', | ||
}, | ||
description: 'set number of shards to one', | ||
}, | ||
template: { | ||
settings: { | ||
number_of_shards: 1, | ||
}, | ||
mappings: { | ||
_source: { | ||
enabled: false, | ||
}, | ||
properties: { | ||
host_name: { | ||
type: 'keyword', | ||
}, | ||
created_at: { | ||
type: 'date', | ||
format: 'EEE MMM dd HH:mm:ss Z yyyy', | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
[ | ||
{ | ||
name: 'my_index_template', | ||
index_template: { | ||
index_patterns: ['foo'], | ||
template: { | ||
settings: { | ||
number_of_replicas: 2, | ||
}, | ||
}, | ||
composed_of: ['my_component_template'], | ||
}, | ||
}, | ||
] | ||
) | ||
).toEqual({ | ||
name: 'my_component_template', | ||
version: 1, | ||
_meta: { | ||
serialization: { | ||
id: 10, | ||
class: 'MyComponentTemplate', | ||
}, | ||
description: 'set number of shards to one', | ||
}, | ||
template: { | ||
settings: { | ||
number_of_shards: 1, | ||
}, | ||
mappings: { | ||
_source: { | ||
enabled: false, | ||
}, | ||
properties: { | ||
host_name: { | ||
type: 'keyword', | ||
}, | ||
created_at: { | ||
type: 'date', | ||
format: 'EEE MMM dd HH:mm:ss Z yyyy', | ||
}, | ||
}, | ||
}, | ||
}, | ||
_kbnMeta: { | ||
usedBy: ['my_index_template'], | ||
}, | ||
}); | ||
}); | ||
}); |
86 changes: 86 additions & 0 deletions
86
x-pack/plugins/index_management/common/lib/component_template_serialization.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
import { | ||
TemplateFromEs, | ||
ComponentTemplateFromEs, | ||
ComponentTemplateDeserialized, | ||
ComponentTemplateListItem, | ||
} from '../types'; | ||
|
||
const hasEntries = (data: object = {}) => Object.entries(data).length > 0; | ||
|
||
/** | ||
* Normalize a list of component templates to a map where each key | ||
* is a component template name, and the value is an array of index templates name using it | ||
* | ||
* @example | ||
* | ||
{ | ||
"comp-1": [ | ||
"template-1", | ||
"template-2" | ||
], | ||
"comp2": [ | ||
"template-1", | ||
"template-2" | ||
] | ||
} | ||
* | ||
* @param indexTemplatesEs List of component templates | ||
*/ | ||
|
||
const getIndexTemplatesToUsedBy = (indexTemplatesEs: TemplateFromEs[]) => { | ||
return indexTemplatesEs.reduce((acc, item) => { | ||
if (item.index_template.composed_of) { | ||
item.index_template.composed_of.forEach((component) => { | ||
acc[component] = acc[component] ? [...acc[component], item.name] : [item.name]; | ||
}); | ||
} | ||
return acc; | ||
}, {} as { [key: string]: string[] }); | ||
}; | ||
|
||
export function deserializeComponentTemplate( | ||
componentTemplateEs: ComponentTemplateFromEs, | ||
indexTemplatesEs: TemplateFromEs[] | ||
) { | ||
const { name, component_template: componentTemplate } = componentTemplateEs; | ||
const { template, _meta, version } = componentTemplate; | ||
|
||
const indexTemplatesToUsedBy = getIndexTemplatesToUsedBy(indexTemplatesEs); | ||
|
||
const deserializedComponentTemplate: ComponentTemplateDeserialized = { | ||
name, | ||
template, | ||
version, | ||
_meta, | ||
_kbnMeta: { | ||
usedBy: indexTemplatesToUsedBy[name] || [], | ||
}, | ||
}; | ||
|
||
return deserializedComponentTemplate; | ||
} | ||
|
||
export function deserializeComponenTemplateList( | ||
componentTemplateEs: ComponentTemplateFromEs, | ||
indexTemplatesEs: TemplateFromEs[] | ||
) { | ||
const { name, component_template: componentTemplate } = componentTemplateEs; | ||
const { template } = componentTemplate; | ||
|
||
const indexTemplatesToUsedBy = getIndexTemplatesToUsedBy(indexTemplatesEs); | ||
|
||
const componentTemplateListItem: ComponentTemplateListItem = { | ||
name, | ||
usedBy: indexTemplatesToUsedBy[name] || [], | ||
hasSettings: hasEntries(template.settings), | ||
hasMappings: hasEntries(template.mappings), | ||
hasAliases: hasEntries(template.aliases), | ||
}; | ||
|
||
return componentTemplateListItem; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
x-pack/plugins/index_management/common/types/component_templates.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { IndexSettings } from './indices'; | ||
import { Aliases } from './aliases'; | ||
import { Mappings } from './mappings'; | ||
|
||
export interface ComponentTemplateSerialized { | ||
template: { | ||
settings?: IndexSettings; | ||
aliases?: Aliases; | ||
mappings?: Mappings; | ||
}; | ||
version?: number; | ||
_meta?: { [key: string]: any }; | ||
} | ||
|
||
export interface ComponentTemplateDeserialized extends ComponentTemplateSerialized { | ||
name: string; | ||
_kbnMeta: { | ||
usedBy: string[]; | ||
}; | ||
} | ||
|
||
export interface ComponentTemplateFromEs { | ||
name: string; | ||
component_template: ComponentTemplateSerialized; | ||
} | ||
|
||
export interface ComponentTemplateListItem { | ||
name: string; | ||
usedBy: string[]; | ||
hasMappings: boolean; | ||
hasAliases: boolean; | ||
hasSettings: boolean; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.