mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-06 21:32:49 +00:00
docker_*: always use client.fail() over module.fail_json(), allow to always return data on failure (#51999)
* Always use client.fail() instead of module.fail_json(). * Allow to pass on results on module failure. * Linting.
This commit is contained in:
@@ -20,7 +20,7 @@ import os
|
||||
import re
|
||||
from distutils.version import LooseVersion
|
||||
|
||||
from ansible.module_utils.basic import AnsibleModule, env_fallback, jsonify
|
||||
from ansible.module_utils.basic import AnsibleModule, env_fallback
|
||||
from ansible.module_utils.six.moves.urllib.parse import urlparse
|
||||
from ansible.module_utils.parsing.convert_bool import BOOLEANS_TRUE, BOOLEANS_FALSE
|
||||
|
||||
@@ -164,7 +164,11 @@ class AnsibleDockerClient(Client):
|
||||
def __init__(self, argument_spec=None, supports_check_mode=False, mutually_exclusive=None,
|
||||
required_together=None, required_if=None, min_docker_version=MIN_DOCKER_VERSION,
|
||||
min_docker_api_version=None, option_minimal_versions=None,
|
||||
option_minimal_versions_ignore_params=None):
|
||||
option_minimal_versions_ignore_params=None, fail_results=None):
|
||||
|
||||
# Modules can put information in here which will always be returned
|
||||
# in case client.fail() is called.
|
||||
self.fail_results = fail_results or {}
|
||||
|
||||
merged_arg_spec = dict()
|
||||
merged_arg_spec.update(DOCKER_COMMON_ARGS)
|
||||
@@ -249,8 +253,9 @@ class AnsibleDockerClient(Client):
|
||||
# else:
|
||||
# log_file.write(msg + u'\n')
|
||||
|
||||
def fail(self, msg):
|
||||
self.module.fail_json(msg=msg)
|
||||
def fail(self, msg, **kwargs):
|
||||
self.fail_results.update(kwargs)
|
||||
self.module.fail_json(msg=msg, **sanitize_result(self.fail_results))
|
||||
|
||||
@staticmethod
|
||||
def _get_value(param_name, param_value, env_variable, default_value):
|
||||
@@ -506,7 +511,7 @@ class AnsibleDockerClient(Client):
|
||||
self.log("Inspecting container Id %s" % result['Id'])
|
||||
result = self.inspect_container(container=result['Id'])
|
||||
self.log("Completed container inspection")
|
||||
except NotFound as exc:
|
||||
except NotFound as dummy:
|
||||
return None
|
||||
except Exception as exc:
|
||||
self.fail("Error inspecting container: %s" % exc)
|
||||
@@ -545,7 +550,7 @@ class AnsibleDockerClient(Client):
|
||||
self.log("Inspecting network Id %s" % id)
|
||||
result = self.inspect_network(id)
|
||||
self.log("Completed network inspection")
|
||||
except NotFound as exc:
|
||||
except NotFound as dummy:
|
||||
return None
|
||||
except Exception as exc:
|
||||
self.fail("Error inspecting network: %s" % exc)
|
||||
|
||||
@@ -30,7 +30,7 @@ class AnsibleDockerSwarmClient(AnsibleDockerClient):
|
||||
try:
|
||||
info = self.info()
|
||||
except APIError as exc:
|
||||
self.fail(msg="Failed to get node information for %s" % to_native(exc))
|
||||
self.fail("Failed to get node information for %s" % to_native(exc))
|
||||
|
||||
if info:
|
||||
json_str = json.dumps(info, ensure_ascii=False)
|
||||
@@ -55,7 +55,7 @@ class AnsibleDockerSwarmClient(AnsibleDockerClient):
|
||||
try:
|
||||
info = self.info()
|
||||
except APIError:
|
||||
self.fail(msg="Failed to get host information.")
|
||||
self.fail("Failed to get host information.")
|
||||
|
||||
if info:
|
||||
json_str = json.dumps(info, ensure_ascii=False)
|
||||
@@ -92,7 +92,7 @@ class AnsibleDockerSwarmClient(AnsibleDockerClient):
|
||||
If host is not a swarm manager then Ansible task on this host should end with 'failed' state
|
||||
"""
|
||||
if not self.check_if_swarm_manager():
|
||||
self.fail(msg="Error running docker swarm module: must run on swarm manager node")
|
||||
self.fail("Error running docker swarm module: must run on swarm manager node")
|
||||
|
||||
def check_if_swarm_worker(self):
|
||||
"""
|
||||
@@ -139,20 +139,20 @@ class AnsibleDockerSwarmClient(AnsibleDockerClient):
|
||||
node_id = self.get_swarm_node_id()
|
||||
|
||||
if node_id is None:
|
||||
self.fail(msg="Failed to get node information.")
|
||||
self.fail("Failed to get node information.")
|
||||
|
||||
try:
|
||||
node_info = self.inspect_node(node_id=node_id)
|
||||
except APIError as exc:
|
||||
if exc.status_code == 503:
|
||||
self.fail(msg="Cannot inspect node: To inspect node execute module on Swarm Manager")
|
||||
self.fail("Cannot inspect node: To inspect node execute module on Swarm Manager")
|
||||
if exc.status_code == 404:
|
||||
if skip_missing is False:
|
||||
self.fail(msg="Error while reading from Swarm manager: %s" % to_native(exc))
|
||||
self.fail("Error while reading from Swarm manager: %s" % to_native(exc))
|
||||
else:
|
||||
return None
|
||||
except Exception as exc:
|
||||
self.module.fail_json(msg="Error inspecting swarm node: %s" % exc)
|
||||
self.fail("Error inspecting swarm node: %s" % exc)
|
||||
|
||||
json_str = json.dumps(node_info, ensure_ascii=False)
|
||||
node_info = json.loads(json_str)
|
||||
@@ -169,10 +169,10 @@ class AnsibleDockerSwarmClient(AnsibleDockerClient):
|
||||
node_info = self.nodes()
|
||||
except APIError as exc:
|
||||
if exc.status_code == 503:
|
||||
self.fail(msg="Cannot inspect node: To inspect node execute module on Swarm Manager")
|
||||
self.fail(msg="Error while reading from Swarm manager: %s" % to_native(exc))
|
||||
self.fail("Cannot inspect node: To inspect node execute module on Swarm Manager")
|
||||
self.fail("Error while reading from Swarm manager: %s" % to_native(exc))
|
||||
except Exception as exc:
|
||||
self.module.fail_json(msg="Error inspecting swarm node: %s" % exc)
|
||||
self.fail("Error inspecting swarm node: %s" % exc)
|
||||
|
||||
json_str = json.dumps(node_info, ensure_ascii=False)
|
||||
node_info = json.loads(json_str)
|
||||
|
||||
Reference in New Issue
Block a user