From 568a5b805452f3339b3af02927c0d458d8fb31eb Mon Sep 17 00:00:00 2001 From: Arthur Date: Tue, 21 Nov 2023 09:19:24 -0800 Subject: [PATCH] 14132 change action_parameters to jsonfield --- netbox/extras/forms/model_forms.py | 9 ++++++--- netbox/extras/migrations/0099_eventrule.py | 2 +- netbox/extras/models/models.py | 6 +++--- netbox/extras/scripts_worker.py | 12 ++++++++++-- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/netbox/extras/forms/model_forms.py b/netbox/extras/forms/model_forms.py index da9d31443..c2514629a 100644 --- a/netbox/extras/forms/model_forms.py +++ b/netbox/extras/forms/model_forms.py @@ -296,7 +296,11 @@ class EventRuleForm(NetBoxModelForm): choices.append((str(module), scripts)) self.fields['action_choice'].choices = choices - self.fields['action_choice'].initial = get_field_value(self, 'action_parameters') + parameters = get_field_value(self, 'action_parameters') + initial = None + if parameters and 'script_choice' in parameters: + initial = parameters['script_choice'] + self.fields['action_choice'].initial = initial def init_webhook_choice(self): initial = None @@ -331,12 +335,11 @@ class EventRuleForm(NetBoxModelForm): 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_id'] = action_choice.id - self.cleaned_data['action_parameters'] = '' elif self.cleaned_data.get('action_type') == EventRuleActionChoices.SCRIPT: script = ScriptModule.objects.get(pk=action_choice.split(":")[0]) self.cleaned_data['action_object_type'] = ContentType.objects.get_for_model(script) self.cleaned_data['action_object_id'] = script.id - self.cleaned_data['action_parameters'] = action_choice + self.cleaned_data['action_parameters'] = {'script_choice': action_choice} return self.cleaned_data diff --git a/netbox/extras/migrations/0099_eventrule.py b/netbox/extras/migrations/0099_eventrule.py index cbf65775b..f31affe74 100644 --- a/netbox/extras/migrations/0099_eventrule.py +++ b/netbox/extras/migrations/0099_eventrule.py @@ -60,7 +60,7 @@ class Migration(migrations.Migration): ('conditions', models.JSONField(blank=True, null=True)), ('action_type', models.CharField(default='webhook', max_length=30)), ('action_object_id', models.PositiveBigIntegerField(blank=True, null=True)), - ('action_parameters', models.CharField(blank=True, max_length=80)), + ('action_parameters', models.JSONField(blank=True, null=True)), ('action_data', models.JSONField(blank=True, null=True)), ('comments', models.TextField(blank=True)), ], diff --git a/netbox/extras/models/models.py b/netbox/extras/models/models.py index 12836b04c..a6a459bcb 100644 --- a/netbox/extras/models/models.py +++ b/netbox/extras/models/models.py @@ -118,9 +118,9 @@ class EventRule(CustomFieldsMixin, ExportTemplatesMixin, TagsMixin, ChangeLogged fk_field='action_object_id' ) # internal (not show in UI) - used by scripts to store function name - action_parameters = models.CharField( - max_length=80, - blank=True + action_parameters = models.JSONField( + blank=True, + null=True, ) action_data = models.JSONField( verbose_name=_('parameters'), diff --git a/netbox/extras/scripts_worker.py b/netbox/extras/scripts_worker.py index d8e23b3c9..5e8a24d44 100644 --- a/netbox/extras/scripts_worker.py +++ b/netbox/extras/scripts_worker.py @@ -19,8 +19,16 @@ def process_script(event_rule, data, username, **kwargs): if not event_rule.eval_conditions(data): return - module_id = event_rule.action_parameters.split(":")[0] - script_name = event_rule.action_parameters.split(":")[1] + script_choice = None + if event_rule.action_parameters and 'script_choice' in event_rule_action_parameters: + script_choice = event_rule.action_parameters['script_choice'] + + if script_choice: + module_id = script_choice.split(":")[0] + script_name = script_choice.split(":")[1] + else: + logger.warning(f"event run script - event_rule: {event_rule.id} no script_choice selected") + return try: module = ScriptModule.objects.get(pk=module_id)