Skip to content

Commit 3131cdd

Browse files
committed
Add support for default status
1 parent d836f42 commit 3131cdd

File tree

4 files changed

+27
-12
lines changed

4 files changed

+27
-12
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ The plugin behavior can be controlled with the following list of settings
3333
- `create_device_type_if_missing` boolean (default True), If True, a new device type object will be created if the model discovered by Napalm do not match an existing device type.
3434
- `create_manufacturer_if_missing` boolean (default True), If True, a new manufacturer object will be created if the manufacturer discovered by Napalm is do not match an existing manufacturer, this option is only valid if `create_device_type_if_missing` is True as well.
3535
- `create_device_role_if_missing` boolean (default True), If True, a new device role object will be created if the device role provided was not provided as part of the onboarding and if the `default_device_role` do not already exist.
36+
- `default_device_status` string (default "active"), status assigned to a new device by default (must be lowercase).
3637
- `default_device_role` string (default "network")
37-
- `default_device_role_color` string (default FF0000) color assigned to the device role if it needs to be created.
38+
- `default_device_role_color` string (default FF0000), color assigned to the device role if it needs to be created.
3839
- `default_management_interface` string (default "PLACEHOLDER"), name of the management interface that will be created, if one can't be identified on the device.
3940
- `default_management_prefix_length` integer ( default 0), length of the prefix that will be used for the management IP address, if the IP can't be found.
4041

netbox_onboarding/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class OnboardingConfig(PluginConfig):
3636
"default_device_role_color": "FF0000",
3737
"default_management_interface": "PLACEHOLDER",
3838
"default_management_prefix_length": 0,
39+
"default_device_status": "active",
3940
}
4041
caching_config = {}
4142

netbox_onboarding/onboard.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -422,16 +422,28 @@ def ensure_device_role(
422422
self.netdev.ot.save()
423423
return
424424

425-
def ensure_device_instance(self):
426-
"""Ensure that the device instance exists in NetBox and is assigned the provided device role or DEFAULT_ROLE."""
427-
# TODO: this can create duplicate entries in NetBox...
428-
device, _ = Device.objects.get_or_create(
429-
name=self.netdev.hostname,
430-
device_type=self.device_type,
431-
device_role=self.netdev.ot.role,
432-
platform=self.netdev.ot.platform,
433-
site=self.netdev.ot.site,
434-
)
425+
def ensure_device_instance(self, default_status=PLUGIN_SETTINGS["default_device_status"]):
426+
"""Ensure that the device instance exists in NetBox and is assigned the provided device role or DEFAULT_ROLE.
427+
428+
Args:
429+
default_status (str) : status assigned to a new device by default.
430+
"""
431+
try:
432+
device = Device.objects.get(
433+
name=self.netdev.hostname,
434+
device_type=self.device_type,
435+
device_role=self.netdev.ot.role,
436+
site=self.netdev.ot.site,
437+
)
438+
except Device.DoesNotExist:
439+
device = Device.objects.create(
440+
name=self.netdev.hostname,
441+
device_type=self.device_type,
442+
device_role=self.netdev.ot.role,
443+
platform=self.netdev.ot.platform,
444+
site=self.netdev.ot.site,
445+
status=default_status,
446+
)
435447

436448
device.serial = self.netdev.serial_number
437449
device.save()

netbox_onboarding/tests/test_onboard.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,9 @@ def test_ensure_device_instance_not_exist(self):
119119
nbk.device_type = self.device_type1
120120
nbk.netdev.ot = self.onboarding_task3
121121

122-
nbk.ensure_device_instance()
122+
nbk.ensure_device_instance(default_status="planned")
123123
self.assertIsInstance(nbk.device, Device)
124+
self.assertEqual(nbk.device.status, "planned")
124125
self.assertEqual(nbk.device, nbk.netdev.ot.created_device)
125126
self.assertEqual(nbk.device.serial, "123456")
126127

0 commit comments

Comments
 (0)