#14132: Simplify form logic for script EventRules

This commit is contained in:
Jeremy Stretch 2023-12-04 10:57:29 -05:00
parent cfc20f910e
commit 5d57e9863d
5 changed files with 23 additions and 24 deletions

View File

@ -86,7 +86,7 @@ class EventRuleSerializer(NetBoxModelSerializer):
context = {'request': self.context['request']} context = {'request': self.context['request']}
# We need to manually instantiate the serializer for scripts # We need to manually instantiate the serializer for scripts
if instance.action_type == EventRuleActionChoices.SCRIPT: if instance.action_type == EventRuleActionChoices.SCRIPT:
module_id, script_name = instance.action_parameters['script_choice'].split(":", maxsplit=1) script_name = instance.action_parameters['script_name']
script = instance.action_object.scripts[script_name]() script = instance.action_object.scripts[script_name]()
return NestedScriptSerializer(script, context=context).data return NestedScriptSerializer(script, context=context).data
else: else:

View File

@ -116,7 +116,7 @@ def process_event_rules(event_rules, model_name, event, data, username, snapshot
elif event_rule.action_type == EventRuleActionChoices.SCRIPT: elif event_rule.action_type == EventRuleActionChoices.SCRIPT:
# Resolve the script from action parameters # Resolve the script from action parameters
script_module = event_rule.action_object script_module = event_rule.action_object
_, script_name = event_rule.action_parameters['script_choice'].split(":", maxsplit=1) script_name = event_rule.action_parameters['script_name']
script = script_module.scripts[script_name]() script = script_module.scripts[script_name]()
# Enqueue a Job to record the script's execution # Enqueue a Job to record the script's execution

View File

@ -179,12 +179,14 @@ class EventRuleImportForm(NetBoxModelImportForm):
action_object = self.cleaned_data.get('action_object') action_object = self.cleaned_data.get('action_object')
action_type = self.cleaned_data.get('action_type') action_type = self.cleaned_data.get('action_type')
if action_object and action_type: if action_object and action_type:
# Webhook
if action_type == EventRuleActionChoices.WEBHOOK: if action_type == EventRuleActionChoices.WEBHOOK:
try: try:
webhook = Webhook.objects.get(name=action_object) webhook = Webhook.objects.get(name=action_object)
except Webhook.ObjectDoesNotExist: except Webhook.DoesNotExist:
raise forms.ValidationError(f"Webhook {action_object} not found") raise forms.ValidationError(f"Webhook {action_object} not found")
self.instance.action_object = webhook self.instance.action_object = webhook
# Script
elif action_type == EventRuleActionChoices.SCRIPT: elif action_type == EventRuleActionChoices.SCRIPT:
from extras.scripts import get_module_and_script from extras.scripts import get_module_and_script
module_name, script_name = action_object.split('.', 1) module_name, script_name = action_object.split('.', 1)
@ -195,9 +197,7 @@ class EventRuleImportForm(NetBoxModelImportForm):
self.instance.action_object = module self.instance.action_object = module
self.instance.action_object_type = ContentType.objects.get_for_model(module, for_concrete_model=False) self.instance.action_object_type = ContentType.objects.get_for_model(module, for_concrete_model=False)
self.instance.action_parameters = { self.instance.action_parameters = {
'script_choice': f"{str(module.pk)}:{script_name}", 'script_name': script_name,
'script_name': script.name,
'script_full_name': script.full_name,
} }

View File

@ -288,16 +288,15 @@ class EventRuleForm(NetBoxModelForm):
for script_name in module.scripts.keys(): for script_name in module.scripts.keys():
name = f"{str(module.pk)}:{script_name}" name = f"{str(module.pk)}:{script_name}"
scripts.append((name, script_name)) scripts.append((name, script_name))
if scripts: if scripts:
choices.append((str(module), scripts)) choices.append((str(module), scripts))
self.fields['action_choice'].choices = choices self.fields['action_choice'].choices = choices
parameters = get_field_value(self, 'action_parameters')
initial = None if self.instance.pk:
if parameters and 'script_choice' in parameters: scriptmodule_id = self.instance.action_object_id
initial = parameters['script_choice'] script_name = self.instance.action_parameters.get('script_name')
self.fields['action_choice'].initial = initial self.fields['action_choice'].initial = f'{scriptmodule_id}:{script_name}'
print(self.fields['action_choice'].initial)
def init_webhook_choice(self): def init_webhook_choice(self):
initial = None initial = None
@ -327,19 +326,20 @@ class EventRuleForm(NetBoxModelForm):
super().clean() super().clean()
action_choice = self.cleaned_data.get('action_choice') action_choice = self.cleaned_data.get('action_choice')
# Webhook
if self.cleaned_data.get('action_type') == EventRuleActionChoices.WEBHOOK: if self.cleaned_data.get('action_type') == EventRuleActionChoices.WEBHOOK:
self.cleaned_data['action_object_type'] = ContentType.objects.get_for_model(action_choice) self.cleaned_data['action_object_type'] = ContentType.objects.get_for_model(action_choice)
self.cleaned_data['action_object_id'] = action_choice.id self.cleaned_data['action_object_id'] = action_choice.id
# Script
elif self.cleaned_data.get('action_type') == EventRuleActionChoices.SCRIPT: elif self.cleaned_data.get('action_type') == EventRuleActionChoices.SCRIPT:
self.cleaned_data['action_object_type'] = ContentType.objects.get_for_model(
ScriptModule,
for_concrete_model=False
)
module_id, script_name = action_choice.split(":", maxsplit=1) module_id, script_name = action_choice.split(":", maxsplit=1)
script_module = ScriptModule.objects.get(pk=module_id) self.cleaned_data['action_object_id'] = module_id
self.cleaned_data['action_object_type'] = ContentType.objects.get_for_model(script_module, for_concrete_model=False)
self.cleaned_data['action_object_id'] = script_module.id
script = script_module.scripts[script_name]()
self.cleaned_data['action_parameters'] = { self.cleaned_data['action_parameters'] = {
'script_choice': action_choice, 'script_name': script_name,
'script_name': script.name,
'script_full_name': script.full_name,
} }
return self.cleaned_data return self.cleaned_data

View File

@ -115,16 +115,15 @@ class EventRule(CustomFieldsMixin, ExportTemplatesMixin, TagsMixin, ChangeLogged
ct_field='action_object_type', ct_field='action_object_type',
fk_field='action_object_id' fk_field='action_object_id'
) )
# internal (not show in UI) - used by scripts to store function name
action_parameters = models.JSONField( action_parameters = models.JSONField(
blank=True, blank=True,
null=True, null=True
) )
action_data = models.JSONField( action_data = models.JSONField(
verbose_name=_('parameters'), verbose_name=_('data'),
blank=True, blank=True,
null=True, null=True,
help_text=_("Parameters to pass to the action.") help_text=_("Additional data to pass to the action object")
) )
comments = models.TextField( comments = models.TextField(
verbose_name=_('comments'), verbose_name=_('comments'),