Script for cable creation fail #2232

Closed
opened 2025-12-29 17:23:58 +01:00 by adam · 1 comment
Owner

Originally created by @coloHsq on GitHub (Dec 20, 2018).

Environment

  • Python version: 3.6.5
  • NetBox version: 2.5.1

Steps to Reproduce

I'm trying to automatically migrate connections data from interface to front and rear ports.
I'm doing this with two scripts, the first to create front and rear ports on patch panels, and the second to migrate all the interface connections on front/rear ports.

When the second script try to add a cable that connect a front port to an interface it throws this error :
"dcim.models.DoesNotExist: Cable matching query does not exist."
Following the stack trace this error is raised in the dcim/signals.py on "update_connected_endpoints" method, so i think that the cable is actually created but when this method enter here :

if instance.termination_a.cable != instance:
        instance.termination_a.cable = instance
        instance.termination_a.save()
    if instance.termination_b.cable != instance:
        instance.termination_b.cable = instance
        instance.termination_b.save()

it can't find the b termination so it crash.

This is the script to migrate connections:

from __future__ import unicode_literals

from django.contrib.contenttypes.models import ContentType

from dcim.constants import CONNECTION_STATUS_CONNECTED
from dcim.models import (Device, FrontPort, RearPort, Cable, Interface)

'''
I'm working on a single patch panel to reduce elaboration time during developement
'''
patchPanels = Device.objects.filter(pk = 232)
        
for device in patchPanels:
    
    '''
    for my naming convention patch panels are named "rack1-rack2.typeOfConnection[numberOfConnections]#1/2"
	typeOfConnection is like OM4 or C5e
	numberOfConnections is like "1-12"
	and the #1/2 at the end identifies the side 
	'''
	
    if '#' in device.name : 
	'''
	check for standard patch panels
	'''
	
        devicePorts = device.vc_interfaces
                        
        for port in devicePorts:
            
            try:            
                rem = port._connected_interface 
                
            except:
                rem = None
                               
            if rem != None:
            
                remDev = rem.device                   
                remPortName = rem.name
				
		'''
		interfaces on patch panels are named by incremantal number for front ports and back for rear 
                ports
		'''
                            
                if 'back' in port.name:
                
                    getLp = RearPort.objects.filter(device = device).filter(name = port.name)
                    getRp = RearPort.objects.filter(device = remDev).filter(name = remPortName)
                    
                    lp = getLp[0]
                    rp = getRp[0]

                else:
                    
                    getLp = FrontPort.objects.filter(device = device).filter(name = port.name)
                    
                    lp = getLp[0]
                    
                    if remDev.device_role == device.device_role:                    
                        getRp = FrontPort.objects.filter(device = remDev).filter(name = remPortName)
                        rp = getRp[0]
                        
                    else:
                        rp = rem                
                
                port.cable.delete()
                
                '''
               lp = local port -- rp = remote port 
               local ports are always front or rear ports because they are on a patch panel, remote ports can 
               be interfaces when they are on other devices like switch or servers
               '''
                if device.device_role.name == 'Cab. Fiber MM':
                    cable = Cable(termination_a = lp, termination_b = rp, type = 3040, color = "ff66ff")    
                    cable.save()
                    
                if device.device_role.name == 'Cab. Fiber SM':
                    c = Cable(termination_a = lp, termination_b = rp, type = 3500, color = "03a9f4")
                    c.save()
                    
                if device.device_role.name == 'Cab. Copper':
                    cable = Cable(termination_a = lp, termination_b = rp, type = 1600, color = "795548")
            
            else:
                pass
       
    else:
        pass

Expected Behavior

If everything goes right, after launching this script, all the cables related to patch panels interfaces should be deleted and replaced by cables related to front and rear port of patch panels.

Observed Behavior

When the cable is created with same type of termination everything goes fine, when it tries to create a cable with different types of endpoint it throws the error decribed above and looking on the web app, the cable figures connected in the right way, but looking on the devices that should be connected, only the one with front port(the patch panel) is really connected.

Originally created by @coloHsq on GitHub (Dec 20, 2018). ### Environment * Python version: 3.6.5 * NetBox version: 2.5.1 ### Steps to Reproduce I'm trying to automatically migrate connections data from interface to front and rear ports. I'm doing this with two scripts, the first to create front and rear ports on patch panels, and the second to migrate all the interface connections on front/rear ports. When the second script try to add a cable that connect a front port to an interface it throws this error : "dcim.models.DoesNotExist: Cable matching query does not exist." Following the stack trace this error is raised in the dcim/signals.py on "update_connected_endpoints" method, so i think that the cable is actually created but when this method enter here : ```python if instance.termination_a.cable != instance: instance.termination_a.cable = instance instance.termination_a.save() if instance.termination_b.cable != instance: instance.termination_b.cable = instance instance.termination_b.save() ``` it can't find the b termination so it crash. This is the script to migrate connections: ```python from __future__ import unicode_literals from django.contrib.contenttypes.models import ContentType from dcim.constants import CONNECTION_STATUS_CONNECTED from dcim.models import (Device, FrontPort, RearPort, Cable, Interface) ''' I'm working on a single patch panel to reduce elaboration time during developement ''' patchPanels = Device.objects.filter(pk = 232) for device in patchPanels: ''' for my naming convention patch panels are named "rack1-rack2.typeOfConnection[numberOfConnections]#1/2" typeOfConnection is like OM4 or C5e numberOfConnections is like "1-12" and the #1/2 at the end identifies the side ''' if '#' in device.name : ''' check for standard patch panels ''' devicePorts = device.vc_interfaces for port in devicePorts: try: rem = port._connected_interface except: rem = None if rem != None: remDev = rem.device remPortName = rem.name ''' interfaces on patch panels are named by incremantal number for front ports and back for rear ports ''' if 'back' in port.name: getLp = RearPort.objects.filter(device = device).filter(name = port.name) getRp = RearPort.objects.filter(device = remDev).filter(name = remPortName) lp = getLp[0] rp = getRp[0] else: getLp = FrontPort.objects.filter(device = device).filter(name = port.name) lp = getLp[0] if remDev.device_role == device.device_role: getRp = FrontPort.objects.filter(device = remDev).filter(name = remPortName) rp = getRp[0] else: rp = rem port.cable.delete() ''' lp = local port -- rp = remote port local ports are always front or rear ports because they are on a patch panel, remote ports can be interfaces when they are on other devices like switch or servers ''' if device.device_role.name == 'Cab. Fiber MM': cable = Cable(termination_a = lp, termination_b = rp, type = 3040, color = "ff66ff") cable.save() if device.device_role.name == 'Cab. Fiber SM': c = Cable(termination_a = lp, termination_b = rp, type = 3500, color = "03a9f4") c.save() if device.device_role.name == 'Cab. Copper': cable = Cable(termination_a = lp, termination_b = rp, type = 1600, color = "795548") else: pass else: pass ``` ### Expected Behavior If everything goes right, after launching this script, all the cables related to patch panels interfaces should be deleted and replaced by cables related to front and rear port of patch panels. ### Observed Behavior When the cable is created with same type of termination everything goes fine, when it tries to create a cable with different types of endpoint it throws the error decribed above and looking on the web app, the cable figures connected in the right way, but looking on the devices that should be connected, only the one with front port(the patch panel) is really connected.
adam closed this issue 2025-12-29 17:23:58 +01:00
Author
Owner

@coloHsq commented on GitHub (Dec 20, 2018):

At the end it was my fault, apparently create a cable giving only the complete instance of a termination works only if both termination are of the same type... Giving the pk and the type everything works.
In any case the half connecting cable issue can maybe cause some problem to someone...

@coloHsq commented on GitHub (Dec 20, 2018): At the end it was my fault, apparently create a cable giving only the complete instance of a termination works only if both termination are of the same type... Giving the pk and the type everything works. In any case the half connecting cable issue can maybe cause some problem to someone...
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#2232