[PR #20596] [CLOSED] DRAFT: 14884 Make script running from UI and EventRules consistent in passing params #15977

Closed
opened 2025-12-30 00:25:07 +01:00 by adam · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/netbox-community/netbox/pull/20596
Author: @arthanson
Created: 10/16/2025
Status: Closed

Base: featureHead: 14884-script


📝 Commits (2)

  • 6930df8 14884 de-reference script params from EventRule
  • 18efe72 14884 log error on form validation fail

📊 Changes

1 file changed (+19 additions, -1 deletions)

View changed files

📝 netbox/extras/events.py (+19 -1)

📄 Description

Fixes: #14884

This makes parameter passing consistent between running a script from the UI and running one from an EventRule. To see add the following script:

from extras.scripts import Script, ObjectVar
from ipam.models import IPAddress

class BranchTesting(Script):

    ip_address = ObjectVar(
        model=IPAddress,
        description='IP Address to test',
        required=True,
    )

    def run(self, data, commit):
        return f"data is {data}"

Run it in the UI, select an IP Address and then inspect the job data and you should see

{
    "log": [],
    "output": "data is {'ip_address': <IPAddress: 172.16.0.1/24>}",
    "tests": {}
}

Now set up an Event Rule:
Object types: DCIM > Site
Event types: Object created, Object updated
Action type: Script
Script: <script file from above>
Action data: {"ip_address": 3}

(Modify ip_address to one in your data). Then go and modify a site and check the output from the new job data and you should see something like:

{
    "log": [],
    "output": "data is {'ip_address': <IPAddress: 172.16.0.1/24>, 'id': 24, 'url': '/api/dcim/sites/24/', 'display_url': '/dcim/sites/24/', 'display': 'Butler Communications', 'name': 'Butler Communications', 'slug': 'ncsu-128', 'status': {'value': 'active', 'label': 'Active'}, 'region': {'id': 40, 'url': '/api/dcim/regions/40/', 'display': 'North Carolina', 'name': 'North Carolina', 'slug': 'us-nc', 'description': '', 'site_count': 0, '_depth': 2}, 'group': None, 'tenant': {'id': 13, 'url': '/api/tenancy/tenants/13/', 'display': 'NC State University', 'name': 'NC State University', 'slug': 'nc-state', 'description': ''}, 'facility': 'BUT', 'time_zone': None, 'description': 'asdf', 'physical_address': '3210 Faucette Dr., Raleigh, NC 27607', 'shipping_address': '', 'latitude': None, 'longitude': None, 'comments': '', 'asns': [], 'tags': [{'id': 7, 'url': '/api/extras/tags/7/', 'display_url': '/extras/tags/7/', 'display': 'Golf', 'name': 'Golf', 'slug': 'golf', 'color': '673ab7'}, {'id': 12, 'url': '/api/extras/tags/12/', 'display_url': '/extras/tags/12/', 'display': 'Lima', 'name': 'Lima', 'slug': 'lima', 'color': '009688'}, {'id': 24, 'url': '/api/extras/tags/24/', 'display_url': '/extras/tags/24/', 'display': 'X-ray', 'name': 'X-ray', 'slug': 'x-ray', 'color': '9e9e9e'}], 'custom_fields': {}, 'created': '2021-04-02T00:00:00Z', 'last_updated': '2025-10-15T21:17:43.035621Z'}",
    "tests": {}
}

The only part of this second data to check in the ip_address as the rest of the data is the Site that triggered the EventRule. Previously ip_address would show 3 (whatever was in action_data), now it de-references it to the correct object just like running it from the UI does.

Note: Moved this to v4.5 milestone as this will be a breaking change from older scripts being run via EventRules.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/netbox-community/netbox/pull/20596 **Author:** [@arthanson](https://github.com/arthanson) **Created:** 10/16/2025 **Status:** ❌ Closed **Base:** `feature` ← **Head:** `14884-script` --- ### 📝 Commits (2) - [`6930df8`](https://github.com/netbox-community/netbox/commit/6930df85f3f3c0ac17b3573c0b95f9429e4d6360) 14884 de-reference script params from EventRule - [`18efe72`](https://github.com/netbox-community/netbox/commit/18efe72b114f90b706c0d461a2f6e62ed5751181) 14884 log error on form validation fail ### 📊 Changes **1 file changed** (+19 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `netbox/extras/events.py` (+19 -1) </details> ### 📄 Description ### Fixes: #14884 This makes parameter passing consistent between running a script from the UI and running one from an EventRule. To see add the following script: ```python from extras.scripts import Script, ObjectVar from ipam.models import IPAddress class BranchTesting(Script): ip_address = ObjectVar( model=IPAddress, description='IP Address to test', required=True, ) def run(self, data, commit): return f"data is {data}" ``` Run it in the UI, select an IP Address and then inspect the job data and you should see ``` { "log": [], "output": "data is {'ip_address': <IPAddress: 172.16.0.1/24>}", "tests": {} } ``` Now set up an Event Rule: **Object types:** DCIM > Site **Event types:** Object created, Object updated **Action type:** Script **Script:** <script file from above> **Action data:** {"ip_address": 3} (Modify `ip_address` to one in your data). Then go and modify a site and check the output from the new job data and you should see something like: ``` { "log": [], "output": "data is {'ip_address': <IPAddress: 172.16.0.1/24>, 'id': 24, 'url': '/api/dcim/sites/24/', 'display_url': '/dcim/sites/24/', 'display': 'Butler Communications', 'name': 'Butler Communications', 'slug': 'ncsu-128', 'status': {'value': 'active', 'label': 'Active'}, 'region': {'id': 40, 'url': '/api/dcim/regions/40/', 'display': 'North Carolina', 'name': 'North Carolina', 'slug': 'us-nc', 'description': '', 'site_count': 0, '_depth': 2}, 'group': None, 'tenant': {'id': 13, 'url': '/api/tenancy/tenants/13/', 'display': 'NC State University', 'name': 'NC State University', 'slug': 'nc-state', 'description': ''}, 'facility': 'BUT', 'time_zone': None, 'description': 'asdf', 'physical_address': '3210 Faucette Dr., Raleigh, NC 27607', 'shipping_address': '', 'latitude': None, 'longitude': None, 'comments': '', 'asns': [], 'tags': [{'id': 7, 'url': '/api/extras/tags/7/', 'display_url': '/extras/tags/7/', 'display': 'Golf', 'name': 'Golf', 'slug': 'golf', 'color': '673ab7'}, {'id': 12, 'url': '/api/extras/tags/12/', 'display_url': '/extras/tags/12/', 'display': 'Lima', 'name': 'Lima', 'slug': 'lima', 'color': '009688'}, {'id': 24, 'url': '/api/extras/tags/24/', 'display_url': '/extras/tags/24/', 'display': 'X-ray', 'name': 'X-ray', 'slug': 'x-ray', 'color': '9e9e9e'}], 'custom_fields': {}, 'created': '2021-04-02T00:00:00Z', 'last_updated': '2025-10-15T21:17:43.035621Z'}", "tests": {} } ``` The only part of this second data to check in the `ip_address` as the rest of the data is the Site that triggered the EventRule. Previously `ip_address` would show `3` (whatever was in action_data), now it de-references it to the correct object just like running it from the UI does. Note: Moved this to v4.5 milestone as this will be a breaking change from older scripts being run via EventRules. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
adam added the pull-request label 2025-12-30 00:25:07 +01:00
adam closed this issue 2025-12-30 00:25:08 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#15977