def validate_actions()

in curator/helpers/testers.py [0:0]


def validate_actions(data):
    """
    Validate the ``actions`` configuration dictionary, as imported from actions.yml,
    for example.

    :param data: The configuration dictionary

    :type data: dict

    :returns: The validated and sanitized configuration dictionary.
    :rtype: dict
    """
    # data is the ENTIRE schema...
    clean_config = {}
    # Let's break it down into smaller chunks...
    # First, let's make sure it has "actions" as a key, with a subdictionary
    root = SchemaCheck(data, actions.root(), 'Actions File', 'root').result()
    # We've passed the first step.  Now let's iterate over the actions...
    for action_id in root['actions']:
        # Now, let's ensure that the basic action structure is correct, with
        # the proper possibilities for 'action'
        action_dict = root['actions'][action_id]
        loc = f'Action ID "{action_id}"'
        valid_structure = SchemaCheck(
            action_dict, actions.structure(action_dict, loc), 'structure', loc
        ).result()
        # With the basic structure validated, now we extract the action name
        current_action = valid_structure['action']
        # And let's update the location with the action.
        loc = f'Action ID "{action_id}", action "{current_action}"'
        clean_options = SchemaCheck(
            prune_nones(valid_structure['options']),
            options.get_schema(current_action),
            'options',
            loc,
        ).result()
        clean_config[action_id] = {
            'action': current_action,
            'description': valid_structure['description'],
            'options': clean_options,
        }
        if current_action == 'alias':
            add_remove = {}
            for k in ['add', 'remove']:
                if k in valid_structure:
                    current_filters = SchemaCheck(
                        valid_structure[k]['filters'],
                        Schema(validfilters(current_action, location=loc)),
                        f'"{k}" filters',
                        f'{loc}, "filters"',
                    ).result()
                    add_remove.update(
                        {
                            k: {
                                'filters': SchemaCheck(
                                    current_filters,
                                    Schema(validfilters(current_action, location=loc)),
                                    'filters',
                                    f'{loc}, "{k}", "filters"',
                                ).result()
                            }
                        }
                    )
            # Add/Remove here
            clean_config[action_id].update(add_remove)
        elif current_action in ['cluster_routing', 'create_index', 'rollover']:
            # neither cluster_routing nor create_index should have filters
            pass
        else:  # Filters key only appears in non-alias actions
            valid_filters = SchemaCheck(
                valid_structure['filters'],
                Schema(validfilters(current_action, location=loc)),
                'filters',
                f'{loc}, "filters"',
            ).result()
            clean_filters = validate_filters(current_action, valid_filters)
            clean_config[action_id].update({'filters': clean_filters})
        # This is a special case for remote reindex
        if current_action == 'reindex':
            # Check only if populated with something.
            if 'remote_filters' in valid_structure['options']:
                valid_filters = SchemaCheck(
                    valid_structure['options']['remote_filters'],
                    Schema(validfilters(current_action, location=loc)),
                    'filters',
                    f'{loc}, "filters"',
                ).result()
                clean_remote_filters = validate_filters(current_action, valid_filters)
                clean_config[action_id]['options'].update(
                    {'remote_filters': clean_remote_filters}
                )

    # if we've gotten this far without any Exceptions raised, it's valid!
    return {'actions': clean_config}