Skip to content

Commit 9c562b0

Browse files
author
Mathieu Martin
authored
Add --oss flag to the ECS generator script (#991)
1 parent b106d3a commit 9c562b0

File tree

5 files changed

+100
-6
lines changed

5 files changed

+100
-6
lines changed

CHANGELOG.next.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Thanks, you're awesome :-) -->
4242
* Introduced `--strict` flag to perform stricter schema validation when running the generator script. #937
4343
* Added check under `--strict` that ensures composite types in example fields are quoted. #966
4444
* Added `ignore_above` and `normalizer` support for keyword multi-fields. #971
45+
* Added `--oss` flag for users who want to generate ECS templates for use on OSS clusters. #991
4546

4647
#### Improvements
4748

USAGE.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ relevant artifacts for their unique set of data sources.
2929
+ [Subset](#subset)
3030
+ [Ref](#ref)
3131
+ [Mapping & Template Settings](#mapping--template-settings)
32+
+ [OSS](#oss)
3233
+ [Strict Mode](#strict-mode)
3334
+ [Intermediate-Only](#intermediate-only)
3435

@@ -295,14 +296,38 @@ The `--template-settings` argument defines [index level settings](https://www.el
295296

296297
For `template.json`, the `mappings` object is left empty: `{}`. Likewise the `properties` object remains empty in the `mapping.json` example. This will be filled in automatically by the script.
297298

299+
#### OSS
300+
301+
**IMPORTANT**: This feature is unnecessary for most users. Our default free distribution
302+
comes with the Elastic Basic license, and supports all data types used by ECS.
303+
Learn more about our licenses [here](https://www.elastic.co/subscriptions).
304+
305+
Users that want to use the open source version of Elasticsearch do not have access to the basic data types.
306+
However some of these types have an OSS replacement that can be used instead, without too much loss of functionality.
307+
308+
This flag performs a best effort fallback, replacing basic data types with their OSS replacement.
309+
310+
Indices using purely OSS types will benefit from the normalization of ECS, but may be missing on some of the added functionality of these basic types.
311+
312+
Current fallbacks applied by this flag are:
313+
314+
- `wildcard` => `keyword`
315+
- `version` => `keyword`
316+
317+
Usage:
318+
319+
```
320+
$ python scripts/generator.py --oss
321+
```
322+
298323
#### Strict Mode
299324

300325
The `--strict` argument enables "strict mode". Strict mode performs a stricter validation step against the schema's contents.
301326

302327
Basic usage:
303328

304329
```
305-
$ python/generator.py --strict
330+
$ python scripts/generator.py --strict
306331
```
307332

308333
Strict mode requires the following conditions, else the script exits on an exception:

scripts/generator.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from generators import intermediate_files
1313

1414
from schema import loader
15+
from schema import oss
1516
from schema import cleaner
1617
from schema import finalizer
1718
from schema import subset_filter
@@ -41,6 +42,8 @@ def main():
4142
# ecs_helpers.yaml_dump('ecs.yml', fields)
4243

4344
fields = loader.load_schemas(ref=args.ref, included_files=args.include)
45+
if args.oss:
46+
oss.fallback(fields)
4447
cleaner.clean(fields, strict=args.strict)
4548
finalizer.finalize(fields)
4649
fields = subset_filter.filter(fields, args.subset, out_dir)
@@ -60,20 +63,21 @@ def main():
6063

6164
def argument_parser():
6265
parser = argparse.ArgumentParser()
63-
parser.add_argument('--intermediate-only', action='store_true',
64-
help='generate intermediary files only')
66+
parser.add_argument('--ref', action='store', help='git reference to use when building schemas')
6567
parser.add_argument('--include', nargs='+',
6668
help='include user specified directory of custom field definitions')
6769
parser.add_argument('--subset', nargs='+',
6870
help='render a subset of the schema')
69-
parser.add_argument('--out', action='store', help='directory to store the generated files')
70-
parser.add_argument('--ref', action='store', help='git reference to use when building schemas')
71+
parser.add_argument('--out', action='store', help='directory to output the generated files')
7172
parser.add_argument('--template-settings', action='store',
7273
help='index template settings to use when generating elasticsearch template')
7374
parser.add_argument('--mapping-settings', action='store',
7475
help='mapping settings to use when generating elasticsearch template')
76+
parser.add_argument('--oss', action='store_true', help='replace basic data types with oss ones where possible')
7577
parser.add_argument('--strict', action='store_true',
76-
help='enforce stricter checking at schema cleanup')
78+
help='enforce strict checking at schema cleanup')
79+
parser.add_argument('--intermediate-only', action='store_true',
80+
help='generate intermediary files only')
7781
args = parser.parse_args()
7882
# Clean up empty include of the Makefile
7983
if args.include and [''] == args.include:

scripts/schema/oss.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# This script performs a best effort fallback of basic data types to equivalent
2+
# OSS data types.
3+
# Note however that not all basic data types have an OSS replacement.
4+
#
5+
# The way this script is currently written, it has to be run on the fields *before*
6+
# the cleaner script applies defaults, as there's no concept of defaults here.
7+
# But since it navigates using the visitor script, it can easily be moved around
8+
# in the chain, provided we add support for defaults as well.
9+
#
10+
# For now, no warning is output on basic fields that don't have a fallback.
11+
# This could be improved if ECS starts using such types.
12+
13+
from schema import visitor
14+
15+
TYPE_FALLBACKS = {
16+
'wildcard': 'keyword',
17+
'version': 'keyword'
18+
}
19+
20+
21+
def fallback(fields):
22+
"""Verify all fields for basic data type usage, and fallback to an OSS equivalent if appropriate."""
23+
visitor.visit_fields(fields, field_func=perform_fallback)
24+
25+
26+
def perform_fallback(field):
27+
"""Performs a best effort fallback of basic data types to equivalent OSS data types."""
28+
if field['field_details']['type'] in TYPE_FALLBACKS.keys():
29+
field['field_details']['type'] = TYPE_FALLBACKS[field['field_details']['type']]
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import os
2+
import pprint
3+
import sys
4+
import unittest
5+
6+
sys.path.append(os.path.join(os.path.dirname(__file__), '../..'))
7+
8+
from schema import oss
9+
from schema import visitor
10+
11+
12+
class TestSchemaOss(unittest.TestCase):
13+
14+
def setUp(self):
15+
self.maxDiff = None
16+
17+
def test_wildcard_fallback(self):
18+
field = {'field_details': {'name': 'myfield', 'type': 'wildcard'}}
19+
oss.perform_fallback(field)
20+
self.assertEqual('keyword', field['field_details']['type'])
21+
22+
def test_version_fallback(self):
23+
field = {'field_details': {'name': 'myfield', 'type': 'version'}}
24+
oss.perform_fallback(field)
25+
self.assertEqual('keyword', field['field_details']['type'])
26+
27+
def test_basic_without_fallback(self):
28+
field = {'field_details': {'name': 'myfield', 'type': 'histogram'}}
29+
oss.perform_fallback(field)
30+
self.assertEqual('histogram', field['field_details']['type'])
31+
32+
def test_oss_no_fallback(self):
33+
field = {'field_details': {'name': 'myfield', 'type': 'keyword'}}
34+
oss.perform_fallback(field)
35+
self.assertEqual('keyword', field['field_details']['type'])

0 commit comments

Comments
 (0)