Skip to content

Add Rack-Types import functionality #170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -131,5 +131,6 @@ dmypy.json

# Editor
.vscode
.idea

repo
15 changes: 14 additions & 1 deletion nb-dt-import.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ def main():
netbox.create_manufacturers(vendors)
netbox.create_module_types(module_types)

if netbox.rack_types:
settings.handle.log("Rack-Types Enabled. Creating Racks...")
files, vendors = settings.dtl_repo.get_devices(
f'{settings.dtl_repo.repo_path}/rack-types/', args.vendors)
settings.handle.log(f'{len(vendors)} Rack Vendors Found')
rack_types = settings.dtl_repo.parse_files(files, slugs=args.slugs)
settings.handle.log(f'{len(rack_types)} Rack-Types Found')
netbox.create_manufacturers(vendors)
netbox.create_rack_types(rack_types)

settings.handle.log('---')
settings.handle.verbose_log(
f'Script took {(datetime.now() - startTime)} to run')
Expand All @@ -43,11 +53,14 @@ def main():
f'{netbox.counter["updated"]} interfaces/ports updated')
settings.handle.log(
f'{netbox.counter["manufacturer"]} manufacturers created')
if settings.NETBOX_FEATURES['modules']:
if netbox.modules:
settings.handle.log(
f'{netbox.counter["module_added"]} modules created')
settings.handle.log(
f'{netbox.counter["module_port_added"]} module interface / ports created')
if netbox.rack_types:
settings.handle.log(
f'{netbox.counter["rack_types_added"]} rack-types created')


if __name__ == "__main__":
Expand Down
28 changes: 28 additions & 0 deletions netbox_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def __init__(self, settings):
manufacturer=0,
module_added=0,
module_port_added=0,
rack_types_added=0,
images=0,
)
self.url = settings.NETBOX_URL
Expand All @@ -24,6 +25,7 @@ def __init__(self, settings):
self.netbox = None
self.ignore_ssl = settings.IGNORE_SSL_ERRORS
self.modules = False
self.rack_types = False
self.new_filters = False
self.connect_api()
self.verify_compatibility()
Expand Down Expand Up @@ -54,6 +56,7 @@ def verify_compatibility(self):
# Might want to check for the module-types entry as well?
if version_split[0] > 3 or (version_split[0] == 3 and version_split[1] >= 2):
self.modules = True
self.rack_types = True

# check if version >= 4.1 in order to use new filter names (https://github.com/netbox-community/netbox/issues/15410)
if version_split[0] >= 4 and version_split[1] >= 1:
Expand Down Expand Up @@ -146,6 +149,31 @@ def create_device_types(self, device_types_to_add):
if saved_images:
self.device_types.upload_images(self.url, self.token, saved_images, dt.id)

def create_rack_types(self, rack_types):
all_rack_types = {}
for curr_nb_mt in self.netbox.dcim.rack_types.all():
if curr_nb_mt.manufacturer.slug not in all_rack_types:
all_rack_types[curr_nb_mt.manufacturer.slug] = {}

all_rack_types[curr_nb_mt.manufacturer.slug][curr_nb_mt.model] = curr_nb_mt


for curr_mt in rack_types:
try:
rack_type_res = all_rack_types[curr_mt['manufacturer']['slug']][curr_mt["model"]]
self.handle.verbose_log(f'Rack Type Exists: {rack_type_res.manufacturer.name} - '
+ f'{rack_type_res.model} - {rack_type_res.id}')
except KeyError:
try:
rack_type_res = self.netbox.dcim.rack_types.create(curr_mt)
self.counter.update({'rack_types_added': 1})
self.handle.verbose_log(f'Rack Type Created: {rack_type_res.manufacturer.name} - '
+ f'{rack_type_res.model} - {rack_type_res.id}')
except pynetbox.RequestError as exce:
self.handle.log(f"Error '{exce.error}' creating rack type: " +
f"{curr_mt}")


def create_module_types(self, module_types):
all_module_types = {}
for curr_nb_mt in self.netbox.dcim.module_types.all():
Expand Down
4 changes: 0 additions & 4 deletions settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@
# optionally load device types through a space separated list as env var
SLUGS = os.getenv("SLUGS", "").split()

NETBOX_FEATURES = {
'modules': False,
}

parser = ArgumentParser(description='Import Netbox Device Types')
parser.add_argument('--vendors', nargs='+', default=VENDORS,
help="List of vendors to import eg. apc cisco")
Expand Down