From 187ae4b2a1a4cc524bd87c95894a7d18966d3d15 Mon Sep 17 00:00:00 2001 From: Schylar Utleye Date: Fri, 18 Dec 2020 23:22:41 -0600 Subject: [PATCH 01/15] add custom link initializers --- initializers/custom_links.yml | 21 +++++++++++++++++++++ startup_scripts/280_custom_links.py | 24 ++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 initializers/custom_links.yml create mode 100644 startup_scripts/280_custom_links.py diff --git a/initializers/custom_links.yml b/initializers/custom_links.yml new file mode 100644 index 0000000..a18acb5 --- /dev/null +++ b/initializers/custom_links.yml @@ -0,0 +1,21 @@ +## Possible Choices: +## new_window: +## - True +## - False +## content_type_id: +## - device +## - site +## - any-other-content-type +## +## Examples: + +# - name: link_to_repo + # text: 'Link to docker repository' + # url: 'https://github.com/netbox-community/netbox-docker' + # new_window: False + # content_type: device +# - name: link_to_localhost + # text: 'Link to the users localhost' + # url: 'http://localhost' + # new_window: True + # content_type: device diff --git a/startup_scripts/280_custom_links.py b/startup_scripts/280_custom_links.py new file mode 100644 index 0000000..6e8bb04 --- /dev/null +++ b/startup_scripts/280_custom_links.py @@ -0,0 +1,24 @@ +from django.contrib.contenttypes.models import ContentType +from extras.models import CustomLink +from startup_script_utils import load_yaml +import sys + + +custom_links = load_yaml('/opt/netbox/initializers/custom_links.yml') + +if custom_links 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 + +for link in custom_links: + content_type = link.pop('content_type') + link['content_type_id'] = get_content_type_id(content_type) + if link['content_type_id'] is None: + print("⚠️ Error determining content type id for user declared var: {0}".format(content_type)) + else: + CustomLink(**link).save() + From 7112a88359dac7951d03fb3beb73bcbd65661f23 Mon Sep 17 00:00:00 2001 From: Schylar Utleye Date: Sun, 20 Dec 2020 21:50:07 +0000 Subject: [PATCH 02/15] add webhook initializer --- initializers/webhooks.yml | 29 +++++++++++++++++++++++++++++ startup_scripts/290_webhooks.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 initializers/webhooks.yml create mode 100644 startup_scripts/290_webhooks.py diff --git a/initializers/webhooks.yml b/initializers/webhooks.yml new file mode 100644 index 0000000..ef4fa5d --- /dev/null +++ b/initializers/webhooks.yml @@ -0,0 +1,29 @@ +## Possible Choices: +## object_types: +## - device +## - site +## - any-other-content-type +## types: +## - type_create +## - type_update +## - type_delete +## Examples: + +# - name: device_creation + # 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_dele1te + payload_url: 'https://gitlab.com' + object_types: + - device + type_delete: True + + diff --git a/startup_scripts/290_webhooks.py b/startup_scripts/290_webhooks.py new file mode 100644 index 0000000..73b953b --- /dev/null +++ b/startup_scripts/290_webhooks.py @@ -0,0 +1,28 @@ +from django.contrib.contenttypes.models import ContentType +from extras.models import Webhook +from startup_script_utils import load_yaml +import sys + + +webhooks = load_yaml('/opt/netbox/initializers/webhooks.yml') + +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 + +for hook in webhooks: + obj_types = hook.pop('object_types') + obj_type_ids = [] + for obj in obj_types: + obj_type_ids.append(get_content_type_id(obj)) + if obj_type_ids is None: + 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 From d0c786e83149448af7221f243101863046fcb5e0 Mon Sep 17 00:00:00 2001 From: Schylar Utleye Date: Sun, 20 Dec 2020 15:53:58 -0600 Subject: [PATCH 03/15] Update webhooks.yml fix comment --- initializers/webhooks.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/initializers/webhooks.yml b/initializers/webhooks.yml index ef4fa5d..ba5b777 100644 --- a/initializers/webhooks.yml +++ b/initializers/webhooks.yml @@ -20,10 +20,10 @@ # object_types: # - device # type_update: True -- name: device_dele1te - payload_url: 'https://gitlab.com' - object_types: - - device - type_delete: True +#- name: device_delete +# payload_url: 'https://gitlab.com' +# object_types: +# - device +# type_delete: True From a3cf645dc5b759cc7b32b441a28c3e82515effa0 Mon Sep 17 00:00:00 2001 From: Schylar Utleye Date: Fri, 15 Jan 2021 22:12:57 -0600 Subject: [PATCH 04/15] 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)) From 618feff63a54bc3e324f6e97a6dbffd95974241e Mon Sep 17 00:00:00 2001 From: Schylar Utleye Date: Fri, 15 Jan 2021 23:12:03 -0600 Subject: [PATCH 05/15] add error handling for webhook and custom links. fix initializer comments --- initializers/custom_links.yml | 20 ++++++++++---------- startup_scripts/280_custom_links.py | 19 ++++++++++++------- startup_scripts/290_webhooks.py | 14 ++++++++------ 3 files changed, 30 insertions(+), 23 deletions(-) diff --git a/initializers/custom_links.yml b/initializers/custom_links.yml index a18acb5..4f3ac01 100644 --- a/initializers/custom_links.yml +++ b/initializers/custom_links.yml @@ -9,13 +9,13 @@ ## ## Examples: -# - name: link_to_repo - # text: 'Link to docker repository' - # url: 'https://github.com/netbox-community/netbox-docker' - # new_window: False - # content_type: device -# - name: link_to_localhost - # text: 'Link to the users localhost' - # url: 'http://localhost' - # new_window: True - # content_type: device +# - name: link_to_repo +# text: 'Link to docker repository' +# url: 'https://github.com/netbox-community/netbox-docker' +# new_window: False +# content_type: device +# - name: link_to_localhost +# text: 'Link to the users localhost' +# url: 'http://localhost' +# new_window: True +# content_type: device diff --git a/startup_scripts/280_custom_links.py b/startup_scripts/280_custom_links.py index 6e8bb04..dc75839 100644 --- a/startup_scripts/280_custom_links.py +++ b/startup_scripts/280_custom_links.py @@ -10,15 +10,20 @@ if custom_links 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 + try: + id = ContentType.objects.get(model=content_type_str).id + return id + except ContentType.DoesNotExist: + print(" Error determining content type id for user declared var: {0}".format(content_type_str)) for link in custom_links: content_type = link.pop('content_type') link['content_type_id'] = get_content_type_id(content_type) - if link['content_type_id'] is None: - print("⚠️ Error determining content type id for user declared var: {0}".format(content_type)) - else: - CustomLink(**link).save() + if link['content_type_id'] is not None: + custom_link = CustomLink(**link) + if not CustomLink.objects.filter(name=custom_link.name): + custom_link.save() + print(" Created Custom Link {0}".format(custom_link.name)) + else: + print("⚠️ Skipping Custom Link {0}, already exists".format(custom_link.name)) diff --git a/startup_scripts/290_webhooks.py b/startup_scripts/290_webhooks.py index c4d7299..3b40fcf 100644 --- a/startup_scripts/290_webhooks.py +++ b/startup_scripts/290_webhooks.py @@ -10,20 +10,22 @@ if webhooks is None: sys.exit() def get_content_type_id(content_type_str): - return ContentType.objects.get(model=content_type_str).id + try: + id = ContentType.objects.get(model=content_type_str).id + return id + except ContentType.DoesNotExist: + print("⚠️ Error determining content type id for user declared var: {0}".format(content_type_str)) for hook in webhooks: obj_types = hook.pop('object_types') obj_type_ids = [] for obj in obj_types: obj_type_ids.append(get_content_type_id(obj)) - if obj_type_ids is None: - print("⚠️ Error determining content type id for user declared var: {0}".format(obj_type)) - else: + if obj_type_ids is not None: webhook = Webhook(**hook) if not Webhook.objects.filter(name=webhook.name): webhook.save() webhook.content_types.set(obj_type_ids) - print(" Created Webhook {0}".format(webhook.name)) + print("🖥️ Created Webhook {0}".format(webhook.name)) else: - print(" Skipping Webhook {0}, already exists".format(webhook.name)) + print("⚠️ Skipping Webhook {0}, already exists".format(webhook.name)) From 8321449cc084f9ebf2caf1578206d16b189b9532 Mon Sep 17 00:00:00 2001 From: Schylar Utleye Date: Fri, 15 Jan 2021 23:34:44 -0600 Subject: [PATCH 06/15] add icons to help messages --- startup_scripts/280_custom_links.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/startup_scripts/280_custom_links.py b/startup_scripts/280_custom_links.py index dc75839..b014a29 100644 --- a/startup_scripts/280_custom_links.py +++ b/startup_scripts/280_custom_links.py @@ -14,7 +14,7 @@ def get_content_type_id(content_type_str): id = ContentType.objects.get(model=content_type_str).id return id except ContentType.DoesNotExist: - print(" Error determining content type id for user declared var: {0}".format(content_type_str)) + print("⚠️ Error determining content type id for user declared var: {0}".format(content_type_str)) for link in custom_links: content_type = link.pop('content_type') @@ -23,7 +23,7 @@ for link in custom_links: custom_link = CustomLink(**link) if not CustomLink.objects.filter(name=custom_link.name): custom_link.save() - print(" Created Custom Link {0}".format(custom_link.name)) + print("🖥️ Created Custom Link {0}".format(custom_link.name)) else: print("⚠️ Skipping Custom Link {0}, already exists".format(custom_link.name)) From fe811f37bd03f52222ff273fb7620989155e7a45 Mon Sep 17 00:00:00 2001 From: Schylar Utley Date: Sat, 16 Jan 2021 00:06:04 -0600 Subject: [PATCH 07/15] replace loop with list comprehension --- startup_scripts/290_webhooks.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/startup_scripts/290_webhooks.py b/startup_scripts/290_webhooks.py index 3b40fcf..315cf44 100644 --- a/startup_scripts/290_webhooks.py +++ b/startup_scripts/290_webhooks.py @@ -18,9 +18,7 @@ def get_content_type_id(content_type_str): for hook in webhooks: obj_types = hook.pop('object_types') - obj_type_ids = [] - for obj in obj_types: - obj_type_ids.append(get_content_type_id(obj)) + obj_type_ids = [ get_content_type_id(obj) for obj in obj_types ] if obj_type_ids is not None: webhook = Webhook(**hook) if not Webhook.objects.filter(name=webhook.name): From 07a0b1d7ef38f472fef745a241e6e425d2a3de02 Mon Sep 17 00:00:00 2001 From: Schylar Utley Date: Wed, 20 Jan 2021 15:47:34 -0600 Subject: [PATCH 08/15] Update startup_scripts/280_custom_links.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Christian Mäder --- startup_scripts/280_custom_links.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/startup_scripts/280_custom_links.py b/startup_scripts/280_custom_links.py index b014a29..7545a0b 100644 --- a/startup_scripts/280_custom_links.py +++ b/startup_scripts/280_custom_links.py @@ -24,6 +24,3 @@ for link in custom_links: if not CustomLink.objects.filter(name=custom_link.name): custom_link.save() print("🖥️ Created Custom Link {0}".format(custom_link.name)) - else: - print("⚠️ Skipping Custom Link {0}, already exists".format(custom_link.name)) - From 95f4d7856a12f8b7ec795784eef2c43e5c3fcbfe Mon Sep 17 00:00:00 2001 From: Schylar Utley Date: Wed, 20 Jan 2021 15:47:44 -0600 Subject: [PATCH 09/15] Update startup_scripts/280_custom_links.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Christian Mäder --- startup_scripts/280_custom_links.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/startup_scripts/280_custom_links.py b/startup_scripts/280_custom_links.py index 7545a0b..83da718 100644 --- a/startup_scripts/280_custom_links.py +++ b/startup_scripts/280_custom_links.py @@ -11,8 +11,7 @@ if custom_links is None: def get_content_type_id(content_type_str): try: - id = ContentType.objects.get(model=content_type_str).id - return id + return ContentType.objects.get(model=content_type_str).id except ContentType.DoesNotExist: print("⚠️ Error determining content type id for user declared var: {0}".format(content_type_str)) From f88f4e15790f2753d0f043f6ddda7b8e72bef46b Mon Sep 17 00:00:00 2001 From: Schylar Utley Date: Wed, 20 Jan 2021 15:47:53 -0600 Subject: [PATCH 10/15] Update startup_scripts/290_webhooks.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Christian Mäder --- startup_scripts/290_webhooks.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/startup_scripts/290_webhooks.py b/startup_scripts/290_webhooks.py index 315cf44..8fc924e 100644 --- a/startup_scripts/290_webhooks.py +++ b/startup_scripts/290_webhooks.py @@ -25,5 +25,3 @@ for hook in webhooks: 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)) From 52c51b5f99e22e73ad98d0cb5d17b72bb98b0db9 Mon Sep 17 00:00:00 2001 From: Schylar Utley Date: Wed, 20 Jan 2021 15:48:17 -0600 Subject: [PATCH 11/15] Update initializers/custom_links.yml MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Christian Mäder --- initializers/custom_links.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/initializers/custom_links.yml b/initializers/custom_links.yml index 4f3ac01..1e621ad 100644 --- a/initializers/custom_links.yml +++ b/initializers/custom_links.yml @@ -2,7 +2,7 @@ ## new_window: ## - True ## - False -## content_type_id: +## content_type: ## - device ## - site ## - any-other-content-type From 744f0e57ad5a47ed0e216158e5290b1bf59cdffe Mon Sep 17 00:00:00 2001 From: Schylar Utley Date: Wed, 20 Jan 2021 15:48:48 -0600 Subject: [PATCH 12/15] Update initializers/webhooks.yml MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Christian Mäder --- initializers/webhooks.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/initializers/webhooks.yml b/initializers/webhooks.yml index 9d78907..ff214a5 100644 --- a/initializers/webhooks.yml +++ b/initializers/webhooks.yml @@ -25,5 +25,3 @@ # object_types: # - device # type_delete: True - - From 2e5d84612d1ee78e7f05917cdc95028fef89c1c3 Mon Sep 17 00:00:00 2001 From: Schylar Utley Date: Wed, 20 Jan 2021 21:25:52 -0600 Subject: [PATCH 13/15] Update initializers/custom_links.yml MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Christian Mäder --- initializers/custom_links.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/initializers/custom_links.yml b/initializers/custom_links.yml index 1e621ad..334efc6 100644 --- a/initializers/custom_links.yml +++ b/initializers/custom_links.yml @@ -10,10 +10,10 @@ ## Examples: # - name: link_to_repo -# text: 'Link to docker repository' -# url: 'https://github.com/netbox-community/netbox-docker' -# new_window: False -# content_type: device +# text: 'Link to docker repository' +# url: 'https://github.com/netbox-community/netbox-docker' +# new_window: False +# content_type: device # - name: link_to_localhost # text: 'Link to the users localhost' # url: 'http://localhost' From e4e2c788a98a01277c64e5d1d30677a3839c3985 Mon Sep 17 00:00:00 2001 From: Schylar Utley Date: Wed, 20 Jan 2021 21:42:24 -0600 Subject: [PATCH 14/15] Update 290_webhooks.py Move to a more standard method of object handling --- startup_scripts/290_webhooks.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/startup_scripts/290_webhooks.py b/startup_scripts/290_webhooks.py index 8fc924e..f6d480b 100644 --- a/startup_scripts/290_webhooks.py +++ b/startup_scripts/290_webhooks.py @@ -20,8 +20,7 @@ for hook in webhooks: obj_types = hook.pop('object_types') obj_type_ids = [ get_content_type_id(obj) for obj in obj_types ] if obj_type_ids is not None: - webhook = Webhook(**hook) - if not Webhook.objects.filter(name=webhook.name): - webhook.save() + webhook, created = Webhook.objects.get_or_create(**hook) + if created: webhook.content_types.set(obj_type_ids) print("🖥️ Created Webhook {0}".format(webhook.name)) From 16ae0633210f4692cff2eeb1ccd20230bb93d3d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20M=C3=A4der?= Date: Mon, 8 Feb 2021 10:35:31 +0100 Subject: [PATCH 15/15] Adjust to repository standards --- initializers/custom_links.yml | 20 +++++----- initializers/webhooks.yml | 54 +++++++++++++------------- startup_scripts/280_custom_links.py | 20 ++++++---- startup_scripts/290_webhooks.py | 60 ++++++++++++++++------------- 4 files changed, 83 insertions(+), 71 deletions(-) diff --git a/initializers/custom_links.yml b/initializers/custom_links.yml index 334efc6..f1b275c 100644 --- a/initializers/custom_links.yml +++ b/initializers/custom_links.yml @@ -9,13 +9,13 @@ ## ## Examples: -# - name: link_to_repo -# text: 'Link to docker repository' -# url: 'https://github.com/netbox-community/netbox-docker' -# new_window: False -# content_type: device -# - name: link_to_localhost -# text: 'Link to the users localhost' -# url: 'http://localhost' -# new_window: True -# content_type: device +# - name: link_to_repo +# text: 'Link to Netbox Docker' +# url: 'https://github.com/netbox-community/netbox-docker' +# new_window: False +# content_type: device +# - name: link_to_localhost +# text: 'Link to localhost' +# url: 'http://localhost' +# new_window: True +# content_type: device diff --git a/initializers/webhooks.yml b/initializers/webhooks.yml index ff214a5..deb1b39 100644 --- a/initializers/webhooks.yml +++ b/initializers/webhooks.yml @@ -1,27 +1,27 @@ -## Possible Choices: -## object_types: -## - device -## - site -## - any-other-content-type -## types: -## - type_create -## - type_update -## - type_delete -## Examples: - -# - name: device_creation -# 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.com1' -# object_types: -# - device -# type_delete: True +## Possible Choices: +## object_types: +## - device +## - site +## - any-other-content-type +## types: +## - type_create +## - type_update +## - type_delete +## Examples: + +# - name: device_creation +# payload_url: 'http://localhost:8080' +# object_types: +# - device +# - cable +# type_create: True +# - name: device_update +# payload_url: 'http://localhost:8080' +# object_types: +# - device +# type_update: True +# - name: device_delete +# payload_url: 'http://localhost:8080' +# object_types: +# - device +# type_delete: True diff --git a/startup_scripts/280_custom_links.py b/startup_scripts/280_custom_links.py index 83da718..b2a7df5 100644 --- a/startup_scripts/280_custom_links.py +++ b/startup_scripts/280_custom_links.py @@ -9,17 +9,21 @@ custom_links = load_yaml('/opt/netbox/initializers/custom_links.yml') if custom_links is None: sys.exit() -def get_content_type_id(content_type_str): + +def get_content_type_id(content_type): try: - return ContentType.objects.get(model=content_type_str).id + return ContentType.objects.get(model=content_type).id except ContentType.DoesNotExist: - print("⚠️ Error determining content type id for user declared var: {0}".format(content_type_str)) + pass + for link in custom_links: content_type = link.pop('content_type') link['content_type_id'] = get_content_type_id(content_type) - if link['content_type_id'] is not None: - custom_link = CustomLink(**link) - if not CustomLink.objects.filter(name=custom_link.name): - custom_link.save() - print("🖥️ Created Custom Link {0}".format(custom_link.name)) + if link['content_type_id'] is None: + print("⚠️ Unable to create Custom Link '{0}': The content_type '{1}' is unknown".format(link.name, content_type)) + continue + + custom_link, created = CustomLink.objects.get_or_create(**link) + if created: + print("🔗 Created Custom Link '{0}'".format(custom_link.name)) diff --git a/startup_scripts/290_webhooks.py b/startup_scripts/290_webhooks.py index f6d480b..ea8352e 100644 --- a/startup_scripts/290_webhooks.py +++ b/startup_scripts/290_webhooks.py @@ -1,26 +1,34 @@ -from django.contrib.contenttypes.models import ContentType -from extras.models import Webhook -from startup_script_utils import load_yaml -import sys - - -webhooks = load_yaml('/opt/netbox/initializers/webhooks.yml') - -if webhooks is None: - sys.exit() - -def get_content_type_id(content_type_str): - try: - id = ContentType.objects.get(model=content_type_str).id - return id - except ContentType.DoesNotExist: - print("⚠️ Error determining content type id for user declared var: {0}".format(content_type_str)) - -for hook in webhooks: - obj_types = hook.pop('object_types') - obj_type_ids = [ get_content_type_id(obj) for obj in obj_types ] - if obj_type_ids is not None: - webhook, created = Webhook.objects.get_or_create(**hook) - if created: - webhook.content_types.set(obj_type_ids) - print("🖥️ Created Webhook {0}".format(webhook.name)) +from django.contrib.contenttypes.models import ContentType +from extras.models import Webhook +from startup_script_utils import load_yaml +import sys + + +webhooks = load_yaml('/opt/netbox/initializers/webhooks.yml') + +if webhooks is None: + sys.exit() + + +def get_content_type_id(hook_name, content_type): + try: + return ContentType.objects.get(model=content_type).id + except ContentType.DoesNotExist as ex: + print("⚠️ Webhook '{0}': The object_type '{1}' is unknown.".format(hook_name, content_type)) + raise ex + + +for hook in webhooks: + obj_types = hook.pop('object_types') + + try: + obj_type_ids = [get_content_type_id(hook['name'], obj) for obj in obj_types] + except ContentType.DoesNotExist: + continue + + webhook, created = Webhook.objects.get_or_create(**hook) + if created: + webhook.content_types.set(obj_type_ids) + webhook.save() + + print("🪝 Created Webhook {0}".format(webhook.name))