Skip to content

Unable to create TunnelTermination in Custom Script #15654

@hackiero

Description

@hackiero

Deployment Type

Self-hosted

NetBox Version

v3.7.5

Python Version

3.11

Steps to Reproduce

  1. Create VPN Tunnel, or have existing VPN tunnel with no terminations
  2. Import Custom Script
from core.models import ContentType
from vpn.models import IPSecProfile, Tunnel, TunnelTermination
from extras.scripts import Script, ObjectVar
name = "Tunnel Termination Test"
class TTTest(Script):
    class Meta:
        name = "VPN Tunnel Termination Test"

    tun = ObjectVar( model = Tunnel, default = 490 )

    def run(self, data, commit):
        tun = data['tun']
        tta = TunnelTermination(
            role = "hub",
            termination_type = ContentType.objects.get(model="interface"),
            tunnel = tun
        )
        tta.full_clean()
        tta.save()
  1. Run script, with commit, choose Tunnel object

Expected Behavior

A new VPN Termination object will be created.

Observed Behavior

Script complete successfully without Commit option selected.
With Commit, script fails with:

An exception occurred: AttributeError: 'NoneType' object has no attribute '_meta'

Traceback (most recent call last):
  File "/opt/netbox/netbox/extras/scripts.py", line 512, in _run_script
    script.output = script.run(data=data, commit=commit)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/netbox/netbox/scripts/palo-test.py", line 24, in run
    tta.save()
  File "/opt/netbox/venv/lib/python3.11/site-packages/django/db/models/base.py", line 814, in save
    self.save_base(
  File "/opt/netbox/venv/lib/python3.11/site-packages/django/db/models/base.py", line 892, in save_base
    post_save.send(
  File "/opt/netbox/venv/lib/python3.11/site-packages/django/dispatch/dispatcher.py", line 176, in send
    return [
           ^
  File "/opt/netbox/venv/lib/python3.11/site-packages/django/dispatch/dispatcher.py", line 177, in <listcomp>
    (receiver, receiver(signal=self, sender=sender, **named))
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/netbox/netbox/extras/signals.py", line 97, in handle_changed_object
    enqueue_object(queue, instance, request.user, request.id, action)
  File "/opt/netbox/netbox/extras/events.py", line 68, in enqueue_object
    'data': serialize_for_event(instance),
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/netbox/netbox/extras/events.py", line 35, in serialize_for_event
    return serializer.data
           ^^^^^^^^^^^^^^^
  File "/opt/netbox/venv/lib/python3.11/site-packages/rest_framework/serializers.py", line 555, in data
    ret = super().data
          ^^^^^^^^^^^^
  File "/opt/netbox/venv/lib/python3.11/site-packages/rest_framework/serializers.py", line 253, in data
    self._data = self.to_representation(self.instance)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/netbox/venv/lib/python3.11/site-packages/rest_framework/serializers.py", line 522, in to_representation
    ret[field.field_name] = field.to_representation(attribute)
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/netbox/venv/lib/python3.11/site-packages/rest_framework/fields.py", line 1838, in to_representation
    return method(value)
           ^^^^^^^^^^^^^
  File "/opt/netbox/netbox/vpn/api/serializers.py", line 101, in get_termination
    serializer = get_serializer_for_model(obj.termination, prefix=NESTED_SERIALIZER_PREFIX)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/netbox/netbox/utilities/api.py", line 26, in get_serializer_for_model
    app_name, model_name = model._meta.label.split('.')
                           ^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute '_meta'

Running similar code in nbshell works as expected:

### NetBox interactive shell (0dbb252f4afe)
### Python 3.11.4 | Django 4.2.11 | NetBox 3.7.5
### lsmodels() will show available models. Use help(<model>) for more info.
>>> tun = Tunnel.objects.get(id=490)
>>> tta = TunnelTermination(
...     role = "hub",
...     termination_type = ContentType.objects.get(model="interface"),
...     tunnel = tun
... )
>>> tta.save()
>>>

Metadata

Metadata

Assignees

Labels

severity: lowDoes not significantly disrupt application functionality, or a workaround is availablestatus: acceptedThis issue has been accepted for implementationtype: bugA confirmed report of unexpected behavior in the application

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions