1# SPDX-License-Identifier: Apache-2.0
2# Copyright (C) 2025 Marcin Zieba <marcinpsk@gmail.com>
3from netbox.plugins import PluginConfig
4
5__version__ = "1.0.1"
6
7DATA_IMPORT_CF_NAME = "data_import_source"
8
9
10def _ensure_import_custom_fields(sender, **kwargs):
11 """Auto-create the data_import_source JSON custom field on Device after migrations."""
12 try:
13 from extras.models import CustomField
14 from django.contrib.contenttypes.models import ContentType
15 from dcim.models import Device
16
17 device_ct = ContentType.objects.get_for_model(Device)
18 cf, created = CustomField.objects.get_or_create(
19 name=DATA_IMPORT_CF_NAME,
20 defaults={
21 "type": "json",
22 "label": "Data Import Source",
23 "description": ("Metadata set by the Data Import plugin: source_id, profile_id, profile_name."),
24 "required": False,
25 "weight": 9999,
26 },
27 )
28 if created:
29 cf.object_types.set([device_ct])
30 except Exception:
31 pass
32
33
34class NetBoxDataImportConfig(PluginConfig):
35 """NetBox plugin configuration for the Data Import plugin."""
36
37 name = "netbox_data_import"
38 verbose_name = "NetBox Data Import"
39 description = "NetBox plugin for importing data from external DCIM systems"
40 version = __version__
41 base_url = "data-import"
42 author = "Marcin Zieba"
43 author_email = "marcinpsk@gmail.com"
44 min_version = "4.2.0"
45
46 def ready(self):
47 """Register post_migrate signal to auto-create the data_import_source custom field."""
48 super().ready()
49 from django.db.models.signals import post_migrate
50
51 post_migrate.connect(_ensure_import_custom_fields, sender=self)
52
53
54config = NetBoxDataImportConfig