From d5719fd9473fbb5b16e2fafd734a7837b8d641a1 Mon Sep 17 00:00:00 2001 From: Lyndon Lapierre <51932439+ljlapierre@users.noreply.github.com> Date: Mon, 7 Apr 2025 12:42:39 -0600 Subject: [PATCH 1/2] Create assign_contacts_to_vm.py Create example script demonstrating how to add contact assignments to a virtual machine. I'm not a python developer, most of this was adapted from the create_vm.py. I couldn't find a lot of documentation on how to create the VM object type, so this is for the next person who comes along with the same problem. --- scripts/assign_contacts_to_vm.py | 50 ++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 scripts/assign_contacts_to_vm.py diff --git a/scripts/assign_contacts_to_vm.py b/scripts/assign_contacts_to_vm.py new file mode 100644 index 0000000..e365e30 --- /dev/null +++ b/scripts/assign_contacts_to_vm.py @@ -0,0 +1,50 @@ +""" +This script assigns primary & secondary contacts to a +virtual machine using a predetermined contact role +""" + +from core.models import ObjectType +from extras.scripts import Script, ObjectVar +from tenancy.choices import ContactPriorityChoices +from tenancy.models import Contact, ContactAssignment, ContactRole +from virtualization.models import VirtualMachine + +class AssignContactVM(Script): + class Meta: + name = "Assign Contacts to VM" + description = "Takes a primary & secondary owner and assigns them to an existing VM" + scheduling_enabled = False + + # Main form + primary_owner = ObjectVar(label="Primary Owner", model=Contact) + secondary_owner = ObjectVar(label="Secondary Owner", model=Contact) + vm = ObjectVar(label="Virtual Machine", model=VirtualMachine) + + def run(self, data, commit): + + # Get VM object type & owner role. Owner role must exist. + vm_object_type = ObjectType.objects.get(model='virtualmachine') + contact_owner_role = ContactRole.objects.get(slug="owner") + + # Create contact assignments + ownerPrimary = ContactAssignment( + object_type=vm_object_type, + object_id=data["vm"].id, + contact=data["primary_owner"], + role=contact_owner_role, + priority=ContactPriorityChoices.PRIORITY_PRIMARY, + ) + ownerPrimary.full_clean() + ownerPrimary.save() + + ownerSecondary = ContactAssignment( + object_type=vm_object_type, + object_id=data["vm"].id, + contact=data["secondary_owner"], + role=contact_owner_role, + priority=ContactPriorityChoices.PRIORITY_SECONDARY, + ) + ownerSecondary.full_clean() + ownerSecondary.save() + + self.log_success(f"Successfully assigned contacts {ownerPrimary.contact.name} & {ownerSecondary.contact.name} to {data['vm'].name}.") From 1b3cd71478e0a2e59ce423d7a6004e4b1c2b097e Mon Sep 17 00:00:00 2001 From: Lyndon Lapierre Date: Sat, 1 Nov 2025 10:27:04 -0600 Subject: [PATCH 2/2] Modify assign_contacts_to_vm.py for dynamic role selection --- scripts/assign_contacts_to_vm.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/scripts/assign_contacts_to_vm.py b/scripts/assign_contacts_to_vm.py index e365e30..759c5f4 100644 --- a/scripts/assign_contacts_to_vm.py +++ b/scripts/assign_contacts_to_vm.py @@ -16,22 +16,22 @@ class Meta: scheduling_enabled = False # Main form - primary_owner = ObjectVar(label="Primary Owner", model=Contact) - secondary_owner = ObjectVar(label="Secondary Owner", model=Contact) + primary_contact = ObjectVar(label="Primary Owner", model=Contact) + secondary_contact = ObjectVar(label="Secondary Owner", model=Contact) + contact_role = ObjectVar(label="Contact Role", model=ContactRole) vm = ObjectVar(label="Virtual Machine", model=VirtualMachine) def run(self, data, commit): - # Get VM object type & owner role. Owner role must exist. + # Get VM object type vm_object_type = ObjectType.objects.get(model='virtualmachine') - contact_owner_role = ContactRole.objects.get(slug="owner") # Create contact assignments ownerPrimary = ContactAssignment( object_type=vm_object_type, object_id=data["vm"].id, - contact=data["primary_owner"], - role=contact_owner_role, + contact=data["primary_contact"], + role=contact_role, priority=ContactPriorityChoices.PRIORITY_PRIMARY, ) ownerPrimary.full_clean() @@ -40,8 +40,8 @@ def run(self, data, commit): ownerSecondary = ContactAssignment( object_type=vm_object_type, object_id=data["vm"].id, - contact=data["secondary_owner"], - role=contact_owner_role, + contact=data["secondary_contact"], + role=contact_role, priority=ContactPriorityChoices.PRIORITY_SECONDARY, ) ownerSecondary.full_clean()