Gabe Black | 9062322 | 2017-01-18 19:52:28 +0000 | [diff] [blame^] | 1 | # models.py - ExampleService Models |
| 2 | |
| 3 | from core.models import Service, TenantWithContainer |
| 4 | from django.db import models, transaction |
| 5 | |
| 6 | PASSIVETEST_KIND = 'PassiveTest' |
| 7 | PASSIVETEST_SERVICE_NAME = 'passivetest' |
| 8 | PASSIVETEST_SERVICE_NAME_VERBOSE = 'PASSIVETEST Service' |
| 9 | PASSIVETEST_SERVICE_NAME_VERBOSE_PLURAL = 'PASSIVETEST Services' |
| 10 | PASSIVETEST_TENANT_NAME_VERBOSE = 'PASSIVETEST Tenant' |
| 11 | PASSIVETEST_TENANT_NAME_VERBOSE_PLURAL = 'PASSIVETEST Tenants' |
| 12 | |
| 13 | class PassiveTestService(Service): |
| 14 | |
| 15 | KIND = PASSIVETEST_KIND |
| 16 | |
| 17 | class Meta: |
| 18 | app_label = PASSIVETEST_SERVICE_NAME |
| 19 | verbose_name = PASSIVETEST_SERVICE_NAME_VERBOSE |
| 20 | |
| 21 | tap_ports = models.CharField(max_length=254, help_text="Neutron Port ids of the ports we want to tap (comma delimited)") |
| 22 | reset_viv = models.BooleanField(default=False, help_text="Reset the VIV's input and output configurations!") |
| 23 | |
| 24 | |
| 25 | class PassiveTestTenant(TenantWithContainer): |
| 26 | |
| 27 | KIND = PASSIVETEST_KIND |
| 28 | |
| 29 | class Meta: |
| 30 | verbose_name = PASSIVETEST_TENANT_NAME_VERBOSE |
| 31 | |
| 32 | def __init__(self, *args, **kwargs): |
| 33 | passivetestservice = PassiveTestService.get_service_objects().all() |
| 34 | if passivetestservice: |
| 35 | self._meta.get_field('provider_service').default = passivetestservice[0].id |
| 36 | super(PassiveTestTenant, self).__init__(*args, **kwargs) |
| 37 | |
| 38 | def save(self, *args, **kwargs): |
| 39 | super(PassiveTestTenant, self).save(*args, **kwargs) |
| 40 | model_policy_passivetesttenant(self.pk) |
| 41 | |
| 42 | def delete(self, *args, **kwargs): |
| 43 | self.cleanup_container() |
| 44 | super(PassiveTestTenant, self).delete(*args, **kwargs) |
| 45 | |
| 46 | @property |
| 47 | def public_ip(self): |
| 48 | for port in self.instance.ports.all(): |
| 49 | if "public" in port.network.name.lower(): |
| 50 | return port.ip |
| 51 | return None |
| 52 | |
| 53 | @property |
| 54 | def synchronizer_ip(self): |
| 55 | for port in self.instance.ports.all(): |
| 56 | if "management" in port.network.name.lower(): |
| 57 | return port.ip |
| 58 | return None |
| 59 | |
| 60 | def model_policy_passivetesttenant(pk): |
| 61 | with transaction.atomic(): |
| 62 | tenant = PassiveTestTenant.objects.select_for_update().filter(pk=pk) |
| 63 | if not tenant: |
| 64 | return |
| 65 | tenant = tenant[0] |
| 66 | tenant.manage_container() |
| 67 | |