Saleil Bhat | 1c5593d | 2017-01-06 15:25:57 -0800 | [diff] [blame] | 1 | # models.py - ExampleService Models |
| 2 | |
Saleil Bhat | 22afba8 | 2017-01-21 23:10:09 -0800 | [diff] [blame] | 3 | from core.models import Service, TenantWithContainer, Image |
Saleil Bhat | 1c5593d | 2017-01-06 15:25:57 -0800 | [diff] [blame] | 4 | from django.db import models, transaction |
| 5 | |
| 6 | |
| 7 | MCORD_KIND = "EPC" #added from vBBU |
| 8 | |
| 9 | #these macros are currently not used, names hard-coded manually |
| 10 | SERVICE_NAME = 'vmme' |
| 11 | SERVICE_NAME_VERBOSE = 'VMME Service' |
| 12 | SERVICE_NAME_VERBOSE_PLURAL = 'VMME Services' |
| 13 | TENANT_NAME_VERBOSE = 'VMME Service Tenant' |
| 14 | TENANT_NAME_VERBOSE_PLURAL = 'VMME Service Tenants' |
| 15 | |
| 16 | class VMMEService(Service): |
| 17 | |
| 18 | KIND = MCORD_KIND |
| 19 | |
| 20 | class Meta: |
| 21 | proxy = True |
| 22 | app_label = "vmme" |
| 23 | verbose_name = "VMME Service" |
| 24 | |
| 25 | class VMMETenant(TenantWithContainer): |
| 26 | |
| 27 | KIND = 'vmme' |
| 28 | class Meta: |
| 29 | verbose_name = "VMME Service Tenant" |
| 30 | |
| 31 | tenant_message = models.CharField(max_length=254, help_text="vMME message") |
Saleil Bhat | 22afba8 | 2017-01-21 23:10:09 -0800 | [diff] [blame] | 32 | image_name = models.CharField(max_length=254, help_text="Name of VM image") |
Saleil Bhat | 1c5593d | 2017-01-06 15:25:57 -0800 | [diff] [blame] | 33 | |
Saleil Bhat | 1c5593d | 2017-01-06 15:25:57 -0800 | [diff] [blame] | 34 | def __init__(self, *args, **kwargs): |
| 35 | vmme_services = VMMEService.get_service_objects().all() |
| 36 | if vmme_services: |
| 37 | self._meta.get_field('provider_service').default = vmme_services[0].id |
| 38 | super(VMMETenant, self).__init__(*args, **kwargs) |
| 39 | |
| 40 | def save(self, *args, **kwargs): |
| 41 | super(VMMETenant, self).save(*args, **kwargs) |
| 42 | model_policy_vmmetenant(self.pk) #defined below |
| 43 | |
| 44 | def delete(self, *args, **kwargs): |
| 45 | self.cleanup_container() |
| 46 | super(VMMETenant, self).delete(*args, **kwargs) |
| 47 | |
Saleil Bhat | 22afba8 | 2017-01-21 23:10:09 -0800 | [diff] [blame] | 48 | @property |
| 49 | def image(self): |
| 50 | img = self.image_name.strip() |
| 51 | if img.lower() != "default": |
| 52 | return Image.objects.get(name=img) |
| 53 | else: |
| 54 | return super(VMMETenant, self).image |
| 55 | |
| 56 | |
Saleil Bhat | 1c5593d | 2017-01-06 15:25:57 -0800 | [diff] [blame] | 57 | |
| 58 | def model_policy_vmmetenant(pk): |
| 59 | with transaction.atomic(): |
| 60 | tenant = VMMETenant.objects.select_for_update().filter(pk=pk) |
| 61 | if not tenant: |
| 62 | return |
| 63 | tenant = tenant[0] |
| 64 | tenant.manage_container() |
| 65 | |