Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nautobot v2.2.x #166

Merged
merged 17 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ jobs:
matrix:
python-version: ["3.8"]
db-backend: ["postgresql"]
nautobot-version: ["2.0.6", "2.1"]
nautobot-version: ["2.0.6", "2.1", "2.2"]
include:
- python-version: "3.11"
db-backend: "postgresql"
Expand Down
2 changes: 2 additions & 0 deletions changes/165.added
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Added `--trace-issues` argument to `nautobot-server import_netbox` command to log exception trace-backs.
Added `created` and `updated` to Nautobot model stats.
1 change: 1 addition & 0 deletions changes/165.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed reporting non-imported (`save()` failed) instances to DiffSync library.
1 change: 1 addition & 0 deletions changes/166.added
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added Nautobot `v2.2.x` support.
1 change: 1 addition & 0 deletions changes/166.changed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Changed importing locations to allow importing to many-to-many field `locations` if defined.
4 changes: 3 additions & 1 deletion development/nautobot_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,14 @@
},
},
"loggers": {
"django": {"handlers": ["normal_console"], "level": "INFO"},
"django": {"handlers": ["normal_console"], "level": "INFO", "propagate": False},
"nautobot": {
"handlers": ["verbose_console" if DEBUG else "normal_console"],
"level": LOG_LEVEL,
"propagate": False,
},
},
"root": {"handlers": ["normal_console"], "level": "INFO"},
}

if DEBUG:
Expand Down
2 changes: 1 addition & 1 deletion docs/dev/demo.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ The initial output of the import process will resemble the following:

```
Running docker compose command "ps --services --filter status=running"
Running docker compose command "exec nautobot nautobot-server import_netbox --save-json-summary-path=generated-mappings.json --bypass-data-validation --dry-run --field-mapping --sitegroup-parent-always-region --summary --no-color https://raw.githubusercontent.com/netbox-community/netbox-demo-data/master/json/netbox-demo-v3.6.json"
Running docker compose command "exec nautobot nautobot-server import_netbox --save-json-summary-path=generated-mappings.json --bypass-data-validation --dry-run --sitegroup-parent-always-region --print-summary --no-color https://raw.githubusercontent.com/netbox-community/netbox-demo-data/master/json/netbox-demo-v3.6.json"
11:01:05.550 DEBUG nautobot.core.celery __init__.py import_jobs_as_celery_tasks() :
Importing system Jobs
11:01:05.552 DEBUG nautobot.core.celery __init__.py register_jobs() :
Expand Down
8 changes: 4 additions & 4 deletions docs/user/app_use_cases.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ This document describes common use-cases and scenarios for this App.
This app provides `import_netbox` management command to import data from NetBox with the following options:

```bash
nautobot-server import_netbox --help

nautobot-server help import_netbox
usage: nautobot-server import_netbox [-h] [--dry-run] [--update-paths] [--bypass-data-validation] [--sitegroup-parent-always-region] [--fix-powerfeed-locations] [--print-summary]
[--no-unrack-zero-uheight-devices] [--save-json-summary-path SAVE_JSON_SUMMARY_PATH] [--save-text-summary-path SAVE_TEXT_SUMMARY_PATH] [--version] [-v {0,1,2,3}]
[--settings SETTINGS] [--pythonpath PYTHONPATH] [--traceback] [--no-color] [--force-color] [--skip-checks]
[--no-unrack-zero-uheight-devices] [--save-json-summary-path SAVE_JSON_SUMMARY_PATH] [--save-text-summary-path SAVE_TEXT_SUMMARY_PATH] [--trace-issues] [--version]
[-v {0,1,2,3}] [--settings SETTINGS] [--pythonpath PYTHONPATH] [--traceback] [--no-color] [--force-color] [--skip-checks]
json_file

Import a NetBox JSON data dump into Nautobot's database
Expand All @@ -38,6 +37,7 @@ options:
File path to write the JSON summary to.
--save-text-summary-path SAVE_TEXT_SUMMARY_PATH
File path to write the text summary to.
--trace-issues Show a detailed trace of issues originated from any `Exception` found during the import.
--version show program's version number and exit
-v {0,1,2,3}, --verbosity {0,1,2,3}
Verbosity level; 0=minimal output, 1=normal output, 2=verbose output, 3=very verbose output
Expand Down
9 changes: 8 additions & 1 deletion nautobot_netbox_importer/diffsync/adapters/netbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class NetBoxImporterOptions(NamedTuple):
unrack_zero_uheight_devices: bool = True
save_json_summary_path: str = ""
save_text_summary_path: str = ""
trace_issues: bool = False


AdapterSetupFunction = Callable[[SourceAdapter], None]
Expand All @@ -71,7 +72,13 @@ class NetBoxAdapter(SourceAdapter):
# pylint: disable=keyword-arg-before-vararg
def __init__(self, input_ref: _FileRef, options: NetBoxImporterOptions, job=None, sync=None, *args, **kwargs):
"""Initialize NetBox Source Adapter."""
super().__init__(name="NetBox", get_source_data=_get_reader(input_ref), *args, **kwargs)
super().__init__(
name="NetBox",
*args,
get_source_data=_get_reader(input_ref),
trace_issues=options.trace_issues,
**kwargs,
)
self.job = job
self.sync = sync

Expand Down
8 changes: 6 additions & 2 deletions nautobot_netbox_importer/diffsync/models/locations.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from nautobot_netbox_importer.base import RecordData
from nautobot_netbox_importer.generator import DiffSyncBaseModel
from nautobot_netbox_importer.generator import InternalFieldType
from nautobot_netbox_importer.generator import SourceAdapter
from nautobot_netbox_importer.generator import SourceField
from nautobot_netbox_importer.generator import SourceModelWrapper
Expand All @@ -17,6 +18,9 @@ def define_location(field: SourceField) -> None:
site_wrapper = wrapper.adapter.wrappers["dcim.site"]
region_wrapper = wrapper.adapter.wrappers["dcim.region"]

# Nautobot v2.2.0 uses a ManyToManyField `locations` field instead of a ForeignKey `location`
locations = wrapper.nautobot.add_field("locations").internal_type == InternalFieldType.MANY_TO_MANY_FIELD

def location_importer(source: RecordData, target: DiffSyncBaseModel) -> None:
location = source.get(field.name, None)
site = source.get("site", None)
Expand All @@ -35,9 +39,9 @@ def location_importer(source: RecordData, target: DiffSyncBaseModel) -> None:
else:
return

field.set_nautobot_value(target, result)
field.set_nautobot_value(target, set([result]) if locations else result)

field.set_importer(location_importer)
field.set_importer(location_importer, "locations" if locations else "location")
field.handle_sibling("site", field.nautobot.name)
field.handle_sibling("region", field.nautobot.name)

Expand Down
2 changes: 2 additions & 0 deletions nautobot_netbox_importer/generator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from . import fields
from .base import EMPTY_VALUES
from .base import InternalFieldType
from .nautobot import NautobotAdapter
from .source import DiffSyncBaseModel
from .source import ImporterPass
Expand All @@ -21,6 +22,7 @@
"DiffSyncBaseModel",
"EMPTY_VALUES",
"ImporterPass",
"InternalFieldType",
"InvalidChoiceValueIssue",
"NautobotAdapter",
"PreImportResult",
Expand Down
Loading