Skip to content

Commit

Permalink
fix: Fixes javascript error for LLDP neighbor view (#5515)
Browse files Browse the repository at this point in the history
* fix: Fixes javascript error for LLDP neighbor view

The vanilla javascript rewrite of the LLDP neighbors view introduced a bug where an LLDP neighbor with a matching row (but no configured device) will raise an exception. This commit fixes that issue.

---------

Co-authored-by: Hanlin Miao <46973263+HanlinMiao@users.noreply.github.com>
  • Loading branch information
abates and HanlinMiao committed Apr 2, 2024
1 parent 3489c9c commit 46eb6b0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
1 change: 1 addition & 0 deletions changes/5515.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed javascript exception thrown in the Device LLDP neighbors view for neighbors without configured devices/interfaces.
16 changes: 12 additions & 4 deletions nautobot/dcim/templates/dcim/device/lldp_neighbors.html
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@
}
};

function getAttribute(node, querySelector, attribute) {
if (node === null or node.querySelector(querySelector) === null) {
return "";
}
return node.querySelector(querySelector).getAttribute(attribute) || "";
}

ready(() => {
fetch("{% url 'dcim-api:device-napalm' pk=object.pk %}?method=get_lldp_neighbors_detail")
.then((response) => {
Expand All @@ -80,10 +87,10 @@
// var row = $('*[data-interface-name="' + iface.split(".")[0].replace(/([\/:])/g, "\\$1") + '"]');

// Glean configured hostnames/interfaces from the DOM
const configured_device = row.querySelector('td.configured_device').getAttribute('data');
const configured_chassis = row.querySelector('td.configured_device').getAttribute('data-chassis');
const configured_interface = row.querySelector('td.configured_interface').getAttribute('data-interface-name').toLowerCase();
const configured_mac_address = row.querySelector('td.configured_mac').getAttribute('data-mac-address').toLowerCase();
const configured_device = getAttribute(row, 'td.configured_device', 'data');
const configured_chassis = getAttribute(row, 'td.configured_device', 'data-chassis');
const configured_interface = getAttribute(row, 'td.configured_interface', 'data-interface-name').toLowerCase();
const configured_mac_address = getAttribute(row, 'td.configured_mac', 'data-mac-address').toLowerCase();
let configured_interface_short = null;
if (configured_interface) {
// Match long-form IOS names against short ones (e.g. Gi0/1 == GigabitEthernet0/1).
Expand Down Expand Up @@ -122,5 +129,6 @@
}
});
});

</script>
{% endblock %}

0 comments on commit 46eb6b0

Please sign in to comment.