Custom links conditional rendering

34 views
Skip to first unread message

Sergey Korobkov

unread,
Jan 3, 2021, 5:58:13 PM1/3/21
to NetBox
Hi,
I'm trying to configure conditional rendering based on the interface name the device has, is this even possible, and if yes, lets say, interface name "bmc" has ip address 192.168.1.1/24, can I pick only ip address part without netmask (by regex or slicing for example).

So in sum, I want for every device with "bmc" (which stands for OOM) interface HTTPS link to be constructed from its interface IP (without mask).
If its not possible - I will need to specify a custom link for every device by id manually instead, which kind of sad.

Thanks,
Sergei

Brian Candler

unread,
Jan 4, 2021, 5:02:10 AM1/4/21
to NetBox
Yes, there's jinja2 templating. Here's a starting point you can use for a custom link (I haven't tested it for a while but I think it should still be OK):

Text:
{% if obj.vc_interfaces.filter(mgmt_only=True).count() == 1 and obj.vc_interfaces.get(mgmt_only=True).ip_addresses.count() == 1 %}Open IPMI{% endif %}
URL:
https://{{ obj.vc_interfaces.get(mgmt_only=True).ip_addresses.first().address.ip }}/

This example doesn't do exactly what you ask: rather than looking for interfaces with name "bmc", it looks for interfaces with the "mgmt_only" flag set to true (which IMO is a better way of doing this).  But it could be modified to do that - just change the filter(...) and get(...) conditions.

The "Text" is the label of the button.  If it expands to empty string, the button is not displayed.  Here it's only displayed if there's exactly one interface which meets that condition, and if that interface has exactly one IP address.

The "URL" is the target URL of the button.  It gets the IP address without netmask by using ".address.ip"

HTH,

Brian. 

Brian Candler

unread,
Jan 4, 2021, 5:14:24 AM1/4/21
to NetBox
Just to clarify, the "mgmt_only" flag is what's displayed in the GUI as "Management only" or "OOB Management" (depending on what screen you're looking at)
Reply all
Reply to author
Forward
0 new messages