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 |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 5 | from plstackapi.core.models import PLUser |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 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 | |
| 9 | # Create your models here. |
| 10 | |
| 11 | class Slice(PlCoreBase): |
Tony Mack | cdec090 | 2013-04-15 00:38:49 -0400 | [diff] [blame] | 12 | tenant_id = models.CharField(max_length=200, help_text="Keystone tenant id") |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 13 | 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] | 14 | enabled = models.BooleanField(default=True, help_text="Status for this Slice") |
| 15 | SLICE_CHOICES = (('plc', 'PLC'), ('delegated', 'Delegated'), ('controller','Controller'), ('none','None')) |
| 16 | instantiation = models.CharField(help_text="The instantiation type of the slice", max_length=80, choices=SLICE_CHOICES) |
| 17 | omf_friendly = models.BooleanField() |
| 18 | description=models.TextField(blank=True,help_text="High level description of the slice and expected activities", max_length=1024) |
| 19 | slice_url = models.URLField(blank=True, max_length=512) |
| 20 | site = models.ForeignKey(Site, related_name='slices', help_text="The Site this Node belongs too") |
| 21 | network_id = models.CharField(max_length=256, help_text="Quantum network") |
| 22 | router_id = models.CharField(max_length=256, help_text="Quantum router id") |
| 23 | |
Siobhan Tully | 231f4c8 | 2013-05-02 05:47:24 -0400 | [diff] [blame] | 24 | SVC_CLASS_CHOICES = (('besteffort', 'Best Effort'), ('silver', 'Silver'), ('gold','Gold')) |
| 25 | serviceClass = models.CharField(verbose_name="Service Class",default="besteffort",help_text="The Service Class of this slice", max_length=30, choices=SVC_CLASS_CHOICES) |
| 26 | |
| 27 | |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 28 | def __unicode__(self): return u'%s' % (self.name) |
| 29 | |
Tony Mack | 62bc59a | 2013-04-14 23:27:12 -0400 | [diff] [blame] | 30 | def save(self, *args, **kwds): |
Tony Mack | 62bc59a | 2013-04-14 23:27:12 -0400 | [diff] [blame] | 31 | if not self.tenant_id: |
| 32 | nova_fields = {'tenant_name': self.name, |
| 33 | 'description': self.description, |
| 34 | 'enabled': self.enabled} |
Tony Mack | fdd4d80 | 2013-04-27 13:02:33 -0400 | [diff] [blame] | 35 | tenant = self.driver.create_tenant(**nova_fields) |
Tony Mack | 62bc59a | 2013-04-14 23:27:12 -0400 | [diff] [blame] | 36 | self.tenant_id = tenant.id |
| 37 | |
Tony Mack | 9bcbe4f | 2013-04-29 08:13:27 -0400 | [diff] [blame] | 38 | # give caller an admin role at the tenant they've created |
| 39 | self.driver.add_user_role(self.caller.user_id, tenant.id, 'admin') |
| 40 | |
Tony Mack | 5d0ce61 | 2013-04-29 17:53:07 -0400 | [diff] [blame] | 41 | # refresh credentials using this tenant |
| 42 | self.driver.shell.connect(username=self.driver.shell.keystone.username, |
| 43 | password=self.driver.shell.keystone.password, |
| 44 | tenant=tenant.name) |
| 45 | |
Tony Mack | 62bc59a | 2013-04-14 23:27:12 -0400 | [diff] [blame] | 46 | # create network |
Tony Mack | fdd4d80 | 2013-04-27 13:02:33 -0400 | [diff] [blame] | 47 | network = self.driver.create_network(self.name) |
Tony Mack | 62bc59a | 2013-04-14 23:27:12 -0400 | [diff] [blame] | 48 | self.network_id = network['id'] |
| 49 | |
| 50 | # create router |
Tony Mack | fdd4d80 | 2013-04-27 13:02:33 -0400 | [diff] [blame] | 51 | router = self.driver.create_router(self.name) |
Tony Mack | 62bc59a | 2013-04-14 23:27:12 -0400 | [diff] [blame] | 52 | self.router_id = router['id'] |
| 53 | |
Tony Mack | 54304c6 | 2013-04-28 11:27:07 -0400 | [diff] [blame] | 54 | if self.id: |
| 55 | self.driver.update_tenant(self.tenant_id, |
| 56 | description=self.description, |
| 57 | enabled=self.enabled) |
| 58 | |
Tony Mack | 62bc59a | 2013-04-14 23:27:12 -0400 | [diff] [blame] | 59 | super(Slice, self).save(*args, **kwds) |
| 60 | |
| 61 | def delete(self, *args, **kwds): |
Tony Mack | 62bc59a | 2013-04-14 23:27:12 -0400 | [diff] [blame] | 62 | if self.tenant_id: |
Tony Mack | fdd4d80 | 2013-04-27 13:02:33 -0400 | [diff] [blame] | 63 | self.driver.delete_router(self.router_id) |
| 64 | self.driver.delete_network(self.network_id) |
| 65 | self.driver.delete_tenant(self.tenant_id) |
Tony Mack | 62bc59a | 2013-04-14 23:27:12 -0400 | [diff] [blame] | 66 | |
| 67 | super(Slice, self).delete(*args, **kwds) |
| 68 | |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 69 | class SliceMembership(PlCoreBase): |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 70 | user = models.ForeignKey('PLUser', related_name='slice_memberships') |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 71 | slice = models.ForeignKey('Slice', related_name='slice_memberships') |
| 72 | role = models.ForeignKey('Role') |
| 73 | |
| 74 | def __unicode__(self): return u'%s %s %s' % (self.slice, self.user, self.role) |
| 75 | |
Tony Mack | 00d361f | 2013-04-28 10:28:42 -0400 | [diff] [blame] | 76 | def save(self, *args, **kwds): |
| 77 | self.driver.add_user_role(self.user.user_id, self.slice.tenant_id, self.role.role_type) |
| 78 | super(SliceMembership, self).save(*args, **kwds) |
| 79 | |
| 80 | def delete(self, *args, **kwds): |
| 81 | self.driver.delete_user_role(self.user.user_id, self.slice.tenant_id, self.role.role_type) |
| 82 | super(SliceMembership, self).delete(*args, **kwds) |