mirror of
https://github.com/ansible-collections/community.routeros.git
synced 2025-06-22 01:49:02 +02:00
Allow api module to fail (#39)
* Allow api module to fail. * Improve error handling. * Fix api unit tests. * Add very basic tests of return values. * Update api.py fix ignoring the Fail task if we get TrapError * Do not mangle fail result, and adjust tests. * Improve changelog fragment. * Reclassify changelog fragment as minor_changes, improve text. * Mark changelog as 'breaking change'. Co-authored-by: Nikolay Dachev <nikolay@dachev.info>
This commit is contained in:
parent
df88a7ec99
commit
69682054e1
3 changed files with 94 additions and 61 deletions
|
@ -266,6 +266,7 @@ import traceback
|
|||
LIB_IMP_ERR = None
|
||||
try:
|
||||
from librouteros import connect
|
||||
from librouteros.exceptions import LibRouterosError
|
||||
from librouteros.query import Key
|
||||
HAS_LIB = True
|
||||
except Exception as e:
|
||||
|
@ -373,7 +374,7 @@ class ROS_api_module:
|
|||
for i in self.api_path:
|
||||
self.result['message'].append(i)
|
||||
self.return_result(False, True)
|
||||
except Exception as e:
|
||||
except LibRouterosError as e:
|
||||
self.errors(e)
|
||||
|
||||
def api_add(self):
|
||||
|
@ -382,7 +383,7 @@ class ROS_api_module:
|
|||
self.result['message'].append("added: .id= %s"
|
||||
% self.api_path.add(**param))
|
||||
self.return_result(True)
|
||||
except Exception as e:
|
||||
except LibRouterosError as e:
|
||||
self.errors(e)
|
||||
|
||||
def api_remove(self):
|
||||
|
@ -390,7 +391,7 @@ class ROS_api_module:
|
|||
self.api_path.remove(self.remove)
|
||||
self.result['message'].append("removed: .id= %s" % self.remove)
|
||||
self.return_result(True)
|
||||
except Exception as e:
|
||||
except LibRouterosError as e:
|
||||
self.errors(e)
|
||||
|
||||
def api_update(self):
|
||||
|
@ -401,7 +402,7 @@ class ROS_api_module:
|
|||
self.api_path.update(**param)
|
||||
self.result['message'].append("updated: %s" % param)
|
||||
self.return_result(True)
|
||||
except Exception as e:
|
||||
except LibRouterosError as e:
|
||||
self.errors(e)
|
||||
|
||||
def api_query(self):
|
||||
|
@ -440,7 +441,7 @@ class ROS_api_module:
|
|||
msg = msg + ' WHERE %s' % ' '.join(self.where)
|
||||
self.result['message'].append(msg)
|
||||
self.return_result(False)
|
||||
except Exception as e:
|
||||
except LibRouterosError as e:
|
||||
self.errors(e)
|
||||
|
||||
def api_arbitrary(self):
|
||||
|
@ -454,12 +455,12 @@ class ROS_api_module:
|
|||
for i in arbitrary_result:
|
||||
self.result['message'].append(i)
|
||||
self.return_result(False)
|
||||
except Exception as e:
|
||||
except LibRouterosError as e:
|
||||
self.errors(e)
|
||||
|
||||
def return_result(self, ch_status=False, status=True):
|
||||
if status == "False":
|
||||
self.module.fail_json(msg=to_native(self.result['message']))
|
||||
if not status:
|
||||
self.module.fail_json(msg=self.result['message'])
|
||||
else:
|
||||
self.module.exit_json(changed=ch_status,
|
||||
msg=self.result['message'])
|
||||
|
@ -467,7 +468,7 @@ class ROS_api_module:
|
|||
def errors(self, e):
|
||||
if e.__class__.__name__ == 'TrapError':
|
||||
self.result['message'].append("%s" % e)
|
||||
self.return_result(False, True)
|
||||
self.return_result(False, False)
|
||||
self.result['message'].append("%s" % e)
|
||||
self.return_result(False, False)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue