Skip to content

Commit

Permalink
Update according PR ansible-collections#8456
Browse files Browse the repository at this point in the history
  • Loading branch information
vbotka committed Jun 3, 2024
1 parent 5352b7b commit b12b604
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 27 deletions.
58 changes: 40 additions & 18 deletions plugins/filter/remove_keys.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2024 Vladimir Botka <vbotka@gmail.com>
# Copyright (c) 2024 Felix Fontein <felix@fontein.de>
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later

Expand All @@ -10,22 +11,22 @@
name: remove_keys
short_description: Remove specific keys from dictionaries in a list
version_added: "9.1.0"
author: Vladimir Botka (@vbotka)
author:
- Vladimir Botka (@vbotka)
- Felix Fontein (@felixfontein)
description: This filter removes only specified keys from a provided list of dictionaries.
options:
_input:
description:
- A list of dictionaries.
- All keys must be strings.
- Top level keys must be strings.
type: list
elements: dictionary
required: true
target:
description:
- A list of keys or keys patterns to remove.
- The interpretation of O(target) depends on the option O(matching_parameter)
- Single item is required in O(target) list for O(matching_parameter=regex)
- The O(target) can be a string for O(matching_parameter=regex)
- A single key or key pattern to keep, or a list of keys or keys patterns to keep.
- If O(matching_parameter=regex) there must be exactly one pattern provided.
type: raw
required: true
matching_parameter:
Expand All @@ -46,33 +47,54 @@
- {k0_x0: A0, k1_x1: B0, k2_x2: [C0], k3_x3: foo}
- {k0_x0: A1, k1_x1: B1, k2_x2: [C1], k3_x3: bar}
# By default match equal keys.
# 1) By default match keys that equal any of the items in the target.
t: [k0_x0, k1_x1]
r: "{{ l | remove_keys(target=t) }}"
r: "{{ l | community.general.remove_keys(target=t) }}"
# Match keys that starts with any of the items in the target.
# 2) Match keys that start with any of the items in the target.
t: [k0, k1]
r: "{{ l | remove_keys(target=t, matching_parameter='starts_with') }}"
r: "{{ l | community.general.remove_keys(target=t, matching_parameter='starts_with') }}"
# Match keys that ends with any of the items in target.
# 3) Match keys that end with any of the items in target.
t: [x0, x1]
r: "{{ l | remove_keys(target=t, matching_parameter='ends_with') }}"
r: "{{ l | community.general.remove_keys(target=t, matching_parameter='ends_with') }}"
# Match keys by the regex.
# 4) Match keys by the regex.
t: ['^.*[01]_x.*$']
r: "{{ l | remove_keys(target=t, matching_parameter='regex') }}"
r: "{{ l | community.general.remove_keys(target=t, matching_parameter='regex') }}"
# Match keys by the regex. The regex does not have to be in list.
# 5) Match keys by the regex.
t: '^.*[01]_x.*$'
r: "{{ l | remove_keys(target=t, matching_parameter='regex') }}"
r: "{{ l | community.general.remove_keys(target=t, matching_parameter='regex') }}"
# The results of all examples are all the same.
# The results of above examples 1-5 are all the same.
r:
- {k2_x2: [C0], k3_x3: foo}
- {k2_x2: [C1], k3_x3: bar}
# 6) By default match keys that equal the target.
t: k0_x0
r: "{{ l | community.general.remove_keys(target=t) }}"
# 7) Match keys that start with the target.
t: k0
r: "{{ l | community.general.remove_keys(target=t, matching_parameter='starts_with') }}"
# 8) Match keys that end with the target.
t: x0
r: "{{ l | community.general.remove_keys(target=t, matching_parameter='ends_with') }}"
# 9) Match keys by the regex.
t: '^.*0_x.*$'
r: "{{ l | community.general.remove_keys(target=t, matching_parameter='regex') }}"
# The results of above examples 6-9 are all the same.
r:
- {k1_x1: B0, k2_x2: [C0], k3_x3: foo}
- {k1_x1: B1, k2_x2: [C1], k3_x3: bar}
'''

RETURN = '''
RETURN = '''community.general.
_value:
description: The list of dictionaries with selected keys.
type: list
Expand Down
19 changes: 11 additions & 8 deletions plugins/plugin_utils/keys_filter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Copyright (c) 2024 Vladimir Botka <vbotka@gmail.com>
# Copyright (c) 2024 Felix Fontein <felix@fontein.de>
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later

Expand Down Expand Up @@ -33,12 +34,11 @@ def _keys_filter_params(data, target, matching_parameter):

for elem in data:
if not all(isinstance(item, string_types) for item in elem.keys()):
msg = "All keys must be strings. keys: %s"
msg = "Top level keys must be strings. keys: %s"
raise AnsibleFilterError(msg % elem.keys())

if not isinstance(target, Sequence):
msg = ("The target must be a list. It can be a string if matching_parameter is regex."
"target is %s.")
msg = ("The target must be a string or a list. target is %s.")
raise AnsibleFilterError(msg % target)

if len(target) == 0:
Expand All @@ -54,10 +54,11 @@ def _keys_filter_params(data, target, matching_parameter):

def _keys_filter_target_str(target, matching_parameter):
"""test:
* target is a list of strings, or
* target is a string or list with single string if matching_parameter=regex
* If target is list all items are strings
* If matching_parameter=regex target is a string or list with single string
convert and return:
* tuple of unique target items, or
* tuple with single item, or
* compiled regex if matching_parameter=regex
"""

Expand All @@ -80,10 +81,12 @@ def _keys_filter_target_str(target, matching_parameter):
tt = re.compile(r)
except re.error:
msg = ("The target must be a valid regex if matching_parameter is regex."
"target is %s")
" target is %s")
raise AnsibleFilterError(msg % r)
elif isinstance(target, string_types):
tt = (target, )
else:
tt = tuple(target)
tt = tuple(set(target))

return tt

Expand All @@ -94,7 +97,7 @@ def _keys_filter_target_dict(target, matching_parameter):
* ...
"""

# TODO: Complete and use this in filter replace_kees
# TODO: Complete and use this in filter replace_keys

if isinstance(target, list):
for elem in target:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,28 @@
fail_msg: |
[ERR] result:
{{ rr | to_yaml }}
quiet: "{{ quite_test | d(true) | bool }}"
quiet: "{{ quiet_test | d(true) | bool }}"
loop: "{{ targets1 }}"
loop_control:
label: "{{ item.mp }}: {{ item.tt }}"
vars:
rr: "{{ list1 | community.general.remove_keys(target=item.tt, matching_parameter=item.mp) }}"
tags: targets1

- name: Test remove keys targets2
ansible.builtin.assert:
that:
- (rr | difference(result2) | length) == 0
success_msg: |
[OK] result:
{{ rr | to_yaml }}
fail_msg: |
[ERR] result:
{{ rr | to_yaml }}
quiet: "{{ quiet_test | d(true) | bool }}"
loop: "{{ targets2 }}"
loop_control:
label: "{{ item.mp }}: {{ item.tt }}"
vars:
rr: "{{ list2 | community.general.remove_keys(target=item.tt, matching_parameter=item.mp) }}"
tags: targets1
14 changes: 14 additions & 0 deletions tests/integration/targets/filter_remove_keys/vars/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,17 @@ list1:
result1:
- {k2_x2: [C0], k3_x3: foo}
- {k2_x2: [C1], k3_x3: bar}

targets2:
- {mp: equal, tt: k0_x0}
- {mp: starts_with, tt: k0}
- {mp: ends_with, tt: x0}
- {mp: regex, tt: '^.*0_x.*$'}

list2:
- {k0_x0: A0, k1_x1: B0, k2_x2: [C0], k3_x3: foo}
- {k0_x0: A1, k1_x1: B1, k2_x2: [C1], k3_x3: bar}

result2:
- {k1_x1: B0, k2_x2: [C0], k3_x3: foo}
- {k1_x1: B1, k2_x2: [C1], k3_x3: bar}

0 comments on commit b12b604

Please sign in to comment.