Skip to content

Commit f01e34e

Browse files
author
Dylan
committed
sync serials and asset tags
1 parent 39c1285 commit f01e34e

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

netbox_inventory/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class NetBoxInventoryConfig(PluginConfig):
2929
'transit_additional_status_names': [],
3030
'used_status_name': 'used',
3131
'used_additional_status_names': [],
32-
'sync_hardware_serial_asset_tag': False,
32+
'sync_hardware_serial_asset_tag': True,
3333
'sync_hardware_eol_date': True,
3434
'asset_sync_ignored_custom_fields': [],
3535
'asset_import_create_purchase': False,

netbox_inventory/signals.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,34 @@
2525

2626
logger = logging.getLogger('netbox.netbox_inventory.signals')
2727

28+
@receiver(post_save, sender=Asset)
29+
def sync_hardware_serial_asset_tag(instance, created, **kwargs):
30+
"""
31+
When an Asset is created or updated, sync its serial and asset_tag to the assigned hardware
32+
(Device, Module, InventoryItem, Rack) if assigned.
33+
34+
Only enforces if `sync_hardware_serial_asset_tag` setting is true.
35+
"""
36+
if not get_plugin_setting('sync_hardware_serial_asset_tag'):
37+
return
38+
39+
hardware = instance.hardware
40+
if not hardware:
41+
return
42+
43+
changed = False
44+
if not is_equal_none(hardware.serial, instance.serial):
45+
hardware.serial = instance.serial
46+
changed = True
47+
if not is_equal_none(hardware.asset_tag, instance.asset_tag):
48+
hardware.asset_tag = instance.asset_tag
49+
changed = True
50+
51+
if changed:
52+
hardware.full_clean()
53+
hardware.save()
54+
logger.info(f'Synced serial and asset tag from Asset {instance} to assigned {hardware}.')
55+
2856

2957
@receiver(pre_save, sender=Device)
3058
@receiver(pre_save, sender=Module)

0 commit comments

Comments
 (0)