netbox-community / netbox

The premier source of truth powering network automation. Open source under Apache 2. Try NetBox Cloud free: https://netboxlabs.com/free-netbox-cloud/
http://netboxlabs.com/oss/netbox/
Apache License 2.0
15.91k stars 2.55k forks source link

Event Rule Action Data passed to Script #14884

Open renatoalmeidaoliveira opened 8 months ago

renatoalmeidaoliveira commented 8 months ago

NetBox version

v3.7.0

Feature type

Change to existing functionality

Proposed functionality

Currently EventRules passes a serialized version of the model as data to the linked Script, I think it would be better if the Action Data was processed like the Body Template of Webhoocks and passed to the Script.

Use case

It would allow users to build Script to react NetBox changes with the change context, currently the user cannot get directly the changelog. And by passing a parsed data with Event Data gives the plugin developer a lot of flexibility to build automations

Database changes

None

External dependencies

None

jeremystretch commented 8 months ago

I don't think we have a mechanism in place within a script right now to accept this data. Could you please expand your proposal above to specify exactly how a script would receive this data, in addition to the form data it currently accepts?

renatoalmeidaoliveira commented 8 months ago

In the current implementation the EventRule passes a serialized version of the instance as data to the script., so it isn't possible to pass any input to the Script using the EventRule. For example: The user wants that for every interface change a Script runs and perform Napalm checks on the interface, to implement that the user might need the Interface, the Device and the changes performed by the end user. But in the current implementation is only receives the changed object and the way it is passed to the Script it can't even be used as Script Inputs. The feature request aims to give the user an interface to setup what data should be passed to the Script when the changes happens

renatoalmeidaoliveira commented 8 months ago

In run_script data is passed as a dict where the keys are the Script Input, but when the Job is schedules in process_event_rules it passed as built in enqueue_object, (the serialized model) and that way it ain't gonna pass the expected dict to the Script since its expecting a dict with keys as the Script Inputs. I think that the Action Data (or any other field) could be used similar as the Templete body of webhooks using something like rende_body

DanSheps commented 8 months ago

I think I get what this is saying.

Basically, you can define your data template for what is passed to a script.

This is originally what I thought "action data" wass for.

renatoalmeidaoliveira commented 8 months ago

@DanSheps I thougt that it was the purpose of "action data" too. And when I look to the rest of the implementation it makes sense for me change the context injected to the template body in the webhook to whatever the user wants to setup inside the action data

jeremystretch commented 7 months ago

I've assigned this to @renatoalmeidaoliveira with the understanding that its scope is limited to passing the action_data associated with an event rule directly to the script being executed, as if it were user input.

jeremystretch commented 4 months ago

@renatoalmeidaoliveira are you still planning to work on this?

renatoalmeidaoliveira commented 4 months ago

@jeremystretch yes, I've talked with @arthanson in slack and waiting for some guidance to move on

renatoalmeidaoliveira commented 4 months ago

From the Script Developer point of view I think that Action Data should be passed to the Script the same way as the Input Form and REST API. That means the only sending the blob JSON isn't a good idea

jeffgdotorg commented 2 months ago

@renatoalmeidaoliveira I notice your PR got robo-closed. Do you anticipate having time to work on it soon, or should I let it go?

misch42 commented 1 month ago

I'd like to see that feature, too.

renatoalmeidaoliveira commented 1 month ago

@jeffgdotorg I think we need to discuss a bit more about that PR, because the maintainer team wants the sent the data field as is without Jinja2 processing but IMO that isn't very usefull for the end user since if he wanna a static field he could just hard code in the Script. And IMO the Script interface should remain the same independent of what calls it meaning that data passed to the script should be processed and parsed by the Script form. I have time to work on that PR, but I would like to wait for a consense before giving more effort to it.