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

To fix bug on switch port issue of L3 interfaces. #53267

Merged
merged 3 commits into from
Mar 6, 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
44 changes: 41 additions & 3 deletions lib/ansible/modules/network/cnos/cnos_l3_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
options:
name:
description:
- Name of the Layer-3 interface to be configured eg. GigabitEthernet0/2
- Name of the Layer-3 interface to be configured eg. Ethernet1/2
ipv4:
description:
- IPv4 address to be set for the Layer-3 interface mentioned in I(name)
Expand Down Expand Up @@ -161,14 +161,14 @@
cnos_l3_interface:
aggregate:
- { name: Ethernet1/33, ipv4: 10.241.107.1/24 }
- { name: GigabitEthernet1/33, ipv4: 10.241.107.1/24,
- { name: Ethernet1/44, ipv4: 10.240.106.1/24,
ipv6: "fd5d:12c9:2201:1::1/64" }

- name: Remove IP addresses on aggregate
cnos_l3_interface:
aggregate:
- { name: Ethernet1/33, ipv4: 10.241.107.1/24 }
- { name: Ethernet1/3``3, ipv4: 10.241.107.1/24,
- { name: Ethernet1/44, ipv4: 10.240.106.1/24,
ipv6: "fd5d:12c9:2201:1::1/64" }
state: absent
"""
Expand All @@ -192,6 +192,7 @@
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.network.cnos.cnos import get_config, load_config
from ansible.module_utils.network.cnos.cnos import cnos_argument_spec
from ansible.module_utils.network.cnos.cnos import run_commands
from ansible.module_utils.network.common.config import NetworkConfig
from ansible.module_utils.network.common.utils import remove_default_spec
from ansible.module_utils.network.common.utils import is_netmask, is_masklen
Expand Down Expand Up @@ -256,6 +257,35 @@ def search_obj_in_list(name, lst):
return None


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 @@ -395,6 +425,14 @@ def main():
result = {'changed': False}

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
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
cnos_config:
lines:
- no shutdown
- no switchport
- no logging monitor
parents:
- "interface {{ item }}"
provider: "{{ cli }}"
Expand Down
5 changes: 5 additions & 0 deletions test/units/modules/network/cnos/test_cnos_l3_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,13 @@ def setUp(self):
self._patch_load_config = patch(
'ansible.modules.network.cnos.cnos_l3_interface.load_config'
)
self._patch_is_switchport = patch(
'ansible.modules.network.cnos.cnos_l3_interface.is_switchport'
)

self._get_config = self._patch_get_config.start()
self._load_config = self._patch_load_config.start()
self._is_switchport = self._patch_is_switchport.start()

def tearDown(self):
super(TestCnosL3InterfaceModule, self).tearDown()
Expand All @@ -52,6 +56,7 @@ def load_fixtures(self, commands=None):
config_file = 'l3_interface_config.cfg'
self._get_config.return_value = load_fixture(config_file)
self._load_config.return_value = None
self._is_switchport.return_value = False

def test_cnos_l3_interface_ipv4_address(self, *args, **kwargs):
set_module_args(dict(
Expand Down