Skip to content

Conversation

@abhi1693
Copy link
Member

Fixes: #14852

Copy link
Member

@jeremystretch jeremystretch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than handling this manually, a generic relation should be added to the ScriptModule class to automate the cascading deletion:

event_rules = GenericRelation(
    to='extras.EventRule',
    content_type_field='action_object_type',
    object_id_field='action_object_id',
    for_concrete_model=False
)

This ensures that the user is informed that the event rule will be deleted when confirming deletion of the script module.

@abhi1693
Copy link
Member Author

I have added the events_rules like suggested but there is an issue that I'm facing with the code. The handle_deleted_object signal gets called when I try to delete the script and the code fails with the following error

  File "/home/asaharan/PycharmProjects/netbox/netbox/extras/api/serializers.py", line 90, in get_action_object
    script = instance.action_object.scripts[script_name]()
KeyError: 'AddDeviceTypeComponents'

I checked the that instance.action_object points to the ScriptModule but calling scripts just returns an empty dictionary which is the reason for the KeyError. How do you want me to handle this? This issue was not with the delete overide I did though

@arthanson
Copy link
Collaborator

@abhi1693 I haven't dug too deeply into this bug, but normally scripts should have the script_name in it, however if this is from the delete case I can think of two potential solutions:

  1. in the serializer check if script_name is in scripts and if not return a null object for the nested serializer.
  2. I think you said this comes from the call to handle_deleted_object? If that is the case then possibly not do the serializer call if there are no script objects? (not sure on this one).

If you could message me the repro and the line of code in the handle_deleted_object where this is getting called it would help to get context. I think the main issue is why is scripts returning none if the action_parameter is set, which shouldn't really be happening. If the scripts is returning none from deletion. then the action_parameter should be empty as well. The action_parameter is the name of the actual method to call as the action_object just points to the script model and not the actual method to call. I hope that helps.

@abhi1693
Copy link
Member Author

@arthanson The repro steps are simple:

  1. Add the code Jeremy has suggested
  2. Upload a script. I chose add_device_type_components
  3. Create an event rule with this script (choose whatever required options)
  4. Then try deleting the script. You will get a nice widget showing the event and the relationship will be deleted, proceed further and you will get hit with the issue

@arthanson
Copy link
Collaborator

closing as addressed in #15888

@arthanson arthanson closed this Apr 29, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 30, 2024
@jeremystretch jeremystretch deleted the fix/14852-event-rule-script branch June 20, 2024 14:58
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Event Rule breaks when script is deleted

4 participants