-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add travis integration to slack (#53) * Add flaskenv to gitignore * Added tracking .txt file for which functions have a test written already * Add tests. Begin Python3 conversion. * Fix broken NXOS unit test * Add TravisCI badge to Readme. Updated docs * Add coveralls support * Update docs with coveralls badge * Fix travis file type * Fix #54 bug when adding new device with local credentials * Fix #55 and #56 issues with underscores in iShell and interface edit Modal * Cleanup code for issue #47. Still WIP * Fix #46 issue * Add description display for NX-OS interfaces * Use Requests for NetboxAPI (#57) * Refactor all Netbox API calls to use Python requests. * Convert NetboxAPI into its own object, define that object in __init__.py, and reference the object in views.py and db_modifyDatabase.py. * Workaround for not having a NetboxServer defined (unittesting) * Update to version 1.3.4
- Loading branch information
Showing
54 changed files
with
10,669 additions
and
240 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,5 +9,7 @@ scripts_bank/logs/* | |
scripts_bank/textfiles/* | ||
instance/settings.py | ||
flask/ | ||
flaskenv/ | ||
flaskenv3/ | ||
tmp/* | ||
venv/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,13 @@ | ||
language: python | ||
python: | ||
- "2.7" | ||
before_install: | ||
- pip install coveralls | ||
install: | ||
- pip install -r requirements.txt | ||
script: | ||
python -m unittest discover | ||
after_success: | ||
- coveralls | ||
notifications: | ||
slack: netconfig-group:gUDIl72WBF0iDG8gAVQxXjyD |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
from cisco_ios import CiscoIOS | ||
from cisco_nxos import CiscoNXOS | ||
from cisco_asa import CiscoASA | ||
from .cisco_ios import CiscoIOS | ||
from .cisco_nxos import CiscoNXOS | ||
from .cisco_asa import CiscoASA | ||
|
||
__all__ = ['CiscoIOS', 'CiscoNXOS', 'CiscoASA'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,174 +1,133 @@ | ||
from app import app | ||
from urllib2 import urlopen | ||
from json import load | ||
|
||
# TODO refactor with requests | ||
import requests | ||
|
||
|
||
class NetboxHost(object): | ||
"""Class for storing device information pulled from Netbox via API calls.""" | ||
|
||
def __init__(self, id, hostname, ipv4_addr, type, ios_type): | ||
"""Initilization method.""" | ||
self.id = id | ||
self.hostname = hostname | ||
self.ipv4_addr = ipv4_addr | ||
self.type = type | ||
self.ios_type = ios_type | ||
|
||
|
||
def getDeviceType(x): | ||
"""Input type of device (network, database, server, etc), returns ID in Netbox database.""" | ||
response = [] | ||
|
||
url = app.config['NETBOXSERVER'] | ||
url += "/api/dcim/device-roles/" | ||
# url += "?limit=0" | ||
|
||
# Open our url, load the JSON | ||
response = urlopen(url) | ||
json_obj = load(response) | ||
|
||
for device in json_obj['results']: | ||
if device['name'] == x: | ||
return device['id'] | ||
""" | ||
Netbox Host class for making calls to Netbox host | ||
""" | ||
# TODO should be a database model | ||
|
||
return False | ||
def __init__(self, url): | ||
self.url = url | ||
|
||
def getDeviceType(self, x): | ||
"""Input type of device (network, database, server, etc), returns ID in Netbox database.""" | ||
r = requests.get(self.url + '/api/dcim/device-roles/') | ||
|
||
def getDeviceTypeOS(x): | ||
"""Input type of device (network, database, server, etc), returns ID in Netbox database.""" | ||
response = [] | ||
if r.status_code == requests.codes.ok: | ||
for device in r.json()['results']: | ||
if device['name'] == x: | ||
return device['id'] | ||
else: | ||
return False | ||
|
||
url = app.config['NETBOXSERVER'] | ||
url += "/api/dcim/device-types/" | ||
url += str(x) | ||
def getDeviceTypeOS(self, x): | ||
"""Get Device Type of specific Netbox Device ID""" | ||
r = requests.get(self.url + '/api/dcim/device-types/' + str(x)) | ||
|
||
# Open our url, load the JSON | ||
response = urlopen(url) | ||
json_obj = load(response) | ||
if r.status_code == requests.codes.ok: | ||
|
||
netconfigOS = str(json_obj['custom_fields']['Netconfig_OS']['label']) | ||
# NOTE should probably put a try/catch around this | ||
netconfigOS = r.json()['custom_fields']['Netconfig_OS']['label'] | ||
|
||
if netconfigOS == 'IOS': | ||
return 'cisco_ios' | ||
elif netconfigOS == 'IOS-XE': | ||
return 'cisco_xe' | ||
elif netconfigOS == 'NX-OS': | ||
return 'cisco_nxos' | ||
elif netconfigOS == 'ASA': | ||
return 'cisco_asa' | ||
else: # Default to simply cisco_ios | ||
return 'cisco_ios' | ||
if netconfigOS == 'IOS': | ||
return 'cisco_ios' | ||
elif netconfigOS == 'IOS-XE': | ||
return 'cisco_xe' | ||
elif netconfigOS == 'NX-OS': | ||
return 'cisco_nxos' | ||
elif netconfigOS == 'ASA': | ||
return 'cisco_asa' | ||
else: # Default to simply cisco_ios | ||
return 'cisco_ios' | ||
|
||
else: | ||
|
||
def getHostByID(x): | ||
"""Return host info in same formate as SQLAlchemy responses, for X-Compatibility with local DB choice. | ||
# NOTE should this be False? | ||
return 'cisco_ios' | ||
|
||
x is host ID | ||
""" | ||
host = NetboxHost('', '', '', '', '') | ||
def getHostByID(self, x): | ||
"""Return host info in same format as SQLAlchemy responses, for X-Compatibility with local DB choice. | ||
response = [] | ||
x is host ID | ||
""" | ||
|
||
url = app.config['NETBOXSERVER'] | ||
url += "/api/dcim/devices/" | ||
url += str(x) | ||
r = requests.get(self.url + '/api/dcim/devices/' + str(x)) | ||
|
||
# Open our url, load the JSON | ||
response = urlopen(url) | ||
json_obj = load(response) | ||
if r.status_code == requests.codes.ok: | ||
return r.json() | ||
|
||
host.id = str(x) | ||
host.hostname = str(json_obj['name']) | ||
host.ipv4_addr = str(json_obj['primary_ip']['address'].split('/', 1)[0]) | ||
host.type = str(json_obj['device_type']['model']) | ||
host.ios_type = str(getDeviceTypeOS(json_obj['device_type']['id'])) | ||
else: | ||
return None | ||
|
||
return host | ||
def getHosts(self): | ||
"""Return all devices stored in Netbox.""" | ||
|
||
r = requests.get(self.url + '/api/dcim/devices/?limit=0') | ||
|
||
def getHosts(): | ||
"""Return all devices stored in Netbox.""" | ||
response = [] | ||
result = [] | ||
if r.status_code == requests.codes.ok: | ||
|
||
url = app.config['NETBOXSERVER'] | ||
url += "/api/dcim/devices/" | ||
url += "?limit=0" | ||
# NOTE probably don't need to strip primary_ip cidr. | ||
# Not seeing this as a problem connecting | ||
result = [host for host in r.json()['results'] | ||
if host['custom_fields']['Netconfig'] and | ||
host['custom_fields']['Netconfig']['label'] == 'Yes'] | ||
|
||
# Open our url, load the JSON | ||
response = urlopen(url) | ||
json_obj = load(response) | ||
return result | ||
|
||
for host in json_obj['results']: | ||
if str(host['custom_fields']['Netconfig']) != 'None': | ||
if str(host['custom_fields']['Netconfig']['label']) == 'Yes': | ||
# Strip the CIDR notation from the end of the IP address, and store it back as the address for the returned host | ||
host['primary_ip']['address'] = str(host['primary_ip']['address'].split('/', 1)[0]) | ||
result.append(host) | ||
else: | ||
|
||
return result | ||
return None | ||
|
||
def getHostID(self, x): | ||
"""Input device name/hostname, returns id as stored in Netbox.""" | ||
r = requests.get(self.url + '/api/dcim/devices/?limit=0') | ||
|
||
def getHostID(x): | ||
"""Input device name/hostname, returns id as stored in Netbox.""" | ||
response = [] | ||
if r.status_code == requests.codes.ok: | ||
|
||
url = app.config['NETBOXSERVER'] | ||
url += "/api/dcim/devices/" | ||
url += "?limit=0" | ||
for host in r.json()['results']: | ||
if host['display_name'] == x: # Network | ||
return host['id'] | ||
|
||
# Open our url, load the JSON | ||
response = urlopen(url) | ||
json_obj = load(response) | ||
else: | ||
|
||
for host in json_obj['results']: | ||
if host['display_name'] == x: # Network | ||
return host['id'] | ||
return None | ||
|
||
def getHostName(self, x): | ||
"""Input ID, return device name from Netbox.""" | ||
r = requests.get(self.url + '/api/dcim/devices/' + str(x)) | ||
|
||
def getHostName(id): | ||
"""Input ID, return device name from Netbox.""" | ||
response = [] | ||
if r.status_code == requests.codes.ok: | ||
|
||
url = app.config['NETBOXSERVER'] | ||
url += "/api/dcim/devices/" | ||
url += str(id) | ||
# TODO add try/catch here | ||
return r.json()['name'] | ||
|
||
# Open our url, load the JSON | ||
response = urlopen(url) | ||
json_obj = load(response) | ||
else: | ||
|
||
return json_obj['name'] | ||
return None | ||
|
||
def getHostIPAddr(self, x): | ||
"""Input ID, return device IP address from Netbox.""" | ||
r = requests.get(self.url + '/api/dcim/devices/' + str(x)) | ||
|
||
def getHostIPAddr(id): | ||
"""Input ID, return device IP address from Netbox.""" | ||
response = [] | ||
if r.status_code == requests.codes.ok: | ||
|
||
url = app.config['NETBOXSERVER'] | ||
url += "/api/dcim/devices/" | ||
url += str(id) | ||
# TODO add try/catch here | ||
return r.json()['primary_ip']['address'].split('/', 1)[0] | ||
|
||
# Open our url, load the JSON | ||
response = urlopen(url) | ||
json_obj = load(response) | ||
else: | ||
|
||
# Return IP address with trailing CIDR notation stripped off | ||
return str(json_obj['primary_ip']['address'].split('/', 1)[0]) | ||
return None | ||
|
||
def getHostType(self, x): | ||
"""Input ID, return device type from Netbox.""" | ||
r = requests.get(self.url + '/api/dcim/devices/' + str(x)) | ||
|
||
def getHostType(id): | ||
"""Input ID, return device type from Netbox.""" | ||
response = [] | ||
if r.status_code == requests.codes.ok: | ||
|
||
url = app.config['NETBOXSERVER'] | ||
url += "/api/dcim/devices/" | ||
url += str(id) | ||
# TODO add try/catch here | ||
return r.json()['device_type']['model'] | ||
|
||
# Open our url, load the JSON | ||
response = urlopen(url) | ||
json_obj = load(response) | ||
else: | ||
|
||
return json_obj['device_type']['model'] | ||
return None |
Oops, something went wrong.