Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The module fails on switchport. Check added to fix. #54970

Merged
merged 1 commit into from
Apr 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 36 additions & 0 deletions lib/ansible/modules/network/cnos/cnos_vrf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions test/integration/targets/cnos_vrf/tests/cli/basic.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions test/units/modules/network/cnos/test_cnos_vrf.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,22 @@ def setUp(self):
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'))
Expand Down