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

replace non-alphanumeric chars in device_tag slug with underscore #296

Merged
merged 2 commits into from
Mar 15, 2023

Conversation

mathiaswegner
Copy link
Contributor

Fixes a traceback when the configured hostname on a device includes non-alphanumeric characters (eg, hostname configured to fqdn)

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))

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?

Copy link
Contributor Author

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.

@jvanderaa
Copy link
Contributor

@glennmatthews want to take a second look here?

Comment on lines 50 to +54
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)}")

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.

@jvanderaa jvanderaa merged commit 4063231 into networktocode:develop Mar 15, 2023
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

Successfully merging this pull request may close these issues.

None yet

3 participants