netbox icon indicating copy to clipboard operation
netbox copied to clipboard

Cascades event rule when the associated script is deleted

Open abhi1693 opened this issue 1 year ago • 3 comments

Fixes: #14852

abhi1693 avatar Jan 19 '24 18:01 abhi1693

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

abhi1693 avatar Jan 25 '24 16:01 abhi1693

@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.

arthanson avatar Jan 25 '24 17:01 arthanson

@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

abhi1693 avatar Jan 25 '24 17:01 abhi1693

closing as addressed in #15888

arthanson avatar Apr 29 '24 16:04 arthanson