mirror of
https://github.com/netbox-community/netbox-docker.git
synced 2024-11-29 11:23:09 +01:00
e4e2c788a9
Move to a more standard method of object handling
27 lines
886 B
Python
27 lines
886 B
Python
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))
|