Omar Abdelkader | 1d9c319 | 2017-08-03 14:54:25 -0700 | [diff] [blame] | 1 | from core.models.plcorebase import * |
| 2 | from models_decl import VMMEService_decl |
| 3 | from models_decl import VMMETenant_decl |
| 4 | |
| 5 | class VMMEService(VMMEService_decl): |
| 6 | class Meta: |
| 7 | proxy = True |
| 8 | |
| 9 | class VMMETenant(VMMETenant_decl): |
| 10 | class Meta: |
| 11 | proxy = True |
| 12 | |
| 13 | def __init__(self, *args, **kwargs): |
| 14 | vmmeservice = VMMEService.get_service_objects().all() |
| 15 | if vmmeservice: |
| 16 | self._meta.get_field( |
| 17 | "provider_service").default = vmmeservice[0].id |
| 18 | super(VMMETenant, self).__init__(*args, **kwargs) |
| 19 | |
| 20 | def save(self, *args, **kwargs): |
| 21 | super(VMMETenant, 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_vmmetenant(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(VMMETenant, self).delete(*args, **kwargs) |
| 30 | |
| 31 | def model_policy_vmmetenant(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 = VMMETenant.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() |