Coverage for curator/validators/filter_functions.py: 100%
31 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-20 21:00 -0600
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-20 21:00 -0600
1"""Functions validating the ``filter`` Schema of an ``action``"""
2import logging
3from voluptuous import Any, In, Required, Schema
4from es_client.helpers.utils import prune_nones
5from curator.defaults import settings, filtertypes
6from curator.exceptions import ConfigurationError
7from curator.validators import SchemaCheck
9logger = logging.getLogger(__name__)
11def filtertype():
12 """
13 Return a :py:class:`~.voluptuous.schema_builder.Schema` object that uses
14 :py:func:`~.curator.defaults.settings.all_filtertypes` to populate acceptable values
16 :returns: A :py:class:`~.voluptuous.schema_builder.Schema` object
17 """
18 return {
19 Required('filtertype'): Any(
20 In(settings.all_filtertypes()),
21 msg=f'filtertype must be one of {settings.all_filtertypes()}'
22 )
23 }
25def filterstructure():
26 """
27 Return a :py:class:`~.voluptuous.schema_builder.Schema` object that uses the return value from
28 :py:func:`~.curator.defaults.settings.structural_filter_elements` to populate acceptable values
29 and updates/merges the Schema object with the return value from
30 :py:func:`filtertype`
32 :returns: A :py:class:`~.voluptuous.schema_builder.Schema` object
33 """
34 # This is to first ensure that only the possible keys/filter elements are
35 # there, and get a dictionary back to work with.
36 retval = settings.structural_filter_elements()
37 retval.update(filtertype())
38 return Schema(retval)
40def singlefilter(action, data):
41 """
42 Return a :py:class:`~.voluptuous.schema_builder.Schema` object that is created using the return
43 value from :py:func:`filtertype` to create a local variable ``ftype``. The values from
44 ``action`` and ``data`` are used to update ``ftype`` based on matching function names in
45 :py:mod:`~.curator.defaults.filtertypes`.
47 :py:func:`~.curator.defaults.settings.structural_filter_elements` to populate acceptable values
48 and updates/merges the Schema object with the return value from
49 :py:func:`filtertype`
51 :param action: The Curator action name
52 :type action: str
53 :param data: The filter block of the action
55 :returns: A :py:class:`~.voluptuous.schema_builder.Schema` object
56 """
57 try:
58 ftdata = data['filtertype']
59 except KeyError as exc:
60 raise ConfigurationError('Missing key "filtertype"') from exc
61 ftype = filtertype()
62 for each in getattr(filtertypes, ftdata)(action, data):
63 ftype.update(each)
64 return Schema(ftype)
66def validfilters(action, location=None):
67 """Validate the filters in a list"""
68 def func(val):
69 """This validator method simply validates all filters in the list."""
70 for idx, value in enumerate(val):
71 pruned = prune_nones(value)
72 filter_dict = SchemaCheck(
73 pruned,
74 singlefilter(action, pruned),
75 'filter',
76 f'{location}, filter #{idx}: {pruned}'
77 ).result()
78 logger.debug('Filter #%s: %s', idx, filter_dict)
79 val[idx] = filter_dict
80 # If we've made it here without raising an Exception, it's valid
81 return val
82 return func