Use device selector dialog in custom script #9169

Closed
opened 2025-12-29 20:46:29 +01:00 by adam · 3 comments
Owner

Originally created by @llamafilm on GitHub (Jan 30, 2024).

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

Originally created by @llamafilm on GitHub (Jan 30, 2024). ### 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: <img width="790" alt="image" src="https://github.com/netbox-community/netbox/assets/3991046/9766666e-907a-435f-a350-4908f36ea0f3"> Current look: <img width="723" alt="image" src="https://github.com/netbox-community/netbox/assets/3991046/ce33ebed-a8c1-49ca-a27a-32914c85dcb5"> ### Use case I'm building a custom script that has a device variable like this: ```py 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_
adam added the type: featurenetboxstatus: backlogcomplexity: low labels 2025-12-29 20:46:29 +01:00
adam closed this issue 2025-12-29 20:46:30 +01:00
Author
Owner

@github-actions[bot] commented on GitHub (May 15, 2024):

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.

@github-actions[bot] commented on GitHub (May 15, 2024): 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](https://github.com/netbox-community/netbox/blob/develop/CONTRIBUTING.md).
Author
Owner

@llamafilm commented on GitHub (Jul 30, 2024):

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
@llamafilm commented on GitHub (Jul 30, 2024): 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. ```py 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 ```
Author
Owner

@llamafilm commented on GitHub (Jun 6, 2025):

This is resolved by #17944

@llamafilm commented on GitHub (Jun 6, 2025): This is resolved by #17944
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#9169