diff --git a/netbox/extras/api/serializers.py b/netbox/extras/api/serializers.py index c5ee1faba..c22841f4e 100644 --- a/netbox/extras/api/serializers.py +++ b/netbox/extras/api/serializers.py @@ -73,7 +73,7 @@ class EventRuleSerializer(NetBoxModelSerializer): model = EventRule fields = [ 'id', 'url', 'display', 'content_types', 'name', 'type_create', 'type_update', 'type_delete', - 'type_job_start', 'type_job_end', 'enabled', 'conditions', 'event_type', + 'type_job_start', 'type_job_end', 'enabled', 'conditions', 'action_type', 'custom_fields', 'tags', 'created', 'last_updated', ] diff --git a/netbox/extras/choices.py b/netbox/extras/choices.py index 447747382..df3b47d9e 100644 --- a/netbox/extras/choices.py +++ b/netbox/extras/choices.py @@ -286,7 +286,7 @@ class DashboardWidgetColorChoices(ChoiceSet): # Event Rules # -class EventRuleTypeChoices(ChoiceSet): +class EventRuleActionChoices(ChoiceSet): WEBHOOK = 'webhook' SCRIPT = 'script' diff --git a/netbox/extras/events_worker.py b/netbox/extras/events_worker.py index a54bf4384..2b6adc17d 100644 --- a/netbox/extras/events_worker.py +++ b/netbox/extras/events_worker.py @@ -7,7 +7,7 @@ from django_rq import job from jinja2.exceptions import TemplateError from .conditions import ConditionSet -from .choices import EventRuleTypeChoices +from .choices import EventRuleActionChoices from .constants import WEBHOOK_EVENT_TYPES from .scripts_worker import process_script from .webhooks import generate_signature @@ -43,9 +43,9 @@ def module_member(name): def process_event_rules(event_rule, model_name, event, data, timestamp, username, request_id, snapshots): - if event_rule.event_type == EventRuleTypeChoices.WEBHOOK: + if event_rule.action_type == EventRuleActionChoices.WEBHOOK: process_webhook(event_rule, model_name, event, data, timestamp, username, request_id, snapshots) - elif event_rule.event_type == EventRuleTypeChoicesEventRuleTypeChoices.SCRIPT: + elif event_rule.action_type == EventRuleActionChoicesEventRuleTypeChoices.SCRIPT: process_script(event_rule, model_name, event, data, timestamp, username, request_id, snapshots) diff --git a/netbox/extras/forms/model_forms.py b/netbox/extras/forms/model_forms.py index a06e500d1..dcdc94675 100644 --- a/netbox/extras/forms/model_forms.py +++ b/netbox/extras/forms/model_forms.py @@ -249,10 +249,27 @@ class EventRuleForm(NetBoxModelForm): limit_choices_to=FeatureQuery('webhooks') ) + # Webhook form fields + # + payload_url = Webhook._meta.get_field('payload_url').formfield() + http_method = Webhook._meta.get_field('http_method').formfield() + http_content_type = Webhook._meta.get_field('http_content_type').formfield() + additional_headers = Webhook._meta.get_field('additional_headers').formfield() + body_template = Webhook._meta.get_field('body_template').formfield() + secret = Webhook._meta.get_field('secret').formfield() + ssl_verification = Webhook._meta.get_field('ssl_verification').formfield() + ca_file_path = Webhook._meta.get_field('ca_file_path').formfield() + fieldsets = ( (_('EventRule'), ('name', 'content_types', 'enabled', 'tags')), (_('Events'), ('type_create', 'type_update', 'type_delete', 'type_job_start', 'type_job_end')), (_('Conditions'), ('conditions',)), + (_('Action'), ('action_type',)), + + (_('HTTP Request'), ( + 'payload_url', 'http_method', 'http_content_type', 'additional_headers', 'body_template', 'secret', + )), + (_('SSL'), ('ssl_verification', 'ca_file_path')), ) class Meta: @@ -269,6 +286,27 @@ class EventRuleForm(NetBoxModelForm): 'conditions': forms.Textarea(attrs={'class': 'font-monospace'}), } + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + SCRIPT_CHOICES = [ + ( + "Audio", + ( + ("vinyl", "Vinyl"), + ("cd", "CD"), + ), + ), + ( + "Video", + ( + ("vhs", "VHS Tape"), + ("dvd", "DVD"), + ), + ), + ("unknown", "Unknown"), + ] + class TagForm(BootstrapMixin, forms.ModelForm): slug = SlugField() diff --git a/netbox/extras/migrations/0099_eventrule.py b/netbox/extras/migrations/0099_eventrule.py index 838c71818..6bc7e268f 100644 --- a/netbox/extras/migrations/0099_eventrule.py +++ b/netbox/extras/migrations/0099_eventrule.py @@ -25,7 +25,7 @@ def move_webhooks(apps, schema_editor): event.enabled = webhook.enabled event.conditions = webhook.conditions - event.event_type = EventRuleTypeChoices.WEBHOOK + event.action_type = EventRuleActionChoices.WEBHOOK event.object_type_id = ContentType.objects.get_for_model(webhook).id event.object_id = webhook.id event.save() @@ -57,7 +57,7 @@ class Migration(migrations.Migration): ('type_job_end', models.BooleanField(default=False)), ('enabled', models.BooleanField(default=True)), ('conditions', models.JSONField(blank=True, null=True)), - ('event_type', models.CharField(default='webhook', max_length=30)), + ('action_type', models.CharField(default='webhook', max_length=30)), ('object_id', models.PositiveBigIntegerField(blank=True, null=True)), ( 'content_types', @@ -76,6 +76,8 @@ class Migration(migrations.Migration): to='contenttypes.contenttype', ), ), + ('object_identifier', models.CharField(max_length=80, blank=True)), + ('parameters', models.JSONField(blank=True, null=True)), ('tags', taggit.managers.TaggableManager(through='extras.TaggedItem', to='extras.Tag')), ], options={ @@ -130,8 +132,4 @@ class Migration(migrations.Migration): to='contenttypes.contenttype', ), ), - migrations.AlterUniqueTogether( - name='eventrule', - unique_together={('object_type', 'object_id')}, - ), ] diff --git a/netbox/extras/models/models.py b/netbox/extras/models/models.py index 137b2cb57..bd16a043d 100644 --- a/netbox/extras/models/models.py +++ b/netbox/extras/models/models.py @@ -93,17 +93,16 @@ class EventRule(CustomFieldsMixin, ExportTemplatesMixin, TagsMixin, ChangeLogged help_text=_("A set of conditions which determine whether the event will be generated.") ) - event_type = models.CharField( + # Action to take + action_type = models.CharField( max_length=30, - choices=EventRuleTypeChoices, - default=EventRuleTypeChoices.WEBHOOK, + choices=EventRuleActionChoices, + default=EventRuleActionChoices.WEBHOOK, verbose_name=_('event type') ) - # Action to take object_type = models.ForeignKey( to=ContentType, related_name='eventrule_actions', - # limit_choices_to=EVENT_TYPE_MODELS, on_delete=models.CASCADE, ) object_id = models.PositiveBigIntegerField( @@ -115,11 +114,22 @@ class EventRule(CustomFieldsMixin, ExportTemplatesMixin, TagsMixin, ChangeLogged fk_field='object_id', ) + # internal (not show in UI) - used by scripts to store function name + object_identifier = models.CharField( + max_length=80, + blank=True + ) + parameters = models.JSONField( + verbose_name=_('parameters'), + blank=True, + null=True, + help_text=_("Parameters to pass to the action.") + ) + class Meta: ordering = ('name',) verbose_name = _('eventrule') verbose_name_plural = _('eventrules') - unique_together = ('object_type', 'object_id') def __str__(self): return self.name diff --git a/netbox/extras/tables/tables.py b/netbox/extras/tables/tables.py index 0fc53cfd7..90cb4eec1 100644 --- a/netbox/extras/tables/tables.py +++ b/netbox/extras/tables/tables.py @@ -297,8 +297,8 @@ class EventRuleTable(NetBoxTable): verbose_name=_('Name'), linkify=True ) - event_type = tables.Column( - verbose_name=_('Event Type'), + action_type = tables.Column( + verbose_name=_('Action Type'), ) content_types = columns.ContentTypesColumn( verbose_name=_('Content Types'), @@ -328,11 +328,11 @@ class EventRuleTable(NetBoxTable): class Meta(NetBoxTable.Meta): model = EventRule fields = ( - 'pk', 'id', 'name', 'event_type', 'content_types', 'enabled', 'type_create', 'type_update', 'type_delete', + 'pk', 'id', 'name', 'action_type', 'content_types', 'enabled', 'type_create', 'type_update', 'type_delete', 'type_job_start', 'type_job_end', 'tags', 'created', 'last_updated', ) default_columns = ( - 'pk', 'name', 'event_type', 'content_types', 'enabled', 'type_create', 'type_update', 'type_delete', 'type_job_start', + 'pk', 'name', 'action_type', 'content_types', 'enabled', 'type_create', 'type_update', 'type_delete', 'type_job_start', 'type_job_end', )