From f5d97205a0e76c5dce628b2ca8014838b831e67e Mon Sep 17 00:00:00 2001 From: Anil Kumar Muraleedharan Date: Mon, 8 Apr 2019 10:41:36 +0530 Subject: [PATCH] The module fails on switchport. Check added to fix. (#54970) --- lib/ansible/modules/network/cnos/cnos_vrf.py | 36 +++++++++++++++++++ .../targets/cnos_vrf/tests/cli/basic.yaml | 9 +++++ .../modules/network/cnos/test_cnos_vrf.py | 7 ++++ 3 files changed, 52 insertions(+) diff --git a/lib/ansible/modules/network/cnos/cnos_vrf.py b/lib/ansible/modules/network/cnos/cnos_vrf.py index b3daf645f7d..b8d0970b560 100644 --- a/lib/ansible/modules/network/cnos/cnos_vrf.py +++ b/lib/ansible/modules/network/cnos/cnos_vrf.py @@ -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) diff --git a/test/integration/targets/cnos_vrf/tests/cli/basic.yaml b/test/integration/targets/cnos_vrf/tests/cli/basic.yaml index 81d051137a2..dbf964f0986 100644 --- a/test/integration/targets/cnos_vrf/tests/cli/basic.yaml +++ b/test/integration/targets/cnos_vrf/tests/cli/basic.yaml @@ -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 diff --git a/test/units/modules/network/cnos/test_cnos_vrf.py b/test/units/modules/network/cnos/test_cnos_vrf.py index 7cfa8a02542..2d5b8274628 100644 --- a/test/units/modules/network/cnos/test_cnos_vrf.py +++ b/test/units/modules/network/cnos/test_cnos_vrf.py @@ -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'))