Skip to content
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

import device-roles if exists #116

Open
wants to merge 1 commit 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
7 changes: 7 additions & 0 deletions nb-dt-import.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ def main():
settings.handle.log(f'{len(vendors)} Vendors Found')
device_types = settings.dtl_repo.parse_files(files, slugs=args.slugs)
settings.handle.log(f'{len(device_types)} Device-Types Found')
files = settings.dtl_repo.get_device_roles(
f'{settings.dtl_repo.repo_path}/device-roles/')
device_roles = settings.dtl_repo.parse_device_roles_files(files)
settings.handle.log(f'{len(device_roles)} Device Roles Found')
netbox.create_manufacturers(vendors)
netbox.create_device_roles(device_roles)
netbox.create_device_types(device_types)

if netbox.modules:
Expand All @@ -43,6 +48,8 @@ def main():
f'{netbox.counter["updated"]} interfaces/ports updated')
settings.handle.log(
f'{netbox.counter["manufacturer"]} manufacturers created')
settings.handle.log(
f'{netbox.counter["device_role"]} device roles created')
if settings.NETBOX_FEATURES['modules']:
settings.handle.log(
f'{netbox.counter["module_added"]} modules created')
Expand Down
26 changes: 26 additions & 0 deletions netbox_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def __init__(self, settings):
added=0,
updated=0,
manufacturer=0,
device_role=0,
module_added=0,
module_port_added=0,
images=0,
Expand Down Expand Up @@ -79,6 +80,31 @@ def create_manufacturers(self, vendors):
self.handle.log("Error creating manufacturers")
self.handle.verbose_log(f"Error during manufacturer creation. - {request_error.error}")

def get_device_roles(self):
return {str(item): item for item in self.netbox.dcim.device_roles.all()}

def create_device_roles(self, roles):
to_create = []
self.existing_device_roles = self.get_device_roles()
for role in roles:
try:
rolGet = self.existing_device_roles[role["name"]]
self.handle.verbose_log(f'Device Roles Exists: {rolGet.name} - {rolGet.id}')
except KeyError:
to_create.append(role)
self.handle.verbose_log(f"Device Role queued for addition: {role['name']}")

if to_create:
try:
created_device_roles = self.netbox.dcim.device_roles.create(to_create)
for role in created_device_roles:
self.handle.verbose_log(f'Device Role Created: {role.name} - '
+ f'{role.id}')
self.counter.update({'device_role': 1})
except pynetbox.RequestError as request_error:
self.handle.log("Error creating device role")
self.handle.verbose_log(f"Error during device role creation. - {request_error.error}")

def create_device_types(self, device_types_to_add):
for device_type in device_types_to_add:

Expand Down
18 changes: 18 additions & 0 deletions repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,24 @@ def get_devices(self, base_path, vendors: list = None):
files.extend(glob(base_path + folder + f'/*.{extension}'))
return files, discovered_vendors

def get_device_roles(self, base_path):
files = []
for extension in self.yaml_extensions:
files.extend(glob(base_path + f'/*.{extension}'))
return files

def parse_device_roles_files(self, files):
deviceRoles = []
for file in files:
with open(file, 'r') as stream:
try:
data = yaml.safe_load(stream)
except yaml.YAMLError as excep:
self.handle.verbose_log(excep)
continue
deviceRoles += data
return deviceRoles

def parse_files(self, files: list, slugs: list = None):
deviceTypes = []
for file in files:
Expand Down