mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-18 13:38:16 -06:00
14132 fix
This commit is contained in:
parent
36285db2ca
commit
4a941775be
71
netbox/extras/events_worker.py
Normal file
71
netbox/extras/events_worker.py
Normal file
@ -0,0 +1,71 @@
|
||||
import logging
|
||||
|
||||
import requests
|
||||
from django.conf import settings
|
||||
from django_rq import job
|
||||
from jinja2.exceptions import TemplateError
|
||||
|
||||
from scripts.workers import process_script
|
||||
from .conditions import ConditionSet
|
||||
from .constants import WEBHOOK_EVENT_TYPES, EVENT_TYPE_MODELS
|
||||
from .webhooks import generate_signature
|
||||
from webhooks_worker import process_webhook
|
||||
|
||||
logger = logging.getLogger('netbox.events_worker')
|
||||
|
||||
|
||||
def eval_conditions(event_rule, data):
|
||||
"""
|
||||
Test whether the given data meets the conditions of the event rule (if any). Return True
|
||||
if met or no conditions are specified.
|
||||
"""
|
||||
if not event_rule.conditions:
|
||||
return True
|
||||
|
||||
logger.debug(f'Evaluating event rule conditions: {event_rule.conditions}')
|
||||
if ConditionSet(event_rule.conditions).eval(data):
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
|
||||
def module_member(name):
|
||||
mod, member = name.rsplit(".", 1)
|
||||
module = import_module(mod)
|
||||
return getattr(module, member)
|
||||
|
||||
|
||||
def process_event_rules(event_rule, model_name, event, data, timestamp, username, request_id):
|
||||
if event_rule.event_type == EVENT_TYPE_MODELS.WEBHOOK:
|
||||
process_webhook(event_rule, model_name, event, data, timestamp, username, request_id)
|
||||
elif event_rule.event_type == EVENT_TYPE_MODELS.SCRIPT:
|
||||
process_script(event_rule, model_name, event, data, timestamp, username, request_id)
|
||||
|
||||
|
||||
@job('default')
|
||||
def process_event(event_rule, model_name, event, data, timestamp, username, request_id=None, snapshots=None):
|
||||
"""
|
||||
Make a POST request to the defined Webhook
|
||||
"""
|
||||
# Evaluate event rule conditions (if any)
|
||||
if not eval_conditions(event_rule, data):
|
||||
return
|
||||
|
||||
# Prepare context data for headers & body templates
|
||||
context = {
|
||||
'event': WEBHOOK_EVENT_TYPES[event],
|
||||
'timestamp': timestamp,
|
||||
'model': model_name,
|
||||
'username': username,
|
||||
'request_id': request_id,
|
||||
'data': data,
|
||||
}
|
||||
if snapshots:
|
||||
context.update({
|
||||
'snapshots': snapshots
|
||||
})
|
||||
|
||||
# process the events pipeline
|
||||
for name in settings.NETBOX_EVENTS_PIPELINE:
|
||||
func = module_member(name)
|
||||
func(event_rule, model_name, event, data, timestamp, username, request_id)
|
20
netbox/extras/scripts_worker.py
Normal file
20
netbox/extras/scripts_worker.py
Normal file
@ -0,0 +1,20 @@
|
||||
import logging
|
||||
|
||||
import requests
|
||||
from django.conf import settings
|
||||
from django_rq import job
|
||||
from jinja2.exceptions import TemplateError
|
||||
|
||||
from .conditions import ConditionSet
|
||||
from .constants import WEBHOOK_EVENT_TYPES
|
||||
from .webhooks import generate_signature
|
||||
|
||||
logger = logging.getLogger('netbox.webhooks_worker')
|
||||
|
||||
|
||||
def process_script(webhook, model_name, event, data, timestamp, username, request_id=None):
|
||||
"""
|
||||
Make a POST request to the defined Webhook
|
||||
"""
|
||||
|
||||
pass
|
Loading…
Reference in New Issue
Block a user