diff --git a/.env.example b/.env.example index 70e1d3e..37a6f36 100644 --- a/.env.example +++ b/.env.example @@ -1,3 +1,4 @@ NETBOX_URL= NETBOX_TOKEN= REPO_URL=https://github.com/netbox-community/devicetype-library.git +IGNORE_SSL_ERRORS=False diff --git a/nb-dt-import.py b/nb-dt-import.py index 9d4a346..f38b9af 100755 --- a/nb-dt-import.py +++ b/nb-dt-import.py @@ -9,20 +9,8 @@ import argparse import os import settings -REPO_URL = settings.REPO_URL -parser = argparse.ArgumentParser(description='Import Netbox Device Types') -parser.add_argument('--vendors', nargs='+', default=settings.VENDORS, - help="List of vendors to import eg. apc cisco") -parser.add_argument('--url', '--git', default=REPO_URL, - help="Git URL with valid Device Type YAML files") -args = parser.parse_args() - -cwd = os.getcwd() counter = Counter(added=0, updated=0, manufacturer=0) -nbUrl = settings.NETBOX_URL -nbToken = settings.NETBOX_TOKEN -startTime = datetime.now() def update_package(path: str): @@ -376,40 +364,65 @@ def createDeviceTypes(deviceTypes, nb): dt.id, nb) -try: - if os.path.isdir('./repo'): - print(f"Package devicetype-library is already installed, " - + f"updating {os.path.join(cwd, 'repo')}") - update_package('./repo') +def main(): + + cwd = os.getcwd() + startTime = datetime.now() + + nbUrl = settings.NETBOX_URL + nbToken = settings.NETBOX_TOKEN + nb = pynetbox.api(nbUrl, token=nbToken) + + if settings.IGNORE_SSL_ERRORS: + import requests + requests.packages.urllib3.disable_warnings() + session = requests.Session() + session.verify = False + nb.http_session = session + + + VENDORS = settings.VENDORS + REPO_URL = settings.REPO_URL + + parser = argparse.ArgumentParser(description='Import Netbox Device Types') + parser.add_argument('--vendors', nargs='+', default=VENDORS, + help="List of vendors to import eg. apc cisco") + parser.add_argument('--url', '--git', default=REPO_URL, + help="Git URL with valid Device Type YAML files") + args = parser.parse_args() + + + try: + if os.path.isdir('./repo'): + print(f"Package devicetype-library is already installed, " + + f"updating {os.path.join(cwd, 'repo')}") + update_package('./repo') + else: + repo = Repo.clone_from(args.url, os.path.join(cwd, 'repo')) + print(f"Package Installed {repo.remotes.origin.url}") + except exc.GitCommandError as error: + print("Couldn't clone {} ({})".format(args.url, error)) + + if not args.vendors: + print("No Vendors Specified, Gathering All Device-Types") + files, vendors = getFiles() else: - repo = Repo.clone_from(args.url, os.path.join(cwd, 'repo')) - print(f"Package Installed {repo.remotes.origin.url}") -except exc.GitCommandError as error: - print("Couldn't clone {} ({})".format(args.url, error)) + print("Vendor Specified, Gathering All Matching Device-Types") + files, vendors = getFiles(args.vendors) -nb = pynetbox.api(nbUrl, token=nbToken) -if args.vendors is None: - print("No Vendors Specified, Gathering All Device-Types") - files, vendors = getFiles() 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.vendors) - print(str(len(vendors)) + " Vendors Found") - print(str(len(files)) + " Device-Types Found") - deviceTypes = readYAMl(files) - createManufacturers(vendors, nb) - createDeviceTypes(deviceTypes, nb) + print('---') + print('Script took {} to run'.format(datetime.now() - startTime)) + print('{} devices created'.format(counter['added'])) + print('{} interfaces/ports updated'.format(counter['updated'])) + print('{} manufacturers created'.format(counter['manufacturer'])) -print('---') -print('Script took {} to run'.format(datetime.now() - startTime)) -print('{} devices created'.format(counter['added'])) -print('{} interfaces/ports updated'.format(counter['updated'])) -print('{} manufacturers created'.format(counter['manufacturer'])) +if __name__ == "__main__": + main() diff --git a/settings.py b/settings.py index 40e42bd..eb9ecb3 100644 --- a/settings.py +++ b/settings.py @@ -2,15 +2,13 @@ import os from dotenv import load_dotenv load_dotenv() -REPO_URL = str(os.getenv("REPO_URL")) -NETBOX_URL = str(os.getenv("NETBOX_URL")) -NETBOX_TOKEN = str(os.getenv("NETBOX_TOKEN")) +REPO_URL = os.getenv("REPO_URL") +NETBOX_URL = os.getenv("NETBOX_URL") +NETBOX_TOKEN = os.getenv("NETBOX_TOKEN") +IGNORE_SSL_ERRORS = (os.getenv("IGNORE_SSL_ERRORS", "False") == "True") # optionnally load vendors through a space separated list as env var -try: - VENDORS = os.getenv("VENDORS").split(" ") -except AttributeError: - VENDORS = None +VENDORS = os.getenv("VENDORS", "").split() MANDATORY_ENV_VARS = ["REPO_URL", "NETBOX_URL", "NETBOX_TOKEN"]