Skip to content

runscript cli comand does not pass cleaned data to script #18480

@matejv

Description

@matejv

Deployment Type

Self-hosted

NetBox Version

v4.2.2

Python Version

3.12

Steps to Reproduce

  1. Verify you have a Site with ID 1. If not, create a Site and input any required data, values don't matter. Verify that ID of Site is 1.
  2. Upload the following example script to netbox:
from dcim.models import Site
from extras.scripts import ObjectVar, Script

class TestScript(Script):
    class Meta:
        name = "Test calling script via cli/api"
        commit_default = False

    site = ObjectVar(
        model=Site,
        required=True,
    )

    def run(self, data, commit):
        site = data['site']
        self.log_debug(f'got {site} is type {type(site).__name__}')
        self.log_debug(f'access site name attribute: {site.name}')
  1. Run runscript via cli:
python3 manage.py runscript --loglevel debug --data '{"site":1}' test_call.TestScript

Expected Behavior

Script is passed Site instance with id 1 in data['site'] and script completes successfully.

Observed Behavior

Script produces an error:

[24/Jan/2025 08:54:50,608] Initialized configuration
[2025-01-24 08:54:50,632][INFO] - Running script (commit=False)
[24/Jan/2025 08:54:50,632] Running script (commit=False)
[2025-01-24 08:54:50,632][DEBUG] - got 1 is type int
[24/Jan/2025 08:54:50,632] got 1 is type int
[2025-01-24 08:54:50,632][ERROR] - An exception occurred: `AttributeError: 'int' object has no attribute 'name'`

Traceback (most recent call last):
  File "/home/matej/temp/netbox-devel/netbox/netbox/extras/jobs.py", line 43, in run_script
    script.output = script.run(data, commit)
                    ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/matej/Projects/netbox/scripts/test_call.py", line 17, in run
    self.log_debug(f'access site name attribute: {site.name}')
                                                  ^^^^^^^^^
AttributeError: 'int' object has no attribute 'name'


[24/Jan/2025 08:54:50,632] An exception occurred: `AttributeError: 'int' object has no attribute 'name'`

Traceback (most recent call last):
  File "/home/matej/temp/netbox-devel/netbox/netbox/extras/jobs.py", line 43, in run_script
    script.output = script.run(data, commit)
                    ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/matej/Projects/netbox/scripts/test_call.py", line 17, in run
    self.log_debug(f'access site name attribute: {site.name}')
                                                  ^^^^^^^^^
AttributeError: 'int' object has no attribute 'name'


[2025-01-24 08:54:50,632][ERROR] - Exception raised during script execution: 'int' object has no attribute 'name'
[24/Jan/2025 08:54:50,632] Exception raised during script execution: 'int' object has no attribute 'name'
[2025-01-24 08:54:50,632][INFO] - Database changes have been reverted due to error.
[24/Jan/2025 08:54:50,632] Database changes have been reverted due to error.
[2025-01-24 08:54:50,640][INFO] - Script completed in 0 minutes, 0.01 seconds
[24/Jan/2025 08:54:50,640] Script completed in 0 minutes, 0.01 seconds

Looking at the Job logs, we can see that script was passed an int in data['site'] instead of Site instance:

{
    "log": [
        {
            "message": "got 1 is type int",
            "obj": null,
            "status": "debug",
            "time": "2025-01-24T08:54:50.632187+00:00",
            "url": null
        },

The problem is that runscript passes data to ScriptJob as given on CLI, instead of passing form.cleaned_data. Since the form is already being processed to validate data in the same file above, it would make sense to pass cleaned data.

I can provide a pull request with a fix if accepted.

The same issue is present when running scripts via REST API (/api/extras/scripts/test_call.TestScript/). I can open a separate bug for that, if this one will be accepted.

Metadata

Metadata

Assignees

Labels

severity: mediumResults in substantial degraded or broken functionality for specfic workflowsstatus: acceptedThis issue has been accepted for implementationtype: bugA confirmed report of unexpected behavior in the application

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions