mirror of
https://github.com/ansible-collections/community.docker.git
synced 2026-07-29 11:55:04 +00:00
Python code modernization, 5/n (#1165)
* Address raise-missing-from. * Address simplifiable-if-expression. * Address unnecessary-dunder-call. * Address unnecessary-pass. * Address use-list-literal. * Address unused-variable. * Address use-dict-literal.
This commit is contained in:
@@ -228,7 +228,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable):
|
||||
}
|
||||
containers = client.get_json("/containers/json", params=params)
|
||||
except APIError as exc:
|
||||
raise AnsibleError(f"Error listing containers: {exc}")
|
||||
raise AnsibleError(f"Error listing containers: {exc}") from exc
|
||||
|
||||
if add_legacy_groups:
|
||||
self.inventory.add_group("running")
|
||||
@@ -247,26 +247,28 @@ class InventoryModule(BaseInventoryPlugin, Constructable):
|
||||
short_container_id = container_id[:13]
|
||||
|
||||
try:
|
||||
name = container.get("Names", list())[0].lstrip("/")
|
||||
name = container.get("Names", [])[0].lstrip("/")
|
||||
full_name = name
|
||||
except IndexError:
|
||||
name = short_container_id
|
||||
full_name = container_id
|
||||
|
||||
facts = dict(
|
||||
docker_name=make_unsafe(name),
|
||||
docker_short_id=make_unsafe(short_container_id),
|
||||
)
|
||||
full_facts = dict()
|
||||
facts = {
|
||||
"docker_name": make_unsafe(name),
|
||||
"docker_short_id": make_unsafe(short_container_id),
|
||||
}
|
||||
full_facts = {}
|
||||
|
||||
try:
|
||||
inspect = client.get_json("/containers/{0}/json", container_id)
|
||||
except APIError as exc:
|
||||
raise AnsibleError(f"Error inspecting container {name} - {exc}")
|
||||
raise AnsibleError(
|
||||
f"Error inspecting container {name} - {exc}"
|
||||
) from exc
|
||||
|
||||
state = inspect.get("State") or dict()
|
||||
config = inspect.get("Config") or dict()
|
||||
labels = config.get("Labels") or dict()
|
||||
state = inspect.get("State") or {}
|
||||
config = inspect.get("Config") or {}
|
||||
labels = config.get("Labels") or {}
|
||||
|
||||
running = state.get("Running")
|
||||
|
||||
@@ -298,7 +300,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable):
|
||||
port_settings = network_settings.get("Ports") or {}
|
||||
port = port_settings.get(f"{ssh_port}/tcp")[0]
|
||||
except (IndexError, AttributeError, TypeError):
|
||||
port = dict()
|
||||
port = {}
|
||||
|
||||
try:
|
||||
ip = default_ip if port["HostIp"] == "0.0.0.0" else port["HostIp"]
|
||||
@@ -306,23 +308,23 @@ class InventoryModule(BaseInventoryPlugin, Constructable):
|
||||
ip = ""
|
||||
|
||||
facts.update(
|
||||
dict(
|
||||
ansible_ssh_host=ip,
|
||||
ansible_ssh_port=port.get("HostPort", 0),
|
||||
)
|
||||
{
|
||||
"ansible_ssh_host": ip,
|
||||
"ansible_ssh_port": port.get("HostPort", 0),
|
||||
}
|
||||
)
|
||||
elif connection_type == "docker-cli":
|
||||
facts.update(
|
||||
dict(
|
||||
ansible_host=full_name,
|
||||
)
|
||||
{
|
||||
"ansible_host": full_name,
|
||||
}
|
||||
)
|
||||
ansible_connection = "community.docker.docker"
|
||||
elif connection_type == "docker-api":
|
||||
facts.update(
|
||||
dict(
|
||||
ansible_host=full_name,
|
||||
)
|
||||
{
|
||||
"ansible_host": full_name,
|
||||
}
|
||||
)
|
||||
facts.update(extra_facts)
|
||||
ansible_connection = "community.docker.docker_api"
|
||||
@@ -401,8 +403,8 @@ class InventoryModule(BaseInventoryPlugin, Constructable):
|
||||
try:
|
||||
self._populate(client)
|
||||
except DockerException as e:
|
||||
raise AnsibleError(f"An unexpected Docker error occurred: {e}")
|
||||
raise AnsibleError(f"An unexpected Docker error occurred: {e}") from e
|
||||
except RequestException as e:
|
||||
raise AnsibleError(
|
||||
f"An unexpected requests error occurred when trying to talk to the Docker daemon: {e}"
|
||||
)
|
||||
) from e
|
||||
|
||||
@@ -132,7 +132,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
|
||||
try:
|
||||
self.docker_machine_path = get_bin_path("docker-machine")
|
||||
except ValueError as e:
|
||||
raise AnsibleError(to_native(e))
|
||||
raise AnsibleError(to_native(e)) from e
|
||||
|
||||
command = [self.docker_machine_path]
|
||||
command.extend(args)
|
||||
|
||||
@@ -184,19 +184,19 @@ class InventoryModule(BaseInventoryPlugin, Constructable):
|
||||
raise AnsibleError(msg)
|
||||
|
||||
def _populate(self):
|
||||
raw_params = dict(
|
||||
docker_host=self.get_option("docker_host"),
|
||||
tls=self.get_option("tls"),
|
||||
tls_verify=self.get_option("validate_certs"),
|
||||
key_path=self.get_option("client_key"),
|
||||
cacert_path=self.get_option("ca_path"),
|
||||
cert_path=self.get_option("client_cert"),
|
||||
tls_hostname=self.get_option("tls_hostname"),
|
||||
api_version=self.get_option("api_version"),
|
||||
timeout=self.get_option("timeout"),
|
||||
use_ssh_client=self.get_option("use_ssh_client"),
|
||||
debug=None,
|
||||
)
|
||||
raw_params = {
|
||||
"docker_host": self.get_option("docker_host"),
|
||||
"tls": self.get_option("tls"),
|
||||
"tls_verify": self.get_option("validate_certs"),
|
||||
"key_path": self.get_option("client_key"),
|
||||
"cacert_path": self.get_option("ca_path"),
|
||||
"cert_path": self.get_option("client_cert"),
|
||||
"tls_hostname": self.get_option("tls_hostname"),
|
||||
"api_version": self.get_option("api_version"),
|
||||
"timeout": self.get_option("timeout"),
|
||||
"use_ssh_client": self.get_option("use_ssh_client"),
|
||||
"debug": None,
|
||||
}
|
||||
update_tls_hostname(raw_params)
|
||||
connect_params = get_connect_params(raw_params, fail_function=self._fail)
|
||||
client = docker.DockerClient(**connect_params)
|
||||
@@ -305,7 +305,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable):
|
||||
except Exception as e:
|
||||
raise AnsibleError(
|
||||
f"Unable to fetch hosts from Docker swarm API, this was the original exception: {e}"
|
||||
)
|
||||
) from e
|
||||
|
||||
def verify_file(self, path):
|
||||
"""Return the possibly of a file being consumable by this plugin."""
|
||||
|
||||
Reference in New Issue
Block a user