-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
lldp: Handling attributes that are defined multiple times #9657
base: main
Are you sure you want to change the base?
Conversation
This fixes crashes when the lldpctl output has lines for unknown tlvs that redefine a key in the middle of the nested dict data structure.
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.
Thanks for your contribution! Can you please add a changelog fragment? Thanks.
plugins/modules/lldp.py
Outdated
@@ -67,7 +67,8 @@ def gather_lldp(module): | |||
for path_component in path_components: | |||
current_dict[path_component] = current_dict.get(path_component, {}) | |||
current_dict = current_dict[path_component] | |||
current_dict[final] = value | |||
if final not in current_dict: |
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'm not sure this is the right fix. The error was TypeError: 'str' object does not support item assignment
, which means that current_dict
was not a dictionary, but a string.
In that case, final not in current_dict
will test whether final
(if it happens to be a string) appears in the string final
. (If final
happens to be an integer, for example, this will cause another exception: TypeError: 'in <string>' requires string as left operand, not int
.) I don't think this is what you intended.
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.
The Idea of my fix was that when
lldp.eno1.unknown-tlvs.unknown-tlv=4A,5A,30,32,31,38,33,30,30,37,31,31
is processed it's not overwriting the dict at lldp['eno1']['unknown-tlvs']['unknown-tlv']
. So current_dict
never becomes a string in the first place if we prevent the overwriting behavior.
But indeed this is not the most robust fix. It would not prevent the problem if the lldp.eno1.unknown-tlvs.unknown-tlv=4A,5A,30,32,31,38,33,30,30,37,31,31
line is the first one in the output.
Let me comeback with a solution that covers more cases.
- Fix crash caused by certain lldpctl output where an attribute is defined as branch and leaf - Adds multivalues parameter to control behavior when lldpctl outputs an attribute multiple times
I've added more robust handling of the |
The test
The test
The test
The test
The test
The test
The test
|
This fixes crashes when the lldpctl output has lines for unknown tlvs that redefine a key in the middle of the nested dict data structure.
Also adds a function to handle attributes that are defined multiple times.
SUMMARY
When the lldpctl command outputs something like the following the module crashes with the following error
Traceback
Example output that causes this problem
As seen above such output has problematic output. lldp.eno1.unknown-tlvs.unknown-tlv has "subkeys" and is also set to a string (The comma separated bytes).
The fix will move the string value to the subkey "value". So the result looks like this:
Under normal condition we do not expect lldpctl to redefine values. Therefore the simple fix is to just ignore those lines.Sometimes
lldpctl
outputs an attribute mutliple times, therefore this PR also introduces themultivalues
options to control how to handle such output.This new option defaults to false to keep the current behavior
Example
lldpctl
output having the same attribute multiple timesIf
multivalues
istrue
it results in this structureISSUE TYPE
COMPONENT NAME
lldp
ADDITIONAL INFORMATION