From a3cf645dc5b759cc7b32b441a28c3e82515effa0 Mon Sep 17 00:00:00 2001 From: Schylar Utleye Date: Fri, 15 Jan 2021 22:12:57 -0600 Subject: [PATCH] fix webhook initializer yaml and webhook startup script to work with latest netbox release --- initializers/webhooks.yml | 28 ++++++++++++++-------------- startup_scripts/290_webhooks.py | 13 +++++++------ 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/initializers/webhooks.yml b/initializers/webhooks.yml index ba5b777..9d78907 100644 --- a/initializers/webhooks.yml +++ b/initializers/webhooks.yml @@ -10,20 +10,20 @@ ## Examples: # - name: device_creation - # payload_url: 'https://github.com/netbox-community/netbox-docker' - # object_types: - # - device - # - cable - # type_create: True +# payload_url: 'https://github.com/netbox-community/netbox-docker' +# object_types: +# - device +# - cable +# type_create: True # - name: device_update - # payload_url: 'https://google.com' - # object_types: - # - device - # type_update: True -#- name: device_delete -# payload_url: 'https://gitlab.com' -# object_types: -# - device -# type_delete: True +# payload_url: 'https://google.com' +# object_types: +# - device +# type_update: True +# - name: device_delete +# payload_url: 'https://gitlab.com1' +# object_types: +# - device +# type_delete: True diff --git a/startup_scripts/290_webhooks.py b/startup_scripts/290_webhooks.py index 73b953b..c4d7299 100644 --- a/startup_scripts/290_webhooks.py +++ b/startup_scripts/290_webhooks.py @@ -10,9 +10,7 @@ if webhooks is None: sys.exit() def get_content_type_id(content_type_str): - for type in ContentType.objects.all(): - if type.name == content_type_str: - return type.id + return ContentType.objects.get(model=content_type_str).id for hook in webhooks: obj_types = hook.pop('object_types') @@ -23,6 +21,9 @@ for hook in webhooks: print("⚠️ Error determining content type id for user declared var: {0}".format(obj_type)) else: webhook = Webhook(**hook) - webhook.save() - webhook.obj_type.set(obj_type_ids) - # webhook.save() \ No newline at end of file + if not Webhook.objects.filter(name=webhook.name): + webhook.save() + webhook.content_types.set(obj_type_ids) + print(" Created Webhook {0}".format(webhook.name)) + else: + print(" Skipping Webhook {0}, already exists".format(webhook.name))