From 047f2abdb59efd1a074df087c9dfddc04ebbff7f Mon Sep 17 00:00:00 2001 From: "Thompson, Lon" Date: Fri, 1 Apr 2022 13:59:41 -0400 Subject: [PATCH 1/2] adding contact startups --- initializers/contact_groups.yml | 12 +++++++++ initializers/contact_roles.yml | 5 ++++ initializers/contacts.yml | 9 +++++++ startup_scripts/470_contact_groups.py | 36 +++++++++++++++++++++++++++ startup_scripts/480_contact_roles.py | 26 +++++++++++++++++++ startup_scripts/490_contacts.py | 34 +++++++++++++++++++++++++ 6 files changed, 122 insertions(+) create mode 100644 initializers/contact_groups.yml create mode 100644 initializers/contact_roles.yml create mode 100644 initializers/contacts.yml create mode 100644 startup_scripts/470_contact_groups.py create mode 100644 startup_scripts/480_contact_roles.py create mode 100644 startup_scripts/490_contacts.py diff --git a/initializers/contact_groups.yml b/initializers/contact_groups.yml new file mode 100644 index 0000000..222620e --- /dev/null +++ b/initializers/contact_groups.yml @@ -0,0 +1,12 @@ +# - name: Network-Team +# slug: network-team +# description: This is a new contact group for the Network-Team +# parent: None +# custom_field_data: +# widget: This is the custom field called widget +# - name: New Contact Group +# slug: new-contact-group +# description: This is a new contact group sub under of Network-Team +# parent: Network-Team +# custom_field_data: +# widget: This is the custom field called widget diff --git a/initializers/contact_roles.yml b/initializers/contact_roles.yml new file mode 100644 index 0000000..dd612d9 --- /dev/null +++ b/initializers/contact_roles.yml @@ -0,0 +1,5 @@ +# - name: New Contact Role +# slug: new-contact-role +# description: This is a new contact role description +# custom_field_data: +# widget: This is the custom field called widget diff --git a/initializers/contacts.yml b/initializers/contacts.yml new file mode 100644 index 0000000..d7a2309 --- /dev/null +++ b/initializers/contacts.yml @@ -0,0 +1,9 @@ +# - name: Lee Widget +# group: Network-Team +# title: CEO of Widget Corp +# phone: 221-555-1212 +# email: widgetCEO@widgetcorp.com +# address: 1200 Nowhere Blvd, Scranton NJ, 555111 +# comments: This is a very important contact +# custom_field_data: +# widget: This is the custom field called widget diff --git a/startup_scripts/470_contact_groups.py b/startup_scripts/470_contact_groups.py new file mode 100644 index 0000000..f5fc4fd --- /dev/null +++ b/startup_scripts/470_contact_groups.py @@ -0,0 +1,36 @@ +import sys + +from startup_script_utils import ( + load_yaml, + pop_custom_fields, + set_custom_fields_values, + split_params, +) +from tenancy.models import ContactGroup + +contact_groups = load_yaml("/opt/netbox/initializers/contact_groups.yml") + +if contact_groups is None: + sys.exit() + +optional_assocs = {"parent": (ContactGroup, "name")} + +for params in contact_groups: + custom_field_data = pop_custom_fields(params) + + for assoc, details in optional_assocs.items(): + if assoc in params: + model, field = details + query = {field: params.pop(assoc)} + + params[assoc] = model.objects.get(**query) + + matching_params, defaults = split_params(params) + contact_group, created = ContactGroup.objects.get_or_create( + **matching_params, defaults=defaults + ) + + if created: + print("🔳 Created Contact Group", contact_group.name) + + set_custom_fields_values(contact_group, custom_field_data) diff --git a/startup_scripts/480_contact_roles.py b/startup_scripts/480_contact_roles.py new file mode 100644 index 0000000..1b3d8bc --- /dev/null +++ b/startup_scripts/480_contact_roles.py @@ -0,0 +1,26 @@ +import sys + +from startup_script_utils import ( + load_yaml, + pop_custom_fields, + set_custom_fields_values, + split_params, +) +from tenancy.models import ContactRole + +contact_roles = load_yaml("/opt/netbox/initializers/contact_roles.yml") + +if contact_roles is None: + sys.exit() + + +for params in contact_roles: + custom_field_data = pop_custom_fields(params) + + matching_params, defaults = split_params(params) + contact_role, created = ContactRole.objects.get_or_create(**matching_params, defaults=defaults) + + if created: + print("🔳 Created Contact Role", contact_role.name) + + set_custom_fields_values(contact_role, custom_field_data) diff --git a/startup_scripts/490_contacts.py b/startup_scripts/490_contacts.py new file mode 100644 index 0000000..80d75d6 --- /dev/null +++ b/startup_scripts/490_contacts.py @@ -0,0 +1,34 @@ +import sys + +from startup_script_utils import ( + load_yaml, + pop_custom_fields, + set_custom_fields_values, + split_params, +) +from tenancy.models import Contact, ContactGroup + +contacts = load_yaml("/opt/netbox/initializers/contacts.yml") + +if contacts is None: + sys.exit() + +optional_assocs = {"group": (ContactGroup, "name")} + +for params in contacts: + custom_field_data = pop_custom_fields(params) + + for assoc, details in optional_assocs.items(): + if assoc in params: + model, field = details + query = {field: params.pop(assoc)} + + params[assoc] = model.objects.get(**query) + + matching_params, defaults = split_params(params) + contact, created = Contact.objects.get_or_create(**matching_params, defaults=defaults) + + if created: + print("👩‍💻 Created Contact", contact.name) + + set_custom_fields_values(contact, custom_field_data) From c9f5e34c0d44dc20c3fdaac5cf4cb63e68752e81 Mon Sep 17 00:00:00 2001 From: Tobias Genannt Date: Mon, 25 Apr 2022 16:37:33 +0200 Subject: [PATCH 2/2] Improved contact initializer examples --- initializers/contact_groups.yml | 5 ----- initializers/contact_roles.yml | 2 -- initializers/contacts.yml | 17 ++++++++++++++--- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/initializers/contact_groups.yml b/initializers/contact_groups.yml index 222620e..a3ead09 100644 --- a/initializers/contact_groups.yml +++ b/initializers/contact_groups.yml @@ -1,12 +1,7 @@ # - name: Network-Team # slug: network-team # description: This is a new contact group for the Network-Team -# parent: None -# custom_field_data: -# widget: This is the custom field called widget # - name: New Contact Group # slug: new-contact-group # description: This is a new contact group sub under of Network-Team # parent: Network-Team -# custom_field_data: -# widget: This is the custom field called widget diff --git a/initializers/contact_roles.yml b/initializers/contact_roles.yml index dd612d9..1a72e43 100644 --- a/initializers/contact_roles.yml +++ b/initializers/contact_roles.yml @@ -1,5 +1,3 @@ # - name: New Contact Role # slug: new-contact-role # description: This is a new contact role description -# custom_field_data: -# widget: This is the custom field called widget diff --git a/initializers/contacts.yml b/initializers/contacts.yml index d7a2309..3ab41a2 100644 --- a/initializers/contacts.yml +++ b/initializers/contacts.yml @@ -1,9 +1,20 @@ # - name: Lee Widget -# group: Network-Team # title: CEO of Widget Corp # phone: 221-555-1212 # email: widgetCEO@widgetcorp.com # address: 1200 Nowhere Blvd, Scranton NJ, 555111 # comments: This is a very important contact -# custom_field_data: -# widget: This is the custom field called widget +# - name: Ali Gator +# group: Network-Team +# title: Consultant for Widget Corp +# phone: 221-555-1213 +# email: Consultant@widgetcorp.com +# address: 1200 Nowhere Blvd, Scranton NJ, 555111 +# comments: This is a very important contact +# - name: Karlchen Maier +# group: New Contact Group +# title: COO of Widget Corp +# phone: 221-555-1214 +# email: Karlchen@widgetcorp.com +# address: 1200 Nowhere Blvd, Scranton NJ, 555111 +# comments: This is a very important contact