Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
scetron committed Nov 1, 2024
1 parent 1a78307 commit a84e851
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 54 deletions.
4 changes: 4 additions & 0 deletions changes/266.housekeeping
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fixed typos in function names, comments, and errors.
Updated documentation to include an example CSV for bulk onboarding.
Updated documentation to make yaml override placement and git repository more clear.
Added a homepage to the app config.
1 change: 1 addition & 0 deletions nautobot_device_onboarding/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class NautobotDeviceOnboardingConfig(NautobotAppConfig):
}
caching_config = {}
docs_view_name = "plugins:nautobot_device_onboarding:docs"
home_view_name = "extras:job_list" # Jobs only for now. May change in the future.


config = NautobotDeviceOnboardingConfig # pylint:disable=invalid-name
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,15 @@ def get_from_orm_cache(self, parameters: Dict, model_class: Type[Model]):
return cached_object
# As we are using `get` here, this will error if there is not exactly one object that corresponds to the
# parameter set. We intentionally pass these errors through.
self._cache[model_cache_key][parameter_set] = model_class.objects.get(
**dict(parameter_set)
)
self._cache[model_cache_key][parameter_set] = model_class.objects.get(**dict(parameter_set))
return self._cache[model_cache_key][parameter_set]

def load_manufacturers(self):
"""Load manufacturer data from Nautobot."""
for manufacturer in Manufacturer.objects.all():
if self.job.debug:
self.job.logger.debug("Loading Manufacturer data from Nautobot...")
onboarding_manufacturer = self.manufacturer(
adapter=self, pk=manufacturer.pk, name=manufacturer.name
)
onboarding_manufacturer = self.manufacturer(adapter=self, pk=manufacturer.pk, name=manufacturer.name)
self.add(onboarding_manufacturer)
if self.job.debug:
self.job.logger.debug(f"Manufacturer: {manufacturer.name} loaded.")
Expand All @@ -82,12 +78,8 @@ def load_platforms(self):
adapter=self,
pk=platform.pk,
name=platform.name,
network_driver=(
platform.network_driver if platform.network_driver else ""
),
manufacturer__name=(
platform.manufacturer.name if platform.manufacturer else None
),
network_driver=(platform.network_driver if platform.network_driver else ""),
manufacturer__name=(platform.manufacturer.name if platform.manufacturer else None),
)
self.add(onboarding_platform)
if self.job.debug:
Expand All @@ -114,9 +106,7 @@ def load_devices(self):
if self.job.debug:
self.job.logger.debug("Loading Device data from Nautobot...")

for device in Device.objects.filter(
primary_ip4__host__in=self.job.ip_addresses
):
for device in Device.objects.filter(primary_ip4__host__in=self.job.ip_addresses):
interface_list = []
# Only interfaces with the device's primary ip should be considered for diff calculations
# Ultimately, only the first matching interface is used but this list could support multiple
Expand All @@ -137,18 +127,12 @@ def load_devices(self):
name=device.name,
platform__name=device.platform.name if device.platform else "",
primary_ip4__host=device.primary_ip4.host if device.primary_ip4 else "",
primary_ip4__status__name=(
device.primary_ip4.status.name if device.primary_ip4 else ""
),
primary_ip4__status__name=(device.primary_ip4.status.name if device.primary_ip4 else ""),
role__name=device.role.name,
status__name=device.status.name,
secrets_group__name=(
device.secrets_group.name if device.secrets_group else ""
),
secrets_group__name=(device.secrets_group.name if device.secrets_group else ""),
interfaces=interfaces,
mask_length=(
device.primary_ip4.mask_length if device.primary_ip4 else None
),
mask_length=(device.primary_ip4.mask_length if device.primary_ip4 else None),
serial=device.serial,
)
self.add(onboarding_device)
Expand Down Expand Up @@ -206,9 +190,7 @@ def _handle_failed_devices(self, device_data):
self.failed_ip_addresses = []
for ip_address in device_data:
if not device_data[ip_address]:
self.job.logger.error(
f"{ip_address}: Connection or data error, this device will not be synced."
)
self.job.logger.error(f"{ip_address}: Connection or data error, this device will not be synced.")
self.failed_ip_addresses.append(ip_address)
for ip_address in self.failed_ip_addresses:
del device_data[ip_address]
Expand All @@ -223,9 +205,7 @@ def execute_command_getter(self):
f"The selected platform, {self.job.platform} "
"does not have a network driver, please update the Platform."
)
raise Exception(
"Platform.network_driver missing"
) # pylint: disable=broad-exception-raised
raise Exception("Platform.network_driver missing") # pylint: disable=broad-exception-raised

result = sync_devices_command_getter(
self.job.job_result,
Expand All @@ -236,9 +216,7 @@ def execute_command_getter(self):
self.job.logger.debug(f"Command Getter Result: {result}")
data_type_check = diffsync_utils.check_data_type(result)
if self.job.debug:
self.job.logger.debug(
f"CommandGetter data type check result: {data_type_check}"
)
self.job.logger.debug(f"CommandGetter data type check result: {data_type_check}")
if data_type_check:
self._handle_failed_devices(device_data=result)
else:
Expand Down Expand Up @@ -330,9 +308,7 @@ def _fields_missing_data(self, device_data, ip_address, platform):
"mask_length",
"serial",
]
if (
platform
): # platform is only returned with device data if not provided on the job form/csv
if platform: # platform is only returned with device data if not provided on the job form/csv
required_fields_from_device.append("platform")
for field in required_fields_from_device:
data = device_data[ip_address]
Expand All @@ -345,9 +321,7 @@ def load_devices(self):
for ip_address in self.device_data:
if self.job.debug:
self.job.logger.debug(f"loading device data for {ip_address}")
platform = (
None # If an exception is caught below, the platform must still be set.
)
platform = None # If an exception is caught below, the platform must still be set.
onboarding_device = None
try:
location = diffsync_utils.retrieve_submitted_value(
Expand Down Expand Up @@ -376,11 +350,7 @@ def load_devices(self):
device_type__model=self.device_data[ip_address]["device_type"],
location__name=location.name,
name=self.device_data[ip_address]["hostname"],
platform__name=(
platform.name
if platform
else self.device_data[ip_address]["platform"]
),
platform__name=(platform.name if platform else self.device_data[ip_address]["platform"]),
primary_ip4__host=ip_address,
primary_ip4__status__name=primary_ip4__status.name,
role__name=device_role.name,
Expand Down Expand Up @@ -414,9 +384,7 @@ def load_devices(self):
try:
self.add(onboarding_device)
if self.job.debug:
self.job.logger.debug(
f"Device: {self.device_data[ip_address]['hostname']} loaded."
)
self.job.logger.debug(f"Device: {self.device_data[ip_address]['hostname']} loaded.")
except diffsync.ObjectAlreadyExists:
self.job.logger.error(
f"Device: {self.device_data[ip_address]['hostname']} has already been loaded! "
Expand All @@ -427,9 +395,7 @@ def load_devices(self):
else:
self._add_ip_address_to_failed_list(ip_address=ip_address)
if self.job.debug:
self.job.logger.debug(
f"{ip_address} was added to the failed ip_address list"
)
self.job.logger.debug(f"{ip_address} was added to the failed ip_address list")

def load(self):
"""Load network data."""
Expand Down
8 changes: 4 additions & 4 deletions nautobot_device_onboarding/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ def load_target_adapter(self):
self.target_adapter = SyncDevicesNautobotAdapter(job=self, sync=self.sync)
self.target_adapter.load()

def _convert_sring_to_bool(self, string, header):
def _convert_string_to_bool(self, string, header):
"""Given a string of 'true' or 'false' convert to bool."""
if string.lower() == "true":
return True
Expand Down Expand Up @@ -369,10 +369,10 @@ def _process_csv_data(self, csv_file):
name=row["platform_name"].strip(),
)

set_mgmgt_only = self._convert_sring_to_bool(
set_mgmt_only = self._convert_string_to_bool(
string=row["set_mgmt_only"].lower().strip(), header="set_mgmt_only"
)
update_devices_without_primary_ip = self._convert_sring_to_bool(
update_devices_without_primary_ip = self._convert_string_to_bool(
string=row["update_devices_without_primary_ip"].lower().strip(),
header="update_devices_without_primary_ip",
)
Expand All @@ -382,7 +382,7 @@ def _process_csv_data(self, csv_file):
processed_csv_data[row["ip_address_host"]]["namespace"] = namespace
processed_csv_data[row["ip_address_host"]]["port"] = int(row["port"].strip())
processed_csv_data[row["ip_address_host"]]["timeout"] = int(row["timeout"].strip())
processed_csv_data[row["ip_address_host"]]["set_mgmt_only"] = set_mgmgt_only
processed_csv_data[row["ip_address_host"]]["set_mgmt_only"] = set_mgmt_only
processed_csv_data[row["ip_address_host"]]["update_devices_without_primary_ip"] = (
update_devices_without_primary_ip
)
Expand Down

0 comments on commit a84e851

Please sign in to comment.