Add vyos_vlan DI module (#32072)

* Add vyos_vlan DI module

Signed-off-by: Trishna Guha <trishnaguha17@gmail.com>

* Add integration tests

Signed-off-by: Trishna Guha <trishnaguha17@gmail.com>

* Improve logic and add more test

Signed-off-by: Trishna Guha <trishnaguha17@gmail.com>

* update boilerplate

Signed-off-by: Trishna Guha <trishnaguha17@gmail.com>
This commit is contained in:
Trishna Guha 2017-10-27 15:21:37 +05:30 committed by GitHub
parent 80c8b99a62
commit a5f6c726f0
7 changed files with 453 additions and 0 deletions

View file

@ -0,0 +1,293 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# (c) 2017, Ansible by Red Hat, inc
# 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
__metaclass__ = type
ANSIBLE_METADATA = {'metadata_version': '1.1',
'status': ['preview'],
'supported_by': 'network'}
DOCUMENTATION = """
---
module: vyos_vlan
version_added: "2.5"
author: "Trishna Guha (@trishnaguha)"
short_description: Manage VLANs on VyOS network devices
description:
- This module provides declarative management of VLANs
on VyOS network devices.
notes:
- Tested against VYOS 1.1.7
options:
name:
description:
- Name of the VLAN.
address:
description:
- Configure Virtual interface address.
vlan_id:
description:
- ID of the VLAN. Range 0-4094.
required: true
interfaces:
description:
- List of interfaces that should be associated to the VLAN.
required: true
delay:
description:
- Delay the play should wait to check for declarative intent params values.
default: 10
aggregate:
description: List of VLANs definitions.
purge:
description:
- Purge VLANs not defined in the I(aggregate) parameter.
default: no
state:
description:
- State of the VLAN configuration.
default: present
choices: ['present', 'absent']
"""
EXAMPLES = """
- name: Create vlan
vyos_vlan:
vlan_id: 100
name: vlan-100
interfaces: eth1
state: present
- name: Add interfaces to VLAN
vyos_vlan:
vlan_id: 100
interfaces:
- eth1
- eth2
- name: Configure virtual interface address
vyos_vlan:
vlan_id: 100
interfaces: eth1
address: 172.26.100.37/24
- name: Delete vlan
vyos_vlan:
vlan_id: 100
interfaces: eth1
state: absent
"""
RETURN = """
commands:
description: The list of configuration mode commands to send to the device
returned: always
type: list
sample:
- set interfaces ethernet eth1 vif 100 description VLAN 100
- set interfaces ethernet eth1 vif 100 address 172.26.100.37/24
- delete interfaces ethernet eth1 vif 100
"""
import re
import time
from copy import deepcopy
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.network_common import remove_default_spec
from ansible.module_utils.vyos import load_config, run_commands
from ansible.module_utils.vyos import vyos_argument_spec
def search_obj_in_list(vlan_id, lst):
obj = list()
for o in lst:
if o['vlan_id'] == vlan_id:
obj.append(o)
return obj
def map_obj_to_commands(updates, module):
commands = list()
want, have = updates
purge = module.params['purge']
for w in want:
vlan_id = w['vlan_id']
name = w['name']
address = w['address']
state = w['state']
interfaces = w['interfaces']
obj_in_have = search_obj_in_list(vlan_id, have)
if state == 'absent':
if obj_in_have:
for obj in obj_in_have:
for i in obj['interfaces']:
commands.append('delete interfaces ethernet {0} vif {1}'.format(i, vlan_id))
elif state == 'present':
if not obj_in_have:
if w['interfaces'] and w['vlan_id']:
for i in w['interfaces']:
cmd = 'set interfaces ethernet {0} vif {1}'.format(i, vlan_id)
if w['name']:
commands.append(cmd + ' description {}'.format(name))
elif w['address']:
commands.append(cmd + ' address {}'.format(address))
else:
commands.append(cmd)
if purge:
for h in have:
obj_in_want = search_obj_in_list(h['vlan_id'], want)
if not obj_in_want:
for i in h['interfaces']:
commands.append('delete interfaces ethernet {0} vif {1}'.format(i, h['vlan_id']))
return commands
def map_params_to_obj(module):
obj = []
aggregate = module.params.get('aggregate')
if aggregate:
for item in aggregate:
for key in item:
if item.get(key) is None:
item[key] = module.params[key]
d = item.copy()
d['vlan_id'] = str(d['vlan_id'])
obj.append(d)
else:
obj.append({
'vlan_id': str(module.params['vlan_id']),
'name': module.params['name'],
'address': module.params['address'],
'state': module.params['state'],
'interfaces': module.params['interfaces']
})
return obj
def map_config_to_obj(module):
objs = []
interfaces = list()
output = run_commands(module, 'show interfaces')
lines = output[0].strip().splitlines()[3:]
for l in lines:
splitted_line = re.split(r'\s{2,}', l.strip())
obj = {}
eth = splitted_line[0].strip("'")
if eth.startswith('eth'):
obj['interfaces'] = []
if '.' in eth:
interface = eth.split('.')[0]
obj['interfaces'].append(interface)
obj['vlan_id'] = eth.split('.')[-1]
else:
obj['interfaces'].append(eth)
obj['vlan_id'] = None
if splitted_line[1].strip("'") != '-':
obj['address'] = splitted_line[1].strip("'")
if len(splitted_line) > 3:
obj['name'] = splitted_line[3].strip("'")
obj['state'] = 'present'
objs.append(obj)
return objs
def check_declarative_intent_params(want, module):
if module.params['interfaces']:
time.sleep(module.params['delay'])
have = map_config_to_obj(module)
want_interface = list()
obj_interface = list()
for w in want:
for i in w['interfaces']:
want_interface.append(i)
obj_in_have = search_obj_in_list(w['vlan_id'], have)
if obj_in_have:
for obj in obj_in_have:
obj_interface.extend(obj['interfaces'])
for w in want:
for i in w['interfaces']:
if (set(obj_interface) - set(want_interface)) != set([]):
module.fail_json(msg='Interface {0} not configured on vlan {1}'.format(i, w['vlan_id']))
def main():
""" main entry point for module execution
"""
element_spec = dict(
vlan_id=dict(type='int', required=True),
name=dict(),
address=dict(),
interfaces=dict(type='list', required=True),
delay=dict(default=10, type='int'),
state=dict(default='present',
choices=['present', 'absent'])
)
aggregate_spec = deepcopy(element_spec)
# remove default in aggregate spec, to handle common arguments
remove_default_spec(aggregate_spec)
argument_spec = dict(
aggregate=dict(type='list', elements='dict', options=aggregate_spec),
purge=dict(default=False, type='bool')
)
argument_spec.update(element_spec)
argument_spec.update(vyos_argument_spec)
required_one_of = [['vlan_id', 'aggregate']]
mutually_exclusive = [['vlan_id', 'aggregate']]
module = AnsibleModule(argument_spec=argument_spec,
supports_check_mode=True,
required_one_of=required_one_of,
mutually_exclusive=mutually_exclusive)
warnings = list()
result = {'changed': False}
if warnings:
result['warnings'] = warnings
want = map_params_to_obj(module)
have = map_config_to_obj(module)
commands = map_obj_to_commands((want, have), module)
result['commands'] = commands
if commands:
commit = not module.check_mode
load_config(module, commands, commit=commit)
result['changed'] = True
if result['changed']:
check_declarative_intent_params(want, module)
module.exit_json(**result)
if __name__ == '__main__':
main()

View file

@ -0,0 +1,3 @@
---
testcase: "*"
test_items: []

View file

@ -0,0 +1,15 @@
---
- name: collect all cli test cases
find:
paths: "{{ role_path }}/tests/cli"
patterns: "{{ testcase }}.yaml"
register: test_cases
- name: set test_items
set_fact: test_items="{{ test_cases.files | map(attribute='path') | list }}"
- name: run test case
include: "{{ test_case_to_run }}"
with_items: "{{ test_items }}"
loop_control:
loop_var: test_case_to_run

View file

@ -0,0 +1,2 @@
---
- { include: cli.yaml, tags: ['cli'] }

View file

@ -0,0 +1,73 @@
---
- name: setup - remove vlan used in test
vyos_config:
lines:
- delete interfaces ethernet eth1 vif 100
- delete interfaces ethernet eth0 vif 5
- delete interfaces ethernet eth0 vif 100
- name: set vlan with name
vyos_vlan: &name
vlan_id: 100
name: vlan-100
interfaces: eth1
register: result
- assert:
that:
- "result.changed == true"
- "'set interfaces ethernet eth1 vif 100 description vlan-100' in result.commands"
- name: set vlan with name(idempotence)
vyos_vlan: *name
register: result
- assert:
that:
- "result.changed == false"
- name: set vlan with address
vyos_vlan: &address
vlan_id: 5
address: 172.24.5.0/24
interfaces: eth0
register: result
- assert:
that:
- "result.changed == true"
- "'set interfaces ethernet eth0 vif 5 address 172.24.5.0/24' in result.commands"
- name: set vlan with address(idempotence)
vyos_vlan: *address
register: result
- assert:
that:
- "result.changed == false"
- name: delete
vyos_vlan: &delete
vlan_id: 100
interfaces: eth1
state: absent
register: result
- assert:
that:
- "result.changed == true"
- "'delete interfaces ethernet eth1 vif 100' in result.commands"
- name: delete(idempotence)
vyos_vlan: *delete
register: result
- assert:
that:
- "result.changed == false"
- name: teardown
vyos_config:
lines:
- delete interfaces ethernet eth1 vif 100
- delete interfaces ethernet eth0 vif 5

View file

@ -0,0 +1,58 @@
---
- name: setup - remove vlan used in test
vyos_config:
lines:
- delete interfaces ethernet eth0 vif 5
- delete interfaces ethernet eth0 vif 100
- delete interfaces ethernet eth1 vif 100
- name: Add multiple interfaces to vlan
vyos_vlan: &multiple
vlan_id: 100
interfaces:
- eth0
- eth1
register: result
- assert:
that:
- "result.changed == true"
- "'set interfaces ethernet eth0 vif 100' in result.commands"
- "'set interfaces ethernet eth1 vif 100' in result.commands"
- name: Add multiple interfaces to vlan(idempotence)
vyos_vlan: *multiple
register: result
- assert:
that:
- "result.changed == false"
- name: delete vlan with multiple interfaces
vyos_vlan: &delete_multiple
vlan_id: 100
interfaces:
- eth0
- eth1
state: absent
register: result
- assert:
that:
- "result.changed == true"
- "'delete interfaces ethernet eth0 vif 100' in result.commands"
- "'delete interfaces ethernet eth1 vif 100' in result.commands"
- name: delete vlan with multiple interfaces(idempotence)
vyos_vlan: *delete_multiple
register: result
- assert:
that:
- "result.changed == false"
- name: teardown
vyos_config:
lines:
- delete interfaces ethernet eth0 vif 100
- delete interfaces ethernet eth1 vif 100

View file

@ -123,6 +123,15 @@
failed_modules: "{{ failed_modules }} + [ 'vyos_facts' ]"
test_failed: true
- block:
- include_role:
name: vyos_vlan
when: "limit_to in ['*', 'vyos_vlan']"
rescue:
- set_fact:
failed_modules: "{{ failed_modules }} + [ 'vyos_vlan' ]"
test_failed: true
###########
- debug: var=failed_modules
when: test_failed