Skip to content

Commit

Permalink
Add option for custom template and mapping settings (#856)
Browse files Browse the repository at this point in the history
  • Loading branch information
marshallmain authored May 21, 2020
1 parent dc199a5 commit 27fed93
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ Thanks, you're awesome :-) -->
* Add full path names to reused fieldsets in `nestings` array in `ecs_nested.yml`. #803
* Allow shorthand notation for including all subfields in subsets. #805
* Add `ref` option to generator allowing schemas to be built for a specific ECS version. #851
* Add `template-settings` and `mapping-settings` options to allow override of defaults in generated ES templates. #856

#### Deprecated

Expand Down
6 changes: 5 additions & 1 deletion scripts/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def main():
exit()

csv_generator.generate(flat, ecs_version, out_dir)
es_template.generate(flat, ecs_version, out_dir)
es_template.generate(flat, ecs_version, out_dir, args.template_settings, args.mapping_settings)
beats.generate(nested, ecs_version, out_dir)
if args.include or args.subset:
exit()
Expand All @@ -90,6 +90,10 @@ def argument_parser():
help='render a subset of the schema')
parser.add_argument('--out', action='store', help='directory to store the generated files')
parser.add_argument('--ref', action='store', help='git reference to use when building schemas')
parser.add_argument('--template-settings', action='store',
help='index template settings to use when generating elasticsearch template')
parser.add_argument('--mapping-settings', action='store',
help='mapping settings to use when generating elasticsearch template')
return parser.parse_args()


Expand Down
26 changes: 18 additions & 8 deletions scripts/generators/es_template.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
import json
import sys
import copy

from os.path import join
from generators import ecs_helpers


def generate(ecs_flat, ecs_version, out_dir):
def generate(ecs_flat, ecs_version, out_dir, template_settings_file, mapping_settings_file):
field_mappings = {}
for flat_name in sorted(ecs_flat):
field = ecs_flat[flat_name]
nestings = flat_name.split('.')
dict_add_nested(field_mappings, nestings, entry_for(field))

mappings_section = mapping_settings(ecs_version)
if mapping_settings_file:
with open(mapping_settings_file) as f:
mappings_section = json.load(f)
else:
mappings_section = default_mapping_settings(ecs_version)

mappings_section['properties'] = field_mappings

generate_template_version(6, mappings_section, out_dir)
generate_template_version(7, mappings_section, out_dir)
generate_template_version(6, mappings_section, out_dir, template_settings_file)
generate_template_version(7, mappings_section, out_dir, template_settings_file)

# Field mappings

Expand Down Expand Up @@ -66,9 +72,13 @@ def entry_for(field):
# Generated files


def generate_template_version(elasticsearch_version, mappings_section, out_dir):
def generate_template_version(elasticsearch_version, mappings_section, out_dir, template_settings_file):
ecs_helpers.make_dirs(join(out_dir, 'elasticsearch', str(elasticsearch_version)))
template = template_settings()
if template_settings_file:
with open(template_settings_file) as f:
template = json.load(f)
else:
template = default_template_settings()
if elasticsearch_version == 6:
template['mappings'] = {'_doc': mappings_section}
else:
Expand All @@ -86,7 +96,7 @@ def save_json(file, data):
jsonfile.write(json.dumps(data, indent=2, sort_keys=True))


def template_settings():
def default_template_settings():
return {
"index_patterns": ["ecs-*"],
"order": 1,
Expand All @@ -104,7 +114,7 @@ def template_settings():
}


def mapping_settings(ecs_version):
def default_mapping_settings(ecs_version):
return {
"_meta": {"version": ecs_version},
"date_detection": False,
Expand Down

0 comments on commit 27fed93

Please sign in to comment.