update: README and added dotenv other small stuff

This commit is contained in:
ndom91 2021-02-26 23:26:32 +01:00
parent 0da2558b6d
commit f8a078220c
8 changed files with 104 additions and 70 deletions

2
.env.example Normal file
View File

@ -0,0 +1,2 @@
NETBOX_URL=
NETBOX_TOKEN=

4
.gitignore vendored
View File

@ -129,4 +129,6 @@ dmypy.json
.pyre/
# Editor
.vscode
.vscode
repo

21
LICENSE Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2021 Alexander Gittings
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

73
README.md Normal file
View File

@ -0,0 +1,73 @@
# 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).
> Tested working with 2.7.8, 2.8.8, 2.9.4, 2.10.4
## 🪄 Description
These instructions will clone a copy of the `netbox-community/devicetype-library` repository to your machine to allow you to import the device types you would like without copy and pasting them into the NetBox UI.
### 🚀 Getting Started
1. This script is written in Python, so lets setup a virtual environment.
```
git clone https://github.com/minitriga/Netbox-Device-Type-Library-Import
cd Netbox-Device-Type-Library-Import
python3 -m venv venv
source venv/bin/activate
```
2. Now that we have the basics setup, we'll need to install the requirements.
```
pip install -r requirements.txt
```
3. 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.
```
cp .env.example .env
vim .env
```
Finally, we are able to execute the script and import some device templates!
### 🔌 Usage
To use the script, simply execute the script as follows. Make sure you're still in the activated virtual environment we created before.
```
./nb-dt-import.py
```
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 reposity instead.
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.
#### 🧰 Arguments
This script currently accepts a list of vendors as an arugment, so that you can selectively import devices.
To import only device by APC, for example:
```
./nb-dt-import.py --vendors apc
```
`--vendors` can also accept a space separated list of vendors if you want to import multiple.
```
./nb-dt-import.py --vendors apc juniper
```
### 🧑‍💻 Contributing
We're happy about any pull requests!
### 📜 License
MIT

View File

@ -1,3 +1,4 @@
#!/usr/bin/env python3
from git import Repo, exc, RemoteProgress
from collections import Counter
import yaml

View File

@ -1,68 +0,0 @@
# 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).
> Tested working with 2.7.8, 2.8.8, 2.9.4
## Getting Started
These instructions will get you a copy of the project on your machine to allow you to import the device types you would like without copy and pasting them into the NetBox UI.
### Prerequisites
This script is written in python so this must be installed.
```
Python3
Python PIP
```
## Using the Repo
Cloning the repo
```
git clone https://github.com/minitriga/Netbox-Device-Type-Library-Import.git
```
Installing the requirements
```
cd Netbox-Device-Type-Library-Import
pip install -r requirements.txt
```
### Setting your variables
There are a number of variables that are required when using this script to import device types into your netbox environment.
```
export NETBOX_URL=http://netbox.company.com
export NETBOX_TOKEN=0123456789abcdef0123456789abcdef01234567
```
### Using the script
To use the script simply run the following.
```
python nb-dt-import.py
```
This will pull the device-type library from Gitlab and install it into the `.repo` directory. if this directory is already there it will perform a git pull to update the reposity.
#### Arguments
This script currently accepts vendors so that only a few vendors are imported into your NetBox Environment.
This can be done the following.
```
python nb-dt-import.py --vendors apc
```
`--vendors` also accepts a list of vendors so that multiple vendors could be imported.
```
python nb-dt-import.py --vendors apc juniper
```

View File

@ -11,3 +11,4 @@ requests==2.22.0
six==1.14.0
smmap2==2.0.5
urllib3==1.25.8
python-dotenv==0.15.0

View File

@ -1,4 +1,6 @@
import os
from dotenv import load_dotenv
load_dotenv()
NETBOX_URL = str(os.getenv("NETBOX_URL"))
NETBOX_TOKEN = str(os.getenv("NETBOX_TOKEN"))
@ -7,4 +9,4 @@ MANDATORY_ENV_VARS = ["NETBOX_URL", "NETBOX_TOKEN"]
for var in MANDATORY_ENV_VARS:
if var not in os.environ:
raise EnvironmentError("Failed because {} is not set.".format(var))
raise EnvironmentError("Failed because {} is not set.".format(var))