From 07331b47724dbb7cd6952c1a2af54275ace7726e Mon Sep 17 00:00:00 2001 From: Kenny Woodson Date: Fri, 13 Jan 2017 12:37:30 -0500 Subject: lib_openshift modules. This is the first one. oc_route. --- roles/lib_utils/library/yedit.py | 108 +++++++++++++++++++-------------------- 1 file changed, 53 insertions(+), 55 deletions(-) (limited to 'roles/lib_utils/library/yedit.py') diff --git a/roles/lib_utils/library/yedit.py b/roles/lib_utils/library/yedit.py index fb545c7c8..90c7fd4d7 100644 --- a/roles/lib_utils/library/yedit.py +++ b/roles/lib_utils/library/yedit.py @@ -24,7 +24,6 @@ # limitations under the License. # - # pylint: disable=wrong-import-order import json import os @@ -164,7 +163,6 @@ EXAMPLES = ''' # c: d ''' - class YeditException(Exception): ''' Exception class for Yedit ''' pass @@ -590,6 +588,48 @@ class Yedit(object): return (False, self.yaml_dict) + @staticmethod + def get_curr_value(invalue, val_type): + '''return the current value''' + if invalue is None: + return None + + curr_value = invalue + if val_type == 'yaml': + curr_value = yaml.load(invalue) + elif val_type == 'json': + curr_value = json.loads(invalue) + + return curr_value + + @staticmethod + def parse_value(inc_value, vtype=''): + '''determine value type passed''' + true_bools = ['y', 'Y', 'yes', 'Yes', 'YES', 'true', 'True', 'TRUE', + 'on', 'On', 'ON', ] + false_bools = ['n', 'N', 'no', 'No', 'NO', 'false', 'False', 'FALSE', + 'off', 'Off', 'OFF'] + + # It came in as a string but you didn't specify value_type as string + # we will convert to bool if it matches any of the above cases + if isinstance(inc_value, str) and 'bool' in vtype: + if inc_value not in true_bools and inc_value not in false_bools: + raise YeditException('Not a boolean type. str=[%s] vtype=[%s]' + % (inc_value, vtype)) + elif isinstance(inc_value, bool) and 'str' in vtype: + inc_value = str(inc_value) + + # If vtype is not str then go ahead and attempt to yaml load it. + if isinstance(inc_value, str) and 'str' not in vtype: + try: + inc_value = yaml.load(inc_value) + except Exception: + raise YeditException('Could not determine type of incoming ' + + 'value. value=[%s] vtype=[%s]' + % (type(inc_value), vtype)) + + return inc_value + # pylint: disable=too-many-return-statements,too-many-branches @staticmethod def run_ansible(module): @@ -610,8 +650,8 @@ class Yedit(object): if module.params['state'] == 'list': if module.params['content']: - content = parse_value(module.params['content'], - module.params['content_type']) + content = Yedit.parse_value(module.params['content'], + module.params['content_type']) yamlfile.yaml_dict = content if module.params['key']: @@ -621,8 +661,8 @@ class Yedit(object): elif module.params['state'] == 'absent': if module.params['content']: - content = parse_value(module.params['content'], - module.params['content_type']) + content = Yedit.parse_value(module.params['content'], + module.params['content_type']) yamlfile.yaml_dict = content if module.params['update']: @@ -639,8 +679,8 @@ class Yedit(object): elif module.params['state'] == 'present': # check if content is different than what is in the file if module.params['content']: - content = parse_value(module.params['content'], - module.params['content_type']) + content = Yedit.parse_value(module.params['content'], + module.params['content_type']) # We had no edits to make and the contents are the same if yamlfile.yaml_dict == content and \ @@ -653,12 +693,13 @@ class Yedit(object): # we were passed a value; parse it if module.params['value']: - value = parse_value(module.params['value'], - module.params['value_type']) + value = Yedit.parse_value(module.params['value'], + module.params['value_type']) key = module.params['key'] if module.params['update']: # pylint: disable=line-too-long - curr_value = get_curr_value(parse_value(module.params['curr_value']), module.params['curr_value_format']) # noqa: #501 + curr_value = Yedit.get_curr_value(Yedit.parse_value(module.params['curr_value']), # noqa: E501 + module.params['curr_value_format']) # noqa: E501 rval = yamlfile.update(key, value, module.params['index'], curr_value) # noqa: E501 @@ -683,49 +724,6 @@ class Yedit(object): return {'failed': True, 'msg': 'Unkown state passed'} - -def get_curr_value(invalue, val_type): - '''return the current value''' - if invalue is None: - return None - - curr_value = invalue - if val_type == 'yaml': - curr_value = yaml.load(invalue) - elif val_type == 'json': - curr_value = json.loads(invalue) - - return curr_value - - -def parse_value(inc_value, vtype=''): - '''determine value type passed''' - true_bools = ['y', 'Y', 'yes', 'Yes', 'YES', 'true', 'True', 'TRUE', - 'on', 'On', 'ON', ] - false_bools = ['n', 'N', 'no', 'No', 'NO', 'false', 'False', 'FALSE', - 'off', 'Off', 'OFF'] - - # It came in as a string but you didn't specify value_type as string - # we will convert to bool if it matches any of the above cases - if isinstance(inc_value, str) and 'bool' in vtype: - if inc_value not in true_bools and inc_value not in false_bools: - raise YeditException('Not a boolean type. str=[%s] vtype=[%s]' - % (inc_value, vtype)) - elif isinstance(inc_value, bool) and 'str' in vtype: - inc_value = str(inc_value) - - # If vtype is not str then go ahead and attempt to yaml load it. - if isinstance(inc_value, str) and 'str' not in vtype: - try: - inc_value = yaml.load(inc_value) - except Exception: - raise YeditException('Could not determine type of incoming ' + - 'value. value=[%s] vtype=[%s]' - % (type(inc_value), vtype)) - - return inc_value - - # pylint: disable=too-many-branches def main(): ''' ansible oc module for secrets ''' @@ -757,7 +755,7 @@ def main(): rval = Yedit.run_ansible(module) if 'failed' in rval and rval['failed']: - module.fail_json(msg=rval['msg']) + module.fail_json(**rval) module.exit_json(**rval) -- cgit v1.2.3 From ea33e223e34bb2b8efae6b165f3ac9729357cb46 Mon Sep 17 00:00:00 2001 From: Kenny Woodson Date: Fri, 13 Jan 2017 14:29:48 -0500 Subject: Adding oc_edit module to lib_openshift. --- roles/lib_utils/library/yedit.py | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'roles/lib_utils/library/yedit.py') diff --git a/roles/lib_utils/library/yedit.py b/roles/lib_utils/library/yedit.py index 90c7fd4d7..d882c983e 100644 --- a/roles/lib_utils/library/yedit.py +++ b/roles/lib_utils/library/yedit.py @@ -162,12 +162,15 @@ EXAMPLES = ''' # b: # c: d ''' +# noqa: E301,E302 + class YeditException(Exception): ''' Exception class for Yedit ''' pass +# pylint: disable=too-many-public-methods class Yedit(object): ''' Class to modify yaml files ''' re_valid_key = r"(((\[-?\d+\])|([0-9a-zA-Z%s/_-]+)).?)+$" @@ -724,6 +727,7 @@ class Yedit(object): return {'failed': True, 'msg': 'Unkown state passed'} + # pylint: disable=too-many-branches def main(): ''' ansible oc module for secrets ''' -- cgit v1.2.3