Skip to content

Commit 047f2ab

Browse files
Lon1tobiasge
authored andcommitted
adding contact startups
1 parent f13a657 commit 047f2ab

File tree

6 files changed

+122
-0
lines changed

6 files changed

+122
-0
lines changed

initializers/contact_groups.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# - name: Network-Team
2+
# slug: network-team
3+
# description: This is a new contact group for the Network-Team
4+
# parent: None
5+
# custom_field_data:
6+
# widget: This is the custom field called widget
7+
# - name: New Contact Group
8+
# slug: new-contact-group
9+
# description: This is a new contact group sub under of Network-Team
10+
# parent: Network-Team
11+
# custom_field_data:
12+
# widget: This is the custom field called widget

initializers/contact_roles.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# - name: New Contact Role
2+
# slug: new-contact-role
3+
# description: This is a new contact role description
4+
# custom_field_data:
5+
# widget: This is the custom field called widget

initializers/contacts.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# - name: Lee Widget
2+
# group: Network-Team
3+
# title: CEO of Widget Corp
4+
# phone: 221-555-1212
5+
# email: widgetCEO@widgetcorp.com
6+
# address: 1200 Nowhere Blvd, Scranton NJ, 555111
7+
# comments: This is a very important contact
8+
# custom_field_data:
9+
# widget: This is the custom field called widget

startup_scripts/470_contact_groups.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import sys
2+
3+
from startup_script_utils import (
4+
load_yaml,
5+
pop_custom_fields,
6+
set_custom_fields_values,
7+
split_params,
8+
)
9+
from tenancy.models import ContactGroup
10+
11+
contact_groups = load_yaml("/opt/netbox/initializers/contact_groups.yml")
12+
13+
if contact_groups is None:
14+
sys.exit()
15+
16+
optional_assocs = {"parent": (ContactGroup, "name")}
17+
18+
for params in contact_groups:
19+
custom_field_data = pop_custom_fields(params)
20+
21+
for assoc, details in optional_assocs.items():
22+
if assoc in params:
23+
model, field = details
24+
query = {field: params.pop(assoc)}
25+
26+
params[assoc] = model.objects.get(**query)
27+
28+
matching_params, defaults = split_params(params)
29+
contact_group, created = ContactGroup.objects.get_or_create(
30+
**matching_params, defaults=defaults
31+
)
32+
33+
if created:
34+
print("🔳 Created Contact Group", contact_group.name)
35+
36+
set_custom_fields_values(contact_group, custom_field_data)

startup_scripts/480_contact_roles.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import sys
2+
3+
from startup_script_utils import (
4+
load_yaml,
5+
pop_custom_fields,
6+
set_custom_fields_values,
7+
split_params,
8+
)
9+
from tenancy.models import ContactRole
10+
11+
contact_roles = load_yaml("/opt/netbox/initializers/contact_roles.yml")
12+
13+
if contact_roles is None:
14+
sys.exit()
15+
16+
17+
for params in contact_roles:
18+
custom_field_data = pop_custom_fields(params)
19+
20+
matching_params, defaults = split_params(params)
21+
contact_role, created = ContactRole.objects.get_or_create(**matching_params, defaults=defaults)
22+
23+
if created:
24+
print("🔳 Created Contact Role", contact_role.name)
25+
26+
set_custom_fields_values(contact_role, custom_field_data)

startup_scripts/490_contacts.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import sys
2+
3+
from startup_script_utils import (
4+
load_yaml,
5+
pop_custom_fields,
6+
set_custom_fields_values,
7+
split_params,
8+
)
9+
from tenancy.models import Contact, ContactGroup
10+
11+
contacts = load_yaml("/opt/netbox/initializers/contacts.yml")
12+
13+
if contacts is None:
14+
sys.exit()
15+
16+
optional_assocs = {"group": (ContactGroup, "name")}
17+
18+
for params in contacts:
19+
custom_field_data = pop_custom_fields(params)
20+
21+
for assoc, details in optional_assocs.items():
22+
if assoc in params:
23+
model, field = details
24+
query = {field: params.pop(assoc)}
25+
26+
params[assoc] = model.objects.get(**query)
27+
28+
matching_params, defaults = split_params(params)
29+
contact, created = Contact.objects.get_or_create(**matching_params, defaults=defaults)
30+
31+
if created:
32+
print("👩‍💻 Created Contact", contact.name)
33+
34+
set_custom_fields_values(contact, custom_field_data)

0 commit comments

Comments
 (0)