summaryrefslogtreecommitdiffstats
path: root/roles/openshift_logging/filter_plugins/openshift_logging.py
blob: f0007dfc7e45dc0e9d316c1c9df50ce82cc117d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
'''
 Openshift Logging class that provides useful filters used in Logging
'''

import random


def random_word(source_alpha, length):
    ''' Returns a random word given the source of characters to pick from and resulting length '''
    return ''.join(random.choice(source_alpha) for i in range(length))


def entry_from_named_pair(register_pairs, key):
    ''' Returns the entry in key given results provided by register_pairs '''
    results = register_pairs.get("results")
    if results is None:
        raise RuntimeError("The dict argument does not have a 'results' entry. "
                           "Must not have been created using 'register' in a loop")
    for result in results:
        item = result.get("item")
        if item is not None:
            name = item.get("name")
            if name == key:
                return result["content"]
    raise RuntimeError("There was no entry found in the dict that had an item with a name that matched {}".format(key))


def map_from_pairs(source, delim="="):
    ''' Returns a dict given the source and delim delimited '''
    if source == '':
      return dict()

    return dict(source.split(delim) for item in source.split(","))

# pylint: disable=too-few-public-methods
class FilterModule(object):
    ''' OpenShift Logging Filters '''

    # pylint: disable=no-self-use, too-few-public-methods
    def filters(self):
        ''' Returns the names of the filters provided by this class '''
        return {
            'random_word': random_word,
            'entry_from_named_pair': entry_from_named_pair,
            'map_from_pairs': map_from_pairs,
        }