14132 misc changes

This commit is contained in:
Arthur 2023-11-08 07:14:23 -08:00
parent 7cb704db47
commit 9c204322e2
7 changed files with 67 additions and 21 deletions

View File

@ -73,7 +73,7 @@ class EventRuleSerializer(NetBoxModelSerializer):
model = EventRule model = EventRule
fields = [ fields = [
'id', 'url', 'display', 'content_types', 'name', 'type_create', 'type_update', 'type_delete', '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', 'custom_fields', 'tags', 'created', 'last_updated',
] ]

View File

@ -286,7 +286,7 @@ class DashboardWidgetColorChoices(ChoiceSet):
# Event Rules # Event Rules
# #
class EventRuleTypeChoices(ChoiceSet): class EventRuleActionChoices(ChoiceSet):
WEBHOOK = 'webhook' WEBHOOK = 'webhook'
SCRIPT = 'script' SCRIPT = 'script'

View File

@ -7,7 +7,7 @@ from django_rq import job
from jinja2.exceptions import TemplateError from jinja2.exceptions import TemplateError
from .conditions import ConditionSet from .conditions import ConditionSet
from .choices import EventRuleTypeChoices from .choices import EventRuleActionChoices
from .constants import WEBHOOK_EVENT_TYPES from .constants import WEBHOOK_EVENT_TYPES
from .scripts_worker import process_script from .scripts_worker import process_script
from .webhooks import generate_signature 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): 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) 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) process_script(event_rule, model_name, event, data, timestamp, username, request_id, snapshots)

View File

@ -249,10 +249,27 @@ class EventRuleForm(NetBoxModelForm):
limit_choices_to=FeatureQuery('webhooks') 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 = ( fieldsets = (
(_('EventRule'), ('name', 'content_types', 'enabled', 'tags')), (_('EventRule'), ('name', 'content_types', 'enabled', 'tags')),
(_('Events'), ('type_create', 'type_update', 'type_delete', 'type_job_start', 'type_job_end')), (_('Events'), ('type_create', 'type_update', 'type_delete', 'type_job_start', 'type_job_end')),
(_('Conditions'), ('conditions',)), (_('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: class Meta:
@ -269,6 +286,27 @@ class EventRuleForm(NetBoxModelForm):
'conditions': forms.Textarea(attrs={'class': 'font-monospace'}), '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): class TagForm(BootstrapMixin, forms.ModelForm):
slug = SlugField() slug = SlugField()

View File

@ -25,7 +25,7 @@ def move_webhooks(apps, schema_editor):
event.enabled = webhook.enabled event.enabled = webhook.enabled
event.conditions = webhook.conditions 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_type_id = ContentType.objects.get_for_model(webhook).id
event.object_id = webhook.id event.object_id = webhook.id
event.save() event.save()
@ -57,7 +57,7 @@ class Migration(migrations.Migration):
('type_job_end', models.BooleanField(default=False)), ('type_job_end', models.BooleanField(default=False)),
('enabled', models.BooleanField(default=True)), ('enabled', models.BooleanField(default=True)),
('conditions', models.JSONField(blank=True, null=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)), ('object_id', models.PositiveBigIntegerField(blank=True, null=True)),
( (
'content_types', 'content_types',
@ -76,6 +76,8 @@ class Migration(migrations.Migration):
to='contenttypes.contenttype', 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')), ('tags', taggit.managers.TaggableManager(through='extras.TaggedItem', to='extras.Tag')),
], ],
options={ options={
@ -130,8 +132,4 @@ class Migration(migrations.Migration):
to='contenttypes.contenttype', to='contenttypes.contenttype',
), ),
), ),
migrations.AlterUniqueTogether(
name='eventrule',
unique_together={('object_type', 'object_id')},
),
] ]

View File

@ -93,17 +93,16 @@ class EventRule(CustomFieldsMixin, ExportTemplatesMixin, TagsMixin, ChangeLogged
help_text=_("A set of conditions which determine whether the event will be generated.") 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, max_length=30,
choices=EventRuleTypeChoices, choices=EventRuleActionChoices,
default=EventRuleTypeChoices.WEBHOOK, default=EventRuleActionChoices.WEBHOOK,
verbose_name=_('event type') verbose_name=_('event type')
) )
# Action to take
object_type = models.ForeignKey( object_type = models.ForeignKey(
to=ContentType, to=ContentType,
related_name='eventrule_actions', related_name='eventrule_actions',
# limit_choices_to=EVENT_TYPE_MODELS,
on_delete=models.CASCADE, on_delete=models.CASCADE,
) )
object_id = models.PositiveBigIntegerField( object_id = models.PositiveBigIntegerField(
@ -115,11 +114,22 @@ class EventRule(CustomFieldsMixin, ExportTemplatesMixin, TagsMixin, ChangeLogged
fk_field='object_id', 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: class Meta:
ordering = ('name',) ordering = ('name',)
verbose_name = _('eventrule') verbose_name = _('eventrule')
verbose_name_plural = _('eventrules') verbose_name_plural = _('eventrules')
unique_together = ('object_type', 'object_id')
def __str__(self): def __str__(self):
return self.name return self.name

View File

@ -297,8 +297,8 @@ class EventRuleTable(NetBoxTable):
verbose_name=_('Name'), verbose_name=_('Name'),
linkify=True linkify=True
) )
event_type = tables.Column( action_type = tables.Column(
verbose_name=_('Event Type'), verbose_name=_('Action Type'),
) )
content_types = columns.ContentTypesColumn( content_types = columns.ContentTypesColumn(
verbose_name=_('Content Types'), verbose_name=_('Content Types'),
@ -328,11 +328,11 @@ class EventRuleTable(NetBoxTable):
class Meta(NetBoxTable.Meta): class Meta(NetBoxTable.Meta):
model = EventRule model = EventRule
fields = ( 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', 'type_job_start', 'type_job_end', 'tags', 'created', 'last_updated',
) )
default_columns = ( 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', 'type_job_end',
) )