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