netbox-community / netbox

The premier source of truth powering network automation. Open source under Apache 2. Public demo: https://demo.netbox.dev
http://netboxlabs.com/oss/netbox/
Apache License 2.0
15.72k stars 2.53k forks source link

Use device selector dialog in custom script #14981

Open llamafilm opened 7 months ago

llamafilm commented 7 months ago

NetBox version

v3.7.0

Feature type

Change to existing functionality

Proposed functionality

When selecting a device variable in a Custom Script, I'd like to use the same device selector dialog available in other pages of Netbox. The button on the right pops up a dialog where you can search and filter by site, location, etc.

Example from the cable connection page:

image

Current look:

image

Use case

I'm building a custom script that has a device variable like this:

devices = MultiObjectVar(model=Device)

Device names are unique per site, but not globally unique. So with the current design it's impossible to tell which device you're actually selecting, as shown in this screenshot. One workaround is the add a Site variable, but then I would have to create a separate script for each site.

Database changes

No response

External dependencies

No response

github-actions[bot] commented 3 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. NetBox is governed by a small group of core maintainers which means not all opened issues may receive direct feedback. Do not attempt to circumvent this process by "bumping" the issue; doing so will result in its immediate closure and you may be barred from participating in any future discussions. Please see our contributing guide.

llamafilm commented 1 month ago

Here is a minimal example script to illustrate the use case. I've removed all error handling and branching to simplify the code.
I have an external service which triggers a PDU power cycle, given 3 inputs: the PDU IP address, outlet number, and model number. (Credentials and API protocol are inferred from the model.) I want the Netbox script to call this external service

I'd like to use this script to power cycle any arbitrary set of devices, by calling that service. Then I could setup various schedules to run the script. For example, every Dolby CP850 should be cycled weekly. Sometimes I need to cycle a handful of devices in the same location. There is no single variable which groups the devices together, so using the selector dialogue would let this one script serve multiple purposes.

from dcim.models import Device
from extras.scripts import Script, MultiObjectVar

class PowerCycleTest(Script):

    class Meta:
        """Meta Class."""

        name = "Cycle PDU power outlets"

    devices = MultiObjectVar(model=Device)

    def run(self, data, commit):
        for dev in data["devices"]:
            self.log_debug(f"Power cycling {dev}")
            outlet = dev.powerports.first().link_peers[0]
            pdu = outlet.device
            pdu_model = pdu.device_type.model
            pdu_ip = str(pdu.primary_ip.address.ip)
            pdu_outlet = int(outlet.name.removeprefix("Outlet").split(" ")[0])
            self.invoke_lambda(pdu_ip, pdu_outlet, pdu_model, )

    def invoke_lambda(self, ip: str, outlet_num: int, model: str):
        """Invoke AWS lambda function to trigger the power cycle"""
        pass