summaryrefslogtreecommitdiffstats
path: root/roles/lib_openshift/src/lib/clusterrole.py
blob: 93ffababf1b217f99a5c7fe5d4ceb45706a35e68 (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# pylint: skip-file
# flake8: noqa


# pylint: disable=too-many-public-methods
class ClusterRole(Yedit):
    ''' Class to model an openshift ClusterRole'''
    rules_path = "rules"

    def __init__(self, name=None, content=None):
        ''' Constructor for clusterrole '''
        if content is None:
            content = ClusterRole.builder(name).yaml_dict

        super(ClusterRole, self).__init__(content=content)

        self.__rules = Rule.parse_rules(self.get(ClusterRole.rules_path)) or []

    @property
    def rules(self):
        return self.__rules

    @rules.setter
    def rules(self, data):
        self.__rules = data
        self.put(ClusterRole.rules_path, self.__rules)

    def rule_exists(self, inc_rule):
        '''attempt to find the inc_rule in the rules list'''
        for rule in self.rules:
            if rule == inc_rule:
                return True

        return False

    def compare(self, other, verbose=False):
        '''compare function for clusterrole'''
        for rule in other.rules:
            if rule not in self.rules:
                if verbose:
                    print('Rule in other not found in self. [{}]'.format(rule))
                return False

        for rule in self.rules:
            if rule not in other.rules:
                if verbose:
                    print('Rule in self not found in other. [{}]'.format(rule))
                return False

        return True

    @staticmethod
    def builder(name='default_clusterrole', rules=None):
        '''return a clusterrole with name and/or rules'''
        if rules is None:
            rules = [{'apiGroups': [""],
                      'attributeRestrictions': None,
                      'verbs': [],
                      'resources': []}]
        content = {
            'apiVersion': 'v1',
            'kind': 'ClusterRole',
            'metadata': {'name': '{}'.format(name)},
            'rules': rules,
        }

        return ClusterRole(content=content)