14132 review changes

This commit is contained in:
Arthur 2023-11-21 08:27:52 -08:00
parent b3a702215a
commit 56a9210d6c
3 changed files with 39 additions and 6 deletions

View File

@ -13,7 +13,6 @@ from django.utils.translation import gettext as _
from core.choices import JobStatusChoices from core.choices import JobStatusChoices
from core.models import ContentType from core.models import ContentType
from extras.constants import EVENT_JOB_END, EVENT_JOB_START from extras.constants import EVENT_JOB_END, EVENT_JOB_START
from extras.utils import process_event_rules
from netbox.config import get_config from netbox.config import get_config
from netbox.constants import RQ_QUEUE_DEFAULT from netbox.constants import RQ_QUEUE_DEFAULT
from utilities.querysets import RestrictedQuerySet from utilities.querysets import RestrictedQuerySet
@ -230,6 +229,7 @@ class Job(models.Model):
Process any EventRules relevant to the passed job event (i.e. start or stop). Process any EventRules relevant to the passed job event (i.e. start or stop).
""" """
from extras.models import EventRule from extras.models import EventRule
from extras.events import process_event_rules
# Fetch any event rules matching this object type and action # Fetch any event rules matching this object type and action
event_rules = EventRule.objects.filter( event_rules = EventRule.objects.filter(

View File

@ -9,7 +9,6 @@ from utilities.api import get_serializer_for_model
from utilities.utils import serialize_object from utilities.utils import serialize_object
from .choices import * from .choices import *
from .models import EventRule from .models import EventRule
from .utils import process_event_rules
logger = logging.getLogger('netbox.events_processor') logger = logging.getLogger('netbox.events_processor')
@ -64,6 +63,40 @@ def enqueue_object(queue, instance, user, request_id, action):
}) })
def process_event_rules(event_rules, model_name, event, data, username, snapshots=None, request_id=None):
rq_queue_name = get_config().QUEUE_MAPPINGS.get('webhook', RQ_QUEUE_DEFAULT)
rq_queue = get_queue(rq_queue_name)
for event_rule in event_rules:
if event_rule.action_type == EventRuleActionChoices.WEBHOOK:
processor = "extras.webhooks_worker.process_webhook"
elif event_rule.action_type == EventRuleActionChoices.SCRIPT:
processor = "extras.scripts_worker.process_script"
else:
raise ValueError(f"Unknown action type for an event rule: {event_rule.action_type}")
params = {
"event_rule": event_rule,
"model_name": model_name,
"event": event,
"data": data,
"snapshots": snapshots,
"timestamp": str(timezone.now()),
"username": username,
"retry": get_rq_retry()
}
if snapshots:
params["snapshots"] = snapshots
if request_id:
params["request_id"] = request_id
rq_queue.enqueue(
processor,
**params
)
def process_event_queue(events): def process_event_queue(events):
""" """
Flush a list of object representation to RQ for EventRule processing. Flush a list of object representation to RQ for EventRule processing.

View File

@ -284,7 +284,7 @@ class EventRuleForm(NetBoxModelForm):
'action_parameters': forms.HiddenInput, 'action_parameters': forms.HiddenInput,
} }
def get_script_choices(self): def init_script_choice(self):
choices = [] choices = []
for module in ScriptModule.objects.all(): for module in ScriptModule.objects.all():
scripts = [] scripts = []
@ -298,7 +298,7 @@ class EventRuleForm(NetBoxModelForm):
self.fields['action_choice'].choices = choices self.fields['action_choice'].choices = choices
self.fields['action_choice'].initial = get_field_value(self, 'action_parameters') self.fields['action_choice'].initial = get_field_value(self, 'action_parameters')
def get_webhook_choices(self): def init_webhook_choice(self):
initial = None initial = None
if self.fields['action_object_type'] and get_field_value(self, 'action_object_id'): if self.fields['action_object_type'] and get_field_value(self, 'action_object_id'):
initial = Webhook.objects.get(pk=get_field_value(self, 'action_object_id')) initial = Webhook.objects.get(pk=get_field_value(self, 'action_object_id'))
@ -318,9 +318,9 @@ class EventRuleForm(NetBoxModelForm):
action_type = get_field_value(self, 'action_type') action_type = get_field_value(self, 'action_type')
if action_type == EventRuleActionChoices.WEBHOOK: if action_type == EventRuleActionChoices.WEBHOOK:
self.get_webhook_choices() self.init_webhook_choice()
elif action_type == EventRuleActionChoices.SCRIPT: elif action_type == EventRuleActionChoices.SCRIPT:
self.get_script_choices() self.init_script_choice()
val = get_field_value(self, 'conditions') val = get_field_value(self, 'conditions')