-
Notifications
You must be signed in to change notification settings - Fork 169
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
Change Record.serialize() to handle tags in NetBox 2.9+ format #311
Conversation
About adding a duplicate tag (with NetBox 2.9.11):
Again, it is possible to add code in
|
About tag handling in general in
or more pythonic
(note that the above does not work with unsaved user-supplied tags!) Removing a tag with NetBox 2.8 or earlier:
and with NetBox 2.9+:
So handling tags in NetBox 2.9+ apps requires significant user mindset shift and application changes anyway. |
😬 Figure this'll end up as boiler-plate in a lot of people's code. Take this with a grain of salt since I've never actually played with the 2.9+ behavior myself. But do client's really have to think about it differently? Could we implement an object with set-like behavior that provided an |
I'd say it's up to you if you want to expand the functionality of |
And obviously the NetBox version handling needs to be present first and only after that we can add the version-independent tag handling code. |
After reading your message again, adding |
One more comment: adding a new tag in NetBox (2.9+) is non-trivial: slug must be given, and generating slug sensibly is surprisingly hard for us non-English language users. For example, if I create a tag with name "Leiniö" (my last name), can I have the slug as "leiniö" or will it cause problems for other users that want to manage tags? Or, if I create a mapping function that sets the slug as "leinio", then I run into risk that "Leinió" would have the same slug and that needs to be handled (same goes if the "strange" character is just removed, like NetBox GUI does). So, I would like to have slug decided by the application programmer, not by I'd like other views for this as well. Edit: to make it extra clear: in NetBox 2.9+ tags must be created separately (if not exist) before they can be assigned to objects. |
Oh, yeah that taps the brakes on that approach then. Generating a slug is definitely something I don't want to get mixed up in.
Ah ok, didn't know that either. |
Reading back over the earlier comments, I think I might've misrepresented the idea I was going for, in part, by explaining it as a bad solution 😆. Really what I was going for was that a list of strings vs objects with a name field may not be so different that we have to put the burden handling new types in client code. Also, I wasn't suggesting we create new tags if the user supplied one that didn't already exist. I think it's sensible to assume 2.9+ users know they have to create the tag (and its slug) first. Question is, do we continue treating tags as a list of strings and fix it up as necessary when dealing with NetBox or do we represent them differently because the NetBox API changed 🤔? They both make pretty good claims to following the principle of least astonishment. |
What I was suggesting in the PR is that we would treat the
If you are looking for normalizing the The current behaviour with this PR is that after Currently, as a So, my answer to your question is that we don't need to "fix up" anything, with this PR it is up to the user to use |
I'm floating the idea that we normalize to a list of strings in all cases. On reads, if we're dealing with <2.9, the response from the server is unmodified. if >=2.9, we pull the name field's value from the object and add it to the list of strings that ends up in
Right, unless you called
Kinda leaning that way too. Just wanted to throw another option out there that might be simpler, from the client's perspective.
I'm not too keen on adding helper methods for tags, users can do that in their code if they want. More looking to nail down how we treat the field by default. |
Will go ahead and merge this. If we decide normalization to a string is the way to go we'd have to cut do it on a major release anyways. |
Feel free to open an issue (or a PR) if you want further discussion about that later. I guess it takes some time for people to settle with 2.9+ habits. |
With this PR
Record.serialize()
only tries to deduplicatetags
(ortagged_vlans
) if all the list members are either strings or integers, so NetBox 2.9+ style dicts will not be causing errors withOrderedDict.fromkeys()
anymore. So this fixes #289.It has to be noted that deduplicating dict-style tags (when dict is input by the user) is not straightforward due to various input formats allowed (id, name, slug). (Or the user can supply just the tag id.) My take is that the
pynetbox
user should do the deduplication checks herself if needed. And AFAIK NetBox does the deduplication anyway. Maybe that would call an extra note in the release notes thatpynetbox
does not enforce user-supplied tag deduplication anymore with new-style tags.PoC with NetBox 2.9.11:
Note: I didn't specifically test this with NetBox 2.8 or older, but it should work as the
tags
list then contains all strings, so the "all-isinstance
" statement returnsTrue
and ensures original behaviour.