2020-12-20 22:50:07 +01:00
|
|
|
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):
|
2021-01-16 06:12:03 +01:00
|
|
|
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))
|
2020-12-20 22:50:07 +01:00
|
|
|
|
|
|
|
for hook in webhooks:
|
|
|
|
obj_types = hook.pop('object_types')
|
2021-01-16 07:06:04 +01:00
|
|
|
obj_type_ids = [ get_content_type_id(obj) for obj in obj_types ]
|
2021-01-16 06:12:03 +01:00
|
|
|
if obj_type_ids is not None:
|
2020-12-20 22:50:07 +01:00
|
|
|
webhook = Webhook(**hook)
|
2021-01-16 05:12:57 +01:00
|
|
|
if not Webhook.objects.filter(name=webhook.name):
|
|
|
|
webhook.save()
|
|
|
|
webhook.content_types.set(obj_type_ids)
|
2021-01-16 06:12:03 +01:00
|
|
|
print("🖥️ Created Webhook {0}".format(webhook.name))
|