Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 1 | import os |
| 2 | from django.db import models |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 3 | from core.models import PlCoreBase |
| 4 | from core.models import Site |
| 5 | from core.models import User |
| 6 | from core.models import Role |
Siobhan Tully | bf1153a | 2013-05-27 20:53:48 -0400 | [diff] [blame] | 7 | from core.models import Deployment |
Scott Baker | e8d596f | 2013-05-13 23:17:13 -0700 | [diff] [blame] | 8 | from core.models import ServiceClass |
Siobhan Tully | de5450d | 2013-06-21 11:35:33 -0400 | [diff] [blame] | 9 | from core.models import Tag |
| 10 | from django.contrib.contenttypes import generic |
Siobhan Tully | ce652d0 | 2013-10-08 21:52:35 -0400 | [diff] [blame] | 11 | from core.models import Service |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 12 | from core.models import Deployment |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 13 | |
| 14 | # Create your models here. |
| 15 | |
| 16 | class Slice(PlCoreBase): |
Tony Mack | cdec090 | 2013-04-15 00:38:49 -0400 | [diff] [blame] | 17 | tenant_id = models.CharField(max_length=200, help_text="Keystone tenant id") |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 18 | 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] | 19 | enabled = models.BooleanField(default=True, help_text="Status for this Slice") |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 20 | omf_friendly = models.BooleanField() |
| 21 | description=models.TextField(blank=True,help_text="High level description of the slice and expected activities", max_length=1024) |
| 22 | slice_url = models.URLField(blank=True, max_length=512) |
Tony Mack | 360afb8 | 2013-12-17 13:32:45 -0500 | [diff] [blame] | 23 | site = models.ForeignKey(Site, related_name='slices', help_text="The Site this Slice belongs to") |
Tony Mack | 85d1883 | 2013-05-09 17:02:31 -0400 | [diff] [blame] | 24 | network_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum network") |
| 25 | router_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum router id") |
| 26 | subnet_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum subnet id") |
Siobhan Tully | ce652d0 | 2013-10-08 21:52:35 -0400 | [diff] [blame] | 27 | service = models.ForeignKey(Service, related_name='service', null=True, blank=True) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 28 | |
Siobhan Tully | de5450d | 2013-06-21 11:35:33 -0400 | [diff] [blame] | 29 | tags = generic.GenericRelation(Tag) |
| 30 | |
Scott Baker | e8d596f | 2013-05-13 23:17:13 -0700 | [diff] [blame] | 31 | serviceClass = models.ForeignKey(ServiceClass, related_name = "slices", null=True, default=ServiceClass.get_default) |
Tony Mack | 2bd5b41 | 2013-06-11 21:05:06 -0400 | [diff] [blame] | 32 | creator = models.ForeignKey(User, related_name='slices', blank=True, null=True) |
Siobhan Tully | 231f4c8 | 2013-05-02 05:47:24 -0400 | [diff] [blame] | 33 | |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 34 | def __unicode__(self): return u'%s' % (self.name) |
| 35 | |
Tony Mack | 62bc59a | 2013-04-14 23:27:12 -0400 | [diff] [blame] | 36 | def save(self, *args, **kwds): |
Scott Baker | e8d596f | 2013-05-13 23:17:13 -0700 | [diff] [blame] | 37 | if self.serviceClass is None: |
| 38 | # We allowed None=True for serviceClass because Django evolution |
| 39 | # will fail unless it is allowed. But, we we really don't want it to |
| 40 | # ever save None, so fix it up here. |
| 41 | self.serviceClass = ServiceClass.get_default() |
Tony Mack | 2bd5b41 | 2013-06-11 21:05:06 -0400 | [diff] [blame] | 42 | if not self.creator and hasattr(self, 'caller'): |
| 43 | self.creator = self.caller |
Tony Mack | 62bc59a | 2013-04-14 23:27:12 -0400 | [diff] [blame] | 44 | super(Slice, self).save(*args, **kwds) |
| 45 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 46 | def can_update(self, user): |
| 47 | if user.is_readonly: |
| 48 | return False |
| 49 | if user.is_admin: |
| 50 | return True |
| 51 | slice_privs = SlicePrivilege.objects.filter(user=user, slice=self) |
| 52 | for slice_priv in slice_privs: |
Tony Mack | b7b4f84 | 2014-02-04 19:50:31 -0500 | [diff] [blame] | 53 | if slice_priv.role.role == 'admin': |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 54 | return True |
| 55 | return False |
| 56 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 57 | @staticmethod |
| 58 | def select_by_user(user): |
| 59 | if user.is_admin: |
| 60 | qs = Slice.objects.all() |
| 61 | else: |
| 62 | slice_ids = [sp.slice.id for sp in SlicePrivilege.objects.filter(user=user)] |
| 63 | qs = Slice.objects.filter(id__in=slice_ids) |
| 64 | return qs |
| 65 | |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 66 | class SliceRole(PlCoreBase): |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 67 | ROLE_CHOICES = (('admin','Admin'),('default','Default')) |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 68 | |
| 69 | role = models.CharField(choices=ROLE_CHOICES, unique=True, max_length=30) |
| 70 | |
| 71 | def __unicode__(self): return u'%s' % (self.role) |
| 72 | |
| 73 | class SlicePrivilege(PlCoreBase): |
| 74 | user = models.ForeignKey('User', related_name='slice_privileges') |
| 75 | slice = models.ForeignKey('Slice', related_name='slice_privileges') |
| 76 | role = models.ForeignKey('SliceRole') |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 77 | |
| 78 | def __unicode__(self): return u'%s %s %s' % (self.slice, self.user, self.role) |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 79 | |
| 80 | def can_update(self, user): |
Tony Mack | b7b4f84 | 2014-02-04 19:50:31 -0500 | [diff] [blame] | 81 | return self.slice.can_update(user) |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 82 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 83 | @staticmethod |
| 84 | def select_by_user(user): |
| 85 | if user.is_admin: |
| 86 | qs = SlicePrivilege.objects.all() |
| 87 | else: |
| 88 | sp_ids = [sp.id for sp in SlicePrivilege.objects.filter(user=user)] |
| 89 | qs = SlicePrivilege.objects.filter(id__in=sp_ids) |
| 90 | return qs |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 91 | |
| 92 | class SliceDeployments(PlCoreBase): |
| 93 | slice = models.ForeignKey(Slice) |
| 94 | deployment = models.ForeignKey(Deployment) |
| 95 | tenant_id = models.CharField(max_length=200, help_text="Keystone tenant id") |
| 96 | network_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum network") |
| 97 | router_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum router id") |
| 98 | subnet_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum subnet id") |
| 99 | |
Tony Mack | fe4be2e | 2014-03-17 19:18:55 -0400 | [diff] [blame] | 100 | def __unicode__(self): return u'%s %s' % (self.slice, self.deployment) |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 101 | |
| 102 | @staticmethod |
| 103 | def select_by_user(user): |
| 104 | if user.is_admin: |
| 105 | qs = SliceDeployments.objects.all() |
| 106 | else: |
| 107 | slices = Slice.select_by_user(user) |
| 108 | qs = SliceDeployments.objects.filter(slice__in=slices) |
| 109 | return qs |