Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: include alias path when generating template #877

Merged
merged 12 commits into from
Oct 16, 2020
8 changes: 8 additions & 0 deletions scripts/generators/beats.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ def fieldset_field_array(source_fields, df_whitelist, fieldset_prefix):
if not ecs_field['flat_name'] in df_whitelist:
beats_field['default_field'] = False

# Include the `path` attribute for `alias` types.
# https:/elastic/ecs/issues/876
if ecs_field['type'] == 'alias':
rgmz marked this conversation as resolved.
Show resolved Hide resolved
if 'path' in ecs_field:
beats_field['path'] = ecs_field['path']
else:
raise ValueError(f'The [path] property must be specified for field [{contextual_name}]')
rgmz marked this conversation as resolved.
Show resolved Hide resolved

fields.append(beats_field)
return sorted(fields, key=lambda x: x['name'])

Expand Down
5 changes: 5 additions & 0 deletions scripts/generators/es_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ def entry_for(field):
ecs_helpers.dict_copy_existing_keys(field, field_entry, ['ignore_above'])
elif field['type'] == 'text':
ecs_helpers.dict_copy_existing_keys(field, field_entry, ['norms'])
elif field['type'] == 'alias':
rgmz marked this conversation as resolved.
Show resolved Hide resolved
if 'path' in field:
field_entry['path'] = field['path']
else:
raise ValueError(f'The [path] property must be specified for field [{field["name"]}]')

if 'multi_fields' in field:
field_entry['fields'] = {}
Expand Down
6 changes: 6 additions & 0 deletions scripts/schema/cleaner.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ def field_mandatory_attributes(field):
return
current_field_attributes = sorted(field['field_details'].keys())
missing_attributes = ecs_helpers.list_subtract(FIELD_MANDATORY_ATTRIBUTES, current_field_attributes)

# The `alias` type requires a target path.
# https:/elastic/ecs/issues/876
if field['field_details'].get('type') == 'alias' and 'path' not in current_field_attributes:
missing_attributes.append('path')

if len(missing_attributes) > 0:
msg = "Field is missing the following mandatory attributes: {}.\nFound these: {}.\nField details: {}"
raise ValueError(msg.format(', '.join(missing_attributes),
Expand Down
23 changes: 23 additions & 0 deletions scripts/tests/test_es_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,29 @@ def test_entry_for_index(self):
}
self.assertEqual(es_template.entry_for(test_map), exp)

def test_entry_for_alias(self):
test_map = {
'name': 'test.alias',
'type': 'alias',
'path': 'alias.target'
}

exp = {
'type': 'alias',
'path': 'alias.target'
}
self.assertEqual(es_template.entry_for(test_map), exp)

def test_entry_for_alias_missing_path(self):
test_map = {
'name': 'test.alias',
'type': 'alias'
}

with self.assertRaisesRegex(ValueError,
"The \[path\] property must be specified for field \[{}\]".format(test_map['name'])):
es_template.entry_for(test_map)


if __name__ == '__main__':
unittest.main()
7 changes: 7 additions & 0 deletions scripts/tests/unit/test_schema_cleaner.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,13 @@ def test_field_raises_on_missing_required_attributes(self):
"mandatory attributes: {}".format(missing_attribute)):
cleaner.field_mandatory_attributes(field)

def test_field_raises_on_alias_missing_path_attribute(self):
field = self.schema_process()['process']['fields']['pid']
field['field_details']['type'] = "alias"
with self.assertRaisesRegex(ValueError,
"mandatory attributes: {}".format("path")):
cleaner.field_mandatory_attributes(field)

def test_field_simple_cleanup(self):
my_field = {
'field_details': {
Expand Down