1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

nxos_vxlan_vtep (#25971)

* nxos_vxlan_vtep tests

* Fix issues with nxos_vxlan_vtep
This commit is contained in:
Nathaniel Case 2017-06-24 14:09:11 -04:00 committed by GitHub
parent 3482a6326b
commit 21c7fcf9c0
4 changed files with 134 additions and 126 deletions

View file

@ -16,9 +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 = '''
@ -86,47 +88,20 @@ EXAMPLES = '''
source_interface: Loopback0 source_interface: Loopback0
source_interface_hold_down_time: 30 source_interface_hold_down_time: 30
shutdown: default shutdown: default
username: "{{ un }}"
password: "{{ pwd }}"
host: "{{ inventory_hostname }}"
''' '''
RETURN = ''' RETURN = '''
proposed: commands:
description: k/v pairs of parameters passed into module
returned: verbose mode
type: dict
sample: {"description": "simple description", "host_reachability": true,
"interface": "nve1", "shutdown": true, "source_interface": "loopback0",
"source_interface_hold_down_time": "30"}
existing:
description: k/v pairs of existing VXLAN VTEP configuration
returned: verbose mode
type: dict
sample: {}
end_state:
description: k/v pairs of VXLAN VTEP configuration after module execution
returned: verbose mode
type: dict
sample: {"description": "simple description", "host_reachability": true,
"interface": "nve1", "shutdown": true, "source_interface": "loopback0",
"source_interface_hold_down_time": "30"}
updates:
description: commands sent to the device description: commands sent to the device
returned: always returned: always
type: list type: list
sample: ["interface nve1", "source-interface loopback0", sample: ["interface nve1", "source-interface loopback0",
"source-interface hold-down-time 30", "description simple description", "source-interface hold-down-time 30", "description simple description",
"shutdown", "host-reachability protocol bgp"] "shutdown", "host-reachability protocol bgp"]
changed:
description: check to see if a change was made on the device
returned: always
type: boolean
sample: true
''' '''
import re import re
from ansible.module_utils.nxos import get_config, load_config, run_commands from ansible.module_utils.nxos import get_config, load_config
from ansible.module_utils.nxos import nxos_argument_spec, check_args 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
@ -148,14 +123,6 @@ PARAM_TO_DEFAULT_KEYMAP = {
'shutdown': True, 'shutdown': True,
} }
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):
if arg in BOOL_PARAMS: if arg in BOOL_PARAMS:
@ -305,36 +272,22 @@ def main():
source_interface=dict(required=False, type='str'), source_interface=dict(required=False, type='str'),
source_interface_hold_down_time=dict(required=False, type='str'), source_interface_hold_down_time=dict(required=False, type='str'),
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=True),
config=dict(),
save=dict(type='bool', default=False)
) )
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()
result = {'changed': False, 'commands': [], 'warnings': warnings}
check_args(module, warnings) check_args(module, warnings)
state = module.params['state'] state = module.params['state']
interface = module.params['interface'].lower()
args = [ args = PARAM_TO_DEFAULT_KEYMAP.keys()
'interface',
'description',
'host_reachability',
'shutdown',
'source_interface',
'source_interface_hold_down_time'
]
existing = invoke('get_existing', module, args) existing = 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)
@ -352,35 +305,27 @@ def main():
value = False value = False
else: else:
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 = {} candidate = CustomNetworkConfig(indent=3)
if state == 'present' or (state == 'absent' and existing): if state == 'present':
if not existing: if not existing:
WARNINGS.append("The proposed NVE interface did not exist. " warnings.append("The proposed NVE interface did not exist. "
"It's recommended to use nxos_interface to create " "It's recommended to use nxos_interface to create "
"all logical interfaces.") "all logical interfaces.")
candidate = CustomNetworkConfig(indent=3) state_present(module, existing, proposed, candidate)
invoke('state_%s' % state, module, existing, proposed, candidate) elif state == 'absent' and existing:
response = load_config(module, candidate) state_absent(module, existing, proposed, candidate)
result.update(response)
else: if candidate:
result['updates'] = [] candidate = candidate.items_text()
result['commands'] = candidate
if module._verbosity > 0: result['changed'] = True
end_state = invoke('get_existing', module, args) load_config(module, candidate)
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()

View file

@ -395,7 +395,6 @@ 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
lib/ansible/modules/network/nxos/nxos_vtp_password.py lib/ansible/modules/network/nxos/nxos_vtp_password.py
lib/ansible/modules/network/nxos/nxos_vtp_version.py lib/ansible/modules/network/nxos/nxos_vtp_version.py
lib/ansible/modules/network/nxos/nxos_vxlan_vtep.py
lib/ansible/modules/network/nxos/nxos_vxlan_vtep_vni.py lib/ansible/modules/network/nxos/nxos_vxlan_vtep_vni.py
lib/ansible/modules/net_tools/omapi_host.py lib/ansible/modules/net_tools/omapi_host.py
lib/ansible/modules/network/openswitch/_ops_template.py lib/ansible/modules/network/openswitch/_ops_template.py

View file

@ -0,0 +1,2 @@
interface nve1
member vni 6000

View file

@ -0,0 +1,62 @@
# (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_vxlan_vtep
from .nxos_module import TestNxosModule, load_fixture, set_module_args
class TestNxosVxlanVtepVniModule(TestNxosModule):
module = nxos_vxlan_vtep
def setUp(self):
self.mock_load_config = patch('ansible.modules.network.nxos.nxos_vxlan_vtep.load_config')
self.load_config = self.mock_load_config.start()
self.mock_get_config = patch('ansible.modules.network.nxos.nxos_vxlan_vtep.get_config')
self.get_config = self.mock_get_config.start()
def tearDown(self):
self.mock_get_config.stop()
self.mock_load_config.stop()
def load_fixtures(self, commands=None, device=''):
self.get_config.return_value = load_fixture('nxos_vxlan_vtep/config.cfg')
self.load_config.return_value = None
def test_nxos_vxlan_vtep(self):
set_module_args(dict(interface='nve1', description='simple description'))
self.execute_module(changed=True, commands=['interface nve1', 'description simple description'])
def test_nxos_vxlan_vtep_present_no_change(self):
set_module_args(dict(interface='nve1'))
self.execute_module(changed=False, commands=[])
def test_nxos_vxlan_vtep_absent(self):
set_module_args(dict(interface='nve1', state='absent'))
self.execute_module(changed=True, commands=['no interface nve1'])
def test_nxos_vxlan_vtep_absent_no_change(self):
set_module_args(dict(interface='nve2', state='absent'))
self.execute_module(changed=False, commands=[])