add more compatibility, now to netbox 2.10

This commit is contained in:
Solvik Blum 2021-05-11 20:42:13 +02:00
parent 3639662961
commit 58c18fc2da
4 changed files with 28 additions and 18 deletions

View file

@ -61,6 +61,7 @@ def get_hostname(config):
def create_netbox_tags(tags):
ret = []
for tag in tags:
nb_tag = nb.extras.tags.get(
name=tag
@ -70,3 +71,5 @@ def create_netbox_tags(tags):
name=tag,
slug=slugify(tag),
)
ret.append(nb_tag)
return ret

View file

@ -12,10 +12,11 @@ from netbox_agent.ethtool import Ethtool
from netbox_agent.ipmi import IPMI
from netbox_agent.lldp import LLDP
from packaging import version
class Network(object):
def __init__(self, server, *args, **kwargs):
self.netbox_version = float(nb.version)
self.netbox_version = nb.version
self.nics = []
self.server = server
@ -323,7 +324,7 @@ class Network(object):
'address': ip,
'status': "active",
}
if self.netbox_version > 2.8:
if version.parse(self.netbox_version) > version.parse('2.8'):
query_params.update({
'assigned_object_type': self.assigned_object_type,
'assigned_object_id': interface.id
@ -335,7 +336,7 @@ class Network(object):
**query_params
)
else:
netbox_ip = netbox_ips[0]
netbox_ip = list(netbox_ips)[0]
# If IP exists in anycast
if netbox_ip.role and netbox_ip.role.label == 'Anycast':
logging.debug('IP {} is Anycast..'.format(ip))
@ -357,7 +358,7 @@ class Network(object):
"role": self.ipam_choices['ip-address:role']['Anycast'],
"tenant": self.tenant.id if self.tenant else None,
}
if self.netbox_version > 2.8:
if version.parse(self.netbox_version) > version.parse('2.8'):
query_params.update({
'assigned_object_type': self.assigned_object_type,
'assigned_object_id': interface.id
@ -373,7 +374,7 @@ class Network(object):
ip=ip, interface=interface))
elif hasattr(netbox_ip, 'interface') and netbox_ip.interface.id != interface.id or \
hasattr(netbox_ip, 'assigned_object') and netbox_ip.assigned_object_id != interface.id:
if self.netbox_version > 2.8:
if version.parse(self.netbox_version) > version.parse('2.8'):
old_interface = netbox_ip.assigned_object
else:
old_interface = netbox_ip.interface
@ -389,7 +390,7 @@ class Network(object):
else:
return netbox_ip
if self.netbox_version > 2.8:
if version.parse(self.netbox_version) > version.parse('2.8'):
netbox_ip.assigned_object_type = self.assigned_object_type
netbox_ip.assigned_object_id = interface.id
else:
@ -403,9 +404,9 @@ class Network(object):
logging.debug('Creating/Updating NIC...')
# delete unknown interface
nb_nics = self.get_netbox_network_cards()
nb_nics = list(self.get_netbox_network_cards())
local_nics = [x['name'] for x in self.nics]
for nic in nb_nics[:]:
for nic in nb_nics:
if nic.name not in local_nics:
logging.info('Deleting netbox interface {name} because not present locally'.format(
name=nic.name
@ -415,7 +416,7 @@ class Network(object):
# delete IP on netbox that are not known on this server
if len(nb_nics):
if self.netbox_version > 2.8:
if version.parse(self.netbox_version) > version.parse('2.8'):
netbox_ips = nb.ipam.ip_addresses.filter(
**{self.intf_type: [x.id for x in nb_nics]}
)
@ -429,7 +430,7 @@ class Network(object):
]))
for netbox_ip in netbox_ips:
if netbox_ip.address not in all_local_ips:
if self.netbox_version < 2.9:
if version.parse(self.netbox_version) < version.parse('2.9'):
logging.info('Unassigning IP {ip} from {interface}'.format(
ip=netbox_ip.address, interface=netbox_ip.interface))
netbox_ip.interface = None
@ -531,7 +532,10 @@ class ServerNetwork(Network):
return nb_server_interface
try:
nb_switch = nb_mgmt_ip.interface.device
if version.parse(self.netbox_version) > version.parse('2.8'):
nb_switch = nb_mgmt_ip.assigned_object.device
else:
nb_switch = nb_mgmt_ip.interface.device
logging.info('Found a switch in Netbox based on LLDP infos: {} (id: {})'.format(
switch_ip,
nb_switch.id

View file

@ -51,7 +51,7 @@ class PowerSupply():
)
def create_or_update_power_supply(self):
nb_psus = self.get_netbox_power_supply()
nb_psus = list(self.get_netbox_power_supply())
psus = self.get_power_supply()
# Delete unknown PSU

View file

@ -13,6 +13,8 @@ from netbox_agent.misc import create_netbox_tags, get_device_role, get_device_ty
from netbox_agent.network import ServerNetwork
from netbox_agent.power import PowerSupply
from packaging import version
class ServerBase():
def __init__(self, dmi=None):
@ -29,8 +31,8 @@ class ServerBase():
self.network = None
self.tags = list(set([x.strip() for x in config.device.tags.split(',') if x.strip()])) if config.device.tags else []
if self.tags and len(self.tags):
create_netbox_tags(self.tags)
self.nb_tags = list(create_netbox_tags(self.tags))
self.netbox_version = nb.version
def get_tenant(self):
tenant = Tenant()
@ -383,11 +385,12 @@ class ServerBase():
update += 1
server.name = self.get_hostname()
if float(nb.version) < 2.8 and sorted(set(server.tags)) != sorted(set(self.tags)):
server.tags = self.tags
if sorted(set([x.name for x in server.tags])) != sorted(set(self.tags)):
update += 1
else:
logging.warning("netbox-agent doesn't support tag updates for Netbox version >=2.8")
if version.parse(self.netbox_version) > version.parse('2.8'):
server.tags = [x.id for x in self.nb_tags]
else:
server.tags = self.tags
if config.update_all or config.update_location:
ret, server = self.update_netbox_location(server)