Skip to content

Commit 0410cf2

Browse files
authored
Merge pull request #728 from Lon1/contact-startups
Contact startups
2 parents f13a657 + c9f5e34 commit 0410cf2

File tree

6 files changed

+126
-0
lines changed

6 files changed

+126
-0
lines changed

initializers/contact_groups.yml

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

initializers/contact_roles.yml

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

initializers/contacts.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# - name: Lee Widget
2+
# title: CEO of Widget Corp
3+
# phone: 221-555-1212
4+
# email: widgetCEO@widgetcorp.com
5+
# address: 1200 Nowhere Blvd, Scranton NJ, 555111
6+
# comments: This is a very important contact
7+
# - name: Ali Gator
8+
# group: Network-Team
9+
# title: Consultant for Widget Corp
10+
# phone: 221-555-1213
11+
# email: Consultant@widgetcorp.com
12+
# address: 1200 Nowhere Blvd, Scranton NJ, 555111
13+
# comments: This is a very important contact
14+
# - name: Karlchen Maier
15+
# group: New Contact Group
16+
# title: COO of Widget Corp
17+
# phone: 221-555-1214
18+
# email: Karlchen@widgetcorp.com
19+
# address: 1200 Nowhere Blvd, Scranton NJ, 555111
20+
# comments: This is a very important contact

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)