mirror of
https://github.com/netbox-community/Device-Type-Library-Import.git
synced 2025-01-22 13:28:34 +01:00
Update netbox_api.py
This commit is contained in:
parent
593e0abda8
commit
3ea22ac097
@ -68,7 +68,7 @@ class NetBox:
|
||||
self.existing_manufacturers = self.get_manufacturers()
|
||||
for vendor in vendors:
|
||||
try:
|
||||
manGet = self.existing_manufacturers[vendor["name"]]
|
||||
manGet = self.existing_manufacturers[vendor['name']]
|
||||
self.handle.verbose_log(f'Manufacturer Exists: {manGet.name} - {manGet.id}')
|
||||
except KeyError:
|
||||
to_create.append(vendor)
|
||||
@ -99,18 +99,18 @@ class NetBox:
|
||||
|
||||
try:
|
||||
if retries == 0:
|
||||
self.handle.verbose_log(f'Processing Source File: {device_type["src"]}')
|
||||
self.handle.verbose_log(f'Processing Source File: {device_type['src']}')
|
||||
else:
|
||||
self.handle.verbose_log(f'(Retry {retries}/{retry_amount}) Processing Source File: {device_type["src"]}')
|
||||
self.handle.verbose_log(f'(Retry {retries}/{retry_amount}) Processing Source File: {device_type['src']}')
|
||||
|
||||
# Remove file base path
|
||||
src_file = device_type["src"]
|
||||
del device_type["src"]
|
||||
src_file = device_type['src']
|
||||
del device_type['src']
|
||||
|
||||
# Pre-process front/rear_image flag, remove it if present
|
||||
saved_images = {}
|
||||
image_base = os.path.dirname(src_file).replace("device-types","elevation-images")
|
||||
for i in ["front_image","rear_image"]:
|
||||
for i in ['front_image','rear_image']:
|
||||
if i in device_type:
|
||||
if device_type[i]:
|
||||
image_glob = f"{image_base}/{device_type['slug']}.{i.split('_')[0]}.*"
|
||||
@ -122,7 +122,7 @@ class NetBox:
|
||||
del device_type[i]
|
||||
|
||||
try:
|
||||
dt = self.device_types.existing_device_types[device_type["model"]]
|
||||
dt = self.device_types.existing_device_types[device_type['model']]
|
||||
self.handle.verbose_log(f'Device Type Exists: {dt.manufacturer.name} - {dt.model} - {dt.id}')
|
||||
except KeyError:
|
||||
try:
|
||||
@ -130,28 +130,28 @@ class NetBox:
|
||||
self.counter.update({'added': 1})
|
||||
self.handle.verbose_log(f'Device Type Created: {dt.manufacturer.name} - {dt.model} - {dt.id}')
|
||||
except pynetbox.RequestError as e:
|
||||
self.handle.log(f'Error {e.error} creating device type: {device_type["manufacturer"]["name"]} {device_type["model"]}')
|
||||
self.handle.log(f'Error {e.error} creating device type: {device_type['manufacturer']['name']} {device_type['model']}')
|
||||
retries += 1
|
||||
continue
|
||||
|
||||
if "interfaces" in device_type:
|
||||
self.device_types.create_interfaces(device_type["interfaces"], dt.id)
|
||||
self.device_types.create_interfaces(device_type['interfaces'], dt.id)
|
||||
if "power-ports" in device_type:
|
||||
self.device_types.create_power_ports(device_type["power-ports"], dt.id)
|
||||
self.device_types.create_power_ports(device_type['power-ports'], dt.id)
|
||||
if "power-port" in device_type:
|
||||
self.device_types.create_power_ports(device_type["power-port"], dt.id)
|
||||
self.device_types.create_power_ports(device_type['power-port'], dt.id)
|
||||
if "console-ports" in device_type:
|
||||
self.device_types.create_console_ports(device_type["console-ports"], dt.id)
|
||||
self.device_types.create_console_ports(device_type['console-ports'], dt.id)
|
||||
if "power-outlets" in device_type:
|
||||
self.device_types.create_power_outlets(device_type["power-outlets"], dt.id)
|
||||
self.device_types.create_power_outlets(device_type['power-outlets'], dt.id)
|
||||
if "console-server-ports" in device_type:
|
||||
self.device_types.create_console_server_ports(device_type["console-server-ports"], dt.id)
|
||||
self.device_types.create_console_server_ports(device_type['console-server-ports'], dt.id)
|
||||
if "rear-ports" in device_type:
|
||||
self.device_types.create_rear_ports(device_type["rear-ports"], dt.id)
|
||||
self.device_types.create_rear_ports(device_type['rear-ports'], dt.id)
|
||||
if "front-ports" in device_type:
|
||||
self.device_types.create_front_ports(device_type["front-ports"], dt.id)
|
||||
self.device_types.create_front_ports(device_type['front-ports'], dt.id)
|
||||
if "device-bays" in device_type:
|
||||
self.device_types.create_device_bays(device_type["device-bays"], dt.id)
|
||||
self.device_types.create_device_bays(device_type['device-bays'], dt.id)
|
||||
if self.modules and 'module-bays' in device_type:
|
||||
self.device_types.create_module_bays(device_type['module-bays'], dt.id)
|
||||
|
||||
@ -164,7 +164,7 @@ class NetBox:
|
||||
except (http.client.RemoteDisconnected, requests.exceptions.ConnectionError) as e:
|
||||
retries += 1
|
||||
self.counter.update({'connection_errors': 1})
|
||||
self.handle.log(f'A connection error occurred (Count: {self.counter["connection_errors"]})! Waiting {self.retry_delay} seconds then retrying... Exception: {e}')
|
||||
self.handle.log(f'A connection error occurred (Count: {self.counter['connection_errors']})! Waiting {self.retry_delay} seconds then retrying... Exception: {e}')
|
||||
|
||||
# As a connection error has just occurred, we should give the remote end a moment then reconnect.
|
||||
time.sleep(self.retry_delay)
|
||||
@ -192,12 +192,12 @@ class NetBox:
|
||||
|
||||
try:
|
||||
if retries == 0:
|
||||
self.handle.verbose_log(f'Processing Source File: {curr_mt["src"]}')
|
||||
self.handle.verbose_log(f'Processing Source File: {curr_mt['src']}')
|
||||
else:
|
||||
self.handle.verbose_log(f'(Retry {retries}/{retry_amount}) Processing Source File: {curr_mt["src"]}')
|
||||
self.handle.verbose_log(f'(Retry {retries}/{retry_amount}) Processing Source File: {curr_mt['src']}')
|
||||
|
||||
try:
|
||||
module_type_res = all_module_types[curr_mt['manufacturer']['slug']][curr_mt["model"]]
|
||||
module_type_res = all_module_types[curr_mt['manufacturer']['slug']][curr_mt['model']]
|
||||
self.handle.verbose_log(f'Module Type Exists: {module_type_res.manufacturer.name} - {module_type_res.model} - {module_type_res.id}')
|
||||
except KeyError:
|
||||
try:
|
||||
@ -205,24 +205,24 @@ class NetBox:
|
||||
self.counter.update({'module_added': 1})
|
||||
self.handle.verbose_log(f'Module Type Created: {module_type_res.manufacturer.name} - {module_type_res.model} - {module_type_res.id}')
|
||||
except pynetbox.RequestError as exce:
|
||||
self.handle.log(f"Error '{exce.error}' creating module type: {curr_mt["manufacturer"]} {curr_mt["model"]} {curr_mt["part_number"]}")
|
||||
self.handle.log(f"Error '{exce.error}' creating module type: {curr_mt['manufacturer']} {curr_mt['model']} {curr_mt['part_number']}")
|
||||
retries += 1
|
||||
continue
|
||||
|
||||
if "interfaces" in curr_mt:
|
||||
self.device_types.create_module_interfaces(curr_mt["interfaces"], module_type_res.id)
|
||||
self.device_types.create_module_interfaces(curr_mt['interfaces'], module_type_res.id)
|
||||
if "power-ports" in curr_mt:
|
||||
self.device_types.create_module_power_ports(curr_mt["power-ports"], module_type_res.id)
|
||||
self.device_types.create_module_power_ports(curr_mt['power-ports'], module_type_res.id)
|
||||
if "console-ports" in curr_mt:
|
||||
self.device_types.create_module_console_ports(curr_mt["console-ports"], module_type_res.id)
|
||||
self.device_types.create_module_console_ports(curr_mt['console-ports'], module_type_res.id)
|
||||
if "power-outlets" in curr_mt:
|
||||
self.device_types.create_module_power_outlets(curr_mt["power-outlets"], module_type_res.id)
|
||||
self.device_types.create_module_power_outlets(curr_mt['power-outlets'], module_type_res.id)
|
||||
if "console-server-ports" in curr_mt:
|
||||
self.device_types.create_module_console_server_ports(curr_mt["console-server-ports"], module_type_res.id)
|
||||
self.device_types.create_module_console_server_ports(curr_mt['console-server-ports'], module_type_res.id)
|
||||
if "rear-ports" in curr_mt:
|
||||
self.device_types.create_module_rear_ports(curr_mt["rear-ports"], module_type_res.id)
|
||||
self.device_types.create_module_rear_ports(curr_mt['rear-ports'], module_type_res.id)
|
||||
if "front-ports" in curr_mt:
|
||||
self.device_types.create_module_front_ports(curr_mt["front-ports"], module_type_res.id)
|
||||
self.device_types.create_module_front_ports(curr_mt['front-ports'], module_type_res.id)
|
||||
|
||||
# We successfully processed the device. Don't retry it.
|
||||
retries = retry_amount
|
||||
@ -230,7 +230,7 @@ class NetBox:
|
||||
except (http.client.RemoteDisconnected, requests.exceptions.ConnectionError) as e:
|
||||
retries += 1
|
||||
self.counter.update({'connection_errors': 1})
|
||||
self.handle.log(f'A connection error occurred (Count: {self.counter["connection_errors"]})! Waiting {self.retry_delay} seconds then retrying... Exception: {e}')
|
||||
self.handle.log(f'A connection error occurred (Count: {self.counter['connection_errors']})! Waiting {self.retry_delay} seconds then retrying... Exception: {e}')
|
||||
|
||||
# As a connection error has just occurred, we should give the remote end a moment then reconnect.
|
||||
time.sleep(self.retry_delay)
|
||||
@ -326,7 +326,7 @@ class DeviceTypes:
|
||||
existing_power_ports = self.get_power_ports(device_type)
|
||||
for outlet in to_create:
|
||||
try:
|
||||
power_port = existing_power_ports[outlet["power_port"]]
|
||||
power_port = existing_power_ports[outlet['power_port']]
|
||||
outlet['power_port'] = power_port.id
|
||||
except KeyError:
|
||||
pass
|
||||
@ -373,11 +373,11 @@ class DeviceTypes:
|
||||
all_rearports = self.get_rear_ports(device_type)
|
||||
for port in to_create:
|
||||
try:
|
||||
rear_port = all_rearports[port["rear_port"]]
|
||||
rear_port = all_rearports[port['rear_port']]
|
||||
port['rear_port'] = rear_port.id
|
||||
except KeyError:
|
||||
self.handle.log(f'Could not find Rear Port for Front Port: {port["name"]} - '
|
||||
+ f'{port["type"]} - {device_type}')
|
||||
self.handle.log(f'Could not find Rear Port for Front Port: {port['name']} - '
|
||||
+ f'{port['type']} - {device_type}')
|
||||
|
||||
try:
|
||||
self.counter.update({'updated':
|
||||
@ -460,7 +460,7 @@ class DeviceTypes:
|
||||
existing_power_ports = self.get_module_power_ports(module_type)
|
||||
for outlet in to_create:
|
||||
try:
|
||||
power_port = existing_power_ports[outlet["power_port"]]
|
||||
power_port = existing_power_ports[outlet['power_port']]
|
||||
outlet['power_port'] = power_port.id
|
||||
except KeyError:
|
||||
pass
|
||||
@ -507,11 +507,11 @@ class DeviceTypes:
|
||||
existing_rear_ports = self.get_module_rear_ports(module_type)
|
||||
for port in to_create:
|
||||
try:
|
||||
rear_port = existing_rear_ports[port["rear_port"]]
|
||||
rear_port = existing_rear_ports[port['rear_port']]
|
||||
port['rear_port'] = rear_port.id
|
||||
except KeyError:
|
||||
self.handle.log(f'Could not find Rear Port for Front Port: {port["name"]} - '
|
||||
+ f'{port["type"]} - {module_type}')
|
||||
self.handle.log(f'Could not find Rear Port for Front Port: {port['name']} - '
|
||||
+ f'{port['type']} - {module_type}')
|
||||
|
||||
try:
|
||||
self.counter.update({'updated':
|
||||
@ -544,4 +544,4 @@ class DeviceTypes:
|
||||
else:
|
||||
self.handle.log( f'Images {images} updated at {url}: {response} (Code {response.status_code})' )
|
||||
|
||||
self.counter["images"] += len(images)
|
||||
self.counter['images'] += len(images)
|
||||
|
Loading…
Reference in New Issue
Block a user