summaryrefslogtreecommitdiffstats
path: root/roles/lib_openshift/src/lib/rule.py
diff options
context:
space:
mode:
authorKenny Woodson <kwoodson@redhat.com>2017-03-28 13:56:00 -0400
committerKenny Woodson <kwoodson@redhat.com>2017-03-28 17:05:49 -0400
commit611e9864e8716ca92a77745403c847c30f2f0d87 (patch)
treeb9a9248d32e6024e3e58d897636b63dfce01ac60 /roles/lib_openshift/src/lib/rule.py
parentd77a9a685c3add9ef9c4d659ee47ebf35e145fe4 (diff)
downloadopenshift-611e9864e8716ca92a77745403c847c30f2f0d87.tar.gz
openshift-611e9864e8716ca92a77745403c847c30f2f0d87.tar.bz2
openshift-611e9864e8716ca92a77745403c847c30f2f0d87.tar.xz
openshift-611e9864e8716ca92a77745403c847c30f2f0d87.zip
Adding clusterrole to the toolbox.
Diffstat (limited to 'roles/lib_openshift/src/lib/rule.py')
-rw-r--r--roles/lib_openshift/src/lib/rule.py143
1 files changed, 143 insertions, 0 deletions
diff --git a/roles/lib_openshift/src/lib/rule.py b/roles/lib_openshift/src/lib/rule.py
new file mode 100644
index 000000000..628129832
--- /dev/null
+++ b/roles/lib_openshift/src/lib/rule.py
@@ -0,0 +1,143 @@
+# pylint: skip-file
+# flake8: noqa
+
+
+class Rule(object):
+ '''class to represent a clusterrole rule
+
+ Example Rule Object's yaml:
+ - apiGroups:
+ - ""
+ attributeRestrictions: null
+ resources:
+ - persistentvolumes
+ verbs:
+ - create
+ - delete
+ - deletecollection
+ - get
+ - list
+ - patch
+ - update
+ - watch
+
+ '''
+ def __init__(self,
+ api_groups=None,
+ attr_restrictions=None,
+ resources=None,
+ verbs=None):
+ self.__api_groups = api_groups if api_groups is not None else [""]
+ self.__verbs = verbs if verbs is not None else []
+ self.__resources = resources if resources is not None else []
+ self.__attribute_restrictions = attr_restrictions if attr_restrictions is not None else None
+
+ @property
+ def verbs(self):
+ '''property for verbs'''
+ if self.__verbs is None:
+ return []
+
+ return self.__verbs
+
+ @verbs.setter
+ def verbs(self, data):
+ '''setter for verbs'''
+ self.__verbs = data
+
+ @property
+ def api_groups(self):
+ '''property for api_groups'''
+ if self.__api_groups is None:
+ return []
+ return self.__api_groups
+
+ @api_groups.setter
+ def api_groups(self, data):
+ '''setter for api_groups'''
+ self.__api_groups = data
+
+ @property
+ def resources(self):
+ '''property for resources'''
+ if self.__resources is None:
+ return []
+
+ return self.__resources
+
+ @resources.setter
+ def resources(self, data):
+ '''setter for resources'''
+ self.__resources = data
+
+ @property
+ def attribute_restrictions(self):
+ '''property for attribute_restrictions'''
+ return self.__attribute_restrictions
+
+ @attribute_restrictions.setter
+ def attribute_restrictions(self, data):
+ '''setter for attribute_restrictions'''
+ self.__attribute_restrictions = data
+
+ def add_verb(self, inc_verb):
+ '''add a verb to the verbs array'''
+ self.verbs.append(inc_verb)
+
+ def add_api_group(self, inc_apigroup):
+ '''add an api_group to the api_groups array'''
+ self.api_groups.append(inc_apigroup)
+
+ def add_resource(self, inc_resource):
+ '''add an resource to the resources array'''
+
+ def remove_verb(self, inc_verb):
+ '''add a verb to the verbs array'''
+ try:
+ self.verbs.remove(inc_verb)
+ return True
+ except ValueError:
+ pass
+
+ return False
+
+ def remove_api_group(self, inc_api_group):
+ '''add a verb to the verbs array'''
+ try:
+ self.api_groups.remove(inc_api_group)
+ return True
+ except ValueError:
+ pass
+
+ return False
+
+ def remove_resource(self, inc_resource):
+ '''add a verb to the verbs array'''
+ try:
+ self.resources.remove(inc_resource)
+ return True
+ except ValueError:
+ pass
+
+ return False
+
+ def __eq__(self, other):
+ '''return whether rules are equal'''
+ return self.attribute_restrictions == other.attribute_restrictions and \
+ self.api_groups == other.api_groups and \
+ self.resources == other.resources and \
+ self.verbs == other.verbs
+
+
+ @staticmethod
+ def parse_rules(inc_rules):
+ '''create rules from an array'''
+
+ results = []
+ for rule in inc_rules:
+ results.append(Rule(rule['apiGroups'],
+ rule['attributeRestrictions'],
+ rule['resources'],
+ rule['verbs']))
+
+ return results