Skip to content

Commit

Permalink
Reorganize the code specific to each approach...
Browse files Browse the repository at this point in the history
And also fix those unacceptable '###' comments :eyeroll:
  • Loading branch information
Mathieu Martin committed Nov 26, 2020
1 parent 6003841 commit 9ca0276
Showing 1 changed file with 29 additions and 24 deletions.
53 changes: 29 additions & 24 deletions scripts/generators/es_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@
from generators import ecs_helpers


### Composable Template
# Composable Template

def generate(ecs_nested, ecs_version, out_dir, template_settings_file, mapping_settings_file):
"""This generates all artifacts for the composable template approach"""
all_component_templates(ecs_nested, ecs_version, out_dir)

# Main template

# Component templates
def composable_template(ecs_version, out_dir, template_settings_file, mapping_settings_file):
"""Generate the master sample composable template"""


def all_component_templates(ecs_nested, ecs_version, out_dir):
"""Generate one component template per field set"""
component_dir = join(out_dir, 'elasticsearch/component')
ecs_helpers.make_dirs(component_dir)

Expand All @@ -32,13 +35,14 @@ def all_component_templates(ecs_nested, ecs_version, out_dir):
def component_template(template_name, ecs_version, out_dir, field_mappings):
filename = join(out_dir, template_name) + ".json"

template = { 'template': { 'mappings': { 'properties': field_mappings }}}
template = {'template': {'mappings': {'properties': field_mappings}}}
save_json(filename, template)

### Legacy template
# Legacy template


def generate_legacy(ecs_flat, ecs_version, out_dir, template_settings_file, mapping_settings_file):
"""Generate the legacy index template"""
field_mappings = {}
for flat_name in sorted(ecs_flat):
field = ecs_flat[flat_name]
Expand All @@ -53,10 +57,27 @@ def generate_legacy(ecs_flat, ecs_version, out_dir, template_settings_file, mapp

mappings_section['properties'] = field_mappings

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


def generate_legacy_template_version(elasticsearch_version, mappings_section, out_dir, template_settings_file):
ecs_helpers.make_dirs(join(out_dir, 'elasticsearch', str(elasticsearch_version)))
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:
template['mappings'] = mappings_section

filename = join(out_dir, "elasticsearch/{}/template.json".format(elasticsearch_version))
save_json(filename, template)


### Field mappings
# Common helpers


def dict_add_nested(dct, name_parts, value):
Expand Down Expand Up @@ -117,22 +138,6 @@ def entry_for(field):
# Generated files


def generate_template_version(elasticsearch_version, mappings_section, out_dir, template_settings_file):
ecs_helpers.make_dirs(join(out_dir, 'elasticsearch', str(elasticsearch_version)))
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:
template['mappings'] = mappings_section

filename = join(out_dir, "elasticsearch/{}/template.json".format(elasticsearch_version))
save_json(filename, template)


def save_json(file, data):
open_mode = "wb"
if sys.version_info >= (3, 0):
Expand Down

0 comments on commit 9ca0276

Please sign in to comment.