diff --git a/netbox/extras/events.py b/netbox/extras/events.py index c50f4488d..0c7bd09b5 100644 --- a/netbox/extras/events.py +++ b/netbox/extras/events.py @@ -119,13 +119,22 @@ def process_event_rules(event_rules, model_name, event, data, username=None, sna script_name = event_rule.action_parameters['script_name'] script = script_module.scripts[script_name]() + context = { + 'event': event, + 'timestamp': timezone.now().isoformat(), + 'model': model_name, + 'username': username, + 'request_id': request_id, + 'model': data, + } + # Enqueue a Job to record the script's execution Job.enqueue( "extras.scripts.run_script", instance=script_module, name=script.class_name, user=user, - data=data + data=event_rule.render_script_data(context) ) else: diff --git a/netbox/extras/models/models.py b/netbox/extras/models/models.py index 4ac36a3ac..46ddc6d39 100644 --- a/netbox/extras/models/models.py +++ b/netbox/extras/models/models.py @@ -171,6 +171,15 @@ class EventRule(CustomFieldsMixin, ExportTemplatesMixin, TagsMixin, ChangeLogged return True return ConditionSet(self.conditions).eval(data) + + def render_script_data(self, context): + """ + Render Script Data, if defined. Otherwise, jump the context as a JSON object. + """ + if self.action_data: + return render_jinja2(str(self.action_data), context) + else: + return json.dumps(context, cls=JSONEncoder) class Webhook(CustomFieldsMixin, ExportTemplatesMixin, TagsMixin, ChangeLoggedModel):