From b4baa2d48455029cfe35b14195b2ca4616e540ac Mon Sep 17 00:00:00 2001 From: Trishna Guha Date: Tue, 29 May 2018 21:06:08 +0530 Subject: [PATCH] fix nxos_vrf and migrate get_interface_type to module_utils (#40825) Signed-off-by: Trishna Guha --- lib/ansible/module_utils/network/nxos/nxos.py | 21 +++++++++++++++ .../network/nxos/_nxos_ip_interface.py | 18 +------------ .../modules/network/nxos/_nxos_switchport.py | 26 +----------------- lib/ansible/modules/network/nxos/nxos_hsrp.py | 18 +------------ .../network/nxos/nxos_igmp_interface.py | 18 +------------ .../modules/network/nxos/nxos_interface.py | 22 +-------------- .../modules/network/nxos/nxos_l2_interface.py | 27 +------------------ .../network/nxos/nxos_pim_interface.py | 18 +------------ lib/ansible/modules/network/nxos/nxos_vrf.py | 17 +++++++----- .../network/nxos/nxos_vrf_interface.py | 18 +------------ lib/ansible/modules/network/nxos/nxos_vrrp.py | 18 +------------ 11 files changed, 41 insertions(+), 180 deletions(-) diff --git a/lib/ansible/module_utils/network/nxos/nxos.py b/lib/ansible/module_utils/network/nxos/nxos.py index 832c2bbc584..10f72a7dfd0 100644 --- a/lib/ansible/module_utils/network/nxos/nxos.py +++ b/lib/ansible/module_utils/network/nxos/nxos.py @@ -479,3 +479,24 @@ def normalize_interface(name): proper_interface = name return proper_interface + + +def get_interface_type(interface): + """Gets the type of interface + """ + if interface.upper().startswith('ET'): + return 'ethernet' + elif interface.upper().startswith('VL'): + return 'svi' + elif interface.upper().startswith('LO'): + return 'loopback' + elif interface.upper().startswith('MG'): + return 'management' + elif interface.upper().startswith('MA'): + return 'management' + elif interface.upper().startswith('PO'): + return 'portchannel' + elif interface.upper().startswith('NV'): + return 'nve' + else: + return 'unknown' diff --git a/lib/ansible/modules/network/nxos/_nxos_ip_interface.py b/lib/ansible/modules/network/nxos/_nxos_ip_interface.py index fcb19d9288c..f4c5581bdb3 100644 --- a/lib/ansible/modules/network/nxos/_nxos_ip_interface.py +++ b/lib/ansible/modules/network/nxos/_nxos_ip_interface.py @@ -179,6 +179,7 @@ except ImportError: from ansible.module_utils.network.nxos.nxos import load_config, run_commands from ansible.module_utils.network.nxos.nxos import get_capabilities, nxos_argument_spec +from ansible.module_utils.network.nxos.nxos import get_interface_type from ansible.module_utils.basic import AnsibleModule @@ -209,23 +210,6 @@ def execute_show_command(command, module): return body -def get_interface_type(interface): - if interface.upper().startswith('ET'): - return 'ethernet' - elif interface.upper().startswith('VL'): - return 'svi' - elif interface.upper().startswith('LO'): - return 'loopback' - elif interface.upper().startswith('MG'): - return 'management' - elif interface.upper().startswith('MA'): - return 'management' - elif interface.upper().startswith('PO'): - return 'portchannel' - else: - return 'unknown' - - def is_default(interface, module): command = 'show run interface {0}'.format(interface) diff --git a/lib/ansible/modules/network/nxos/_nxos_switchport.py b/lib/ansible/modules/network/nxos/_nxos_switchport.py index a82b1c8d184..1540440e5e8 100644 --- a/lib/ansible/modules/network/nxos/_nxos_switchport.py +++ b/lib/ansible/modules/network/nxos/_nxos_switchport.py @@ -119,34 +119,10 @@ commands: from ansible.module_utils.network.nxos.nxos import load_config, run_commands from ansible.module_utils.network.nxos.nxos import get_capabilities, nxos_argument_spec +from ansible.module_utils.network.nxos.nxos import get_interface_type from ansible.module_utils.basic import AnsibleModule -def get_interface_type(interface): - """Gets the type of interface - Args: - interface (str): full name of interface, i.e. Ethernet1/1, loopback10, - port-channel20, vlan20 - Returns: - type of interface: ethernet, svi, loopback, management, portchannel, - or unknown - """ - if interface.upper().startswith('ET'): - return 'ethernet' - elif interface.upper().startswith('VL'): - return 'svi' - elif interface.upper().startswith('LO'): - return 'loopback' - elif interface.upper().startswith('MG'): - return 'management' - elif interface.upper().startswith('MA'): - return 'management' - elif interface.upper().startswith('PO'): - return 'portchannel' - else: - return 'unknown' - - def get_interface_mode(interface, module): """Gets current mode of interface: layer2 or layer3 Args: diff --git a/lib/ansible/modules/network/nxos/nxos_hsrp.py b/lib/ansible/modules/network/nxos/nxos_hsrp.py index 84f6b4658aa..734c6346397 100644 --- a/lib/ansible/modules/network/nxos/nxos_hsrp.py +++ b/lib/ansible/modules/network/nxos/nxos_hsrp.py @@ -148,6 +148,7 @@ commands: from ansible.module_utils.network.nxos.nxos import load_config, run_commands from ansible.module_utils.network.nxos.nxos import get_capabilities, nxos_argument_spec +from ansible.module_utils.network.nxos.nxos import get_interface_type from ansible.module_utils.basic import AnsibleModule @@ -187,23 +188,6 @@ def apply_key_map(key_map, table): return new_dict -def get_interface_type(interface): - if interface.upper().startswith('ET'): - return 'ethernet' - elif interface.upper().startswith('VL'): - return 'svi' - elif interface.upper().startswith('LO'): - return 'loopback' - elif interface.upper().startswith('MG'): - return 'management' - elif interface.upper().startswith('MA'): - return 'management' - elif interface.upper().startswith('PO'): - return 'portchannel' - else: - return 'unknown' - - def get_interface_mode(interface, intf_type, module): command = 'show interface {0}'.format(interface) interface = {} diff --git a/lib/ansible/modules/network/nxos/nxos_igmp_interface.py b/lib/ansible/modules/network/nxos/nxos_igmp_interface.py index 712ad2e2b56..a3dc1b26e14 100644 --- a/lib/ansible/modules/network/nxos/nxos_igmp_interface.py +++ b/lib/ansible/modules/network/nxos/nxos_igmp_interface.py @@ -191,6 +191,7 @@ changed: from ansible.module_utils.network.nxos.nxos import get_config, load_config, run_commands from ansible.module_utils.network.nxos.nxos import nxos_argument_spec, check_args +from ansible.module_utils.network.nxos.nxos import get_interface_type from ansible.module_utils.basic import AnsibleModule import re @@ -227,23 +228,6 @@ def get_interface_mode(interface, intf_type, module): return mode -def get_interface_type(interface): - if interface.upper().startswith('ET'): - return 'ethernet' - elif interface.upper().startswith('VL'): - return 'svi' - elif interface.upper().startswith('LO'): - return 'loopback' - elif interface.upper().startswith('MG'): - return 'management' - elif interface.upper().startswith('MA'): - return 'management' - elif interface.upper().startswith('PO'): - return 'portchannel' - else: - return 'unknown' - - def apply_key_map(key_map, table): new_dict = {} for key, value in table.items(): diff --git a/lib/ansible/modules/network/nxos/nxos_interface.py b/lib/ansible/modules/network/nxos/nxos_interface.py index 8de5aa43121..e98da4b679e 100644 --- a/lib/ansible/modules/network/nxos/nxos_interface.py +++ b/lib/ansible/modules/network/nxos/nxos_interface.py @@ -203,6 +203,7 @@ from copy import deepcopy from ansible.module_utils.network.nxos.nxos import load_config, run_commands from ansible.module_utils.network.nxos.nxos import nxos_argument_spec, normalize_interface +from ansible.module_utils.network.nxos.nxos import get_interface_type from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.network.common.utils import conditional, remove_default_spec @@ -231,27 +232,6 @@ def search_obj_in_list(name, lst): return None -def get_interface_type(interface): - """Gets the type of interface - """ - if interface.upper().startswith('ET'): - return 'ethernet' - elif interface.upper().startswith('VL'): - return 'svi' - elif interface.upper().startswith('LO'): - return 'loopback' - elif interface.upper().startswith('MG'): - return 'management' - elif interface.upper().startswith('MA'): - return 'management' - elif interface.upper().startswith('PO'): - return 'portchannel' - elif interface.upper().startswith('NV'): - return 'nve' - else: - return 'unknown' - - def get_interfaces_dict(module): """Gets all active interfaces on a given switch """ diff --git a/lib/ansible/modules/network/nxos/nxos_l2_interface.py b/lib/ansible/modules/network/nxos/nxos_l2_interface.py index bb7ba8892fa..cb71d5871b3 100644 --- a/lib/ansible/modules/network/nxos/nxos_l2_interface.py +++ b/lib/ansible/modules/network/nxos/nxos_l2_interface.py @@ -120,36 +120,11 @@ import re from copy import deepcopy from ansible.module_utils.network.nxos.nxos import get_config, load_config, run_commands -from ansible.module_utils.network.nxos.nxos import nxos_argument_spec +from ansible.module_utils.network.nxos.nxos import nxos_argument_spec, get_interface_type from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.network.common.utils import remove_default_spec -def get_interface_type(name): - """Gets the type of interface - Args: - interface (str): full name of interface, i.e. Ethernet1/1, loopback10, - port-channel20, vlan20 - Returns: - type of interface: ethernet, svi, loopback, management, portchannel, - or unknown - """ - if name.upper().startswith('ET'): - return 'ethernet' - elif name.upper().startswith('VL'): - return 'svi' - elif name.upper().startswith('LO'): - return 'loopback' - elif name.upper().startswith('MG'): - return 'management' - elif name.upper().startswith('MA'): - return 'management' - elif name.upper().startswith('PO'): - return 'portchannel' - else: - return 'unknown' - - def get_interface_mode(name, module): """Gets current mode of interface: layer2 or layer3 Args: diff --git a/lib/ansible/modules/network/nxos/nxos_pim_interface.py b/lib/ansible/modules/network/nxos/nxos_pim_interface.py index aa6e80a782b..3eb041fd85e 100644 --- a/lib/ansible/modules/network/nxos/nxos_pim_interface.py +++ b/lib/ansible/modules/network/nxos/nxos_pim_interface.py @@ -135,6 +135,7 @@ import re from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.network.nxos.nxos import get_config, load_config, run_commands from ansible.module_utils.network.nxos.nxos import nxos_argument_spec, check_args +from ansible.module_utils.network.nxos.nxos import get_interface_type from ansible.module_utils.six import string_types @@ -200,23 +201,6 @@ def local_existing(gexisting): return gexisting, jp_bidir, isauth -def get_interface_type(interface): - if interface.upper().startswith('ET'): - return 'ethernet' - elif interface.upper().startswith('VL'): - return 'svi' - elif interface.upper().startswith('LO'): - return 'loopback' - elif interface.upper().startswith('MG'): - return 'management' - elif interface.upper().startswith('MA'): - return 'management' - elif interface.upper().startswith('PO'): - return 'portchannel' - else: - return 'unknown' - - def get_interface_mode(interface, intf_type, module): mode = 'unknown' command = 'show interface {0}'.format(interface) diff --git a/lib/ansible/modules/network/nxos/nxos_vrf.py b/lib/ansible/modules/network/nxos/nxos_vrf.py index 3c3aad2fcdb..c5c92ed5049 100644 --- a/lib/ansible/modules/network/nxos/nxos_vrf.py +++ b/lib/ansible/modules/network/nxos/nxos_vrf.py @@ -175,7 +175,7 @@ from copy import deepcopy from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.network.nxos.nxos import load_config, run_commands -from ansible.module_utils.network.nxos.nxos import nxos_argument_spec +from ansible.module_utils.network.nxos.nxos import nxos_argument_spec, get_interface_type from ansible.module_utils.network.common.utils import remove_default_spec @@ -262,7 +262,8 @@ def map_obj_to_commands(updates, module): if interfaces and interfaces[0] != 'default': for i in interfaces: commands.append('interface {0}'.format(i)) - commands.append('no switchport') + if get_interface_type(i) in ('ethernet', 'portchannel'): + commands.append('no switchport') commands.append('vrf member {0}'.format(name)) else: @@ -296,7 +297,8 @@ def map_obj_to_commands(updates, module): commands.append('vrf context {0}'.format(name)) commands.append('exit') commands.append('interface {0}'.format(i)) - commands.append('no switchport') + if get_interface_type(i) in ('ethernet', 'portchannel'): + commands.append('no switchport') commands.append('vrf member {0}'.format(name)) elif set(interfaces) != set(obj_in_have['interfaces']): @@ -305,7 +307,8 @@ def map_obj_to_commands(updates, module): commands.append('vrf context {0}'.format(name)) commands.append('exit') commands.append('interface {0}'.format(i)) - commands.append('no switchport') + if get_interface_type(i) in ('ethernet', 'portchannel'): + commands.append('no switchport') commands.append('vrf member {0}'.format(name)) superfluous_interfaces = list(set(obj_in_have['interfaces']) - set(interfaces)) @@ -313,7 +316,8 @@ def map_obj_to_commands(updates, module): commands.append('vrf context {0}'.format(name)) commands.append('exit') commands.append('interface {0}'.format(i)) - commands.append('no switchport') + if get_interface_type(i) in ('ethernet', 'portchannel'): + commands.append('no switchport') commands.append('no vrf member {0}'.format(name)) elif interfaces and interfaces[0] == 'default': if obj_in_have['interfaces']: @@ -321,7 +325,8 @@ def map_obj_to_commands(updates, module): commands.append('vrf context {0}'.format(name)) commands.append('exit') commands.append('interface {0}'.format(i)) - commands.append('no switchport') + if get_interface_type(i) in ('ethernet', 'portchannel'): + commands.append('no switchport') commands.append('no vrf member {0}'.format(name)) if purge: diff --git a/lib/ansible/modules/network/nxos/nxos_vrf_interface.py b/lib/ansible/modules/network/nxos/nxos_vrf_interface.py index c08e87b8f64..65a4323a11c 100644 --- a/lib/ansible/modules/network/nxos/nxos_vrf_interface.py +++ b/lib/ansible/modules/network/nxos/nxos_vrf_interface.py @@ -82,6 +82,7 @@ import re from ansible.module_utils.network.nxos.nxos import load_config, run_commands from ansible.module_utils.network.nxos.nxos import get_capabilities, nxos_argument_spec +from ansible.module_utils.network.nxos.nxos import get_interface_type from ansible.module_utils.basic import AnsibleModule @@ -97,23 +98,6 @@ def execute_show_command(command, module): return run_commands(module, cmds)[0] -def get_interface_type(interface): - if interface.upper().startswith('ET'): - return 'ethernet' - elif interface.upper().startswith('VL'): - return 'svi' - elif interface.upper().startswith('LO'): - return 'loopback' - elif interface.upper().startswith('MG'): - return 'management' - elif interface.upper().startswith('MA'): - return 'management' - elif interface.upper().startswith('PO'): - return 'portchannel' - else: - return 'unknown' - - def get_interface_mode(interface, intf_type, module): command = 'show interface {0}'.format(interface) interface = {} diff --git a/lib/ansible/modules/network/nxos/nxos_vrrp.py b/lib/ansible/modules/network/nxos/nxos_vrrp.py index cc5f954d9f3..a1f393a3ec9 100644 --- a/lib/ansible/modules/network/nxos/nxos_vrrp.py +++ b/lib/ansible/modules/network/nxos/nxos_vrrp.py @@ -118,6 +118,7 @@ commands: from ansible.module_utils.network.nxos.nxos import load_config, run_commands from ansible.module_utils.network.nxos.nxos import get_capabilities, nxos_argument_spec +from ansible.module_utils.network.nxos.nxos import get_interface_type from ansible.module_utils.basic import AnsibleModule @@ -154,23 +155,6 @@ def apply_key_map(key_map, table): return new_dict -def get_interface_type(interface): - if interface.upper().startswith('ET'): - return 'ethernet' - elif interface.upper().startswith('VL'): - return 'svi' - elif interface.upper().startswith('LO'): - return 'loopback' - elif interface.upper().startswith('MG'): - return 'management' - elif interface.upper().startswith('MA'): - return 'management' - elif interface.upper().startswith('PO'): - return 'portchannel' - else: - return 'unknown' - - def is_default(interface, module): command = 'show run interface {0}'.format(interface)