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/>.
|
||||
#
|
||||
|
||||
ANSIBLE_METADATA = {'metadata_version': '1.0',
|
||||
ANSIBLE_METADATA = {
|
||||
'metadata_version': '1.0',
|
||||
'status': ['preview'],
|
||||
'supported_by': 'community'}
|
||||
|
||||
'supported_by': 'community'
|
||||
}
|
||||
|
||||
DOCUMENTATION = '''
|
||||
---
|
||||
|
@ -64,45 +65,24 @@ options:
|
|||
default: present
|
||||
choices: ['present','absent']
|
||||
'''
|
||||
|
||||
EXAMPLES = '''
|
||||
- nxos_vrf_af:
|
||||
interface: nve1
|
||||
vni: 6000
|
||||
ingress_replication: true
|
||||
username: "{{ un }}"
|
||||
password: "{{ pwd }}"
|
||||
host: "{{ inventory_hostname }}"
|
||||
vrf: ntc
|
||||
afi: ipv4
|
||||
safi: unicast
|
||||
route_target_both_auto_evpn: True
|
||||
state: present
|
||||
'''
|
||||
|
||||
RETURN = '''
|
||||
proposed:
|
||||
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:
|
||||
commands:
|
||||
description: commands sent to the device
|
||||
returned: always
|
||||
type: list
|
||||
sample: ["vrf context test", "address-family ipv4 unicast",
|
||||
"route-target both auto evpn"]
|
||||
changed:
|
||||
description: check to see if a change was made on the device
|
||||
returned: always
|
||||
type: boolean
|
||||
sample: true
|
||||
sample: ["vrf context ntc", "address-family ipv4 unicast",
|
||||
"afi ipv4", "route-target both auto evpn", "vrf ntc",
|
||||
"safi unicast"]
|
||||
'''
|
||||
|
||||
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.netcfg import CustomNetworkConfig
|
||||
|
||||
|
||||
BOOL_PARAMS = ['route_target_both_auto_evpn']
|
||||
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 = {}
|
||||
WARNINGS = []
|
||||
|
||||
def invoke(name, *args, **kwargs):
|
||||
func = globals().get(name)
|
||||
if func:
|
||||
return func(*args, **kwargs)
|
||||
|
||||
|
||||
def get_value(arg, config, module):
|
||||
command = PARAM_TO_COMMAND_KEYMAP.get(arg)
|
||||
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
|
||||
try:
|
||||
if REGEX.search(config):
|
||||
if command_re.search(config):
|
||||
value = True
|
||||
except TypeError:
|
||||
value = False
|
||||
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 = ''
|
||||
if PARAM_TO_COMMAND_KEYMAP[arg] in config:
|
||||
value = REGEX.search(config).group('value')
|
||||
if command in config:
|
||||
value = command_re.search(config).group('value')
|
||||
return value
|
||||
|
||||
|
||||
|
@ -173,14 +152,10 @@ def get_existing(module, args):
|
|||
|
||||
def apply_key_map(key_map, table):
|
||||
new_dict = {}
|
||||
for key, value in table.items():
|
||||
for key in table:
|
||||
new_key = key_map.get(key)
|
||||
if new_key:
|
||||
value = table.get(key)
|
||||
if value:
|
||||
new_dict[new_key] = value
|
||||
else:
|
||||
new_dict[new_key] = value
|
||||
new_dict[new_key] = table.get(key)
|
||||
return new_dict
|
||||
|
||||
|
||||
|
@ -222,12 +197,11 @@ def state_absent(module, existing, proposed, candidate):
|
|||
def main():
|
||||
argument_spec = dict(
|
||||
vrf=dict(required=True, type='str'),
|
||||
safi=dict(required=True, type='str', choices=['unicast','multicast']),
|
||||
afi=dict(required=True, type='str', choices=['ipv4','ipv6']),
|
||||
safi=dict(required=True, type='str', choices=['unicast', 'multicast']),
|
||||
afi=dict(required=True, type='str', choices=['ipv4', 'ipv6']),
|
||||
route_target_both_auto_evpn=dict(required=False, type='bool'),
|
||||
m_facts=dict(required=False, default=False, type='bool'),
|
||||
state=dict(choices=['present', 'absent'], default='present',
|
||||
required=False),
|
||||
state=dict(choices=['present', 'absent'], default='present', required=False),
|
||||
include_defaults=dict(default=False),
|
||||
config=dict(),
|
||||
save=dict(type='bool', default=False)
|
||||
|
@ -235,24 +209,15 @@ def main():
|
|||
|
||||
argument_spec.update(nxos_argument_spec)
|
||||
|
||||
module = AnsibleModule(argument_spec=argument_spec,
|
||||
supports_check_mode=True)
|
||||
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
|
||||
|
||||
warnings = list()
|
||||
check_args(module, warnings)
|
||||
|
||||
result = dict(changed=False, warnings=warnings)
|
||||
|
||||
state = module.params['state']
|
||||
|
||||
args = [
|
||||
'vrf',
|
||||
'safi',
|
||||
'afi',
|
||||
'route_target_both_auto_evpn'
|
||||
]
|
||||
|
||||
existing = invoke('get_existing', module, args)
|
||||
end_state = existing
|
||||
args = PARAM_TO_COMMAND_KEYMAP.keys()
|
||||
existing = get_existing(module, args)
|
||||
proposed_args = dict((k, v) for k, v in module.params.items()
|
||||
if v is not None and k in args)
|
||||
|
||||
|
@ -263,31 +228,24 @@ def main():
|
|||
value = PARAM_TO_DEFAULT_KEYMAP.get(key)
|
||||
if value is None:
|
||||
value = 'default'
|
||||
if existing.get(key) or (not existing.get(key) and value):
|
||||
if existing.get(key) != value:
|
||||
proposed[key] = value
|
||||
|
||||
result = {}
|
||||
if state == 'present' or (state == 'absent' and existing):
|
||||
candidate = CustomNetworkConfig(indent=3)
|
||||
invoke('state_%s' % state, module, existing, proposed, candidate)
|
||||
response = load_config(module, candidate)
|
||||
result.update(response)
|
||||
if state == 'present':
|
||||
state_present(module, existing, proposed, candidate)
|
||||
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:
|
||||
result['updates'] = []
|
||||
|
||||
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
|
||||
|
||||
result['commands'] = []
|
||||
module.exit_json(**result)
|
||||
|
||||
|
||||
if __name__ == '__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_interface.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_vrrp.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