Skip to content

Commit 0bfc78b

Browse files
committed
network interface
Signed-off-by: Trishna Guha <[email protected]>
1 parent 28f29be commit 0bfc78b

File tree

6 files changed

+604
-0
lines changed

6 files changed

+604
-0
lines changed

library/interfaces.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
4+
# (c) 2018, Red Hat, Inc.
5+
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6+
7+
from __future__ import absolute_import, division, print_function
8+
__metaclass__ = type
9+
10+
11+
ANSIBLE_METADATA = {'metadata_version': '1.1',
12+
'status': ['preview'],
13+
'supported_by': 'network'}
14+
15+
16+
DOCUMENTATION = """
17+
---
18+
module: interfaces
19+
version_added: "2.8"
20+
short_description: Collect device capabilities from Network devices
21+
description:
22+
- Collect basic fact capabilities from Network devices and return
23+
the capabilities as Ansible facts.
24+
author:
25+
- Trishna Guha (@trishnaguha)
26+
options: {}
27+
"""
28+
29+
EXAMPLES = """
30+
- interfaces:
31+
"""
32+
33+
RETURN = """
34+
"""
35+
36+
37+
from ansible.module_utils.basic import AnsibleModule
38+
from ansible.module_utils.connection import Connection, ConnectionError
39+
from ansible.module_utils.cisco_nxos.config.interfaces.interfaces import Interface
40+
41+
42+
def main():
43+
""" main entry point for module execution
44+
"""
45+
module = AnsibleModule(argument_spec=Interface.argument_spec,
46+
supports_check_mode=True)
47+
48+
connection = Connection(module._socket_path)
49+
try:
50+
config = connection.get('show running-config all | section ^interface')
51+
except ConnectionError:
52+
config = None
53+
54+
result = {'changed': False}
55+
commands = list()
56+
57+
intf = Interface(**module.params)
58+
59+
resp = intf.set_config(module, config)
60+
if resp:
61+
commands.extend(resp)
62+
63+
if commands:
64+
if not module.check_mode:
65+
connection.edit_config(commands)
66+
result['changed'] = True
67+
68+
result['commands'] = commands
69+
if result['changed']:
70+
failed_conditions = intf.set_state(module)
71+
72+
if failed_conditions:
73+
msg = 'One or more conditional statements have not been satisfied'
74+
module.fail_json(msg=msg, failed_conditions=failed_conditions)
75+
76+
module.exit_json(**result)
77+
78+
79+
if __name__ == '__main__':
80+
main()

module_utils/__init__.py

Whitespace-only changes.

module_utils/cisco_nxos/__init__.py

Whitespace-only changes.
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
from itertools import chain
2+
3+
from ansible.module_utils.six import iteritems
4+
from ansible.module_utils.network.common.utils import to_list, sort_list
5+
from ansible.module_utils.network.common.config import NetworkConfig
6+
7+
8+
class ConfigBase(object):
9+
10+
argument_spec = {}
11+
identifier = ()
12+
13+
def __init__(self, **kwargs):
14+
self.values = {}
15+
16+
for item in self.identifier:
17+
self.values[item] = kwargs.pop(item)
18+
19+
for key, value in iteritems(kwargs):
20+
if key in self.argument_spec:
21+
setattr(self, key, value)
22+
23+
def __getattr__(self, key):
24+
if key in self.argument_spec:
25+
return self.values.get(key)
26+
27+
def __setattr__(self, key, value):
28+
if key in self.argument_spec:
29+
if key in self.identifier:
30+
raise TypeError('cannot set value')
31+
elif value is not None:
32+
self.values[key] = value
33+
else:
34+
super(ConfigBase, self).__setattr__(key, value)
35+
36+
def render(self, config=None):
37+
raise NotImplementedError
38+
39+
def get_section(self, config, section):
40+
if config is not None:
41+
netcfg = NetworkConfig(indent=2, contents=config)
42+
try:
43+
config = netcfg.get_block_config(to_list(section))
44+
except ValueError:
45+
config = None
46+
return config
47+
48+
def search_obj_in_list(self, name, lst):
49+
for o in lst:
50+
if o['name'] == name:
51+
return o
52+
return None
53+
54+
def normalize_interface(self, name):
55+
"""Return the normalized interface name
56+
"""
57+
if not name:
58+
return
59+
60+
def _get_number(name):
61+
digits = ''
62+
for char in name:
63+
if char.isdigit() or char in '/.':
64+
digits += char
65+
return digits
66+
67+
if name.lower().startswith('et'):
68+
if_type = 'Ethernet'
69+
elif name.lower().startswith('vl'):
70+
if_type = 'Vlan'
71+
elif name.lower().startswith('lo'):
72+
if_type = 'loopback'
73+
elif name.lower().startswith('po'):
74+
if_type = 'port-channel'
75+
elif name.lower().startswith('nv'):
76+
if_type = 'nve'
77+
else:
78+
if_type = None
79+
80+
number_list = name.split(' ')
81+
if len(number_list) == 2:
82+
number = number_list[-1].strip()
83+
else:
84+
number = _get_number(name)
85+
86+
if if_type:
87+
proper_interface = if_type + number
88+
else:
89+
proper_interface = name
90+
91+
return proper_interface
92+
93+
def get_interface_type(self, interface):
94+
"""Gets the type of interface
95+
"""
96+
if interface.upper().startswith('ET'):
97+
return 'ethernet'
98+
elif interface.upper().startswith('VL'):
99+
return 'svi'
100+
elif interface.upper().startswith('LO'):
101+
return 'loopback'
102+
elif interface.upper().startswith('MG'):
103+
return 'management'
104+
elif interface.upper().startswith('MA'):
105+
return 'management'
106+
elif interface.upper().startswith('PO'):
107+
return 'portchannel'
108+
elif interface.upper().startswith('NV'):
109+
return 'nve'
110+
else:
111+
return 'unknown'

module_utils/cisco_nxos/config/interfaces/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)