From 260a3eb83d4cb5cbe58b1e0ba4526d57da70ed57 Mon Sep 17 00:00:00 2001 From: Mathieu Martin Date: Wed, 21 Oct 2020 14:58:49 -0400 Subject: [PATCH] Add support for `scaling_factor` in the generator (#1042) --- CHANGELOG.next.md | 1 + scripts/generators/beats.py | 2 +- scripts/generators/es_template.py | 2 ++ scripts/schema/cleaner.py | 6 ++++-- scripts/tests/test_es_template.py | 13 +++++++++++++ scripts/tests/unit/test_schema_cleaner.py | 7 +++++++ 6 files changed, 28 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.next.md b/CHANGELOG.next.md index 4c63b041eb..3c8577cb6c 100644 --- a/CHANGELOG.next.md +++ b/CHANGELOG.next.md @@ -30,6 +30,7 @@ Thanks, you're awesome :-) --> * Added ability to supply free-form usage documentation per fieldset. #988 * Added the `path` key when type is `alias`, to support the [alias field type](https://www.elastic.co/guide/en/elasticsearch/reference/current/alias.html). #877 +* Added support for `scaled_float`'s mandatory parameter `scaling_factor`. #1042 #### Improvements diff --git a/scripts/generators/beats.py b/scripts/generators/beats.py index 457fecc5ec..0d182b40db 100644 --- a/scripts/generators/beats.py +++ b/scripts/generators/beats.py @@ -34,7 +34,7 @@ def fieldset_field_array(source_fields, df_whitelist, fieldset_prefix): allowed_keys = ['name', 'level', 'required', 'type', 'object_type', 'ignore_above', 'multi_fields', 'format', 'input_format', 'output_format', 'output_precision', 'description', - 'example', 'enabled', 'index', 'path'] + 'example', 'enabled', 'index', 'path', 'scaling_factor'] multi_fields_allowed_keys = ['name', 'type', 'norms', 'default_field', 'normalizer', 'ignore_above'] fields = [] diff --git a/scripts/generators/es_template.py b/scripts/generators/es_template.py index 08e925f0ae..9fed37ee05 100644 --- a/scripts/generators/es_template.py +++ b/scripts/generators/es_template.py @@ -61,6 +61,8 @@ def entry_for(field): ecs_helpers.dict_copy_existing_keys(field, field_entry, ['norms']) elif field['type'] == 'alias': ecs_helpers.dict_copy_existing_keys(field, field_entry, ['path']) + elif field['type'] == 'scaled_float': + ecs_helpers.dict_copy_existing_keys(field, field_entry, ['scaling_factor']) if 'multi_fields' in field: field_entry['fields'] = {} diff --git a/scripts/schema/cleaner.py b/scripts/schema/cleaner.py index fa5838cbb7..ab3acfcaeb 100644 --- a/scripts/schema/cleaner.py +++ b/scripts/schema/cleaner.py @@ -159,10 +159,12 @@ def field_mandatory_attributes(field): 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://github.com/elastic/ecs/issues/876 + # `alias` fields require a target `path` attribute. if field['field_details'].get('type') == 'alias' and 'path' not in current_field_attributes: missing_attributes.append('path') + # `scaled_float` fields require a `scaling_factor` attribute. + if field['field_details'].get('type') == 'scaled_float' and 'scaling_factor' not in current_field_attributes: + missing_attributes.append('scaling_factor') if len(missing_attributes) > 0: msg = "Field is missing the following mandatory attributes: {}.\nFound these: {}.\nField details: {}" diff --git a/scripts/tests/test_es_template.py b/scripts/tests/test_es_template.py index 9136f8b99e..a1491d2241 100644 --- a/scripts/tests/test_es_template.py +++ b/scripts/tests/test_es_template.py @@ -122,6 +122,19 @@ def test_entry_for_alias(self): } self.assertEqual(es_template.entry_for(test_map), exp) + def test_entry_for_scaled_float(self): + test_map = { + 'name': 'test.scaled_float', + 'type': 'scaled_float', + 'scaling_factor': 1000 + } + + exp = { + 'type': 'scaled_float', + 'scaling_factor': 1000 + } + self.assertEqual(es_template.entry_for(test_map), exp) + if __name__ == '__main__': unittest.main() diff --git a/scripts/tests/unit/test_schema_cleaner.py b/scripts/tests/unit/test_schema_cleaner.py index ba86728e2d..13f78c4e91 100644 --- a/scripts/tests/unit/test_schema_cleaner.py +++ b/scripts/tests/unit/test_schema_cleaner.py @@ -164,6 +164,13 @@ def test_field_raises_on_alias_missing_path_attribute(self): "mandatory attributes: {}".format("path")): cleaner.field_mandatory_attributes(field) + def test_raises_on_missing_scaling_factor(self): + field = self.schema_process()['process']['fields']['pid'] + field['field_details']['type'] = "scaled_float" + with self.assertRaisesRegex(ValueError, + "mandatory attributes: {}".format("scaling_factor")): + cleaner.field_mandatory_attributes(field) + def test_field_simple_cleanup(self): my_field = { 'field_details': {