From 54907aaffbc53105bf6dd10fed786ab666a8ff8f Mon Sep 17 00:00:00 2001 From: Mark Tinberg Date: Thu, 12 Nov 2020 13:20:21 -0600 Subject: [PATCH 1/6] Add CLI options for git URL help text to CLI options --- nb-dt-import.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/nb-dt-import.py b/nb-dt-import.py index 3cf5219..7d3d722 100644 --- a/nb-dt-import.py +++ b/nb-dt-import.py @@ -3,13 +3,12 @@ from collections import Counter import yaml, pynetbox, glob, argparse, os, settings parser = argparse.ArgumentParser(description='Import Netbox Device Types') -parser.add_argument('--vendor', nargs='+') - +parser.add_argument('--vendor', nargs='+', help="List of vendors to import eg. apc cisco") +parser.add_argument('--url','--git', default='https://github.com/netbox-community/devicetype-library.git', help="Git URL with valid Device Type YAML files") args = parser.parse_args() cwd = os.getcwd() counter = Counter(added=0,updated=0,manufacturer=0) -url = 'https://github.com/netbox-community/devicetype-library.git' nbUrl = settings.NETBOX_URL nbToken = settings.NETBOX_TOKEN @@ -249,10 +248,10 @@ try: update_package('./repo') print(msg) else: - repo = Repo.clone_from(url, os.path.join(cwd, 'repo')) + repo = Repo.clone_from(args.url, os.path.join(cwd, 'repo')) print("Package Installed") except exc.GitCommandError as error: - print("Couldn't clone {} ({})".format(url, error)) + print("Couldn't clone {} ({})".format(args.url, error)) nb = pynetbox.api(nbUrl, token=nbToken) From 2188ac7a62098876106d29c8515187419a123535 Mon Sep 17 00:00:00 2001 From: Mark Tinberg Date: Thu, 12 Nov 2020 13:22:35 -0600 Subject: [PATCH 2/6] Return empty deviceTypes in readYAML on error instead of continuing with uninitilized data --- nb-dt-import.py | 1 + 1 file changed, 1 insertion(+) diff --git a/nb-dt-import.py b/nb-dt-import.py index 7d3d722..2405069 100644 --- a/nb-dt-import.py +++ b/nb-dt-import.py @@ -48,6 +48,7 @@ def readYAMl(files): data = yaml.safe_load(stream) except yaml.YAMLError as exc: print(exc) + return deviceTypes manufacturer = data['manufacturer'] data['manufacturer'] = {} data['manufacturer']['name'] = manufacturer From 0d9773c0e00db294e33cc076a267b5ec3c700b3f Mon Sep 17 00:00:00 2001 From: Mark Tinberg Date: Thu, 12 Nov 2020 13:22:46 -0600 Subject: [PATCH 3/6] Add VSCode tmp files to gitignore --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index b6e4761..3481434 100644 --- a/.gitignore +++ b/.gitignore @@ -127,3 +127,6 @@ dmypy.json # Pyre type checker .pyre/ + +# Editor +.vscode \ No newline at end of file From 00e9b3d73f984afe441137f9b4441fd9dd233fa0 Mon Sep 17 00:00:00 2001 From: Mark Tinberg Date: Thu, 12 Nov 2020 13:38:48 -0600 Subject: [PATCH 4/6] Update output messages with git remote and local path so it's clear what data is being used if a non-default git url is specified --- nb-dt-import.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/nb-dt-import.py b/nb-dt-import.py index 2405069..4120009 100644 --- a/nb-dt-import.py +++ b/nb-dt-import.py @@ -17,7 +17,7 @@ def update_package(path: str): repo = Repo(path) if repo.remotes.origin.url.endswith('.git'): repo.remotes.origin.pull() - print("Pulled Repo") + print(f"Pulled Repo {repo.remotes.origin.url}") except exc.InvalidGitRepositoryError: pass @@ -245,12 +245,11 @@ def createDeviceTypes(deviceTypes, nb): try: if os.path.isdir('./repo'): - msg = 'Package devicetype-library is already installed, updating' + print(f"Package devicetype-library is already installed, updating {os.path.join(cwd, 'repo')}") update_package('./repo') - print(msg) else: repo = Repo.clone_from(args.url, os.path.join(cwd, 'repo')) - print("Package Installed") + print(f"Package Installed {repo.remotes.origin.url}") except exc.GitCommandError as error: print("Couldn't clone {} ({})".format(args.url, error)) From a78df750744408e656e29d45e97f99997de344d4 Mon Sep 17 00:00:00 2001 From: Mark Tinberg Date: Thu, 12 Nov 2020 13:44:11 -0600 Subject: [PATCH 5/6] Switch early return so readYAML can continue with other files --- nb-dt-import.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nb-dt-import.py b/nb-dt-import.py index 4120009..78a5981 100644 --- a/nb-dt-import.py +++ b/nb-dt-import.py @@ -48,7 +48,7 @@ def readYAMl(files): data = yaml.safe_load(stream) except yaml.YAMLError as exc: print(exc) - return deviceTypes + continue manufacturer = data['manufacturer'] data['manufacturer'] = {} data['manufacturer']['name'] = manufacturer From d57b93faaebb6515ad773cd56359627e78100dcc Mon Sep 17 00:00:00 2001 From: Mark Tinberg Date: Fri, 13 Nov 2020 09:36:53 -0600 Subject: [PATCH 6/6] Strip invalid spaces from Manufacturer slug generation --- nb-dt-import.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nb-dt-import.py b/nb-dt-import.py index 78a5981..222b506 100644 --- a/nb-dt-import.py +++ b/nb-dt-import.py @@ -52,7 +52,7 @@ def readYAMl(files): manufacturer = data['manufacturer'] data['manufacturer'] = {} data['manufacturer']['name'] = manufacturer - data['manufacturer']['slug'] = manufacturer.lower() + data['manufacturer']['slug'] = manufacturer.lower().replace(" ", "") deviceTypes.append(data) manufacturers.append(manufacturer) return deviceTypes