From 954d6a7c03e309e32b8181308b961afe5ee7c157 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Mon, 24 Feb 2020 20:42:24 -0500 Subject: [PATCH] Add http_method field to Webhook --- netbox/extras/admin.py | 16 ++++++++++---- netbox/extras/choices.py | 21 +++++++++++++++++++ .../0038_webhook_template_support.py | 5 +++++ netbox/extras/models.py | 6 ++++++ netbox/extras/webhooks_worker.py | 6 ++++-- 5 files changed, 48 insertions(+), 6 deletions(-) diff --git a/netbox/extras/admin.py b/netbox/extras/admin.py index 9a75fb53f..7122f3842 100644 --- a/netbox/extras/admin.py +++ b/netbox/extras/admin.py @@ -47,16 +47,24 @@ class WebhookAdmin(admin.ModelAdmin): form = WebhookForm fieldsets = ( (None, { - 'fields': ('name', 'obj_type', 'enabled') + 'fields': ( + 'name', 'obj_type', 'enabled', + ) }), ('Events', { - 'fields': ('type_create', 'type_update', 'type_delete') + 'fields': ( + 'type_create', 'type_update', 'type_delete', + ) }), ('HTTP Request', { - 'fields': ('payload_url', 'http_content_type', 'additional_headers', 'body_template', 'secret') + 'fields': ( + 'http_method', 'payload_url', 'http_content_type', 'additional_headers', 'body_template', 'secret', + ) }), ('SSL', { - 'fields': ('ssl_verification', 'ca_file_path') + 'fields': ( + 'ssl_verification', 'ca_file_path', + ) }) ) diff --git a/netbox/extras/choices.py b/netbox/extras/choices.py index 434461e76..9811cc0b0 100644 --- a/netbox/extras/choices.py +++ b/netbox/extras/choices.py @@ -118,3 +118,24 @@ class TemplateLanguageChoices(ChoiceSet): LANGUAGE_DJANGO: 10, LANGUAGE_JINJA2: 20, } + + +# +# Webhooks +# + +class WebhookHttpMethodChoices(ChoiceSet): + + METHOD_GET = 'GET' + METHOD_POST = 'POST' + METHOD_PUT = 'PUT' + METHOD_PATCH = 'PATCH' + METHOD_DELETE = 'DELETE' + + CHOICES = ( + (METHOD_GET, 'GET'), + (METHOD_POST, 'POST'), + (METHOD_PUT, 'PUT'), + (METHOD_PATCH, 'PATCH'), + (METHOD_DELETE, 'DELETE'), + ) diff --git a/netbox/extras/migrations/0038_webhook_template_support.py b/netbox/extras/migrations/0038_webhook_template_support.py index 80a1d2b7d..7d563820f 100644 --- a/netbox/extras/migrations/0038_webhook_template_support.py +++ b/netbox/extras/migrations/0038_webhook_template_support.py @@ -21,6 +21,11 @@ class Migration(migrations.Migration): ] operations = [ + migrations.AddField( + model_name='webhook', + name='http_method', + field=models.CharField(default='POST', max_length=30), + ), migrations.AddField( model_name='webhook', name='body_template', diff --git a/netbox/extras/models.py b/netbox/extras/models.py index 94392189d..eaeface6b 100644 --- a/netbox/extras/models.py +++ b/netbox/extras/models.py @@ -85,6 +85,12 @@ class Webhook(models.Model): enabled = models.BooleanField( default=True ) + http_method = models.CharField( + max_length=30, + choices=WebhookHttpMethodChoices, + default=WebhookHttpMethodChoices.METHOD_POST, + verbose_name='HTTP method' + ) http_content_type = models.CharField( max_length=100, default=HTTP_CONTENT_TYPE_JSON, diff --git a/netbox/extras/webhooks_worker.py b/netbox/extras/webhooks_worker.py index 5513915ce..1b1b76dd9 100644 --- a/netbox/extras/webhooks_worker.py +++ b/netbox/extras/webhooks_worker.py @@ -43,13 +43,15 @@ def process_webhook(webhook, data, model_name, event, timestamp, username, reque # Prepare the HTTP request params = { - 'method': 'POST', + 'method': webhook.http_method, 'url': webhook.payload_url, 'headers': headers, 'data': body, } logger.info( - "Sending webhook to {}: {} {}".format(params['url'], context['model'], context['event']) + "Sending {} request to {} ({} {})".format( + params['method'], params['url'], context['model'], context['event'] + ) ) logger.debug(params) try: