Skip to content

Commit 16ae063

Browse files
committed
Adjust to repository standards
1 parent e4e2c78 commit 16ae063

File tree

4 files changed

+83
-71
lines changed

4 files changed

+83
-71
lines changed

initializers/custom_links.yml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
##
1010
## Examples:
1111

12-
# - name: link_to_repo
13-
# text: 'Link to docker repository'
14-
# url: 'https://github.com/netbox-community/netbox-docker'
15-
# new_window: False
16-
# content_type: device
17-
# - name: link_to_localhost
18-
# text: 'Link to the users localhost'
19-
# url: 'http://localhost'
20-
# new_window: True
21-
# content_type: device
12+
# - name: link_to_repo
13+
# text: 'Link to Netbox Docker'
14+
# url: 'https://github.com/netbox-community/netbox-docker'
15+
# new_window: False
16+
# content_type: device
17+
# - name: link_to_localhost
18+
# text: 'Link to localhost'
19+
# url: 'http://localhost'
20+
# new_window: True
21+
# content_type: device

initializers/webhooks.yml

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
## Possible Choices:
2-
## object_types:
3-
## - device
4-
## - site
5-
## - any-other-content-type
6-
## types:
7-
## - type_create
8-
## - type_update
9-
## - type_delete
10-
## Examples:
11-
12-
# - name: device_creation
13-
# payload_url: 'https://github.com/netbox-community/netbox-docker'
14-
# object_types:
15-
# - device
16-
# - cable
17-
# type_create: True
18-
# - name: device_update
19-
# payload_url: 'https://google.com'
20-
# object_types:
21-
# - device
22-
# type_update: True
23-
# - name: device_delete
24-
# payload_url: 'https://gitlab.com1'
25-
# object_types:
26-
# - device
27-
# type_delete: True
1+
## Possible Choices:
2+
## object_types:
3+
## - device
4+
## - site
5+
## - any-other-content-type
6+
## types:
7+
## - type_create
8+
## - type_update
9+
## - type_delete
10+
## Examples:
11+
12+
# - name: device_creation
13+
# payload_url: 'http://localhost:8080'
14+
# object_types:
15+
# - device
16+
# - cable
17+
# type_create: True
18+
# - name: device_update
19+
# payload_url: 'http://localhost:8080'
20+
# object_types:
21+
# - device
22+
# type_update: True
23+
# - name: device_delete
24+
# payload_url: 'http://localhost:8080'
25+
# object_types:
26+
# - device
27+
# type_delete: True

startup_scripts/280_custom_links.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,21 @@
99
if custom_links is None:
1010
sys.exit()
1111

12-
def get_content_type_id(content_type_str):
12+
13+
def get_content_type_id(content_type):
1314
try:
14-
return ContentType.objects.get(model=content_type_str).id
15+
return ContentType.objects.get(model=content_type).id
1516
except ContentType.DoesNotExist:
16-
print("⚠️ Error determining content type id for user declared var: {0}".format(content_type_str))
17+
pass
18+
1719

1820
for link in custom_links:
1921
content_type = link.pop('content_type')
2022
link['content_type_id'] = get_content_type_id(content_type)
21-
if link['content_type_id'] is not None:
22-
custom_link = CustomLink(**link)
23-
if not CustomLink.objects.filter(name=custom_link.name):
24-
custom_link.save()
25-
print("🖥️ Created Custom Link {0}".format(custom_link.name))
23+
if link['content_type_id'] is None:
24+
print("⚠️ Unable to create Custom Link '{0}': The content_type '{1}' is unknown".format(link.name, content_type))
25+
continue
26+
27+
custom_link, created = CustomLink.objects.get_or_create(**link)
28+
if created:
29+
print("🔗 Created Custom Link '{0}'".format(custom_link.name))

startup_scripts/290_webhooks.py

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,34 @@
1-
from django.contrib.contenttypes.models import ContentType
2-
from extras.models import Webhook
3-
from startup_script_utils import load_yaml
4-
import sys
5-
6-
7-
webhooks = load_yaml('/opt/netbox/initializers/webhooks.yml')
8-
9-
if webhooks is None:
10-
sys.exit()
11-
12-
def get_content_type_id(content_type_str):
13-
try:
14-
id = ContentType.objects.get(model=content_type_str).id
15-
return id
16-
except ContentType.DoesNotExist:
17-
print("⚠️ Error determining content type id for user declared var: {0}".format(content_type_str))
18-
19-
for hook in webhooks:
20-
obj_types = hook.pop('object_types')
21-
obj_type_ids = [ get_content_type_id(obj) for obj in obj_types ]
22-
if obj_type_ids is not None:
23-
webhook, created = Webhook.objects.get_or_create(**hook)
24-
if created:
25-
webhook.content_types.set(obj_type_ids)
26-
print("🖥️ Created Webhook {0}".format(webhook.name))
1+
from django.contrib.contenttypes.models import ContentType
2+
from extras.models import Webhook
3+
from startup_script_utils import load_yaml
4+
import sys
5+
6+
7+
webhooks = load_yaml('/opt/netbox/initializers/webhooks.yml')
8+
9+
if webhooks is None:
10+
sys.exit()
11+
12+
13+
def get_content_type_id(hook_name, content_type):
14+
try:
15+
return ContentType.objects.get(model=content_type).id
16+
except ContentType.DoesNotExist as ex:
17+
print("⚠️ Webhook '{0}': The object_type '{1}' is unknown.".format(hook_name, content_type))
18+
raise ex
19+
20+
21+
for hook in webhooks:
22+
obj_types = hook.pop('object_types')
23+
24+
try:
25+
obj_type_ids = [get_content_type_id(hook['name'], obj) for obj in obj_types]
26+
except ContentType.DoesNotExist:
27+
continue
28+
29+
webhook, created = Webhook.objects.get_or_create(**hook)
30+
if created:
31+
webhook.content_types.set(obj_type_ids)
32+
webhook.save()
33+
34+
print("🪝 Created Webhook {0}".format(webhook.name))

0 commit comments

Comments
 (0)