Make netbox_agent compatible with recent Netbox versions and pynetbox v7 #277

Closed
Solvik wants to merge 2 commits from cable into main
3 changed files with 17 additions and 9 deletions

View file

@ -19,6 +19,7 @@ MANUFACTURERS = {
'Generic': GenericHost, 'Generic': GenericHost,
} }
MIN_NETBOX_VERSION = '3.3'
def run(config): def run(config):
dmi = dmidecode.parse() dmi = dmidecode.parse()
@ -34,8 +35,8 @@ def run(config):
except KeyError: except KeyError:
server = GenericHost(dmi=dmi) server = GenericHost(dmi=dmi)
if version.parse(nb.version) < version.parse('2.9'): if version.parse(nb.version) < version.parse(MIN_NETBOX_VERSION):
print('netbox-agent is not compatible with Netbox prior to verison 2.9') print(f'netbox-agent is not compatible with Netbox prior to verison {MIN_NETBOX_VERSION}')
return False return False
if config.register or config.update_all or config.update_network or \ if config.register or config.update_all or config.update_network or \

View file

@ -556,12 +556,19 @@ class ServerNetwork(Network):
switch_ip, switch_ip,
)) ))
cable = nb.dcim.cables.create( cable = nb.dcim.cables.create(
termination_a_id=nb_server_interface.id, a_terminations=[
termination_a_type="dcim.interface", {
termination_b_id=nb_switch_interface.id, 'object_id': nb_server_interface.id,
termination_b_type="dcim.interface", 'object_type':'dcim.interface'
}
],
b_terminations=[
{
'object_id': nb_switch_interface.id,
'object_type': 'dcim.interface'
}
],
) )
nb_server_interface.cable = cable
logging.info( logging.info(
'Connected interface {interface} with {switch_interface} of {switch_ip}'.format( 'Connected interface {interface} with {switch_interface} of {switch_ip}'.format(
interface=nb_server_interface.name, interface=nb_server_interface.name,
@ -579,7 +586,7 @@ class ServerNetwork(Network):
switch_ip, switch_interface, nb_server_interface switch_ip, switch_interface, nb_server_interface
) )
else: else:
nb_sw_int = nb_server_interface.cable.termination_b nb_sw_int = nb_server_interface.cable.b_terminations[0]
nb_sw = nb_sw_int.device nb_sw = nb_sw_int.device
nb_mgmt_int = nb.dcim.interfaces.get( nb_mgmt_int = nb.dcim.interfaces.get(
device_id=nb_sw.id, device_id=nb_sw.id,

View file

@ -1,4 +1,4 @@
pynetbox==6.1.2 pynetbox==7.0.1
netaddr==0.8.0 netaddr==0.8.0
netifaces==0.10.9 netifaces==0.10.9
pyyaml==6.0.1 pyyaml==6.0.1