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

Closed
opened 2025-12-29 21:34:54 +01:00 by adam · 4 comments
Owner

Originally created by @matejv on GitHub (Jan 24, 2025).

Originally assigned to: @matejv on GitHub.

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.

Originally created by @matejv on GitHub (Jan 24, 2025). Originally assigned to: @matejv on GitHub. ### 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: ```py 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}') ``` 3. 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](https://github.com/netbox-community/netbox/blob/da9b4523276a3e8a644ffa718ec91cda0421a581/netbox/extras/management/commands/runscript.py#L89) 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.
adam added the type: bugstatus: acceptedseverity: medium labels 2025-12-29 21:34:54 +01:00
adam closed this issue 2025-12-29 21:34:54 +01:00
Author
Owner

@jeremystretch commented on GitHub (Feb 7, 2025):

I think this was an oversight when we introduced support for object variables some time ago.

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

Thanks @matejv, I'll assign this to you.

@jeremystretch commented on GitHub (Feb 7, 2025): I think this was an oversight when we introduced support for object variables some time ago. > I can provide a pull request with a fix if accepted. Thanks @matejv, I'll assign this to you.
Author
Owner

@sjurtf commented on GitHub (Apr 2, 2025):

@matejv @jeremystretch Hi,

I think this issue should be reopened, the change did only fix the issue when calling the script from the CLI.

This issue is still present for me when using the REST API on version 4.2.6 using the execute the script, but not when using the CLI. I'm running 4.2.6 in Docker locally, and I've uploaded the script from this issue.

Calling the API

-H "Authorization: Token <token>" \
-H "Content-Type: application/json" \
-H "Accept: application/json; indent=4" \
http://localhost:8000/api/extras/scripts/9/ \
--data '{"data": {"site": 1}, "commit": true}'

The response

    "log": [
        {
            "message": "got 1 is type int",
            "obj": null,
            "status": "debug",
            "time": "2025-04-02T06:39:27.311753+00:00",
            "url": null
        },
        {
            "message": "An exception occurred: `AttributeError: 'int' object has no attribute 'name'`\n```\nTraceback (most recent call last):\n  File \"/opt/netbox/netbox/extras/jobs.py\", line 43, in run_script\n    script.output = script.run(data, commit)\n                    ^^^^^^^^^^^^^^^^^^^^^^^^\n  File \"/opt/netbox/netbox/scripts/test.py\", line 17, in run\n    self.log_debug(f'access site name attribute: {site.name}')\n                                                  ^^^^^^^^^\nAttributeError: 'int' object has no attribute 'name'\n\n```",
            "obj": null,
            "status": "failure",
            "time": "2025-04-02T06:39:27.312534+00:00",
            "url": null
        },
        {
            "message": "Database changes have been reverted due to error.",
            "obj": null,
            "status": "info",
            "time": "2025-04-02T06:39:27.312962+00:00",
            "url": null
        }
    ],
    "output": "",
    "tests": {}
}

Calling using the CLI (works)

🧬 loaded config '/etc/netbox/config/configuration.py'
🧬 loaded config '/etc/netbox/config/extra.py'
🧬 loaded config '/etc/netbox/config/logging.py'
🧬 loaded config '/etc/netbox/config/plugins.py'
[2025-04-02 06:46:39,632][INFO] - Running script (commit=False)
[2025-04-02 06:46:39,632][DEBUG] - got Marienlyst is type Site
[2025-04-02 06:46:39,632][DEBUG] - access site name attribute: Marienlyst
[2025-04-02 06:46:39,633][INFO] - Database changes have been reverted automatically.
[2025-04-02 06:46:39,635][INFO] - Script completed in 0 minutes, 0.00 seconds
@sjurtf commented on GitHub (Apr 2, 2025): @matejv @jeremystretch Hi, I think this issue should be reopened, the change did only fix the issue when calling the script from the CLI. This issue is still present for me when using the REST API on version 4.2.6 using the execute the script, but not when using the CLI. I'm running 4.2.6 in Docker locally, and I've uploaded the script from this issue. ## Calling the API ```curl -X POST \ -H "Authorization: Token <token>" \ -H "Content-Type: application/json" \ -H "Accept: application/json; indent=4" \ http://localhost:8000/api/extras/scripts/9/ \ --data '{"data": {"site": 1}, "commit": true}' ``` ## The response ```{ "log": [ { "message": "got 1 is type int", "obj": null, "status": "debug", "time": "2025-04-02T06:39:27.311753+00:00", "url": null }, { "message": "An exception occurred: `AttributeError: 'int' object has no attribute 'name'`\n```\nTraceback (most recent call last):\n File \"/opt/netbox/netbox/extras/jobs.py\", line 43, in run_script\n script.output = script.run(data, commit)\n ^^^^^^^^^^^^^^^^^^^^^^^^\n File \"/opt/netbox/netbox/scripts/test.py\", line 17, in run\n self.log_debug(f'access site name attribute: {site.name}')\n ^^^^^^^^^\nAttributeError: 'int' object has no attribute 'name'\n\n```", "obj": null, "status": "failure", "time": "2025-04-02T06:39:27.312534+00:00", "url": null }, { "message": "Database changes have been reverted due to error.", "obj": null, "status": "info", "time": "2025-04-02T06:39:27.312962+00:00", "url": null } ], "output": "", "tests": {} } ``` ## Calling using the CLI (works) ```unit@a4b2009ed772:/opt/netbox/netbox$ python3 manage.py runscript --loglevel debug --data '{"site":1}' test_call.TestScript 🧬 loaded config '/etc/netbox/config/configuration.py' 🧬 loaded config '/etc/netbox/config/extra.py' 🧬 loaded config '/etc/netbox/config/logging.py' 🧬 loaded config '/etc/netbox/config/plugins.py' [2025-04-02 06:46:39,632][INFO] - Running script (commit=False) [2025-04-02 06:46:39,632][DEBUG] - got Marienlyst is type Site [2025-04-02 06:46:39,632][DEBUG] - access site name attribute: Marienlyst [2025-04-02 06:46:39,633][INFO] - Database changes have been reverted automatically. [2025-04-02 06:46:39,635][INFO] - Script completed in 0 minutes, 0.00 seconds
Author
Owner

@matejv commented on GitHub (Apr 2, 2025):

This issue tackled only CLI. For API changes we would need to open a separate bug report / feature request.

However before that we should figure out exactly what the behavior should be. See for example this discussion:
#18229 (thare are others as well) and give your feedback there.

@matejv commented on GitHub (Apr 2, 2025): This issue tackled only CLI. For API changes we would need to open a separate bug report / feature request. However before that we should figure out exactly what the behavior should be. See for example this discussion: #18229 (thare are others as well) and give your feedback there.
Author
Owner

@sjurtf commented on GitHub (Apr 2, 2025):

Great, thank you.
I'll continue in that discussion.

@sjurtf commented on GitHub (Apr 2, 2025): Great, thank you. I'll continue in that discussion.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#10693