mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-26 12:21:26 -07:00
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:
parent
a44adc1dc9
commit
21dcaa4349
13 changed files with 241 additions and 117 deletions
|
@ -22,7 +22,7 @@ from contextlib import contextmanager
|
|||
from copy import deepcopy
|
||||
|
||||
from ansible.module_utils.basic import env_fallback, return_values
|
||||
from ansible.module_utils.connection import Connection
|
||||
from ansible.module_utils.connection import Connection, ConnectionError
|
||||
from ansible.module_utils.network.common.netconf import NetconfConnection
|
||||
from ansible.module_utils._text import to_text
|
||||
|
||||
|
@ -93,7 +93,10 @@ def get_capabilities(module):
|
|||
if hasattr(module, '_junos_capabilities'):
|
||||
return module._junos_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._junos_capabilities = json.loads(capabilities)
|
||||
return module._junos_capabilities
|
||||
|
||||
|
@ -125,12 +128,15 @@ def load_configuration(module, candidate=None, action='merge', rollback=None, fo
|
|||
module.fail_json(msg='format must be text when action is set')
|
||||
|
||||
conn = get_connection(module)
|
||||
if rollback is not None:
|
||||
_validate_rollback_id(module, rollback)
|
||||
obj = Element('load-configuration', {'rollback': str(rollback)})
|
||||
conn.execute_rpc(tostring(obj))
|
||||
else:
|
||||
return conn.load_configuration(config=candidate, action=action, format=format)
|
||||
try:
|
||||
if rollback is not None:
|
||||
_validate_rollback_id(module, rollback)
|
||||
obj = Element('load-configuration', {'rollback': str(rollback)})
|
||||
conn.execute_rpc(tostring(obj))
|
||||
else:
|
||||
return conn.load_configuration(config=candidate, action=action, format=format)
|
||||
except ConnectionError as exc:
|
||||
module.fail_json(msg=to_text(exc, errors='surrogate_then_replace'))
|
||||
|
||||
|
||||
def get_configuration(module, compare=False, format='xml', rollback='0', filter=None):
|
||||
|
@ -138,26 +144,30 @@ def get_configuration(module, compare=False, format='xml', rollback='0', filter=
|
|||
module.fail_json(msg='invalid config format specified')
|
||||
|
||||
conn = get_connection(module)
|
||||
if compare:
|
||||
xattrs = {'format': format}
|
||||
_validate_rollback_id(module, rollback)
|
||||
xattrs['compare'] = 'rollback'
|
||||
xattrs['rollback'] = str(rollback)
|
||||
reply = conn.execute_rpc(tostring(Element('get-configuration', xattrs)))
|
||||
else:
|
||||
reply = conn.get_configuration(format=format, filter=filter)
|
||||
|
||||
try:
|
||||
if compare:
|
||||
xattrs = {'format': format}
|
||||
_validate_rollback_id(module, rollback)
|
||||
xattrs['compare'] = 'rollback'
|
||||
xattrs['rollback'] = str(rollback)
|
||||
reply = conn.execute_rpc(tostring(Element('get-configuration', xattrs)))
|
||||
else:
|
||||
reply = conn.get_configuration(format=format, filter=filter)
|
||||
except ConnectionError as exc:
|
||||
module.fail_json(msg=to_text(exc, errors='surrogate_then_replace'))
|
||||
return reply
|
||||
|
||||
|
||||
def commit_configuration(module, confirm=False, check=False, comment=None, confirm_timeout=None, synchronize=False,
|
||||
at_time=None, exit=False):
|
||||
conn = get_connection(module)
|
||||
if check:
|
||||
reply = conn.validate()
|
||||
else:
|
||||
reply = conn.commit(confirmed=confirm, timeout=confirm_timeout, comment=comment, synchronize=synchronize, at_time=at_time)
|
||||
|
||||
try:
|
||||
if check:
|
||||
reply = conn.validate()
|
||||
else:
|
||||
reply = conn.commit(confirmed=confirm, timeout=confirm_timeout, comment=comment, synchronize=synchronize, at_time=at_time)
|
||||
except ConnectionError as exc:
|
||||
module.fail_json(msg=to_text(exc, errors='surrogate_then_replace'))
|
||||
return reply
|
||||
|
||||
|
||||
|
@ -165,17 +175,29 @@ def command(module, cmd, format='text', rpc_only=False):
|
|||
conn = get_connection(module)
|
||||
if rpc_only:
|
||||
cmd += ' | display xml rpc'
|
||||
return conn.command(command=cmd, format=format)
|
||||
try:
|
||||
response = conn.command(command=cmd, format=format)
|
||||
except ConnectionError as exc:
|
||||
module.fail_json(msg=to_text(exc, errors='surrogate_then_replace'))
|
||||
return response
|
||||
|
||||
|
||||
def lock_configuration(x):
|
||||
conn = get_connection(x)
|
||||
return conn.lock()
|
||||
def lock_configuration(module):
|
||||
conn = get_connection(module)
|
||||
try:
|
||||
response = conn.lock()
|
||||
except ConnectionError as exc:
|
||||
module.fail_json(msg=to_text(exc, errors='surrogate_then_replace'))
|
||||
return response
|
||||
|
||||
|
||||
def unlock_configuration(x):
|
||||
conn = get_connection(x)
|
||||
return conn.unlock()
|
||||
def unlock_configuration(module):
|
||||
conn = get_connection(module)
|
||||
try:
|
||||
response = conn.unlock()
|
||||
except ConnectionError as exc:
|
||||
module.fail_json(msg=to_text(exc, errors='surrogate_then_replace'))
|
||||
return response
|
||||
|
||||
|
||||
@contextmanager
|
||||
|
@ -189,7 +211,11 @@ def locked_config(module):
|
|||
|
||||
def discard_changes(module):
|
||||
conn = get_connection(module)
|
||||
return conn.discard_changes()
|
||||
try:
|
||||
response = conn.discard_changes()
|
||||
except ConnectionError as exc:
|
||||
module.fail_json(msg=to_text(exc, errors='surrogate_then_replace'))
|
||||
return response
|
||||
|
||||
|
||||
def get_diff(module, rollback='0'):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue