Add support for IOS vlan parsing filter. (#40555)

* Add support for IOS vlan parsing filter.
Example usage below:

{% set parsed_vlans = vlans | vlan_parser %}
switchport trunk allowed vlan {{ parsed_vlans[0] }}
{% for i in range (1, parsed_vlans | count) %}
switchport trunk allowed vlan add {{ parsed_vlans[i] }}

* Update test_network.py

Add import statement for filter

* Fixed PEP8 issues relating to comments

* Fix PEP8 issues related to blank lines

* Removed magic numbers for line lengths. This should generalize support
to other IOS-like NOS that use similar methods for listing vlans. The
default arguments for line lengths will still be specific to Cisco IOS.
The unit tests for line length are still specific to Cisco IOS.
This commit is contained in:
Steve Dodd
2018-10-17 09:20:28 -06:00
committed by Sumit Jaiswal
parent 61ae6424a3
commit ee6ab5d5aa
2 changed files with 95 additions and 2 deletions

View File

@@ -23,7 +23,8 @@ import sys
import pytest
from units.compat import unittest
from ansible.plugins.filter.network import parse_xml, type5_pw, hash_salt, comp_type5
from ansible.plugins.filter.network import parse_xml, type5_pw, hash_salt, comp_type5, vlan_parser
from ansible.errors import AnsibleFilterError
fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures', 'network')
@@ -165,3 +166,21 @@ class TestCompareType5(unittest.TestCase):
encrypted_password = '$1$nTc1$Z28sUTcWfXlvVe2x.3XAa.'
parsed = comp_type5(unencrypted_password, encrypted_password)
self.assertEqual(parsed, False)
class TestVlanParser(unittest.TestCase):
def test_compression(self):
raw_list = [1, 2, 3]
parsed_list = ['1-3']
self.assertEqual(vlan_parser(raw_list), parsed_list)
def test_single_line(self):
raw_list = [100, 1688, 3002, 3003, 3004, 3005, 3102, 3103, 3104, 3105, 3802, 3900, 3998, 3999]
parsed_list = ['100,1688,3002-3005,3102-3105,3802,3900,3998,3999']
self.assertEqual(vlan_parser(raw_list), parsed_list)
def test_multi_line(self):
raw_list = [100, 1688, 3002, 3004, 3005, 3050, 3102, 3104, 3105, 3151, 3802, 3900, 3998, 3999]
parsed_list = ['100,1688,3002,3004,3005,3050,3102,3104,3105,3151', '3802,3900,3998,3999']
self.assertEqual(vlan_parser(raw_list), parsed_list)