From 49c8fd0aa55aaf6d0ff09733376731ae5602333f Mon Sep 17 00:00:00 2001 From: Ajpantuso Date: Tue, 22 Jun 2021 16:27:38 -0400 Subject: [PATCH] docker_host_info - Allow filters which are passed as lists (#160) * Initial Commit * Adding integration tests * Adding example in docs * Adding changelog fragment * Applying initial review suggestions --- ...60-docker_host_info-label-fitler-lists.yml | 4 ++ plugins/module_utils/common.py | 23 +++++++---- plugins/modules/docker_host_info.py | 19 ++++++++- .../docker_host_info/tasks/test_host_info.yml | 41 +++++++++++++++++++ 4 files changed, 77 insertions(+), 10 deletions(-) create mode 100644 changelogs/fragments/160-docker_host_info-label-fitler-lists.yml diff --git a/changelogs/fragments/160-docker_host_info-label-fitler-lists.yml b/changelogs/fragments/160-docker_host_info-label-fitler-lists.yml new file mode 100644 index 00000000..4b58bfc4 --- /dev/null +++ b/changelogs/fragments/160-docker_host_info-label-fitler-lists.yml @@ -0,0 +1,4 @@ +--- +minor_changes: + - docker_host_info - allow values for keys in ``containers_filters``, ``images_filters``, ``networks_filters``, and + ``volumes_filters`` to be passed as YAML lists (https://github.com/ansible-collections/community.docker/pull/160). diff --git a/plugins/module_utils/common.py b/plugins/module_utils/common.py index 7e9f4a2e..99fb59c5 100644 --- a/plugins/module_utils/common.py +++ b/plugins/module_utils/common.py @@ -15,6 +15,7 @@ from distutils.version import LooseVersion from ansible.module_utils.basic import AnsibleModule, env_fallback, missing_required_lib +from ansible.module_utils.common.collections import is_sequence from ansible.module_utils.common._collections_compat import Mapping, Sequence from ansible.module_utils.six import string_types from ansible.module_utils.six.moves.urllib.parse import urlparse @@ -927,23 +928,27 @@ class DifferenceTracker(object): return result -def clean_dict_booleans_for_docker_api(data): +def clean_dict_booleans_for_docker_api(data, allow_sequences=False): ''' Go doesn't like Python booleans 'True' or 'False', while Ansible is just fine with them in YAML. As such, they need to be converted in cases where we pass dictionaries to the Docker API (e.g. docker_network's - driver_options and docker_prune's filters). + driver_options and docker_prune's filters). When `allow_sequences=True` + YAML sequences (lists, tuples) are converted to [str] instead of str([...]) + which is the expected format of filters which accept lists such as labels. ''' + def sanitize(value): + if value is True: + return 'true' + elif value is False: + return 'false' + else: + return str(value) + result = dict() if data is not None: for k, v in data.items(): - if v is True: - v = 'true' - elif v is False: - v = 'false' - else: - v = str(v) - result[str(k)] = v + result[str(k)] = [sanitize(e) for e in v] if allow_sequences and is_sequence(v) else sanitize(v) return result diff --git a/plugins/modules/docker_host_info.py b/plugins/modules/docker_host_info.py index 3700a32a..599a92d7 100644 --- a/plugins/modules/docker_host_info.py +++ b/plugins/modules/docker_host_info.py @@ -33,6 +33,8 @@ options: description: - A dictionary of filter values used for selecting containers to list. - "For example, C(until: 24h)." + - C(label) is a special case of filter which can be a string C() matching when a label is present, a string + C(=) matching when a label has a particular value, or a list of strings C()/C(=) matching when a label is present, a string + C(=) matching when a label has a particular value, or a list of strings C()/C(=) matching when a label is present, a string + C(=) matching when a label has a particular value, or a list of strings C()/C(=) matching when a label is present, a string + C(=) matching when a label has a particular value, or a list of strings C()/C(=