[MCORD-183] Fix instance creation from UI
Change-Id: I5c7b0bfa0ae9bc8323c2747324c3dcea8e267d5b
diff --git a/xos/models.py b/xos/models.py
new file mode 100644
index 0000000..8e19f54
--- /dev/null
+++ b/xos/models.py
@@ -0,0 +1,40 @@
+from core.models.plcorebase import *
+from models_decl import VMMService_decl
+from models_decl import VMMTenant_decl
+
+class VMMService(VMMService_decl):
+ class Meta:
+ proxy = True
+
+class VMMTenant(VMMTenant_decl):
+ class Meta:
+ proxy = True
+
+ def __init__(self, *args, **kwargs):
+ vmmservice = VMMService.get_service_objects().all()
+ if vmmservice:
+ self._meta.get_field(
+ "provider_service").default = vmmservice[0].id
+ super(VMMTenant, self).__init__(*args, **kwargs)
+
+ def save(self, *args, **kwargs):
+ super(VMMTenant, self).save(*args, **kwargs)
+ # This call needs to happen so that an instance is created for this
+ # tenant is created in the slice. One instance is created per tenant.
+ model_policy_vmmtenant(self.pk)
+
+ def delete(self, *args, **kwargs):
+ # Delete the instance that was created for this tenant
+ self.cleanup_container()
+ super(VMMTenant, self).delete(*args, **kwargs)
+
+def model_policy_vmmtenant(pk):
+ # This section of code is atomic to prevent race conditions
+ with transaction.atomic():
+ # We find all of the tenants that are waiting to update
+ tenant = VMMTenant.objects.select_for_update().filter(pk=pk)
+ if not tenant:
+ return
+ # Since this code is atomic it is safe to always use the first tenant
+ tenant = tenant[0]
+ tenant.manage_container()