Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to create TunnelTermination in Custom Script #15654

Closed
hackiero opened this issue Apr 8, 2024 · 0 comments · Fixed by #15708
Closed

Unable to create TunnelTermination in Custom Script #15654

hackiero opened this issue Apr 8, 2024 · 0 comments · Fixed by #15708
Assignees
Labels
severity: low Does not significantly disrupt application functionality, or a workaround is available status: accepted This issue has been accepted for implementation type: bug A confirmed report of unexpected behavior in the application

Comments

@hackiero
Copy link

hackiero commented Apr 8, 2024

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()
>>>
@hackiero hackiero added status: needs triage This issue is awaiting triage by a maintainer type: bug A confirmed report of unexpected behavior in the application labels Apr 8, 2024
@arthanson arthanson removed their assignment Apr 8, 2024
@arthanson arthanson added status: needs owner This issue is tentatively accepted pending a volunteer committed to its implementation severity: low Does not significantly disrupt application functionality, or a workaround is available and removed status: needs triage This issue is awaiting triage by a maintainer labels Apr 8, 2024
@arthanson arthanson self-assigned this Apr 11, 2024
@arthanson arthanson removed the status: needs owner This issue is tentatively accepted pending a volunteer committed to its implementation label Apr 11, 2024
@jeremystretch jeremystretch added the status: accepted This issue has been accepted for implementation label Apr 15, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
severity: low Does not significantly disrupt application functionality, or a workaround is available status: accepted This issue has been accepted for implementation type: bug A confirmed report of unexpected behavior in the application
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants