networktocode/network-importer

Traceback when hostname contains non-alphanumeric characters

mathiaswegner opened this issue · 1 comments

Environment

  • Python version: 3.10.9
  • Network Importer version: 3.1.0
  • Nautobot version: 1.5.6
  • pynautobot: 1.2.2

Steps to Reproduce

  1. select a device where the hostname in config includes a nonalphanumeric character, eg the hostname of the device is configured as hostname.3rdleveldomain instead of hostname
  2. model the device in Nautobot
  3. network-importer check --update-configs
  4. network-importer apply

Expected Behavior

import the device correctly

Observed Behavior

pynautobot.core.query.RequestError: The request failed with code 400 Bad Request: {'slug': ['Enter a valid “slug” consisting of Unicode letters, numbers, underscores, or hyphens.', 'Enter a valid "slug" consisting of Unicode letters, numbers, underscores, or hyphens.']}

It appears that when the nautobot adapter is creating device tags as part of vlan creation, it is passing a slug value. I tried removing the slug value from the call in the hopes that AutoSlugField would work, but that just changed the error:

pynautobot.core.query.RequestError: The request failed with code 400 Bad Request: {'slug': ['This field is required.']}

I was able to resolve the issue for network-importer by changing the slug field:

index 068dd3d..252ed51 100644
--- a/network_importer/adapters/nautobot_api/models.py
+++ b/network_importer/adapters/nautobot_api/models.py
@@ -48,9 +48,9 @@ class NautobotDevice(Device):
             return self.device_tag_id
 
         tag = self.diffsync.nautobot.extras.tags.get(name=f"device={self.name}")
-
         if not tag:
-            tag = self.diffsync.nautobot.extras.tags.create(name=f"device={self.name}", slug=f"device__{self.name}")
+            tag = self.diffsync.nautobot.extras.tags.create(
+                name=f"device={self.name}", slug=''.join(c if c.isalnum() else '_' for c in self.name))
 
         self.device_tag_id = tag.id
         return self.device_tag_id

Is this worth submitting a PR for since I know that the name field is expected to be alphanumeric only? I'm happy to patch my own instance to handle our badly configured brownfield devices. I'm also wondering if I should be submitting an issue to pynautobot about slug being required or if that is FAD.

Yes, I think this would be great to have updated.