The module fails on switchport. Check added to fix. (#54970)
This commit is contained in:
parent
e3c79979c2
commit
f5d97205a0
3 changed files with 52 additions and 0 deletions
|
@ -137,6 +137,35 @@ def search_obj_in_list(name, lst):
|
|||
return o
|
||||
|
||||
|
||||
def get_interface_type(interface):
|
||||
intf_type = 'unknown'
|
||||
if interface.upper()[:2] in ('ET', 'GI', 'FA', 'TE', 'FO', 'HU', 'TWE'):
|
||||
intf_type = 'ethernet'
|
||||
elif interface.upper().startswith('VL'):
|
||||
intf_type = 'svi'
|
||||
elif interface.upper().startswith('LO'):
|
||||
intf_type = 'loopback'
|
||||
elif interface.upper()[:2] in ('MG', 'MA'):
|
||||
intf_type = 'management'
|
||||
elif interface.upper().startswith('PO'):
|
||||
intf_type = 'portchannel'
|
||||
elif interface.upper().startswith('NV'):
|
||||
intf_type = 'nve'
|
||||
|
||||
return intf_type
|
||||
|
||||
|
||||
def is_switchport(name, module):
|
||||
intf_type = get_interface_type(name)
|
||||
|
||||
if intf_type in ('ethernet', 'portchannel'):
|
||||
config = run_commands(module,
|
||||
['show interface {0} switchport'.format(name)])[0]
|
||||
match = re.search(r'Switchport : enabled', config)
|
||||
return bool(match)
|
||||
return False
|
||||
|
||||
|
||||
def map_obj_to_commands(updates, module):
|
||||
commands = list()
|
||||
want, have = updates
|
||||
|
@ -317,6 +346,13 @@ def main():
|
|||
result['warnings'] = warnings
|
||||
|
||||
want = map_params_to_obj(module)
|
||||
for w in want:
|
||||
name = w['name']
|
||||
name = name.lower()
|
||||
if is_switchport(name, module):
|
||||
module.fail_json(msg='Ensure interface is configured to be a L3'
|
||||
'\nport first before using this module. You can use'
|
||||
'\nthe cnos_interface module for this.')
|
||||
have = map_config_to_obj(module)
|
||||
|
||||
commands = map_obj_to_commands((want, have), module)
|
||||
|
|
|
@ -12,6 +12,15 @@
|
|||
- test4
|
||||
- test5
|
||||
|
||||
- name: Setup - Ensure interfaces are not switchport
|
||||
cnos_config:
|
||||
lines:
|
||||
- no shutdown
|
||||
- no switchport
|
||||
- no logging monitor
|
||||
parents:
|
||||
- "interface ethernet1/33"
|
||||
|
||||
- name: Create vrf
|
||||
cnos_vrf:
|
||||
name: test
|
||||
|
|
|
@ -38,15 +38,22 @@ class TestCnosVrfModule(TestCnosModule):
|
|||
self.mock_run_commands = patch('ansible.modules.network.cnos.cnos_vrf.run_commands')
|
||||
self.run_commands = self.mock_run_commands.start()
|
||||
|
||||
self._patch_is_switchport = patch(
|
||||
'ansible.modules.network.cnos.cnos_vrf.is_switchport'
|
||||
)
|
||||
self._is_switchport = self._patch_is_switchport.start()
|
||||
|
||||
def tearDown(self):
|
||||
super(TestCnosVrfModule, self).tearDown()
|
||||
self.mock_load_config.stop()
|
||||
self.mock_run_commands.stop()
|
||||
self._patch_is_switchport.stop()
|
||||
|
||||
def load_fixtures(self, commands=None):
|
||||
config_file = 'cnos_vrf_config.cfg'
|
||||
self.load_config.return_value = load_fixture(config_file)
|
||||
self.run_commands.return_value = load_fixture(config_file)
|
||||
self._is_switchport.return_value = False
|
||||
|
||||
def test_cnos_vrf_present(self):
|
||||
set_module_args(dict(name='test1', state='present'))
|
||||
|
|
Loading…
Reference in a new issue