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

* To fix bug on switch port issue of L3 interfaces.

* Adding mock return value for is_switch_port method
pull/53455/head
Anil Kumar Muraleedharan 6 years ago committed by Nathaniel Case
parent d241794daa
commit 77565554c8

@ -41,7 +41,7 @@ notes:
options: options:
name: name:
description: 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: ipv4:
description: description:
- IPv4 address to be set for the Layer-3 interface mentioned in I(name) - IPv4 address to be set for the Layer-3 interface mentioned in I(name)
@ -161,14 +161,14 @@ EXAMPLES = """
cnos_l3_interface: cnos_l3_interface:
aggregate: aggregate:
- { name: Ethernet1/33, ipv4: 10.241.107.1/24 } - { 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" } ipv6: "fd5d:12c9:2201:1::1/64" }
- name: Remove IP addresses on aggregate - name: Remove IP addresses on aggregate
cnos_l3_interface: cnos_l3_interface:
aggregate: aggregate:
- { name: Ethernet1/33, ipv4: 10.241.107.1/24 } - { 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" } ipv6: "fd5d:12c9:2201:1::1/64" }
state: absent state: absent
""" """
@ -192,6 +192,7 @@ from ansible.module_utils._text import to_text
from ansible.module_utils.basic import AnsibleModule 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 get_config, load_config
from ansible.module_utils.network.cnos.cnos import cnos_argument_spec 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.config import NetworkConfig
from ansible.module_utils.network.common.utils import remove_default_spec from ansible.module_utils.network.common.utils import remove_default_spec
from ansible.module_utils.network.common.utils import is_netmask, is_masklen from ansible.module_utils.network.common.utils import is_netmask, is_masklen
@ -256,6 +257,35 @@ def search_obj_in_list(name, lst):
return None 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): def map_obj_to_commands(updates, module):
commands = list() commands = list()
want, have = updates want, have = updates
@ -395,6 +425,14 @@ def main():
result = {'changed': False} result = {'changed': False}
want = map_params_to_obj(module) 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) have = map_config_to_obj(module)
commands = map_obj_to_commands((want, have), module) commands = map_obj_to_commands((want, have), module)

@ -19,6 +19,8 @@
cnos_config: cnos_config:
lines: lines:
- no shutdown - no shutdown
- no switchport
- no logging monitor
parents: parents:
- "interface {{ item }}" - "interface {{ item }}"
provider: "{{ cli }}" provider: "{{ cli }}"

@ -39,9 +39,13 @@ class TestCnosL3InterfaceModule(TestCnosModule):
self._patch_load_config = patch( self._patch_load_config = patch(
'ansible.modules.network.cnos.cnos_l3_interface.load_config' '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._get_config = self._patch_get_config.start()
self._load_config = self._patch_load_config.start() self._load_config = self._patch_load_config.start()
self._is_switchport = self._patch_is_switchport.start()
def tearDown(self): def tearDown(self):
super(TestCnosL3InterfaceModule, self).tearDown() super(TestCnosL3InterfaceModule, self).tearDown()
@ -52,6 +56,7 @@ class TestCnosL3InterfaceModule(TestCnosModule):
config_file = 'l3_interface_config.cfg' config_file = 'l3_interface_config.cfg'
self._get_config.return_value = load_fixture(config_file) self._get_config.return_value = load_fixture(config_file)
self._load_config.return_value = None self._load_config.return_value = None
self._is_switchport.return_value = False
def test_cnos_l3_interface_ipv4_address(self, *args, **kwargs): def test_cnos_l3_interface_ipv4_address(self, *args, **kwargs):
set_module_args(dict( set_module_args(dict(

Loading…
Cancel
Save