Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 1 | import os |
| 2 | from django.db import models |
| 3 | from plstackapi.core.models import PlCoreBase |
| 4 | from plstackapi.core.models import Site |
| 5 | from plstackapi.core.models import User |
| 6 | from plstackapi.core.models import Role |
| 7 | from plstackapi.core.models import DeploymentNetwork |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 8 | from plstackapi.openstack.driver import OpenStackDriver |
| 9 | |
| 10 | # Create your models here. |
| 11 | |
| 12 | class Slice(PlCoreBase): |
Tony Mack | cdec090 | 2013-04-15 00:38:49 -0400 | [diff] [blame] | 13 | tenant_id = models.CharField(max_length=200, help_text="Keystone tenant id") |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 14 | name = models.CharField(unique=True, help_text="The Name of the Slice", max_length=80) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 15 | enabled = models.BooleanField(default=True, help_text="Status for this Slice") |
| 16 | SLICE_CHOICES = (('plc', 'PLC'), ('delegated', 'Delegated'), ('controller','Controller'), ('none','None')) |
| 17 | instantiation = models.CharField(help_text="The instantiation type of the slice", max_length=80, choices=SLICE_CHOICES) |
| 18 | omf_friendly = models.BooleanField() |
| 19 | description=models.TextField(blank=True,help_text="High level description of the slice and expected activities", max_length=1024) |
| 20 | slice_url = models.URLField(blank=True, max_length=512) |
| 21 | site = models.ForeignKey(Site, related_name='slices', help_text="The Site this Node belongs too") |
| 22 | network_id = models.CharField(max_length=256, help_text="Quantum network") |
| 23 | router_id = models.CharField(max_length=256, help_text="Quantum router id") |
| 24 | |
| 25 | def __unicode__(self): return u'%s' % (self.name) |
| 26 | |
Tony Mack | 62bc59a | 2013-04-14 23:27:12 -0400 | [diff] [blame] | 27 | def save(self, *args, **kwds): |
| 28 | |
| 29 | driver = OpenStackDriver() |
| 30 | if not self.tenant_id: |
| 31 | nova_fields = {'tenant_name': self.name, |
| 32 | 'description': self.description, |
| 33 | 'enabled': self.enabled} |
| 34 | tenant = driver.create_tenant(**nova_fields) |
| 35 | self.tenant_id = tenant.id |
| 36 | |
| 37 | # create network |
| 38 | network = driver.create_network(self.name) |
| 39 | self.network_id = network['id'] |
| 40 | |
| 41 | # create router |
| 42 | router = driver.create_router(self.name) |
| 43 | self.router_id = router['id'] |
| 44 | |
| 45 | super(Slice, self).save(*args, **kwds) |
| 46 | |
| 47 | def delete(self, *args, **kwds): |
| 48 | driver = OpenStackDriver() |
| 49 | if self.tenant_id: |
| 50 | driver.delete_router(self.router_id) |
| 51 | driver.delete_network(self.network_id) |
| 52 | driver.delete_tenant(self.tenant_id) |
| 53 | |
| 54 | super(Slice, self).delete(*args, **kwds) |
| 55 | |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 56 | class SliceMembership(PlCoreBase): |
| 57 | user = models.ForeignKey('User', related_name='slice_memberships') |
| 58 | slice = models.ForeignKey('Slice', related_name='slice_memberships') |
| 59 | role = models.ForeignKey('Role') |
| 60 | |
| 61 | def __unicode__(self): return u'%s %s %s' % (self.slice, self.user, self.role) |
| 62 | |