From 7b81c3b0cda9f12f36147e515b2b044c11abe15c Mon Sep 17 00:00:00 2001 From: Alexander GITTINGS Date: Wed, 26 Feb 2020 14:49:12 +0000 Subject: [PATCH] add readme and update some bits --- nb-dt-import.py | 19 ++++++-------- readme.md | 65 ++++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 13 ++++++++++ settings.py | 10 ++++++++ 4 files changed, 95 insertions(+), 12 deletions(-) create mode 100644 readme.md create mode 100644 requirements.txt create mode 100644 settings.py diff --git a/nb-dt-import.py b/nb-dt-import.py index e600dde..a03b313 100644 --- a/nb-dt-import.py +++ b/nb-dt-import.py @@ -1,9 +1,5 @@ from git import Repo, exc, RemoteProgress -import os -import argparse -import glob -import yaml -import pynetbox +import yaml, pynetbox, glob, argparse, os, settings parser = argparse.ArgumentParser(description='Import Netbox Device Types') parser.add_argument('--vendor', nargs='+') @@ -12,8 +8,8 @@ args = parser.parse_args() cwd = os.getcwd() url = 'https://github.com/netbox-community/devicetype-library.git' -nbUrl = 'http://192.168.3.119' -nbToken = '0123456789abcdef0123456789abcdef01234567' +nbUrl = settings.NETBOX_URL +nbToken = settings.NETBOX_TOKEN def update_package(path: str): try: @@ -243,13 +239,12 @@ nb = pynetbox.api(nbUrl, token=nbToken) if args.vendor is None: print("No Vendor Specified, Gathering All Device-Types") files, vendors = getFiles() - deviceTypes = readYAMl(files) - print(vendors) - createManufacturers(vendors, nb) - createDeviceTypes(deviceTypes, nb) - # print(json) print(str(len(vendors)) + " Vendors Found") print(str(len(files)) + " Device-Types Found") + deviceTypes = readYAMl(files) + createManufacturers(vendors, nb) + createDeviceTypes(deviceTypes, nb) + else: print("Vendor Specified, Gathering All Matching Device-Types") files, vendors = getFiles(args.vendor) diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..30f317b --- /dev/null +++ b/readme.md @@ -0,0 +1,65 @@ +# 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). + +## 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 + +``` +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 +``` diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..ec39f10 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,13 @@ +certifi==2019.11.28 +chardet==3.0.4 +gitdb2==3.0.2 +GitPython==3.0.8 +idna==2.8 +progressbar2==3.47.0 +pynetbox==4.2.5 +python-utils==2.3.0 +PyYAML==5.3 +requests==2.22.0 +six==1.14.0 +smmap2==2.0.5 +urllib3==1.25.8 diff --git a/settings.py b/settings.py new file mode 100644 index 0000000..2713c09 --- /dev/null +++ b/settings.py @@ -0,0 +1,10 @@ +import os + +NETBOX_URL = str(os.getenv("NETBOX_URL")) +NETBOX_TOKEN = str(os.getenv("NETBOX_TOKEN")) + +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)) \ No newline at end of file