Skip to content

Commit

Permalink
Improving error handling with conflicting SN and PN's
Browse files Browse the repository at this point in the history
If Netbox has multiple device types with the same Part Number the DeviceType.objects.get function throws an error. Same applies for devices with the same Serial Number. Neither condition should technically happen but Netbox doesn't treat PN or SN as unique so rather than throwing an exception, handle it gracefully.
  • Loading branch information
Jason Yates committed Oct 30, 2021
1 parent bd62f75 commit 43c9623
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions netbox_cisco_support/management/commands/sync_eox_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from django.conf import settings
from django.core.management.base import BaseCommand, CommandError
from django.core.exceptions import MultipleObjectsReturned
from datetime import datetime
from requests import api
from dcim.models import Manufacturer
Expand All @@ -28,7 +29,13 @@ def update_device_eox_data(self, device):
self.stdout.write(self.style.SUCCESS("Trying to update device %s" % device['sr_no']))

# Get the device object from NetBox
d = Device.objects.get(serial=device['sr_no'])
try:
d = Device.objects.get(serial=device['sr_no'])
except MultipleObjectsReturned:

# Error if netbox has multiple SN's and skip updating
self.stdout.write(self.style.NOTICE("ERROR: Multiple objects exist within Netbox with Serial Number " + device['sr_no']))
return

# Check if a CiscoSupport object already exists, if not, create a new one
try:
Expand Down Expand Up @@ -83,8 +90,16 @@ def update_device_eox_data(self, device):
return

def update_device_type_eox_data(self, pid, eox_data):
# Get the device type object for the supplied PID
dt = DeviceType.objects.get(part_number=pid)

try:
# Get the device type object for the supplied PID
dt = DeviceType.objects.get(part_number=pid)

except MultipleObjectsReturned:

# Error if netbox has multiple PN's
self.stdout.write(self.style.NOTICE("ERROR: Multiple objects exist within Netbox with Part Number " + pid))
return

# Check if CiscoDeviceTypeSupport record already exists
try:
Expand Down

0 comments on commit 43c9623

Please sign in to comment.