blob: b52029c9d80f688b0e98c72c66846ce16bb0b4a5 [file] [log] [blame]
Omar Abdelkaderf886c612017-08-03 14:56:11 -07001from core.models.plcorebase import *
2from models_decl import VSMService_decl
3from models_decl import VSMTenant_decl
4
5class VSMService(VSMService_decl):
6 class Meta:
7 proxy = True
8
9class VSMTenant(VSMTenant_decl):
10 class Meta:
11 proxy = True
12
13 def __init__(self, *args, **kwargs):
14 vsmservice = VSMService.get_service_objects().all()
15 if vsmservice:
16 self._meta.get_field(
17 "provider_service").default = vsmservice[0].id
18 super(VSMTenant, self).__init__(*args, **kwargs)
19
20 def save(self, *args, **kwargs):
21 super(VSMTenant, 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_vsmtenant(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(VSMTenant, self).delete(*args, **kwargs)
30
31def model_policy_vsmtenant(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 = VSMTenant.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()