change .interface foreignkey with .assigned_object
This commit is contained in:
parent
51efa8edba
commit
dc582b5de6
1 changed files with 69 additions and 26 deletions
|
@ -15,6 +15,7 @@ from netbox_agent.lldp import LLDP
|
||||||
|
|
||||||
class Network(object):
|
class Network(object):
|
||||||
def __init__(self, server, *args, **kwargs):
|
def __init__(self, server, *args, **kwargs):
|
||||||
|
self.netbox_version = float(nb.version)
|
||||||
self.nics = []
|
self.nics = []
|
||||||
|
|
||||||
self.server = server
|
self.server = server
|
||||||
|
@ -318,10 +319,15 @@ class Network(object):
|
||||||
if not len(netbox_ips):
|
if not len(netbox_ips):
|
||||||
logging.info('Create new IP {ip} on {interface}'.format(
|
logging.info('Create new IP {ip} on {interface}'.format(
|
||||||
ip=ip, interface=interface))
|
ip=ip, interface=interface))
|
||||||
|
query_params = {
|
||||||
|
'address': ip,
|
||||||
|
'status': "active",
|
||||||
|
}
|
||||||
|
if self.netbox_version > 2.8:
|
||||||
|
query_params.update({self.intf_type: "assigned_object_id"})
|
||||||
|
|
||||||
netbox_ip = nb.ipam.ip_addresses.create(
|
netbox_ip = nb.ipam.ip_addresses.create(
|
||||||
address=ip,
|
**query_params
|
||||||
interface=interface.id,
|
|
||||||
status=1,
|
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
netbox_ip = netbox_ips[0]
|
netbox_ip = netbox_ips[0]
|
||||||
|
@ -340,29 +346,48 @@ class Network(object):
|
||||||
# or if everything is assigned to other servers
|
# or if everything is assigned to other servers
|
||||||
elif not len(assigned_anycast_ip):
|
elif not len(assigned_anycast_ip):
|
||||||
logging.info('Creating Anycast IP {} and assigning it to interface'.format(ip))
|
logging.info('Creating Anycast IP {} and assigning it to interface'.format(ip))
|
||||||
netbox_ip = nb.ipam.ip_addresses.create(
|
query_params = {
|
||||||
address=ip,
|
"address": ip,
|
||||||
interface=interface.id,
|
"status": "active",
|
||||||
status=1,
|
"role": self.ipam_choices['ip-address:role']['Anycast'],
|
||||||
role=self.ipam_choices['ip-address:role']['Anycast'],
|
"tenant": self.tenant.id if self.tenant else None,
|
||||||
tenant=self.tenant.id if self.tenant else None,
|
}
|
||||||
)
|
if self.netbox_version > 2.8:
|
||||||
|
query_params.update({
|
||||||
|
'assigned_object_type': self.assigned_object_type,
|
||||||
|
'assigned_object_id': interface.id
|
||||||
|
})
|
||||||
|
else:
|
||||||
|
query_params.update({'interface': interface.id})
|
||||||
|
netbox_ip = nb.ipam.ip_addresses.create(**query_params)
|
||||||
return netbox_ip
|
return netbox_ip
|
||||||
else:
|
else:
|
||||||
if netbox_ip.interface is None:
|
if hasattr(netbox_ip, 'interface') and netbox_ip.interface is None or \
|
||||||
|
hasattr(netbox_ip, 'assigned_object') and netbox_ip.assigned_object is None:
|
||||||
logging.info('Assigning existing IP {ip} to {interface}'.format(
|
logging.info('Assigning existing IP {ip} to {interface}'.format(
|
||||||
ip=ip, interface=interface))
|
ip=ip, interface=interface))
|
||||||
elif netbox_ip.interface.id != interface.id:
|
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:
|
||||||
|
old_interface = netbox_ip.assigned_object
|
||||||
|
else:
|
||||||
|
old_interface = netbox_ip.interface
|
||||||
|
|
||||||
logging.info(
|
logging.info(
|
||||||
'Detected interface change for ip {ip}: old interface is '
|
'Detected interface change for ip {ip}: old interface is '
|
||||||
'{old_interface} (id: {old_id}), new interface is {new_interface} '
|
'{old_interface} (id: {old_id}), new interface is {new_interface} '
|
||||||
' (id: {new_id})'
|
' (id: {new_id})'
|
||||||
.format(
|
.format(
|
||||||
old_interface=netbox_ip.interface, new_interface=interface,
|
old_interface=old_interface, new_interface=interface,
|
||||||
old_id=netbox_ip.id, new_id=interface.id, ip=netbox_ip.address
|
old_id=netbox_ip.id, new_id=interface.id, ip=netbox_ip.address
|
||||||
))
|
))
|
||||||
else:
|
else:
|
||||||
return netbox_ip
|
return netbox_ip
|
||||||
|
|
||||||
|
if self.netbox_version > 2.8:
|
||||||
|
netbox_ip.assigned_object_type = self.assigned_object_type
|
||||||
|
netbox_ip.assigned_object_id = interface.id
|
||||||
|
else:
|
||||||
netbox_ip.interface = interface
|
netbox_ip.interface = interface
|
||||||
netbox_ip.save()
|
netbox_ip.save()
|
||||||
return netbox_ip
|
return netbox_ip
|
||||||
|
@ -385,17 +410,29 @@ class Network(object):
|
||||||
|
|
||||||
# delete IP on netbox that are not known on this server
|
# delete IP on netbox that are not known on this server
|
||||||
if len(nb_nics):
|
if len(nb_nics):
|
||||||
|
if self.netbox_version > 2.8:
|
||||||
|
netbox_ips = nb.ipam.ip_addresses.filter(
|
||||||
|
assigned_object=[x.id for x in nb_nics],
|
||||||
|
)
|
||||||
|
else:
|
||||||
netbox_ips = nb.ipam.ip_addresses.filter(
|
netbox_ips = nb.ipam.ip_addresses.filter(
|
||||||
interface_id=[x.id for x in nb_nics],
|
interface_id=[x.id for x in nb_nics],
|
||||||
)
|
)
|
||||||
|
|
||||||
all_local_ips = list(chain.from_iterable([
|
all_local_ips = list(chain.from_iterable([
|
||||||
x['ip'] for x in self.nics if x['ip'] is not None
|
x['ip'] for x in self.nics if x['ip'] is not None
|
||||||
]))
|
]))
|
||||||
for netbox_ip in netbox_ips:
|
for netbox_ip in netbox_ips:
|
||||||
if netbox_ip.address not in all_local_ips:
|
if netbox_ip.address not in all_local_ips:
|
||||||
|
if self.netbox_version < 2.9:
|
||||||
logging.info('Unassigning IP {ip} from {interface}'.format(
|
logging.info('Unassigning IP {ip} from {interface}'.format(
|
||||||
ip=netbox_ip.address, interface=netbox_ip.interface))
|
ip=netbox_ip.address, interface=netbox_ip.interface))
|
||||||
netbox_ip.interface = None
|
netbox_ip.interface = None
|
||||||
|
else:
|
||||||
|
logging.info('Unassigning IP {ip} from {interface}'.format(
|
||||||
|
ip=netbox_ip.address, interface=netbox_ip.assigned_object))
|
||||||
|
netbox_ip.assigned_object_type = None
|
||||||
|
netbox_ip.assigned_object_id = None
|
||||||
netbox_ip.save()
|
netbox_ip.save()
|
||||||
|
|
||||||
# update each nic
|
# update each nic
|
||||||
|
@ -417,6 +454,7 @@ class Network(object):
|
||||||
ret, interface = self.reset_vlan_on_interface(nic, interface)
|
ret, interface = self.reset_vlan_on_interface(nic, interface)
|
||||||
nic_update += ret
|
nic_update += ret
|
||||||
|
|
||||||
|
if hasattr(interface, 'type'):
|
||||||
_type = self.get_netbox_type_for_nic(nic)
|
_type = self.get_netbox_type_for_nic(nic)
|
||||||
if not interface.type or \
|
if not interface.type or \
|
||||||
_type != interface.type.value:
|
_type != interface.type.value:
|
||||||
|
@ -465,6 +503,9 @@ class ServerNetwork(Network):
|
||||||
self.nb_net = nb.dcim
|
self.nb_net = nb.dcim
|
||||||
self.custom_arg = {'device': getattr(self.device, "id", None)}
|
self.custom_arg = {'device': getattr(self.device, "id", None)}
|
||||||
self.custom_arg_id = {'device_id': getattr(self.device, "id", None)}
|
self.custom_arg_id = {'device_id': getattr(self.device, "id", None)}
|
||||||
|
self.intf_type = "interface_id"
|
||||||
|
self.assigned_object_type = "dcim.interface"
|
||||||
|
|
||||||
|
|
||||||
def get_network_type(self):
|
def get_network_type(self):
|
||||||
return 'server'
|
return 'server'
|
||||||
|
@ -585,6 +626,8 @@ class VirtualNetwork(Network):
|
||||||
self.nb_net = nb.virtualization
|
self.nb_net = nb.virtualization
|
||||||
self.custom_arg = {'virtual_machine': getattr(self.device, "id", None)}
|
self.custom_arg = {'virtual_machine': getattr(self.device, "id", None)}
|
||||||
self.custom_arg_id = {'virtual_machine_id': getattr(self.device, "id", None)}
|
self.custom_arg_id = {'virtual_machine_id': getattr(self.device, "id", None)}
|
||||||
|
self.intf_type = "vminterface_id"
|
||||||
|
self.assigned_object_type = "virtualization.vminterface"
|
||||||
|
|
||||||
dcim_c = nb.virtualization.interfaces.choices()
|
dcim_c = nb.virtualization.interfaces.choices()
|
||||||
for _choice_type in dcim_c:
|
for _choice_type in dcim_c:
|
||||||
|
|
Loading…
Add table
Reference in a new issue