Skip to content

Commit e616611

Browse files
authored
Merge pull request #45 from networktocode/dga-issue-23
Add support for default status
2 parents d36a4ae + 280888a commit e616611

File tree

4 files changed

+44
-12
lines changed

4 files changed

+44
-12
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ The plugin behavior can be controlled with the following list of settings
3939
- `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.
4040
- `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.
4141
- `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.
42+
- `default_device_status` string (default "active"), status assigned to a new device by default (must be lowercase).
4243
- `default_device_role` string (default "network")
43-
- `default_device_role_color` string (default FF0000) color assigned to the device role if it needs to be created.
44+
- `default_device_role_color` string (default FF0000), color assigned to the device role if it needs to be created.
4445
- `default_management_interface` string (default "PLACEHOLDER"), name of the management interface that will be created, if one can't be identified on the device.
4546
- `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.
4647

netbox_onboarding/__init__.py

+1
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

+17-10
Original file line numberDiff line numberDiff line change
@@ -422,17 +422,24 @@ 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.
435427
428+
Args:
429+
default_status (str) : status assigned to a new device by default.
430+
"""
431+
try:
432+
device = Device.objects.get(name=self.netdev.hostname, site=self.netdev.ot.site)
433+
except Device.DoesNotExist:
434+
device = Device.objects.create(
435+
name=self.netdev.hostname,
436+
site=self.netdev.ot.site,
437+
device_type=self.device_type,
438+
device_role=self.netdev.ot.role,
439+
status=default_status,
440+
)
441+
442+
device.platform = self.netdev.ot.platform
436443
device.serial = self.netdev.serial_number
437444
device.save()
438445

netbox_onboarding/tests/test_onboard.py

+24-1
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,34 @@ 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")
125+
self.assertEqual(nbk.device.platform, self.platform1)
124126
self.assertEqual(nbk.device, nbk.netdev.ot.created_device)
125127
self.assertEqual(nbk.device.serial, "123456")
126128

129+
def test_ensure_device_instance_exist(self):
130+
"""Verify ensure_device_instance function."""
131+
device = Device.objects.create(
132+
name=self.ndk2.hostname,
133+
site=self.site1,
134+
device_type=self.device_type1,
135+
device_role=self.device_role1,
136+
status="planned",
137+
serial="987654",
138+
)
139+
140+
nbk = NetboxKeeper(self.ndk2)
141+
nbk.netdev.ot = self.onboarding_task3
142+
self.assertEqual(nbk.device, None)
143+
nbk.ensure_device_instance(default_status="active")
144+
self.assertIsInstance(nbk.device, Device)
145+
self.assertEqual(nbk.device.status, "planned")
146+
self.assertEqual(nbk.device.platform, self.platform1)
147+
self.assertEqual(nbk.device, device)
148+
self.assertEqual(nbk.device.serial, "123456")
149+
127150
def test_ensure_interface_not_exist(self):
128151
"""Verify ensure_interface function when the interface do not exist."""
129152
nbk = NetboxKeeper(self.ndk2)

0 commit comments

Comments
 (0)