Skip to content

Commit

Permalink
Merge pull request ansible-network#78 from trishnaguha/vlan_filter_pl…
Browse files Browse the repository at this point in the history
…ugin

Add vlan_expand, vlan_compress filter plugins
  • Loading branch information
trishnaguha authored Jun 5, 2018
2 parents bb4cd7a + 9e52086 commit 1c2d1cd
Show file tree
Hide file tree
Showing 13 changed files with 156 additions and 1 deletion.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions changelogs/fragments/v252-filter-plugins.yaml
Original file line number Diff line number Diff line change
@@ -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``
3 changes: 3 additions & 0 deletions changelogs/fragments/v252-lookup-plugins.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
new_lookup_plugins:
- New lookup plugin ``json_template``
- New lookup plugin ``network_template``
51 changes: 50 additions & 1 deletion filter_plugins/network_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,60 @@ 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 AnsibleFilterError('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
}
2 changes: 2 additions & 0 deletions tests/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions tests/vlan_compress/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- hosts: localhost
connection: local
roles:
- vlan_compress
3 changes: 3 additions & 0 deletions tests/vlan_compress/vlan_compress/meta/main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
dependencies:
- ../../../network-engine
3 changes: 3 additions & 0 deletions tests/vlan_compress/vlan_compress/tasks/main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
- name: vlan_compress test
import_tasks: vlan_compress.yaml
35 changes: 35 additions & 0 deletions tests/vlan_compress/vlan_compress/tasks/vlan_compress.yaml
Original file line number Diff line number Diff line change
@@ -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"
4 changes: 4 additions & 0 deletions tests/vlan_expand/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- hosts: localhost
connection: local
roles:
- vlan_expand
3 changes: 3 additions & 0 deletions tests/vlan_expand/vlan_expand/meta/main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
dependencies:
- ../../../network-engine
3 changes: 3 additions & 0 deletions tests/vlan_expand/vlan_expand/tasks/main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
- name: vlan_expand test
import_tasks: vlan_expand.yaml
34 changes: 34 additions & 0 deletions tests/vlan_expand/vlan_expand/tasks/vlan_expand.yaml
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 1c2d1cd

Please sign in to comment.