Skip to content

Commit 3081b39

Browse files
authored
Import Improvements (#197)
* chore: Cleanup and docs * chore: Build test fixtures * fix: Test NetBox v4 * chore: NetBox 4.2 fixtures * fix: Add changelo fragment
1 parent 8922258 commit 3081b39

File tree

73 files changed

+211993
-122
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+211993
-122
lines changed

changes/197.added

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added NetBox v4.2 tests.

docs/dev/generator.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,16 @@ The first data iteration constructs the wrapping structure, which includes:
6363

6464
- `SourceAdapter` with all source model `adapter.wrappers`.
6565
- The `SourceAdapter` manages `SourceModelWrapper` and `NautobotModelWrapper` instances.
66-
- A `SourceModelWrapper` for each source content type, with `source_wrapper.fields` detailing how to import the source data.
66+
- A `SourceModelWrapper` for each source content type, with `fields` detailing how to import the source data.
6767
- Each `SourceModelWrapper` instance corresponds to a single `NautobotModelWrapper` instance.
6868
- A `NautobotModelWrapper` for each Nautobot content type, detailing `nautobot_wrapper.fields` and types, aiding in constructing the `DiffSyncModel` instances.
6969
- A single `NautobotModelWrapper` instance can be referenced by multiple `SourceModelWrapper` instances.
7070

71-
During this phase, all non-defined but present source fields are appended to the `source_wrapper.fields`, focusing on field names, not values.
71+
During this phase, all non-defined but present source fields are appended to the `SourceModelWrapper.fields`, focusing on field names, not values.
7272

7373
### Creating Source Importers
7474

75-
Convert each `source_wrapper.fields` item into a callable based on previously-established field definitions. The callables convert the source data into the `DiffSyncModel` constructor's expected structure.
75+
Convert each `SourceModelWrapper.fields` item into a callable based on previously-established field definitions. The callables convert the source data into the `DiffSyncModel` constructor's expected structure.
7676

7777
In this stage, the structure described in the previous section is enhanced.
7878

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
"""DiffSync adapter and model implementation for nautobot-netbox-importer."""
1+
"""DiffSync adapter and model implementation for nautobot-netbox-importer.
2+
3+
This folder is an importer implementation specific to NetBox, in opposite to `generator` folder, that is a generic Source => Nautobot importer.
4+
"""

nautobot_netbox_importer/diffsync/models/base.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@
22

33
from packaging.version import Version
44

5+
from nautobot_netbox_importer.diffsync.adapters.netbox import NetBoxAdapter
56
from nautobot_netbox_importer.diffsync.models.locations import define_locations
6-
from nautobot_netbox_importer.generator import SourceAdapter, fields
7+
from nautobot_netbox_importer.generator import fields
78

89

9-
def setup(adapter: SourceAdapter) -> None:
10+
def setup(adapter: NetBoxAdapter) -> None:
1011
"""Map NetBox base models to Nautobot."""
11-
adapter.disable_model("sessions.session", "Nautobot has own sessions, sessions should never cross apps.")
12-
netbox_version = adapter.options.netbox_version # type: ignore[no-member]
12+
netbox_version = adapter.options.netbox_version
13+
1314
adapter.disable_model("admin.logentry", "Not directly used in Nautobot.")
14-
adapter.disable_model("users.userconfig", "May not have a 1 to 1 translation to Nautobot.")
1515
adapter.disable_model("auth.permission", "Handled via a Nautobot model and may not be a 1 to 1.")
16+
adapter.disable_model("extras.imageattachment", "Images are not imported yet.")
17+
adapter.disable_model("sessions.session", "Nautobot has own sessions, sessions should never cross apps.")
18+
adapter.disable_model("users.userconfig", "May not have a 1 to 1 translation to Nautobot.")
1619

1720
adapter.configure_model(
1821
"extras.Status",

nautobot_netbox_importer/diffsync/models/locations.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,11 @@ def forward_references(wrapper: SourceModelWrapper, references: SourceReferences
175175
"dcim.SiteGroup",
176176
nautobot_content_type="dcim.LocationType",
177177
fields={
178-
"parent": fields.constant(region_type_uid, reference=location_type_wrapper)
179-
if sitegroup_parent_always_region
180-
else "parent",
178+
"parent": (
179+
fields.constant(region_type_uid, reference=location_type_wrapper)
180+
if sitegroup_parent_always_region
181+
else "parent"
182+
),
181183
"nestable": fields.constant(True),
182184
},
183185
)

nautobot_netbox_importer/generator/nautobot.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,14 @@ def diffsync_class(self) -> Type["DiffSyncBaseModel"]:
310310

311311
return result
312312

313+
@property
314+
def content_type_id(self) -> Optional[int]:
315+
"""Get the Nautobot content type ID."""
316+
if self.disabled:
317+
return None
318+
319+
return self.content_type_instance.pk # type: ignore
320+
313321
def get_summary(self) -> NautobotModelSummary:
314322
"""Get the summary."""
315323
issues = sorted(self.get_importer_issues())
@@ -318,7 +326,7 @@ def get_summary(self) -> NautobotModelSummary:
318326

319327
return NautobotModelSummary(
320328
content_type=self.content_type,
321-
content_type_id=None if self.disabled else self.content_type_instance.pk,
329+
content_type_id=self.content_type_id,
322330
stats=self.stats,
323331
issues=issues,
324332
flags=str(self.flags),

0 commit comments

Comments
 (0)