Skip to content

Commit

Permalink
add normalize_interface in module_utils and fix nxos_l3_interface mod…
Browse files Browse the repository at this point in the history
…ule (#40598)

Signed-off-by: Trishna Guha <trishnaguha17@gmail.com>
  • Loading branch information
trishnaguha committed May 23, 2018
1 parent 012003c commit 0b7932d
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 85 deletions.
40 changes: 40 additions & 0 deletions lib/ansible/module_utils/network/nxos/nxos.py
Expand Up @@ -439,3 +439,43 @@ def load_config(module, config, return_error=False, opts=None):
def get_capabilities(module):
conn = get_connection(module)
return conn.get_capabilities()


def normalize_interface(name):
"""Return the normalized interface name
"""
if not name:
return

def _get_number(name):
digits = ''
for char in name:
if char.isdigit() or char in '/.':
digits += char
return digits

if name.lower().startswith('et'):
if_type = 'Ethernet'
elif name.lower().startswith('vl'):
if_type = 'Vlan'
elif name.lower().startswith('lo'):
if_type = 'loopback'
elif name.lower().startswith('po'):
if_type = 'port-channel'
elif name.lower().startswith('nv'):
if_type = 'nve'
else:
if_type = None

number_list = name.split(' ')
if len(number_list) == 2:
number = number_list[-1].strip()
else:
number = _get_number(name)

if if_type:
proper_interface = if_type + number
else:
proper_interface = name

return proper_interface
42 changes: 1 addition & 41 deletions lib/ansible/modules/network/nxos/nxos_interface.py
Expand Up @@ -215,7 +215,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
from ansible.module_utils.network.nxos.nxos import nxos_argument_spec, normalize_interface
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.network.common.utils import conditional, remove_default_spec

Expand Down Expand Up @@ -293,46 +293,6 @@ def get_interfaces_dict(module):
return interfaces


def normalize_interface(name):
"""Return the normalized interface name
"""
if not name:
return None

def _get_number(name):
digits = ''
for char in name:
if char.isdigit() or char in '/.':
digits += char
return digits

if name.lower().startswith('et'):
if_type = 'Ethernet'
elif name.lower().startswith('vl'):
if_type = 'Vlan'
elif name.lower().startswith('lo'):
if_type = 'loopback'
elif name.lower().startswith('po'):
if_type = 'port-channel'
elif name.lower().startswith('nv'):
if_type = 'nve'
else:
if_type = None

number_list = name.split(' ')
if len(number_list) == 2:
number = number_list[-1].strip()
else:
number = _get_number(name)

if if_type:
proper_interface = if_type + number
else:
proper_interface = name

return proper_interface


def get_vlan_interface_attributes(name, intf_type, module):
""" Returns dictionary that has two k/v pairs:
admin_state & description if not an svi, returns None
Expand Down
12 changes: 8 additions & 4 deletions lib/ansible/modules/network/nxos/nxos_l3_interface.py
Expand Up @@ -90,7 +90,7 @@
from ansible.module_utils.network.common.config import CustomNetworkConfig
from ansible.module_utils.network.common.utils import remove_default_spec
from ansible.module_utils.network.nxos.nxos import get_config, load_config
from ansible.module_utils.network.nxos.nxos import nxos_argument_spec
from ansible.module_utils.network.nxos.nxos import nxos_argument_spec, normalize_interface


def search_obj_in_list(name, lst):
Expand Down Expand Up @@ -151,10 +151,14 @@ def map_params_to_obj(module):
if item.get(key) is None:
item[key] = module.params[key]

obj.append(item.copy())
d = item.copy()
name = d['name']
d['name'] = normalize_interface(name)
obj.append(d)

else:
obj.append({
'name': module.params['name'],
'name': normalize_interface(module.params['name']),
'ipv4': module.params['ipv4'],
'ipv6': module.params['ipv6'],
'state': module.params['state']
Expand All @@ -175,7 +179,7 @@ def map_config_to_obj(want, module):
if config:
match_name = re.findall(r'interface (\S+)', config, re.M)
if match_name:
obj['name'] = match_name[0]
obj['name'] = normalize_interface(match_name[0])

match_ipv4 = re.findall(r'ip address (\S+)', config, re.M)
if match_ipv4:
Expand Down
41 changes: 1 addition & 40 deletions lib/ansible/modules/network/nxos/nxos_linkagg.py
Expand Up @@ -128,50 +128,11 @@

from ansible.module_utils.network.nxos.nxos import get_config, 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 normalize_interface
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.network.common.utils import remove_default_spec


def normalize_interface(name):
"""Return the normalized interface name
"""
if not name:
return

def _get_number(name):
digits = ''
for char in name:
if char.isdigit() or char in '/.':
digits += char
return digits

if name.lower().startswith('et'):
if_type = 'Ethernet'
elif name.lower().startswith('vl'):
if_type = 'Vlan'
elif name.lower().startswith('lo'):
if_type = 'loopback'
elif name.lower().startswith('po'):
if_type = 'port-channel'
elif name.lower().startswith('nv'):
if_type = 'nve'
else:
if_type = None

number_list = name.split(' ')
if len(number_list) == 2:
number = number_list[-1].strip()
else:
number = _get_number(name)

if if_type:
proper_interface = if_type + number
else:
proper_interface = name

return proper_interface


def execute_show_command(command, module):
device_info = get_capabilities(module)
network_api = device_info.get('network_api', 'nxapi')
Expand Down

0 comments on commit 0b7932d

Please sign in to comment.