Join changes in process_event_rule and limit to process and send action data to Script
This commit is contained in:
parent
17ad376fce
commit
ae828adc7a
|
@ -1,4 +1,5 @@
|
|||
import logging
|
||||
import json
|
||||
|
||||
from django.conf import settings
|
||||
from django.contrib.auth import get_user_model
|
||||
|
@ -14,7 +15,7 @@ from netbox.constants import RQ_QUEUE_DEFAULT
|
|||
from netbox.registry import registry
|
||||
from utilities.api import get_serializer_for_model
|
||||
from utilities.rqworker import get_rq_retry
|
||||
from utilities.utils import serialize_object
|
||||
from utilities.utils import serialize_object, render_jinja2
|
||||
from .choices import *
|
||||
from .models import EventRule, ScriptModule
|
||||
|
||||
|
@ -119,14 +120,21 @@ 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,
|
||||
}
|
||||
# Process Action Data
|
||||
|
||||
if event_rule.action_data:
|
||||
context = {
|
||||
'event': event,
|
||||
'timestamp': timezone.now().isoformat(),
|
||||
'model': model_name,
|
||||
'username': username,
|
||||
'request_id': request_id,
|
||||
'model': data,
|
||||
}
|
||||
rendered_data = render_jinja2(json.dumps(event_rule.action_data), context)
|
||||
form = script.as_form(json.loads(rendered_data))
|
||||
if form.is_valid():
|
||||
data = form.cleaned_data
|
||||
|
||||
# Enqueue a Job to record the script's execution
|
||||
Job.enqueue(
|
||||
|
@ -134,7 +142,7 @@ def process_event_rules(event_rules, model_name, event, data, username=None, sna
|
|||
instance=script_module,
|
||||
name=script.class_name,
|
||||
user=user,
|
||||
data=event_rule.render_script_data(context)
|
||||
data=data
|
||||
)
|
||||
|
||||
else:
|
||||
|
|
|
@ -172,15 +172,6 @@ class EventRule(CustomFieldsMixin, ExportTemplatesMixin, TagsMixin, ChangeLogged
|
|||
|
||||
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(json.dumps(self.action_data), context)
|
||||
else:
|
||||
return json.dumps(context, cls=JSONEncoder)
|
||||
|
||||
|
||||
class Webhook(CustomFieldsMixin, ExportTemplatesMixin, TagsMixin, ChangeLoggedModel):
|
||||
"""
|
||||
|
|
|
@ -488,12 +488,6 @@ def run_script(data, job, request=None, commit=True, **kwargs):
|
|||
module = ScriptModule.objects.get(pk=job.object_id)
|
||||
script = module.scripts.get(job.name)()
|
||||
|
||||
if isinstance(data, str):
|
||||
data = json.loads(data)
|
||||
form = script.as_form(data)
|
||||
if form.is_valid():
|
||||
data = form.cleaned_data
|
||||
|
||||
logger = logging.getLogger(f"netbox.scripts.{script.full_name}")
|
||||
logger.info(f"Running script (commit={commit})")
|
||||
|
||||
|
|
Loading…
Reference in New Issue