mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
nxos_vrf_af fix and unit test (#24399)
* nxos_vrf_af fix and unit test Signed-off-by: Trishna Guha <trishnaguha17@gmail.com> * ansibot told me to do this * use sorted() as the test list elements differ in order for python2.x and 3.x
This commit is contained in:
parent
62eafa8837
commit
b2a2f69a6e
3 changed files with 156 additions and 127 deletions
|
@ -16,10 +16,11 @@
|
||||||
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||||||
#
|
#
|
||||||
|
|
||||||
ANSIBLE_METADATA = {'metadata_version': '1.0',
|
ANSIBLE_METADATA = {
|
||||||
|
'metadata_version': '1.0',
|
||||||
'status': ['preview'],
|
'status': ['preview'],
|
||||||
'supported_by': 'community'}
|
'supported_by': 'community'
|
||||||
|
}
|
||||||
|
|
||||||
DOCUMENTATION = '''
|
DOCUMENTATION = '''
|
||||||
---
|
---
|
||||||
|
@ -64,45 +65,24 @@ options:
|
||||||
default: present
|
default: present
|
||||||
choices: ['present','absent']
|
choices: ['present','absent']
|
||||||
'''
|
'''
|
||||||
|
|
||||||
EXAMPLES = '''
|
EXAMPLES = '''
|
||||||
- nxos_vrf_af:
|
- nxos_vrf_af:
|
||||||
interface: nve1
|
vrf: ntc
|
||||||
vni: 6000
|
afi: ipv4
|
||||||
ingress_replication: true
|
safi: unicast
|
||||||
username: "{{ un }}"
|
route_target_both_auto_evpn: True
|
||||||
password: "{{ pwd }}"
|
state: present
|
||||||
host: "{{ inventory_hostname }}"
|
|
||||||
'''
|
'''
|
||||||
|
|
||||||
RETURN = '''
|
RETURN = '''
|
||||||
proposed:
|
commands:
|
||||||
description: k/v pairs of parameters passed into module
|
|
||||||
returned: verbose mode
|
|
||||||
type: dict
|
|
||||||
sample: {"afi": "ipv4", "route_target_both_auto_evpn": true,
|
|
||||||
"safi": "unicast", "vrf": "test"}
|
|
||||||
existing:
|
|
||||||
description: k/v pairs of existing configuration
|
|
||||||
returned: verbose mode
|
|
||||||
type: dict
|
|
||||||
sample: {"afi": "ipv4", "route_target_both_auto_evpn": false,
|
|
||||||
"safi": "unicast", "vrf": "test"}
|
|
||||||
end_state:
|
|
||||||
description: k/v pairs of configuration after module execution
|
|
||||||
returned: verbose mode
|
|
||||||
type: dict
|
|
||||||
sample: {"afi": "ipv4", "route_target_both_auto_evpn": true,
|
|
||||||
"safi": "unicast", "vrf": "test"}
|
|
||||||
updates:
|
|
||||||
description: commands sent to the device
|
description: commands sent to the device
|
||||||
returned: always
|
returned: always
|
||||||
type: list
|
type: list
|
||||||
sample: ["vrf context test", "address-family ipv4 unicast",
|
sample: ["vrf context ntc", "address-family ipv4 unicast",
|
||||||
"route-target both auto evpn"]
|
"afi ipv4", "route-target both auto evpn", "vrf ntc",
|
||||||
changed:
|
"safi unicast"]
|
||||||
description: check to see if a change was made on the device
|
|
||||||
returned: always
|
|
||||||
type: boolean
|
|
||||||
sample: true
|
|
||||||
'''
|
'''
|
||||||
|
|
||||||
import re
|
import re
|
||||||
|
@ -112,33 +92,32 @@ from ansible.module_utils.nxos import nxos_argument_spec, check_args
|
||||||
from ansible.module_utils.basic import AnsibleModule
|
from ansible.module_utils.basic import AnsibleModule
|
||||||
from ansible.module_utils.netcfg import CustomNetworkConfig
|
from ansible.module_utils.netcfg import CustomNetworkConfig
|
||||||
|
|
||||||
|
|
||||||
BOOL_PARAMS = ['route_target_both_auto_evpn']
|
BOOL_PARAMS = ['route_target_both_auto_evpn']
|
||||||
PARAM_TO_COMMAND_KEYMAP = {
|
PARAM_TO_COMMAND_KEYMAP = {
|
||||||
'route_target_both_auto_evpn': 'route-target both auto evpn',
|
'vrf': 'vrf',
|
||||||
|
'safi': 'safi',
|
||||||
|
'afi': 'afi',
|
||||||
|
'route_target_both_auto_evpn': 'route-target both auto evpn'
|
||||||
}
|
}
|
||||||
PARAM_TO_DEFAULT_KEYMAP = {}
|
PARAM_TO_DEFAULT_KEYMAP = {}
|
||||||
WARNINGS = []
|
|
||||||
|
|
||||||
def invoke(name, *args, **kwargs):
|
|
||||||
func = globals().get(name)
|
|
||||||
if func:
|
|
||||||
return func(*args, **kwargs)
|
|
||||||
|
|
||||||
|
|
||||||
def get_value(arg, config, module):
|
def get_value(arg, config, module):
|
||||||
|
command = PARAM_TO_COMMAND_KEYMAP.get(arg)
|
||||||
if arg in BOOL_PARAMS:
|
if arg in BOOL_PARAMS:
|
||||||
REGEX = re.compile(r'\s+{0}\s*$'.format(PARAM_TO_COMMAND_KEYMAP[arg]), re.M)
|
command_re = re.compile(r'\s+{0}\s*$'.format(command), re.M)
|
||||||
value = False
|
value = False
|
||||||
try:
|
try:
|
||||||
if REGEX.search(config):
|
if command_re.search(config):
|
||||||
value = True
|
value = True
|
||||||
except TypeError:
|
except TypeError:
|
||||||
value = False
|
value = False
|
||||||
else:
|
else:
|
||||||
REGEX = re.compile(r'(?:{0}\s)(?P<value>.*)$'.format(PARAM_TO_COMMAND_KEYMAP[arg]), re.M)
|
command_re = re.compile(r'(?:{0}\s)(?P<value>.*)$'.format(command), re.M)
|
||||||
value = ''
|
value = ''
|
||||||
if PARAM_TO_COMMAND_KEYMAP[arg] in config:
|
if command in config:
|
||||||
value = REGEX.search(config).group('value')
|
value = command_re.search(config).group('value')
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
@ -173,14 +152,10 @@ def get_existing(module, args):
|
||||||
|
|
||||||
def apply_key_map(key_map, table):
|
def apply_key_map(key_map, table):
|
||||||
new_dict = {}
|
new_dict = {}
|
||||||
for key, value in table.items():
|
for key in table:
|
||||||
new_key = key_map.get(key)
|
new_key = key_map.get(key)
|
||||||
if new_key:
|
if new_key:
|
||||||
value = table.get(key)
|
new_dict[new_key] = table.get(key)
|
||||||
if value:
|
|
||||||
new_dict[new_key] = value
|
|
||||||
else:
|
|
||||||
new_dict[new_key] = value
|
|
||||||
return new_dict
|
return new_dict
|
||||||
|
|
||||||
|
|
||||||
|
@ -226,8 +201,7 @@ def main():
|
||||||
afi=dict(required=True, type='str', choices=['ipv4', 'ipv6']),
|
afi=dict(required=True, type='str', choices=['ipv4', 'ipv6']),
|
||||||
route_target_both_auto_evpn=dict(required=False, type='bool'),
|
route_target_both_auto_evpn=dict(required=False, type='bool'),
|
||||||
m_facts=dict(required=False, default=False, type='bool'),
|
m_facts=dict(required=False, default=False, type='bool'),
|
||||||
state=dict(choices=['present', 'absent'], default='present',
|
state=dict(choices=['present', 'absent'], default='present', required=False),
|
||||||
required=False),
|
|
||||||
include_defaults=dict(default=False),
|
include_defaults=dict(default=False),
|
||||||
config=dict(),
|
config=dict(),
|
||||||
save=dict(type='bool', default=False)
|
save=dict(type='bool', default=False)
|
||||||
|
@ -235,24 +209,15 @@ def main():
|
||||||
|
|
||||||
argument_spec.update(nxos_argument_spec)
|
argument_spec.update(nxos_argument_spec)
|
||||||
|
|
||||||
module = AnsibleModule(argument_spec=argument_spec,
|
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
|
||||||
supports_check_mode=True)
|
|
||||||
|
|
||||||
warnings = list()
|
warnings = list()
|
||||||
check_args(module, warnings)
|
check_args(module, warnings)
|
||||||
|
result = dict(changed=False, warnings=warnings)
|
||||||
|
|
||||||
state = module.params['state']
|
state = module.params['state']
|
||||||
|
args = PARAM_TO_COMMAND_KEYMAP.keys()
|
||||||
args = [
|
existing = get_existing(module, args)
|
||||||
'vrf',
|
|
||||||
'safi',
|
|
||||||
'afi',
|
|
||||||
'route_target_both_auto_evpn'
|
|
||||||
]
|
|
||||||
|
|
||||||
existing = invoke('get_existing', module, args)
|
|
||||||
end_state = existing
|
|
||||||
proposed_args = dict((k, v) for k, v in module.params.items()
|
proposed_args = dict((k, v) for k, v in module.params.items()
|
||||||
if v is not None and k in args)
|
if v is not None and k in args)
|
||||||
|
|
||||||
|
@ -263,31 +228,24 @@ def main():
|
||||||
value = PARAM_TO_DEFAULT_KEYMAP.get(key)
|
value = PARAM_TO_DEFAULT_KEYMAP.get(key)
|
||||||
if value is None:
|
if value is None:
|
||||||
value = 'default'
|
value = 'default'
|
||||||
if existing.get(key) or (not existing.get(key) and value):
|
if existing.get(key) != value:
|
||||||
proposed[key] = value
|
proposed[key] = value
|
||||||
|
|
||||||
result = {}
|
|
||||||
if state == 'present' or (state == 'absent' and existing):
|
|
||||||
candidate = CustomNetworkConfig(indent=3)
|
candidate = CustomNetworkConfig(indent=3)
|
||||||
invoke('state_%s' % state, module, existing, proposed, candidate)
|
if state == 'present':
|
||||||
response = load_config(module, candidate)
|
state_present(module, existing, proposed, candidate)
|
||||||
result.update(response)
|
elif state == 'absent' and existing:
|
||||||
|
state_absent(module, existing, proposed, candidate)
|
||||||
|
|
||||||
|
if candidate:
|
||||||
|
load_config(module, candidate)
|
||||||
|
result['changed'] = True
|
||||||
|
result['commands'] = candidate.items_text()
|
||||||
|
|
||||||
else:
|
else:
|
||||||
result['updates'] = []
|
result['commands'] = []
|
||||||
|
|
||||||
if module._verbosity > 0:
|
|
||||||
end_state = invoke('get_existing', module, args)
|
|
||||||
result['end_state'] = end_state
|
|
||||||
result['existing'] = existing
|
|
||||||
result['proposed'] = proposed_args
|
|
||||||
|
|
||||||
if WARNINGS:
|
|
||||||
result['warnings'] = WARNINGS
|
|
||||||
|
|
||||||
module.exit_json(**result)
|
module.exit_json(**result)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
||||||
|
|
|
@ -536,7 +536,6 @@ lib/ansible/modules/network/nxos/nxos_user.py
|
||||||
lib/ansible/modules/network/nxos/nxos_vpc.py
|
lib/ansible/modules/network/nxos/nxos_vpc.py
|
||||||
lib/ansible/modules/network/nxos/nxos_vpc_interface.py
|
lib/ansible/modules/network/nxos/nxos_vpc_interface.py
|
||||||
lib/ansible/modules/network/nxos/nxos_vrf.py
|
lib/ansible/modules/network/nxos/nxos_vrf.py
|
||||||
lib/ansible/modules/network/nxos/nxos_vrf_af.py
|
|
||||||
lib/ansible/modules/network/nxos/nxos_vrf_interface.py
|
lib/ansible/modules/network/nxos/nxos_vrf_interface.py
|
||||||
lib/ansible/modules/network/nxos/nxos_vrrp.py
|
lib/ansible/modules/network/nxos/nxos_vrrp.py
|
||||||
lib/ansible/modules/network/nxos/nxos_vtp_domain.py
|
lib/ansible/modules/network/nxos/nxos_vtp_domain.py
|
||||||
|
|
72
test/units/modules/network/nxos/test_nxos_vrf_af.py
Normal file
72
test/units/modules/network/nxos/test_nxos_vrf_af.py
Normal file
|
@ -0,0 +1,72 @@
|
||||||
|
# (c) 2016 Red Hat Inc.
|
||||||
|
#
|
||||||
|
# This file is part of Ansible
|
||||||
|
#
|
||||||
|
# Ansible is free software: you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU General Public License as published by
|
||||||
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
|
# (at your option) any later version.
|
||||||
|
#
|
||||||
|
# Ansible is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License
|
||||||
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
# Make coding more python3-ish
|
||||||
|
from __future__ import (absolute_import, division, print_function)
|
||||||
|
__metaclass__ = type
|
||||||
|
|
||||||
|
import json
|
||||||
|
|
||||||
|
from ansible.compat.tests.mock import patch
|
||||||
|
from ansible.modules.network.nxos import nxos_vrf_af
|
||||||
|
from .nxos_module import TestNxosModule, load_fixture, set_module_args
|
||||||
|
|
||||||
|
|
||||||
|
class TestNxosVrfafModule(TestNxosModule):
|
||||||
|
|
||||||
|
module = nxos_vrf_af
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
self.mock_run_commands = patch('ansible.modules.network.nxos.nxos_vrf_af.run_commands')
|
||||||
|
self.run_commands = self.mock_run_commands.start()
|
||||||
|
self.mock_load_config = patch('ansible.modules.network.nxos.nxos_vrf_af.load_config')
|
||||||
|
self.load_config = self.mock_load_config.start()
|
||||||
|
|
||||||
|
self.mock_get_config = patch('ansible.modules.network.nxos.nxos_vrf_af.get_config')
|
||||||
|
self.get_config = self.mock_get_config.start()
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
self.mock_run_commands.stop()
|
||||||
|
self.mock_load_config.stop()
|
||||||
|
self.mock_get_config.stop()
|
||||||
|
|
||||||
|
def load_fixtures(self, commands=None):
|
||||||
|
self.load_config.return_value = None
|
||||||
|
|
||||||
|
def test_nxos_vrf_af_present(self):
|
||||||
|
set_module_args(dict(vrf='ntc', afi='ipv4', safi='unicast', state='present'))
|
||||||
|
result = self.execute_module(changed=True)
|
||||||
|
self.assertEqual(sorted(result['commands']), sorted(['vrf context ntc',
|
||||||
|
'address-family ipv4 unicast',
|
||||||
|
'afi ipv4',
|
||||||
|
'vrf ntc',
|
||||||
|
'safi unicast']))
|
||||||
|
|
||||||
|
def test_nxos_vrf_af_absent(self):
|
||||||
|
set_module_args(dict(vrf='ntc', afi='ipv4', safi='unicast', state='absent'))
|
||||||
|
result = self.execute_module(changed=False)
|
||||||
|
self.assertEqual(result['commands'], [])
|
||||||
|
|
||||||
|
def test_nxos_vrf_af_route_target(self):
|
||||||
|
set_module_args(dict(vrf='ntc', afi='ipv4', safi='unicast', route_target_both_auto_evpn=True))
|
||||||
|
result = self.execute_module(changed=True)
|
||||||
|
self.assertEqual(sorted(result['commands']), sorted(['vrf context ntc',
|
||||||
|
'address-family ipv4 unicast',
|
||||||
|
'afi ipv4',
|
||||||
|
'route-target both auto evpn',
|
||||||
|
'vrf ntc',
|
||||||
|
'safi unicast']))
|
Loading…
Reference in a new issue