forked from netbox-community/Device-Type-Library-Import
-
Notifications
You must be signed in to change notification settings - Fork 0
Update library import / small refactor #9
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
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
5a48621
chore: updated python deps
marcinpsk d54e492
chore: updated python deps
marcinpsk c6c422d
chore: updated python deps
marcinpsk 887585c
chore: added .envrc
marcinpsk 813b5cf
Merge main into develop to sync upstream changes
marcinpsk 89994b1
feat: add change detection and --update flag for device types
marcinpsk 04a1592
fix: handle pynetbox Record objects in change detection
marcinpsk 498529b
fix: normalize trailing whitespace in change detection
marcinpsk 07962a6
fix: updated new device creation
marcinpsk 69fc19a
fix: added --remove-components to remove components from models when …
marcinpsk 45fd416
chore: added dependabot.yml, updated tests.yml and removed stale.yml
marcinpsk 3883d97
fix: changed black to ruff format
marcinpsk aa21e48
refactor: consolidate change detection, DRY up netbox_api, add markdo…
marcinpsk c981780
fix: correct module counter keys, alias-aware component additions, pe…
marcinpsk 04d1510
fix: respect empty cache in _get_cached_or_fetch, fix NetBox capitali…
marcinpsk 86a2e30
fix: use _get_cached_or_fetch in update/remove_components, fix endpoi…
marcinpsk 28c3eae
fix: use item.name instead of str(item) for component cache keys
marcinpsk 50b7ba3
fix: invalidate component cache after successful creation in _create_…
marcinpsk 45ddfe6
perf: scope component preload to relevant device types when vendors a…
marcinpsk 8a21f19
fix: invalidate component cache after successful removal in remove_co…
marcinpsk abb1c87
fix: use _get_cached_or_fetch in _create_generic to fix module compon…
marcinpsk c6d19d8
perf: scope component preload by vendor, global fetch when no vendors…
marcinpsk 1f229f8
fix: updated progress on compare
marcinpsk 641ff69
fix: detect property removals, guard component removal detection, and…
marcinpsk 17d3561
fix: skip absent YAML properties in change detection, remove unused m…
marcinpsk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| # direnv configuration | ||
| # Run `direnv allow` to enable | ||
|
|
||
| # Source .env file if it exists (contains secrets, not in git) | ||
| if [[ -f .env ]]; then | ||
| log_status "Loading .env variables..." | ||
| dotenv .env | ||
| fi | ||
|
|
||
| # Create and activate uv virtual environment | ||
| if command -v uv &> /dev/null; then | ||
| # Create venv if it doesn't exist | ||
| if [[ ! -d .venv ]]; then | ||
| log_status "Creating virtual environment with uv..." | ||
| uv venv .venv | ||
| fi | ||
|
|
||
| # Sync dependencies | ||
| if [[ -f pyproject.toml ]]; then | ||
| log_status "Syncing dependencies with uv..." | ||
| uv sync --quiet --native-tls | ||
| fi | ||
|
|
||
| # Activate the virtual environment | ||
| source .venv/bin/activate | ||
| else | ||
| log_error "uv not found. Please install uv: https://docs.astral.sh/uv/" | ||
| fi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -104,6 +104,7 @@ celerybeat.pid | |
| # Environments | ||
| .env* | ||
| !.env.example | ||
| !.envrc | ||
| .venv | ||
| env/ | ||
| venv/ | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| "MD013": { | ||
| "line_length": 120 | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,112 +1,153 @@ | ||
| # Netbox Device Type Import | ||
| # NetBox Device Type Import | ||
|
|
||
| This library is intended to be your friend and help you import all the device-types defined within the the [NetBox Device Type Library Repository](https://github.com/netbox-community/devicetype-library). | ||
| This library is intended to be your friend and help you import all the device-types defined within | ||
| the [NetBox Device Type Library Repository](https://github.com/netbox-community/devicetype-library). | ||
|
|
||
| > Tested working with 2.9.4, 2.10.4 | ||
|
|
||
| ## 🪄 Description | ||
| ## Description | ||
|
|
||
| This script will clone a copy of the `netbox-community/devicetype-library` repository to your machine to allow it to import the device types you would like without copy and pasting them into the Netbox UI. | ||
| This script will clone a copy of the `netbox-community/devicetype-library` repository to your | ||
| machine to allow it to import the device types you would like without copy and pasting them | ||
| into the NetBox UI. | ||
|
|
||
| ## 🚀 Getting Started | ||
| ## Getting Started | ||
|
|
||
| 3. This script uses `uv` for dependency management. | ||
| 1. This script uses `uv` for dependency management. | ||
|
|
||
| ``` | ||
| uv sync | ||
| ``` | ||
| ```shell | ||
| uv sync | ||
| ``` | ||
|
|
||
| 4. There are two variables that are required when using this script to import device types into your NetBox installation. (1) Your Netbox instance URL and (2) a token with **write rights**. | ||
| 1. There are two variables that are required when using this script to import device types | ||
| into your NetBox installation. (1) Your NetBox instance URL and (2) a token with | ||
| **write rights**. | ||
|
|
||
| Copy the existing `.env.example` to your own `.env` file, and fill in the variables. | ||
|
|
||
| ``` | ||
| ```shell | ||
| cp .env.example .env | ||
| vim .env | ||
| ``` | ||
|
|
||
| Finally, we are able to execute the script and import some device templates! | ||
marcinpsk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ## 🔌 Usage | ||
| ## Usage | ||
|
|
||
| To use the script, simply execute the script as follows. `uv` will handle the virtual environment. | ||
| To use the script, simply execute the script as follows. `uv` will handle the virtual | ||
| environment. | ||
|
|
||
| ``` | ||
| ```shell | ||
| uv run nb-dt-import.py | ||
| ``` | ||
|
|
||
| Copy the existing `.env.example` to your own `.env` file, and fill in the variables. | ||
| This will clone the latest master branch from the `netbox-community/devicetype-library` | ||
| from GitHub and install it into the `repo` subdirectory. If this directory already exists, | ||
| it will perform a `git pull` to update the repository instead. | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ``` | ||
| cp .env.example .env | ||
| vim .env | ||
| ``` | ||
| Next, it will loop over every manufacturer and every device of every manufacturer and begin | ||
| checking if your NetBox install already has them, and if not, creates them. It will skip | ||
| preexisting manufacturers, devices, interfaces, etc. so as to not end up with duplicate | ||
| entries in your NetBox instance. | ||
|
|
||
| Finally, we are able to execute the script and import some device templates! | ||
| ### Arguments | ||
|
|
||
| ## 🔌 Usage | ||
| This script currently accepts a list of vendors as an argument, so that you can selectively | ||
| import devices. | ||
|
|
||
| To use the script, simply execute the script as follows. Make sure you're still in the activated virtual environment we created before. | ||
| To import only device by APC, for example: | ||
|
|
||
| ```shell | ||
| ./nb-dt-import.py --vendors apc | ||
| ``` | ||
| ./nb-dt-import.py | ||
|
|
||
| `--vendors` can also accept a comma-separated list of vendors if you want to import multiple. | ||
|
|
||
| ```shell | ||
| ./nb-dt-import.py --vendors apc,juniper | ||
| ``` | ||
|
|
||
| This will clone the latest master branch from the `netbox-community/devicetype-library` from Github and install it into the `repo` subdirectory. If this directory already exists, it will perform a `git pull` to update the repository instead. | ||
| #### Update Mode | ||
|
|
||
| Next, it will loop over every manufacturer and every device of every manufacturer and begin checking if your Netbox install already has them, and if not, creates them. It will skip preexisting manufacturers, devices, interfaces, etc. so as to not end up with duplicate entries in your Netbox instance. | ||
| By default, the script only creates new device types and skips existing ones. To update | ||
| existing device types: | ||
|
|
||
| ### 🧰 Arguments | ||
| ```shell | ||
| ./nb-dt-import.py --update | ||
| ``` | ||
|
|
||
| This script currently accepts a list of vendors as an argument, so that you can selectively import devices. | ||
| This will: | ||
|
|
||
| To import only device by APC, for example: | ||
| - Add new components (interfaces, power ports, etc.) that are in YAML but missing from NetBox | ||
| - Update properties of existing components if they've changed | ||
| - Update device type properties (u_height, part_number, etc.) if they've changed | ||
| - **Report** components that exist in NetBox but are missing from YAML (won't delete by default) | ||
|
|
||
| ``` | ||
| ./nb-dt-import.py --vendors apc | ||
| ``` | ||
| #### Component Removal (Use with Caution) | ||
|
|
||
| `--vendors` can also accept a comma separated list of vendors if you want to import multiple. | ||
| > **WARNING**: Removing components can affect existing device instances in NetBox. | ||
|
|
||
| If you've changed a device type definition (for example, converting interfaces to module-bays | ||
| to support SFP modules), you can remove obsolete components with: | ||
|
|
||
| ```shell | ||
| ./nb-dt-import.py --update --remove-components | ||
| ``` | ||
| ./nb-dt-import.py --vendors apc,juniper | ||
| ``` | ||
|
|
||
| This will delete any components (interfaces, ports, bays, etc.) that exist in NetBox but are | ||
| no longer present in the YAML definition. | ||
|
|
||
| **Use cases**: | ||
|
|
||
| - Converting fixed interfaces to module-bays for modular devices | ||
| - Removing incorrectly defined components from device templates | ||
| - Cleaning up after major device type definition changes | ||
|
|
||
| **Important considerations**: | ||
|
|
||
| - Components attached to actual device instances may prevent deletion | ||
| - Review the change detection report before enabling component removal | ||
| - Test on a staging NetBox instance first if possible | ||
|
|
||
| ## Docker build | ||
|
|
||
| It's possible to use this project as a docker container. | ||
|
|
||
| To build : | ||
| To build: | ||
|
|
||
| ``` | ||
| ```shell | ||
| docker build -t netbox-devicetype-import-library . | ||
| ``` | ||
|
|
||
| Alternatively you can pull a pre-built image from Github Container Registry (ghcr.io): | ||
| Alternatively you can pull a pre-built image from GitHub Container Registry (ghcr.io): | ||
|
|
||
| ``` | ||
| ```shell | ||
| docker pull ghcr.io/minitriga/netbox-device-type-library-import | ||
| ``` | ||
|
|
||
| The container supports the following env var as configuration : | ||
| The container supports the following env var as configuration: | ||
|
|
||
| - `REPO_URL`, the repo to look for device types (defaults to _https://github.com/netbox-community/devicetype-library.git_) | ||
| - `REPO_URL`, the repo to look for device types | ||
| (defaults to `https://github.com/netbox-community/devicetype-library.git`) | ||
| - `REPO_BRANCH`, the branch to check out if appropriate, defaults to master. | ||
| - `NETBOX_URL`, used to access netbox | ||
| - `NETBOX_TOKEN`, token for accessing netbox | ||
| - `VENDORS`, a comma-separated list of vendors to import (defaults to None) | ||
| - `REQUESTS_CA_BUNDLE`, path to a CA_BUNDLE for validation if you are using self-signed certificates(file must be included in the container) | ||
| - `REQUESTS_CA_BUNDLE`, path to a CA_BUNDLE for validation if you are using | ||
| self-signed certificates (file must be included in the container) | ||
|
|
||
| To run : | ||
| To run: | ||
|
|
||
| ``` | ||
| docker run -e "NETBOX_URL=http://netbox:8080/" -e "NETBOX_TOKEN=98765434567890" ghcr.io/minitriga/netbox-device-type-library-import | ||
| ```shell | ||
| docker run -e "NETBOX_URL=http://netbox:8080/" \ | ||
| -e "NETBOX_TOKEN=98765434567890" \ | ||
| ghcr.io/minitriga/netbox-device-type-library-import | ||
| ``` | ||
|
|
||
| ## 🧑💻 Contributing | ||
| ## Contributing | ||
|
|
||
| We're happy about any pull requests! | ||
|
|
||
| ## 📜 License | ||
| ## License | ||
|
|
||
| MIT | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.