Skip to content

Commit

Permalink
Use choices api (#107)
Browse files Browse the repository at this point in the history
* Ask netbox for choices using the API
  • Loading branch information
ThomasADavis authored and Solvik committed Jan 28, 2020
1 parent b36d61e commit 2280169
Showing 1 changed file with 32 additions and 41 deletions.
73 changes: 32 additions & 41 deletions netbox_agent/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,6 @@
from netbox_agent.ipmi import IPMI
from netbox_agent.lldp import LLDP

IFACE_TYPE_100ME_FIXED = 800
IFACE_TYPE_1GE_FIXED = 1000
IFACE_TYPE_1GE_GBIC = 1050
IFACE_TYPE_1GE_SFP = 1100
IFACE_TYPE_2GE_FIXED = 1120
IFACE_TYPE_5GE_FIXED = 1130
IFACE_TYPE_10GE_FIXED = 1150
IFACE_TYPE_10GE_CX4 = 1170
IFACE_TYPE_10GE_SFP_PLUS = 1200
IFACE_TYPE_10GE_XFP = 1300
IFACE_TYPE_10GE_XENPAK = 1310
IFACE_TYPE_10GE_X2 = 1320
IFACE_TYPE_25GE_SFP28 = 1350
IFACE_TYPE_40GE_QSFP_PLUS = 1400
IFACE_TYPE_50GE_QSFP28 = 1420
IFACE_TYPE_100GE_CFP = 1500
IFACE_TYPE_100GE_CFP2 = 1510
IFACE_TYPE_100GE_CFP4 = 1520
IFACE_TYPE_100GE_CPAK = 1550
IFACE_TYPE_100GE_QSFP28 = 1600
IFACE_TYPE_200GE_CFP2 = 1650
IFACE_TYPE_200GE_QSFP56 = 1700
IFACE_TYPE_400GE_QSFP_DD = 1750
IFACE_TYPE_OTHER = 32767
IFACE_TYPE_LAG = 200

IPADDRESS_ROLE_ANYCAST = 30


class Network():
def __init__(self, server, *args, **kwargs):
Expand All @@ -49,6 +21,22 @@ def __init__(self, server, *args, **kwargs):
self.lldp = LLDP() if config.network.lldp else None
self.scan()

self.dcim_choices = {}
dcim_c = nb.dcim.choices()

for choice in dcim_c:
self.dcim_choices[choice] = {}
for c in dcim_c[choice]:
self.dcim_choices[choice][c['label']] = c['value']

self.ipam_choices = {}
ipam_c = nb.ipam.choices()

for choice in ipam_c:
self.ipam_choices[choice] = {}
for c in ipam_c[choice]:
self.ipam_choices[choice][c['label']] = c['value']

def scan(self):
for interface in os.listdir('/sys/class/net/'):
# ignore if it's not a link (ie: bonding_masters etc)
Expand Down Expand Up @@ -161,18 +149,20 @@ def get_netbox_network_cards(self):

def get_netbox_type_for_nic(self, nic):
if nic.get('bonding'):
return IFACE_TYPE_LAG
return self.dcim_choices['interface:type']['Link Aggregation Group (LAG)']
if nic.get('ethtool') is None:
return IFACE_TYPE_OTHER
return self.dcim_choices['interface:type']['Other']

if nic['ethtool']['speed'] == '10000Mb/s':
if nic['ethtool']['port'] == 'FIBRE':
return IFACE_TYPE_10GE_SFP_PLUS
return IFACE_TYPE_10GE_FIXED
return self.dcim_choices['interface:type']['SFP+ (10GE)']
return self.dcim_choices['interface:type']['10GBASE-T (10GE)']

elif nic['ethtool']['speed'] == '1000Mb/s':
if nic['ethtool']['port'] == 'FIBRE':
return IFACE_TYPE_1GE_SFP
return IFACE_TYPE_1GE_FIXED
return IFACE_TYPE_OTHER
return self.dcim_choices['interface:type']['SFP (1GE)']
return self.dcim_choices['interface:type']['1000BASE-T (1GE)']
return self.dcim_choices['interface:type']['Other']

def get_ipmi(self):
ipmi = IPMI().parse()
Expand Down Expand Up @@ -214,29 +204,30 @@ def reset_vlan_on_interface(self, nic, interface):
interface.untagged_vlan = None
# if it's a vlan interface
elif vlan_id and (
interface.mode is None or interface.mode.value != 200 or
interface.mode is None or
interface.mode.value != self.dcim_choices['interface:mode']['Access'] or
len(interface.tagged_vlans) != 1 or
interface.tagged_vlans[0].vid != vlan_id):
logging.info('Resetting tagged VLAN(s) on interface {interface}'.format(
interface=interface))
update = True
nb_vlan = self.get_or_create_vlan(vlan_id)
interface.mode = 200
interface.mode = self.dcim_choices['interface:mode']['Tagged']
interface.tagged_vlans = [nb_vlan] if nb_vlan else []
interface.untagged_vlan = None
# if lldp reports a vlan-id with pvid
elif lldp_vlan:
pvid_vlan = [key for (key, value) in lldp_vlan.items() if value['pvid']]
if len(pvid_vlan) > 0 and (
interface.mode is None or
interface.mode.value != 100 or
interface.mode.value != self.dcim_choices['interface:mode']['Access'] or
interface.untagged_vlan is None or
interface.untagged_vlan.vid != int(pvid_vlan[0])):
logging.info('Resetting access VLAN on interface {interface}'.format(
interface=interface))
update = True
nb_vlan = self.get_or_create_vlan(pvid_vlan[0])
interface.mode = 100
interface.mode = self.dcim_choices['interface:mode']['Access']
interface.untagged_vlan = nb_vlan.id
return update, interface

Expand Down Expand Up @@ -304,7 +295,7 @@ def create_netbox_nic(self, nic, mgmt=False):
for vid, vlan_infos in vlans.items():
nb_vlan = self.get_or_create_vlan(vid)
if vlan_infos.get('vid'):
interface.mode = 100
interface.mode = self.dcim_choices['interface:mode']['Access']
interface.untagged_vlan = nb_vlan.id
interface.save()

Expand Down Expand Up @@ -366,7 +357,7 @@ def create_or_update_netbox_ip_on_interface(self, ip, interface):
address=ip,
interface=interface.id,
status=1,
role=IPADDRESS_ROLE_ANYCAST,
role=self.ipam_choices['ip-address:role']['Anycast'],
)
return netbox_ip
else:
Expand Down

0 comments on commit 2280169

Please sign in to comment.