Fix regex pattern for character conversion#1499
Fix regex pattern for character conversion#1499sc68cal merged 4 commits intonetbox-community:develfrom
Conversation
plugins/module_utils/netbox_utils.py
Outdated
| removed_chars = re.sub(r"[^\-\.\w\s]", "", value) | ||
| convert_chars = re.sub(r"[\-\.\s]+", "-", removed_chars) | ||
| convert_chars = re.sub(r"[\-\.\s]", "-", removed_chars) |
There was a problem hiding this comment.
At this point, however, I would like to question why dashes are replaced in slugs at all, since dashes are valid characters in slugs.
So, it may be that the issue of Ansible disallowing hyphens for group names, may have accidentally been applied to slugs as well. I agree, the documentation for slug fields explicitly permits hyphens.
So really, I have to wonder what exactly this was trying to prevent? Was it meant to just protect people from adding too many hyphens? I wonder if we shouldn't just remove this part completely?
Should it just be
return re.sub(r"[^\-\.\w\s]", "", value)?
There was a problem hiding this comment.
Are you sure that this would cover all valid cases?
I hadn't thought about it before, but the regex allows periods, which should actually be prohibited.
It also doesn't accept underscores, which are definitely allowed.
A slug is a short label for something, containing only letters, numbers, underscores or hyphens.
There was a problem hiding this comment.
I wasn't sure, so I went and looked. I think we need to update this function to be more aligned with what NetBox does in their code, see my comment
There was a problem hiding this comment.
So, should we just copy the regular expressions from the Netbox project?
My honest opinion:
Depending on the use case, I can also imagine not running any filter at all. Worst case no result is returned by the API.
Detailed input validation is not always necessary in my opinion, because things like regular expressions can diverge after some time.
Especially when it comes to tasks where I just assign a tag, the developer should learn to make correct inputs.
But I can also simply adjust the regular expressions :)
There was a problem hiding this comment.
I think at the very least we should copy the regex expressions from the Netbox project. However, if the NetBox API gives us back a useful error code about the slug not passing validation, then I could be convinced to drop validation on our side completely
There was a problem hiding this comment.
Ok, I am 👍 on us removing our regex logic client side
There was a problem hiding this comment.
This seems to be the source of this message:
https://github.com/django/django/blob/main/django/core/validators.py#L293-L301
(But I'm not 100% sure tbh)
There was a problem hiding this comment.
I will make the removal (maybe tomorrow?) and try to test all of the affected functions (as far as possible).
There was a problem hiding this comment.
A small side note: The regex logic cannot be completely removed at the moment. Correcting the incorrect regular expressions still appears necessary.
Reason:
ansible_modules/docs/getting_started/how-to-use/modules.rst
Lines 120 to 141 in 6b9cc00
The automatic generation of a slug when creating tags etc. is explicitly intended as a feature.
Removing this feature might be a disproportionately large change. (Or what do you think about it?
There was a problem hiding this comment.
No I had not considered that. I think that we'll have to go with the first suggestion of lining up the regex to match what NetBox's front end does. The regex pattern has never changed, it was just slightly incorrect, so I think we can just fix the problem and we'll be ok.
So to whit - we do not need to replace multiple -'s with a single. Just do what NetBox does. Same pattern.
|
I'm digging into this a bit more, I used The code has obviously changed since then, but doing a search I came across https://github.com/netbox-community/netbox/blob/f0507d00bfd077755619b420eae8a5d8dc979d94/netbox/project-static/src/buttons/reslug.ts#L10 which has the code, but also does not appear to trim the number of hyphens. So, I think we should take the opportunity to make |
|
I’ve now given this a lot of thought and considered how best to solve it. Simply adjusting the regular expressions one-to-one isn't sufficient, because at the moment slugs are being replaced even when one is explicitly provided. Completely removing the function doesn’t make sense either, since generating slugs from the name is a desired feature. This led to the following solution:
|
|
The unit tests do not agree with my solution yet. Well 🤨 |
a839d52 to
5c43f37
Compare
|
Fixed & rebased to devel. The CI errors that still occur should have nothing to do with my changes. |
|
CI tests are green on |
0bf7433 to
99936ef
Compare
|
I have found my mistake in thinking and developed a new plan! From now on it will be like this:
A very simplified example: tasks:
- name: Create IP address within NetBox with only required information
netbox.netbox.netbox_ip_address:
netbox_url: http://netbox.local
netbox_token: thisIsMyToken
data:
address: 192.168.1.10
tags:
- "This Tag will be replaced with proper slug"
state: present
- name: Create IP address within NetBox with only required information
netbox.netbox.netbox_ip_address:
netbox_url: http://netbox.local
netbox_token: thisIsMyToken
data:
address: 192.168.1.10
tags:
- slug: "this-tag-wont-be-replaced-with-proper-slug"
state: presentThis should provide maximum compatibility. |
|
Great job, I know that this has turned into a pretty difficult thing to manage, since you have to deal with a lot of corner cases. I think it's a good idea to try and document this new behavior as much as possible as a For the most part when I use NetBox, the slugs were generated either by NetBox via the web application and the slugs are just used in my Ansible playbooks to properly reference things after the fact. My hope is that most people are fetching the slug values and just using them, and aren't really depending on the specific corner case behaviors that you have described. That should hopefully take some of the pressure off. I could be wrong, but that's at least my hope. I'll do a deeper dive on your work at a later time so I'm fully up to speed with you, but great job |
sc68cal
left a comment
There was a problem hiding this comment.
This looks good, I just want a bit more time to fully understand before merging
|
Is it already clear whether the patch is included with the next version? :) Thanks in advance. |
sc68cal
left a comment
There was a problem hiding this comment.
Some thoughts after looking a bit deeper.
This scenario now tests if slug is left intact if explicitly specified
81cf0ad to
23ba767
Compare
Related Issue
#1404
New Behavior
Correct replacement of dashes and dots in slugs.
Contrast to Current Behavior
Incorrect replacement leads to errors when using slugs with multiple consecutive dashes.
Discussion: Benefits and Drawbacks
This would make it possible to use tags like
customer--123without errors.There are also open source projects (e.g. Yaook) that frequently use multiple consecutive dashes in Netbox tags (
yaook--default-gateway).At this point, however, I would like to question why dashes are replaced in slugs at all, since dashes are valid characters in slugs.
Changes to the Documentation
not needed
Proposed Release Note Entry
Double Check
develbranch.