From 618feff63a54bc3e324f6e97a6dbffd95974241e Mon Sep 17 00:00:00 2001 From: Schylar Utleye Date: Fri, 15 Jan 2021 23:12:03 -0600 Subject: [PATCH] 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))