netbox-community / netbox

The premier source of truth powering network automation. Open source under Apache 2. Try NetBox Cloud free: https://netboxlabs.com/free-netbox-cloud/
http://netboxlabs.com/oss/netbox/
Apache License 2.0
16.2k stars 2.59k forks source link

Netbox JSON Conditions for Visibility of Custom Fields and Custom Links #14745

Closed cmcknz77 closed 10 months ago

cmcknz77 commented 10 months ago

NetBox version

v3.7.0

Feature type

Change to existing functionality

Proposed functionality

Currently Custom Fields have their visibility on a page controlled by the UI Visible model value which has 3 options: Always, Hidden and "If Set". Custom Links have their visibility controlled by the Enabled binary button.

I'd like to propose adding a Visibility Condition field to both of those models that takes input in the same form as the Netbox JSON Conditions https://docs.netbox.dev/en/stable/reference/conditions/ schema.

That way, if a Custom Link is set to Enabled and the condition in the Condition field doesn't evaluate to "True", the Custom Link doesn't show and if it evaluates to True it does. If my example Custom Link points to the device page on my arbitrary monitoring platform (prtg in this case), I could have a Custom Link that points to

https://myprtg.virtual-server.org/device.htm?id={{ object.cf.prtg_id }}&tabid=1

only display if the associated custom field on that object has a value. That custom field exists on the device model but would not be populated on a device that we don't need to monitor. So it'd be viewable/clickable on a device with a non-null prtg_id field but not visible on, say, a patch panel or ODF (where that prtg_id field would probably be null). Given the link value of our custom Link is only going to point to a valid URL if the field it references has a value for the vast majority of the occasions on which this custom link is displayed currently, it's invalid (because the field upon which it relies is only filled in for a subset of the devices in the device list).

On a Custom Field the "If Set" option could change to "If condition is met", and we'd be able to recreate the Visible "If Set" functionality by setting the condition value to something like:

       {
               "attr": "custom_fields.prtg_id"
               "negate": true,
               "value": null
        }

But we'd also be able to only display a custom Field if other conditions were met. Say, for example you had a Base_MAC_Address custom field, you would be able to set the conditions so that that field becomes visible only for devices where such a field is relevant:

{
    "and": [
               {
               "attr": "role.name"
               "value": ["Router","Firewall","Gateway","Switch","AccessPoint"],
               "op": "in"
        }
   ]
}

So, Base_MAC_Address might show up on a Router, Firewall, Gateway, Switch or Access-Point but won't show up on an Antenna or a Patch Panel or a Power Panel. If I have fields for GPS Lat & Long, elevation, and azimuth I could have those fields show up on the GUI for devices with the PtP or PtMP Radio device roles but not where they're irrelevant like on a Firewall, Media Converter or a console server without a network interface.

Use case

So, Base_MAC_Address might show up on a Router, Firewall, Gateway, Switch or AccessPoint but won't show up on an Antenna or a Patch Panel or a Power Panel. If I have fields for GPS Lat & Long, elevation, and azimuth I could have those fields show up on the GUI for devices with the PtP or PtMP Radio device roles but not where they're irrelevant like on a Firewall, Media Converter or a console server without a network interface.

Database changes

I don't know what database changes would be required to support this functionality - perhaps a field on the Custom Link and Custom Field models to store the Visibility Conditions?

External dependencies

No response

jeremystretch commented 10 months ago

I'm sorry, but similar functionality has already been proposed and rejected in various discussions. This is simply exceeds the intended purpose of custom fields. For use cases where the current implementation does not suffice, you'll need to create a custom plugin to extend NetBox's data model.