blob: c465d9668c371494a7c64d138d98e758bf77d7ab [file] [log] [blame]
Omar Abdelkader60480312017-08-03 14:55:20 -07001from core.models.plcorebase import *
2from models_decl import VSGWService_decl
3from models_decl import VSGWTenant_decl
4
5class VSGWService(VSGWService_decl):
6 class Meta:
7 proxy = True
8
9class VSGWTenant(VSGWTenant_decl):
10 class Meta:
11 proxy = True
12
13 def __init__(self, *args, **kwargs):
14 vsgwservice = VSGWService.get_service_objects().all()
15 if vsgwservice:
16 self._meta.get_field(
17 "provider_service").default = vsgwservice[0].id
18 super(VSGWTenant, self).__init__(*args, **kwargs)
19
20 def save(self, *args, **kwargs):
21 super(VSGWTenant, 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_vsgwtenant(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(VSGWTenant, self).delete(*args, **kwargs)
30
31def model_policy_vsgwtenant(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 = VSGWTenant.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()
41