Handle ConnectionError exception in network modules (#43353)

* Handle ConnectionError exception in network modules

* Catch ConnectionError expection and fail module in case
  expection is raised.

* Fix CI failure
This commit is contained in:
Ganesh Nalawade 2018-07-30 10:24:58 +05:30 committed by GitHub
commit 21dcaa4349
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 241 additions and 117 deletions

View file

@ -81,7 +81,11 @@ def get_capabilities(module):
if hasattr(module, '_vyos_capabilities'):
return module._vyos_capabilities
capabilities = Connection(module._socket_path).get_capabilities()
try:
capabilities = Connection(module._socket_path).get_capabilities()
except ConnectionError as exc:
module.fail_json(msg=to_text(exc, errors='surrogate_then_replace'))
module._vyos_capabilities = json.loads(capabilities)
return module._vyos_capabilities
@ -93,7 +97,10 @@ def get_config(module):
return _DEVICE_CONFIGS
else:
connection = get_connection(module)
out = connection.get_config()
try:
out = connection.get_config()
except ConnectionError as exc:
module.fail_json(msg=to_text(exc, errors='surrogate_then_replace'))
cfg = to_text(out, errors='surrogate_then_replace').strip()
_DEVICE_CONFIGS = cfg
return cfg
@ -101,15 +108,19 @@ def get_config(module):
def run_commands(module, commands, check_rc=True):
connection = get_connection(module)
return connection.run_commands(commands=commands, check_rc=check_rc)
try:
response = connection.run_commands(commands=commands, check_rc=check_rc)
except ConnectionError as exc:
module.fail_json(msg=to_text(exc, errors='surrogate_then_replace'))
return response
def load_config(module, commands, commit=False, comment=None):
connection = get_connection(module)
try:
resp = connection.edit_config(candidate=commands, commit=commit, comment=comment)
response = connection.edit_config(candidate=commands, commit=commit, comment=comment)
except ConnectionError as exc:
module.fail_json(msg=to_text(exc))
module.fail_json(msg=to_text(exc, errors='surrogate_then_replace'))
return resp.get('diff')
return response.get('diff')