Skip to content

Commit

Permalink
Fixed bug in which a device's device redundancy group priority was no…
Browse files Browse the repository at this point in the history
…t being set to `None` when the device redundancy group was deleted. (nautobot#4737)

Co-authored-by: Hanlin Miao <[email protected]>
  • Loading branch information
nrnvgh and HanlinMiao authored Nov 2, 2023
1 parent 6e1e897 commit fd7cab0
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions changes/4718.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed bug in which a device's device redundancy group priority was not being set to `None` when the device redundancy group was deleted.
17 changes: 17 additions & 0 deletions nautobot/dcim/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
Cable,
CablePath,
Device,
DeviceRedundancyGroup,
PathEndpoint,
PowerPanel,
Rack,
Expand Down Expand Up @@ -146,6 +147,22 @@ def handle_rack_location_change(instance, created, raw=False, **kwargs):
device.save()


#
# Device redundancy group
#


@receiver(pre_delete, sender=DeviceRedundancyGroup)
def clear_deviceredundancygroup_members(instance, **kwargs):
"""
When a DeviceRedundancyGroup is deleted, nullify the device_redundancy_group_priority field of its prior members.
"""
devices = Device.objects.filter(device_redundancy_group=instance.pk)
for device in devices:
device.device_redundancy_group_priority = None
device.save()


#
# Virtual chassis
#
Expand Down
40 changes: 40 additions & 0 deletions nautobot/dcim/tests/test_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from nautobot.dcim.models import (
Device,
DeviceRedundancyGroup,
DeviceType,
Location,
LocationType,
Expand Down Expand Up @@ -69,3 +70,42 @@ def test_master_device_null_vc_assignment(self):
self.device.refresh_from_db()
self.assertEqual(self.device.vc_position, 1)
self.assertEqual(self.device.virtual_chassis, virtualchassis)


class DeviceRedundancyGroupTest(TestCase):
"""Class to test signals for DeviceRedundancyGroup."""

@classmethod
def setUpTestData(cls):
"""Setup Test Data for DeviceRedundancyGroup Signal tests."""
location = Location.objects.filter(location_type=LocationType.objects.get(name="Campus")).first()
manufacturer = Manufacturer.objects.first()
devicetype = DeviceType.objects.create(manufacturer=manufacturer, model="Device Type")
devicerole = Role.objects.get_for_model(Device).first()
devicestatus = Status.objects.get_for_model(Device).first()

cls.device = Device.objects.create(
name="Device 1",
device_type=devicetype,
role=devicerole,
status=devicestatus,
location=location,
)

def test_device_redundancy_group_priority_is_null(self):
"""Test device with not null device_redundancy_group_priority is null after its device redundancy group is deleted.
This test is for https://github.com/nautobot/nautobot/issues/4718
"""
deviceredundancygroup = DeviceRedundancyGroup.objects.first()

self.device.device_redundancy_group = deviceredundancygroup
self.device.device_redundancy_group_priority = 1
self.device.validated_save()

deviceredundancygroup.delete()

self.device.refresh_from_db()

self.assertIsNone(self.device.device_redundancy_group)
self.assertIsNone(self.device.device_redundancy_group_priority)

0 comments on commit fd7cab0

Please sign in to comment.