Skip to content

Commit 187ae4b

Browse files
MajesticFalconcimnine
authored andcommitted
add custom link initializers
1 parent 3a0b3fe commit 187ae4b

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

initializers/custom_links.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
## Possible Choices:
2+
## new_window:
3+
## - True
4+
## - False
5+
## content_type_id:
6+
## - device
7+
## - site
8+
## - any-other-content-type
9+
##
10+
## Examples:
11+
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

startup_scripts/280_custom_links.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from django.contrib.contenttypes.models import ContentType
2+
from extras.models import CustomLink
3+
from startup_script_utils import load_yaml
4+
import sys
5+
6+
7+
custom_links = load_yaml('/opt/netbox/initializers/custom_links.yml')
8+
9+
if custom_links is None:
10+
sys.exit()
11+
12+
def get_content_type_id(content_type_str):
13+
for type in ContentType.objects.all():
14+
if type.name == content_type_str:
15+
return type.id
16+
17+
for link in custom_links:
18+
content_type = link.pop('content_type')
19+
link['content_type_id'] = get_content_type_id(content_type)
20+
if link['content_type_id'] is None:
21+
print("⚠️ Error determining content type id for user declared var: {0}".format(content_type))
22+
else:
23+
CustomLink(**link).save()
24+

0 commit comments

Comments
 (0)