blob: 31691857e41c3e79c5ea02362b16d15e434c5c55 [file] [log] [blame]
Omar Abdelkader7e5b3d32017-08-03 14:50:55 -07001from core.models.plcorebase import *
2from models_decl import VBBUService_decl
3from models_decl import VBBUTenant_decl
4
5class VBBUService(VBBUService_decl):
6 class Meta:
7 proxy = True
8
9class VBBUTenant(VBBUTenant_decl):
10 class Meta:
11 proxy = True
12
13 def __init__(self, *args, **kwargs):
14 vbbuservice = VBBUService.get_service_objects().all()
15 if vbbuservice:
16 self._meta.get_field(
17 "provider_service").default = vbbuservice[0].id
18 super(VBBUTenant, self).__init__(*args, **kwargs)
19
20 def save(self, *args, **kwargs):
21 super(VBBUTenant, self).save(*args, **kwargs)
22 # This call needs to happen so that an instance is created for this
23 # tenant is created in the slice. One instance is created per tenant.
24 model_policy_vbbutenant(self.pk)
25
26 def delete(self, *args, **kwargs):
27 # Delete the instance that was created for this tenant
28 self.cleanup_container()
29 super(VBBUTenant, self).delete(*args, **kwargs)
30
31def model_policy_vbbutenant(pk):
32 # This section of code is atomic to prevent race conditions
33 with transaction.atomic():
34 # We find all of the tenants that are waiting to update
35 tenant = VBBUTenant.objects.select_for_update().filter(pk=pk)
36 if not tenant:
37 return
38 # Since this code is atomic it is safe to always use the first tenant
39 tenant = tenant[0]
40 tenant.manage_container()