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

Traceback when hostname contains non-alphanumeric characters #294

Open
mathiaswegner opened this issue Feb 1, 2023 · 1 comment
Open

Comments

@mathiaswegner
Copy link
Contributor

mathiaswegner commented Feb 1, 2023

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.

@jvanderaa
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants