-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #106 from khalford/argparse_user
Created Argparse user method.
- Loading branch information
Showing
11 changed files
with
223 additions
and
75 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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
File renamed without changes.
116 changes: 116 additions & 0 deletions
116
Pynetbox_Data_Uploader/lib/user_methods/csv_to_netbox.py
This file contains 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,116 @@ | ||
from typing import List | ||
import argparse | ||
from lib.netbox_api.netbox_create import NetboxCreate | ||
from lib.netbox_api.netbox_connect import NetboxConnect | ||
from lib.netbox_api.netbox_check import NetboxCheck | ||
from lib.utils.format_dict import FormatDict | ||
|
||
# pylint:disable = broad-exception-raised | ||
# Disabled this pylint warning as the exception doesn't catch an error. | ||
# We want it to stop the program if a device already exists in netbox. | ||
|
||
|
||
class CsvToNetbox: | ||
""" | ||
This class contains organised methods in the 4 step proccess of reading csv's to then uploading to Netbox. | ||
""" | ||
def __init__(self, url: str, token: str): | ||
""" | ||
This initialises the class with the following parameters. | ||
It also allows the rest of the class to access the imported Classes. | ||
:param url: The Netbox url. | ||
:param token: The Netbox auth token. | ||
""" | ||
self.netbox = NetboxConnect(url, token).api_object() | ||
self.format_dict = FormatDict(self.netbox) | ||
self.exist = NetboxCheck(self.netbox) | ||
self.create = NetboxCreate(self.netbox) | ||
|
||
def read_csv(self, file_path) -> List: | ||
""" | ||
This method calls the csv_to_python and seperate_data method. | ||
This will take the csv file and return a list of device dictionaries. | ||
:param file_path: The file path to the csv file to be read. | ||
:return: Returns a list of devices | ||
""" | ||
print("Reading CSV...") | ||
device_data = self.format_dict.csv_to_python(file_path) | ||
device_list = self.format_dict.separate_data(device_data) | ||
print("Read CSV.") | ||
return device_list | ||
|
||
def check_netbox(self, device_list: List) -> bool: | ||
""" | ||
This method calls the check_device_exists and check_device_type_exists method on each device in the list. | ||
:param device_list: A list of devices. | ||
:return: Returns True if the devices don't exist and device types do exist. Raises an Exception otherwise. | ||
""" | ||
print("Checking devices in Netbox...") | ||
for device in device_list: | ||
device_exist = self.exist.check_device_exists(device["name"]) | ||
if device_exist: | ||
raise Exception(f'Device {device["name"]} already exists in Netbox.') | ||
type_exist = self.exist.check_device_type_exists(device["device_type"]) | ||
if not type_exist: | ||
raise Exception(f'Type {device["device_type"]} does not exist.') | ||
print("Checked devices.") | ||
return True | ||
|
||
def convert_data(self, device_list: List) -> List: | ||
""" | ||
This method calls the iterate_dict method. | ||
:param device_list: A list of devices. | ||
:return: Returns the updated list of devices. | ||
""" | ||
print("Formatting data...") | ||
formatted_list = self.format_dict.iterate_dicts(device_list) | ||
print("Formatted data.") | ||
return formatted_list | ||
|
||
def send_data(self, device_list: List) -> bool: | ||
""" | ||
This method calls the device create method to create devices in Netbox. | ||
:param device_list: A list of devices. | ||
:return: Returns bool whether the devices where created. | ||
""" | ||
print("Sending data to Netbox...") | ||
devices = self.create.create_device(device_list) | ||
print("Sent data.") | ||
return bool(devices) | ||
|
||
|
||
def arg_parser(): | ||
""" | ||
This function creates a parser object and adds 3 arguments to it. | ||
This allows users to run the python file with arguments. Like a script. | ||
""" | ||
parser = argparse.ArgumentParser( | ||
description="Create devices in Netbox from CSV files.", | ||
usage="python csv_to_netbox.py url token file_path", | ||
) | ||
parser.add_argument("url", help="The Netbox URL.") | ||
parser.add_argument("token", help="Your Netbox Token.") | ||
parser.add_argument("file_path", help="Your file path to csv files.") | ||
return parser.parse_args() | ||
|
||
|
||
def do_csv_to_netbox(args) -> bool: | ||
""" | ||
This function calls the methods from CsvToNetbox class. | ||
:param args: The arguments from argparse. Supplied when the user runs the file from CLI. | ||
:return: Returns bool if devices where created or not. | ||
""" | ||
class_object = CsvToNetbox(url=args.url, token=args.token) | ||
device_list = class_object.read_csv(args.file_path) | ||
class_object.check_netbox(device_list) | ||
format_list = class_object.convert_data(device_list) | ||
result = class_object.send_data(format_list) | ||
return result | ||
|
||
|
||
if __name__ == "__main__": | ||
arguments = arg_parser() | ||
if do_csv_to_netbox(arguments): | ||
print("Done.") | ||
else: | ||
print("Uh Oh.") |
This file contains 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,66 +1,67 @@ | ||
from typing import Dict, List | ||
from pandas import read_csv | ||
from lib.netbox_api.netbox_data import NetboxGetID | ||
|
||
|
||
class FormatDict: | ||
""" | ||
This class takes dictionaries with string values and changes those to ID values from Netbox. | ||
""" | ||
|
||
def __init__(self, netbox): | ||
""" | ||
This method initialises the class with the following parameters. | ||
:param netbox: The Netbox object to pass into NetboxGetID | ||
""" | ||
self.netbox = netbox | ||
|
||
def iterate_dicts(self, dicts: list) -> List: | ||
""" | ||
This method iterates through each dictionary and calls a format method on each. | ||
:return: Returns the formatted dictionaries. | ||
""" | ||
new_dicts = [] | ||
for dictionary in dicts: | ||
new_dicts.append(self.format_dict(dictionary)) | ||
return new_dicts | ||
|
||
def format_dict(self, dictionary) -> Dict: | ||
""" | ||
This method iterates through each value in the dictionary. | ||
If the value needs to be converted into a Pynetbox ID it calls the .get() method. | ||
:param dictionary: The dictionary to be formatted | ||
:return: Returns the formatted dictionary | ||
""" | ||
for key in dictionary: | ||
netbox_id = NetboxGetID(self.netbox).get_id_from_key(key=key, dictionary=dictionary) | ||
dictionary[key] = netbox_id | ||
return dictionary | ||
|
||
|
||
@staticmethod | ||
def csv_to_python(file_path: str) -> Dict: | ||
""" | ||
This method reads data from csv files and writes them to a dictionary. | ||
:param file_path: The file path of the utils file to be read from. | ||
:return: Returns the data from the csv as a dictionary. | ||
""" | ||
dataframe = read_csv(file_path) | ||
return dataframe.to_dict(orient="list") | ||
|
||
@staticmethod | ||
def separate_data(data: dict) -> List: | ||
""" | ||
This method reduces Pandas utils to Dict conversion to individual dictionaries. | ||
:param data: The data from the utils file. | ||
:return: Returns a list of dictionaries which each represent a row of data from utils. | ||
""" | ||
data_keys = list(data.keys()) | ||
len_rows = len(data[data_keys[0]]) | ||
dicts = [] | ||
for index in range(len_rows): | ||
new_dict = {} | ||
for key in data_keys: | ||
new_dict.update({key: data[key][index]}) | ||
dicts.append(new_dict) | ||
return dicts | ||
from typing import Dict, List | ||
from pandas import read_csv | ||
from lib.netbox_api.netbox_data import NetboxGetID | ||
|
||
|
||
class FormatDict: | ||
""" | ||
This class takes dictionaries with string values and changes those to ID values from Netbox. | ||
""" | ||
|
||
def __init__(self, api): | ||
""" | ||
This method initialises the class with the following parameters. | ||
:param api: The Netbox object to pass into NetboxGetID | ||
""" | ||
self.netbox = api | ||
|
||
def iterate_dicts(self, dicts: list) -> List: | ||
""" | ||
This method iterates through each dictionary and calls a format method on each. | ||
:param dicts: A list of dictionaries to be formatted. | ||
:return: Returns the formatted dictionaries. | ||
""" | ||
new_dicts = [] | ||
for dictionary in dicts: | ||
new_dicts.append(self.format_dict(dictionary)) | ||
return new_dicts | ||
|
||
def format_dict(self, dictionary) -> Dict: | ||
""" | ||
This method iterates through each value in the dictionary. | ||
If the value needs to be converted into a Pynetbox ID it calls the .get() method. | ||
:param dictionary: The dictionary to be formatted | ||
:return: Returns the formatted dictionary | ||
""" | ||
for key in dictionary: | ||
netbox_id = NetboxGetID(self.netbox).get_id_from_key(key=key, dictionary=dictionary) | ||
dictionary[key] = netbox_id | ||
return dictionary | ||
|
||
|
||
@staticmethod | ||
def csv_to_python(file_path: str) -> Dict: | ||
""" | ||
This method reads data from csv files and writes them to a dictionary. | ||
:param file_path: The file path of the utils file to be read from. | ||
:return: Returns the data from the csv as a dictionary. | ||
""" | ||
dataframe = read_csv(file_path) | ||
return dataframe.to_dict(orient="list") | ||
|
||
@staticmethod | ||
def separate_data(data: dict) -> List: | ||
""" | ||
This method reduces Pandas utils to Dict conversion to individual dictionaries. | ||
:param data: The data from the utils file. | ||
:return: Returns a list of dictionaries which each represent a row of data from utils. | ||
""" | ||
data_keys = list(data.keys()) | ||
len_rows = len(data[data_keys[0]]) | ||
dicts = [] | ||
for index in range(len_rows): | ||
new_dict = {} | ||
for key in data_keys: | ||
new_dict.update({key: data[key][index]}) | ||
dicts.append(new_dict) | ||
return dicts |
This file contains 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
Binary file not shown.
This file contains 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,20 @@ | ||
from setuptools import setup, find_packages | ||
|
||
VERSION = "0.1.0" | ||
DESCRIPTION = "python package for PYNETBOX tools" | ||
|
||
LONG_DESCRIPTION = """Python package to interact with Netbox from cli.""" | ||
|
||
setup( | ||
name="Pynetbox_Data_Uploader", | ||
version=VERSION, | ||
author="Kalibh Halford", | ||
author_email="<[email protected]>", | ||
description=DESCRIPTION, | ||
long_description=LONG_DESCRIPTION, | ||
packages=find_packages(), | ||
package_dir={"Pynetbox_Data_Uploader": "lib"}, | ||
python_requires=">=3.9", | ||
install_requires=[], | ||
keywords=["python"], | ||
) |