From c962cc7ec805fa0b6e1960937cbeb94ad1c6dde0 Mon Sep 17 00:00:00 2001 From: Trishna Guha Date: Fri, 18 May 2018 10:43:01 +0530 Subject: [PATCH 1/4] Add vlan_expand and vlan_compress filter plugins Signed-off-by: Trishna Guha --- README.md | 7 +++++ filter_plugins/network_engine.py | 52 +++++++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f3f53e1..df0402a 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,13 @@ The following is a list of plugins that are provided by this role. * `json_template` [[source]](https://github.com/ansible-network/network-engine/blob/devel/lookup_plugins/json_template.py) * `network_template` [[source]](https://github.com/ansible-network/network-engine/blob/devel/lookup_plugins/network_template.py) +### Filter + +* `interface_split` [[source]](https://github.com/ansible-network/network-engine/blob/devel/filter_plugins/network_engine.py) +* `interface_range` [[source]](https://github.com/ansible-network/network-engine/blob/devel/filter_plugins/network_engine.py) +* `vlan_expand` [[source]](https://github.com/ansible-network/network-engine/blob/devel/filter_plugins/network_engine.py) +* `vlan_compress` [[source]](https://github.com/ansible-network/network-engine/blob/devel/filter_plugins/network_engine.py) + ## Dependencies The following is the list of dependencies on other roles this role requires. diff --git a/filter_plugins/network_engine.py b/filter_plugins/network_engine.py index c35f723..88e776b 100644 --- a/filter_plugins/network_engine.py +++ b/filter_plugins/network_engine.py @@ -54,11 +54,61 @@ def interface_range(interface): return ['%s%s' % (prefix, index) for index in indicies] +def _gen_ranges(vlan): + s = e = None + for i in sorted(vlan): + if s is None: + s = e = i + elif i == e or i == e + 1: + e = i + else: + yield (s, e) + s = e = i + if s is not None: + yield (s, e) + + +def vlan_compress(vlan): + if not isinstance(vlan, list): + raise AnsibleFilterError('value must be of type list, got %s' % type(vlan)) + + return (','.join(['%d' % s if s == e else '%d-%d' % (s, e) for (s, e) in _gen_ranges(vlan)])) + + +def vlan_expand(vlan): + if not isinstance(vlan, string_types): + raise AnsibleFilterError('value must be of type string, got %s' % type(vlan)) + + match = re.match(r'([A-Za-z]*)(.+)', vlan) + if not match: + raise FilterError('unable to parse vlan %s' % vlan) + + index = match.group(2) + indices = list() + + for item in index.split(','): + tokens = item.split('-') + + if len(tokens) == 1: + indices.append(int(tokens[0])) + + elif len(tokens) == 2: + start, end = tokens + for i in range(int(start), int(end) + 1): + indices.append(i) + i += 1 + + return ['%d' % int(index) for index in indices] + + + class FilterModule(object): ''' Network interface filter ''' def filters(self): return { 'interface_split': interface_split, - 'interface_range': interface_range + 'interface_range': interface_range, + 'vlan_compress': vlan_compress, + 'vlan_expand': vlan_expand } From cedda184a8e43ea25cb1d8fa7e8937d76cd6d963 Mon Sep 17 00:00:00 2001 From: Trishna Guha Date: Fri, 18 May 2018 10:47:00 +0530 Subject: [PATCH 2/4] Add test for vlan filter plugins Signed-off-by: Trishna Guha --- tests/test.yml | 2 ++ tests/vlan_compress/test.yml | 4 +++ .../vlan_compress/meta/main.yaml | 3 ++ .../vlan_compress/tasks/main.yaml | 3 ++ .../vlan_compress/tasks/vlan_compress.yaml | 35 +++++++++++++++++++ tests/vlan_expand/test.yml | 4 +++ tests/vlan_expand/vlan_expand/meta/main.yaml | 3 ++ tests/vlan_expand/vlan_expand/tasks/main.yaml | 3 ++ .../vlan_expand/tasks/vlan_expand.yaml | 34 ++++++++++++++++++ 9 files changed, 91 insertions(+) create mode 100644 tests/vlan_compress/test.yml create mode 100644 tests/vlan_compress/vlan_compress/meta/main.yaml create mode 100644 tests/vlan_compress/vlan_compress/tasks/main.yaml create mode 100644 tests/vlan_compress/vlan_compress/tasks/vlan_compress.yaml create mode 100644 tests/vlan_expand/test.yml create mode 100644 tests/vlan_expand/vlan_expand/meta/main.yaml create mode 100644 tests/vlan_expand/vlan_expand/tasks/main.yaml create mode 100644 tests/vlan_expand/vlan_expand/tasks/vlan_expand.yaml diff --git a/tests/test.yml b/tests/test.yml index e4f0426..fc39ee2 100644 --- a/tests/test.yml +++ b/tests/test.yml @@ -6,3 +6,5 @@ - import_playbook: command_parser/test.yml - import_playbook: textfsm_parser/test.yml - import_playbook: json_template/test.yml +- import_playbook: vlan_compress/test.yml +- import_playbook: vlan_expand/test.yml diff --git a/tests/vlan_compress/test.yml b/tests/vlan_compress/test.yml new file mode 100644 index 0000000..ebd2789 --- /dev/null +++ b/tests/vlan_compress/test.yml @@ -0,0 +1,4 @@ +- hosts: localhost + connection: local + roles: + - vlan_compress diff --git a/tests/vlan_compress/vlan_compress/meta/main.yaml b/tests/vlan_compress/vlan_compress/meta/main.yaml new file mode 100644 index 0000000..d64f158 --- /dev/null +++ b/tests/vlan_compress/vlan_compress/meta/main.yaml @@ -0,0 +1,3 @@ +--- +dependencies: + - ../../../network-engine diff --git a/tests/vlan_compress/vlan_compress/tasks/main.yaml b/tests/vlan_compress/vlan_compress/tasks/main.yaml new file mode 100644 index 0000000..f893cf0 --- /dev/null +++ b/tests/vlan_compress/vlan_compress/tasks/main.yaml @@ -0,0 +1,3 @@ +--- +- name: vlan_compress test + import_tasks: vlan_compress.yaml diff --git a/tests/vlan_compress/vlan_compress/tasks/vlan_compress.yaml b/tests/vlan_compress/vlan_compress/tasks/vlan_compress.yaml new file mode 100644 index 0000000..49bca8b --- /dev/null +++ b/tests/vlan_compress/vlan_compress/tasks/vlan_compress.yaml @@ -0,0 +1,35 @@ +- name: vlan_compress single vlan + debug: + msg: "{{ [1] | vlan_compress }}" + register: result + +- assert: + that: + - "'1' in result.msg" + +- name: vlan_compress list of vlans1 + debug: + msg: "{{ [1,2,3,4,5] | vlan_compress }}" + register: result + +- assert: + that: + - "'1-5' in result.msg" + +- name: vlan_compress list of vlans2 + debug: + msg: "{{ [1,2,3,5] | vlan_compress }}" + register: result + +- assert: + that: + - "'1-3,5' in result.msg" + +- name: vlan_compress list of vlans3 + debug: + msg: "{{ [1,2,4,5,6] | vlan_compress }}" + register: result + +- assert: + that: + - "'1-2,4-6' in result.msg" diff --git a/tests/vlan_expand/test.yml b/tests/vlan_expand/test.yml new file mode 100644 index 0000000..dba8945 --- /dev/null +++ b/tests/vlan_expand/test.yml @@ -0,0 +1,4 @@ +- hosts: localhost + connection: local + roles: + - vlan_expand diff --git a/tests/vlan_expand/vlan_expand/meta/main.yaml b/tests/vlan_expand/vlan_expand/meta/main.yaml new file mode 100644 index 0000000..d64f158 --- /dev/null +++ b/tests/vlan_expand/vlan_expand/meta/main.yaml @@ -0,0 +1,3 @@ +--- +dependencies: + - ../../../network-engine diff --git a/tests/vlan_expand/vlan_expand/tasks/main.yaml b/tests/vlan_expand/vlan_expand/tasks/main.yaml new file mode 100644 index 0000000..642a405 --- /dev/null +++ b/tests/vlan_expand/vlan_expand/tasks/main.yaml @@ -0,0 +1,3 @@ +--- +- name: vlan_expand test + import_tasks: vlan_expand.yaml diff --git a/tests/vlan_expand/vlan_expand/tasks/vlan_expand.yaml b/tests/vlan_expand/vlan_expand/tasks/vlan_expand.yaml new file mode 100644 index 0000000..47a7d42 --- /dev/null +++ b/tests/vlan_expand/vlan_expand/tasks/vlan_expand.yaml @@ -0,0 +1,34 @@ +- name: vlan_expand single vlan + debug: + msg: "{{ 'vlan1' | vlan_expand }}" + register: result + +- assert: + that: + - "'1' in result.msg" + +- name: vlan_expand range of vlans1 + debug: + msg: "{{ 'vlan1-5' | vlan_expand }}" + register: result + +- assert: + that: + - "'1' in result.msg" + - "'2' in result.msg" + - "'3' in result.msg" + - "'4' in result.msg" + - "'5' in result.msg" + +- name: vlan_expand range of vlans2 + debug: + msg: "{{ 'vlan1,3-5,7' | vlan_expand }}" + register: result + +- assert: + that: + - "'1' in result.msg" + - "'3' in result.msg" + - "'4' in result.msg" + - "'5' in result.msg" + - "'7' in result.msg" From 8c417f4a998217bb0556eee61e2ebe561519a0c0 Mon Sep 17 00:00:00 2001 From: Trishna Guha Date: Fri, 18 May 2018 15:39:05 +0530 Subject: [PATCH 3/4] Add changelog Signed-off-by: Trishna Guha --- changelogs/fragments/v252-filter-plugins.yaml | 5 +++++ changelogs/fragments/v252-lookup-plugins.yaml | 3 +++ 2 files changed, 8 insertions(+) create mode 100644 changelogs/fragments/v252-filter-plugins.yaml create mode 100644 changelogs/fragments/v252-lookup-plugins.yaml diff --git a/changelogs/fragments/v252-filter-plugins.yaml b/changelogs/fragments/v252-filter-plugins.yaml new file mode 100644 index 0000000..427d070 --- /dev/null +++ b/changelogs/fragments/v252-filter-plugins.yaml @@ -0,0 +1,5 @@ +new_filter_plugins: +- New filter plugin ``interface_range`` +- New filter plugin ``interface_split`` +- New filter plugin ``vlan_compress`` +- New filter plugin ``vlan_expand`` diff --git a/changelogs/fragments/v252-lookup-plugins.yaml b/changelogs/fragments/v252-lookup-plugins.yaml new file mode 100644 index 0000000..2a23cab --- /dev/null +++ b/changelogs/fragments/v252-lookup-plugins.yaml @@ -0,0 +1,3 @@ +new_lookup_plugins: +- New lookup plugin ``json_template`` +- New lookup plugin ``network_template`` From 9e520869b45fa76070cfde02b63d39d8da384320 Mon Sep 17 00:00:00 2001 From: Trishna Guha Date: Tue, 22 May 2018 13:29:05 +0530 Subject: [PATCH 4/4] fix CI issues Signed-off-by: Trishna Guha --- filter_plugins/network_engine.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/filter_plugins/network_engine.py b/filter_plugins/network_engine.py index 88e776b..044365e 100644 --- a/filter_plugins/network_engine.py +++ b/filter_plugins/network_engine.py @@ -81,7 +81,7 @@ def vlan_expand(vlan): match = re.match(r'([A-Za-z]*)(.+)', vlan) if not match: - raise FilterError('unable to parse vlan %s' % vlan) + raise AnsibleFilterError('unable to parse vlan %s' % vlan) index = match.group(2) indices = list() @@ -101,7 +101,6 @@ def vlan_expand(vlan): return ['%d' % int(index) for index in indices] - class FilterModule(object): ''' Network interface filter '''