-
Notifications
You must be signed in to change notification settings - Fork 42
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
replace non-alphanumeric chars in device_tag slug with underscore #296
replace non-alphanumeric chars in device_tag slug with underscore #296
Conversation
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)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
might be better to use -
rather than _
, since that's more usual in slugs? Not critical though.
Also, looks like this is removing the device__
prefix from the tags, not sure if that's intentional?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I went with underscore rather than dash because of the slug tip here: https://docs.nautobot.com/projects/core/en/stable/user-guides/custom-fields/?h=slug#grouping
Omitting device__
was inadvertent, sorry. I'll fix that.
@glennmatthews want to take a second look here? |
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=f"device__{''.join(c if c.isalnum() else '_' for c in self.name)}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think a more concise alternative would be to replace the entire tag = ...tags.get(...) ; if not tag: ...tags.create(...)
sequence with something like:
tag, _ = self.diffsync.nautobot.extras.tags.get_or_create(
name=f"device={self.name}",
defaults={"slug": f"device__{''.join(c if c.isalnum() else '_' for c in self.name)}"},
)
Not blocking though.
Fixes a traceback when the configured hostname on a device includes non-alphanumeric characters (eg, hostname configured to fqdn)