Skip to content

Commit

Permalink
[minor_change] Add support for multi level nested dictionaries in aci…
Browse files Browse the repository at this point in the history
…_listify filter plugin
  • Loading branch information
akinross authored and lhercot committed Mar 1, 2024
1 parent dd6a4b7 commit 0d90d05
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
24 changes: 24 additions & 0 deletions plugins/filter/listify.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

# Copyright: (c) 2017, Ramses Smeyers <[email protected]>
# Copyright: (c) 2023, Shreyas Srish <[email protected]>
# Copyright: (c) 2024, Akini Ross <[email protected]>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import absolute_import, division, print_function
Expand Down Expand Up @@ -58,6 +59,26 @@
vrf: vrf_test
vrf:
- name: vrf_test
policies:
protocol:
bfd:
- name: BFD-ON
description: Enable BFD
admin_state: enabled
detection_multiplier: 3
min_tx_interval: 50
min_rx_interval: 50
echo_rx_interval: 50
echo_admin_state: enabled
sub_interface_optimization_state: enabled
ospf:
interface:
- name: OSPF-P2P-IntPol
network_type: p2p
priority: 1
- name: OSPF-Broadcast-IntPol
network_type: bcast
priority: 1
- name: Create tenants
cisco.aci.aci_tenant:
Expand Down Expand Up @@ -259,6 +280,9 @@ def listify_worker(d, keys, depth, cache, prefix):
if k == keys[depth + 1] and isinstance(v, (dict, list)):
for result in listify_worker({k: v}, keys, depth + 1, cache_work, prefix):
yield result
elif isinstance(item, str) and isinstance(d[keys[depth]], dict):
for result in listify_worker({item: d[keys[depth]][item]}, keys, depth + 1, cache_work, prefix):
yield result


class FilterModule(object):
Expand Down
51 changes: 50 additions & 1 deletion tests/integration/targets/aci_filter_listify/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Test code for the ACI modules
# Copyright: (c) 2023, Shreyas Srish ([email protected])
# Copyright: (c) 2024, Akini Ross <[email protected]>
#
# GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)

Expand All @@ -18,7 +19,7 @@
use_ssl: '{{ aci_use_ssl | default(true) }}'
use_proxy: '{{ aci_use_proxy | default(true) }}'
output_level: '{{ aci_output_level | default("info") }}'
cisco.aci.aci_model_data:
aci_model_data:
tenant:
- name: ansible_test
description: Created using listify
Expand Down Expand Up @@ -55,6 +56,26 @@
bd: web_bd2
- name: app2
bd: app_bd2
policies:
protocol:
bfd:
- name: BFD-ON
description: Enable BFD
admin_state: enabled
detection_multiplier: 3
min_tx_interval: 50
min_rx_interval: 50
echo_rx_interval: 50
echo_admin_state: enabled
sub_interface_optimization_state: enabled
ospf:
interface:
- name: OSPF-P2P-IntPol
network_type: p2p
priority: 1
- name: OSPF-Broadcast-IntPol
network_type: bcast
priority: 1

- name: Verify Cloud and Non-Cloud Sites in use.
ansible.builtin.include_tasks: ../../../../../../integration/targets/aci_cloud_provider/tasks/main.yml
Expand All @@ -63,6 +84,34 @@
when: query_cloud.current == [] # This condition will execute only non-cloud sites
block: # block specifies execution of tasks within, based on conditions

- name: Set facts for nested dictionaries
ansible.builtin.set_fact:
bfd_listify_output: '{{ aci_model_data|cisco.aci.aci_listify("tenant", "policies", "protocol", "bfd") }}'
ospf_listify_output: '{{ aci_model_data|cisco.aci.aci_listify("tenant", "policies", "protocol", "ospf", "interface") }}'

- name: Validate listify for nested dictionaries
ansible.builtin.assert:
that:
- bfd_listify_output.0.tenant_name == "ansible_test2"
- bfd_listify_output.0.tenant_description == "Created using listify"
- bfd_listify_output.0.tenant_policies_protocol_bfd_admin_state == "enabled"
- bfd_listify_output.0.tenant_policies_protocol_bfd_description == "Enable BFD"
- bfd_listify_output.0.tenant_policies_protocol_bfd_detection_multiplier == 3
- bfd_listify_output.0.tenant_policies_protocol_bfd_echo_admin_state == "enabled"
- bfd_listify_output.0.tenant_policies_protocol_bfd_echo_rx_interval == 50
- bfd_listify_output.0.tenant_policies_protocol_bfd_min_rx_interval == 50
- bfd_listify_output.0.tenant_policies_protocol_bfd_min_tx_interval == 50
- bfd_listify_output.0.tenant_policies_protocol_bfd_name == "BFD-ON"
- bfd_listify_output.0.tenant_policies_protocol_bfd_sub_interface_optimization_state == "enabled"
- ospf_listify_output.0.tenant_name == "ansible_test2"
- ospf_listify_output.0.tenant_description == "Created using listify"
- ospf_listify_output.0.tenant_policies_protocol_ospf_interface_name == "OSPF-P2P-IntPol"
- ospf_listify_output.0.tenant_policies_protocol_ospf_interface_network_type == "p2p"
- ospf_listify_output.0.tenant_policies_protocol_ospf_interface_priority == 1
- ospf_listify_output.1.tenant_policies_protocol_ospf_interface_name == "OSPF-Broadcast-IntPol"
- ospf_listify_output.1.tenant_policies_protocol_ospf_interface_network_type == "bcast"
- ospf_listify_output.1.tenant_policies_protocol_ospf_interface_priority == 1

- name: Create tenants
cisco.aci.aci_tenant:
<<: *aci_info
Expand Down

0 comments on commit 0d90d05

Please sign in to comment.