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] 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))