mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-17 21:18:16 -06:00
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 logging
|
||||||
|
import json
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.contrib.auth import get_user_model
|
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 netbox.registry import registry
|
||||||
from utilities.api import get_serializer_for_model
|
from utilities.api import get_serializer_for_model
|
||||||
from utilities.rqworker import get_rq_retry
|
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 .choices import *
|
||||||
from .models import EventRule, ScriptModule
|
from .models import EventRule, ScriptModule
|
||||||
|
|
||||||
@ -119,6 +120,9 @@ def process_event_rules(event_rules, model_name, event, data, username=None, sna
|
|||||||
script_name = event_rule.action_parameters['script_name']
|
script_name = event_rule.action_parameters['script_name']
|
||||||
script = script_module.scripts[script_name]()
|
script = script_module.scripts[script_name]()
|
||||||
|
|
||||||
|
# Process Action Data
|
||||||
|
|
||||||
|
if event_rule.action_data:
|
||||||
context = {
|
context = {
|
||||||
'event': event,
|
'event': event,
|
||||||
'timestamp': timezone.now().isoformat(),
|
'timestamp': timezone.now().isoformat(),
|
||||||
@ -127,6 +131,10 @@ def process_event_rules(event_rules, model_name, event, data, username=None, sna
|
|||||||
'request_id': request_id,
|
'request_id': request_id,
|
||||||
'model': data,
|
'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
|
# Enqueue a Job to record the script's execution
|
||||||
Job.enqueue(
|
Job.enqueue(
|
||||||
@ -134,7 +142,7 @@ def process_event_rules(event_rules, model_name, event, data, username=None, sna
|
|||||||
instance=script_module,
|
instance=script_module,
|
||||||
name=script.class_name,
|
name=script.class_name,
|
||||||
user=user,
|
user=user,
|
||||||
data=event_rule.render_script_data(context)
|
data=data
|
||||||
)
|
)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
@ -172,15 +172,6 @@ class EventRule(CustomFieldsMixin, ExportTemplatesMixin, TagsMixin, ChangeLogged
|
|||||||
|
|
||||||
return ConditionSet(self.conditions).eval(data)
|
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):
|
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)
|
module = ScriptModule.objects.get(pk=job.object_id)
|
||||||
script = module.scripts.get(job.name)()
|
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 = logging.getLogger(f"netbox.scripts.{script.full_name}")
|
||||||
logger.info(f"Running script (commit={commit})")
|
logger.info(f"Running script (commit={commit})")
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user