File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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 ,
Original file line number Diff line number Diff line change 2525
2626logger = 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 )
You can’t perform that action at this time.
0 commit comments