diff --git a/nb-dt-import.py b/nb-dt-import.py index 82555283..508ca951 100755 --- a/nb-dt-import.py +++ b/nb-dt-import.py @@ -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: @@ -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') diff --git a/netbox_api.py b/netbox_api.py index f848d492..c628bcdf 100644 --- a/netbox_api.py +++ b/netbox_api.py @@ -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, @@ -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: diff --git a/repo.py b/repo.py index f52e0b00..3f3be1e8 100644 --- a/repo.py +++ b/repo.py @@ -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: