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 | |
| 24 | def __unicode__(self): return u'%s' % (self.name) |
| 25 | |
Tony Mack | 62bc59a | 2013-04-14 23:27:12 -0400 | [diff] [blame] | 26 | def save(self, *args, **kwds): |
Tony Mack | 62bc59a | 2013-04-14 23:27:12 -0400 | [diff] [blame] | 27 | if not self.tenant_id: |
| 28 | nova_fields = {'tenant_name': self.name, |
| 29 | 'description': self.description, |
| 30 | 'enabled': self.enabled} |
Tony Mack | fdd4d80 | 2013-04-27 13:02:33 -0400 | [diff] [blame] | 31 | tenant = self.driver.create_tenant(**nova_fields) |
Tony Mack | 62bc59a | 2013-04-14 23:27:12 -0400 | [diff] [blame] | 32 | self.tenant_id = tenant.id |
| 33 | |
Tony Mack | 9bcbe4f | 2013-04-29 08:13:27 -0400 | [diff] [blame] | 34 | # give caller an admin role at the tenant they've created |
| 35 | self.driver.add_user_role(self.caller.user_id, tenant.id, 'admin') |
| 36 | |
Tony Mack | 5d0ce61 | 2013-04-29 17:53:07 -0400 | [diff] [blame] | 37 | # refresh credentials using this tenant |
| 38 | self.driver.shell.connect(username=self.driver.shell.keystone.username, |
| 39 | password=self.driver.shell.keystone.password, |
| 40 | tenant=tenant.name) |
| 41 | |
Tony Mack | 62bc59a | 2013-04-14 23:27:12 -0400 | [diff] [blame] | 42 | # create network |
Tony Mack | fdd4d80 | 2013-04-27 13:02:33 -0400 | [diff] [blame] | 43 | network = self.driver.create_network(self.name) |
Tony Mack | 62bc59a | 2013-04-14 23:27:12 -0400 | [diff] [blame] | 44 | self.network_id = network['id'] |
| 45 | |
| 46 | # create router |
Tony Mack | fdd4d80 | 2013-04-27 13:02:33 -0400 | [diff] [blame] | 47 | router = self.driver.create_router(self.name) |
Tony Mack | 62bc59a | 2013-04-14 23:27:12 -0400 | [diff] [blame] | 48 | self.router_id = router['id'] |
| 49 | |
Tony Mack | 54304c6 | 2013-04-28 11:27:07 -0400 | [diff] [blame] | 50 | if self.id: |
| 51 | self.driver.update_tenant(self.tenant_id, |
| 52 | description=self.description, |
| 53 | enabled=self.enabled) |
| 54 | |
Tony Mack | 62bc59a | 2013-04-14 23:27:12 -0400 | [diff] [blame] | 55 | super(Slice, self).save(*args, **kwds) |
| 56 | |
| 57 | def delete(self, *args, **kwds): |
Tony Mack | 62bc59a | 2013-04-14 23:27:12 -0400 | [diff] [blame] | 58 | if self.tenant_id: |
Tony Mack | fdd4d80 | 2013-04-27 13:02:33 -0400 | [diff] [blame] | 59 | self.driver.delete_router(self.router_id) |
| 60 | self.driver.delete_network(self.network_id) |
| 61 | self.driver.delete_tenant(self.tenant_id) |
Tony Mack | 62bc59a | 2013-04-14 23:27:12 -0400 | [diff] [blame] | 62 | |
| 63 | super(Slice, self).delete(*args, **kwds) |
| 64 | |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 65 | class SliceMembership(PlCoreBase): |
Siobhan Tully | 5343728 | 2013-04-26 19:30:27 -0400 | [diff] [blame] | 66 | user = models.ForeignKey('PLUser', related_name='slice_memberships') |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 67 | slice = models.ForeignKey('Slice', related_name='slice_memberships') |
| 68 | role = models.ForeignKey('Role') |
| 69 | |
| 70 | def __unicode__(self): return u'%s %s %s' % (self.slice, self.user, self.role) |
| 71 | |
Tony Mack | 00d361f | 2013-04-28 10:28:42 -0400 | [diff] [blame] | 72 | def save(self, *args, **kwds): |
| 73 | self.driver.add_user_role(self.user.user_id, self.slice.tenant_id, self.role.role_type) |
| 74 | super(SliceMembership, self).save(*args, **kwds) |
| 75 | |
| 76 | def delete(self, *args, **kwds): |
| 77 | self.driver.delete_user_role(self.user.user_id, self.slice.tenant_id, self.role.role_type) |
| 78 | super(SliceMembership, self).delete(*args, **kwds) |