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 |
Tony Mack | c195d72 | 2014-08-20 11:15:17 -0400 | [diff] [blame] | 5 | from core.models.site import SitePrivilege |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 6 | from core.models import User |
| 7 | from core.models import Role |
Sapan Bhatia | a16be69 | 2014-09-22 14:55:08 -0400 | [diff] [blame] | 8 | from core.models import Deployment,DeploymentLinkManager,DeploymentLinkDeletionManager |
Scott Baker | e8d596f | 2013-05-13 23:17:13 -0700 | [diff] [blame] | 9 | from core.models import ServiceClass |
Scott Baker | cfe0fd9 | 2014-09-12 12:24:24 -0700 | [diff] [blame] | 10 | from core.models.serviceclass import get_default_serviceclass |
Siobhan Tully | de5450d | 2013-06-21 11:35:33 -0400 | [diff] [blame] | 11 | from core.models import Tag |
| 12 | from django.contrib.contenttypes import generic |
Siobhan Tully | ce652d0 | 2013-10-08 21:52:35 -0400 | [diff] [blame] | 13 | from core.models import Service |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 14 | from core.models import Deployment |
Tony Mack | dac8576 | 2014-09-03 13:19:42 -0400 | [diff] [blame] | 15 | from django.core.exceptions import ValidationError |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 16 | |
| 17 | # Create your models here. |
| 18 | |
| 19 | class Slice(PlCoreBase): |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 20 | 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] | 21 | enabled = models.BooleanField(default=True, help_text="Status for this Slice") |
Scott Baker | cfe0fd9 | 2014-09-12 12:24:24 -0700 | [diff] [blame] | 22 | omf_friendly = models.BooleanField(default=False) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 23 | description=models.TextField(blank=True,help_text="High level description of the slice and expected activities", max_length=1024) |
| 24 | slice_url = models.URLField(blank=True, max_length=512) |
Tony Mack | 360afb8 | 2013-12-17 13:32:45 -0500 | [diff] [blame] | 25 | site = models.ForeignKey(Site, related_name='slices', help_text="The Site this Slice belongs to") |
Scott Baker | cfe0fd9 | 2014-09-12 12:24:24 -0700 | [diff] [blame] | 26 | max_slivers = models.IntegerField(default=10) |
Scott Baker | 27b5a8a | 2014-04-22 22:34:44 -0700 | [diff] [blame] | 27 | imagePreference = models.CharField(default="Ubuntu 12.04 LTS", null=True, blank=True, max_length=256) |
Siobhan Tully | ce652d0 | 2013-10-08 21:52:35 -0400 | [diff] [blame] | 28 | service = models.ForeignKey(Service, related_name='service', null=True, blank=True) |
Scott Baker | 4b0cedd | 2014-04-22 21:01:21 -0700 | [diff] [blame] | 29 | network = models.CharField(default="Private Only",null=True, blank=True, max_length=256) |
| 30 | mountDataSets = models.CharField(default="GenBank",null=True, blank=True, max_length=256) |
Siobhan Tully | de5450d | 2013-06-21 11:35:33 -0400 | [diff] [blame] | 31 | tags = generic.GenericRelation(Tag) |
| 32 | |
Scott Baker | cfe0fd9 | 2014-09-12 12:24:24 -0700 | [diff] [blame] | 33 | serviceClass = models.ForeignKey(ServiceClass, related_name = "slices", null=True, default=get_default_serviceclass) |
Tony Mack | 2bd5b41 | 2013-06-11 21:05:06 -0400 | [diff] [blame] | 34 | creator = models.ForeignKey(User, related_name='slices', blank=True, null=True) |
Siobhan Tully | 231f4c8 | 2013-05-02 05:47:24 -0400 | [diff] [blame] | 35 | |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 36 | def __unicode__(self): return u'%s' % (self.name) |
| 37 | |
Tony Mack | 2721d6f | 2014-08-11 11:14:58 -0400 | [diff] [blame] | 38 | @property |
| 39 | def slicename(self): |
| 40 | return "%s_%s" % (self.site.login_base, self.name) |
| 41 | |
Tony Mack | 62bc59a | 2013-04-14 23:27:12 -0400 | [diff] [blame] | 42 | def save(self, *args, **kwds): |
Tony Mack | dac8576 | 2014-09-03 13:19:42 -0400 | [diff] [blame] | 43 | |
| 44 | site = Site.objects.get(id=self.site.id) |
Tony Mack | ab5d887 | 2014-09-24 12:44:22 -0400 | [diff] [blame] | 45 | # allow preexisting slices to keep their original name for now |
| 46 | if not self.id and not self.name.startswith(site.login_base): |
Tony Mack | dac8576 | 2014-09-03 13:19:42 -0400 | [diff] [blame] | 47 | raise ValidationError('slice name must begin with %s' % site.login_base) |
| 48 | |
Scott Baker | e8d596f | 2013-05-13 23:17:13 -0700 | [diff] [blame] | 49 | if self.serviceClass is None: |
| 50 | # We allowed None=True for serviceClass because Django evolution |
| 51 | # will fail unless it is allowed. But, we we really don't want it to |
| 52 | # ever save None, so fix it up here. |
| 53 | self.serviceClass = ServiceClass.get_default() |
Tony Mack | 2bd5b41 | 2013-06-11 21:05:06 -0400 | [diff] [blame] | 54 | if not self.creator and hasattr(self, 'caller'): |
| 55 | self.creator = self.caller |
Tony Mack | 62bc59a | 2013-04-14 23:27:12 -0400 | [diff] [blame] | 56 | super(Slice, self).save(*args, **kwds) |
| 57 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 58 | def can_update(self, user): |
| 59 | if user.is_readonly: |
| 60 | return False |
| 61 | if user.is_admin: |
| 62 | return True |
Tony Mack | c195d72 | 2014-08-20 11:15:17 -0400 | [diff] [blame] | 63 | # slice admins can update |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 64 | slice_privs = SlicePrivilege.objects.filter(user=user, slice=self) |
| 65 | for slice_priv in slice_privs: |
Tony Mack | c195d72 | 2014-08-20 11:15:17 -0400 | [diff] [blame] | 66 | if slice_priv.role.role == 'admin': |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 67 | return True |
Tony Mack | c195d72 | 2014-08-20 11:15:17 -0400 | [diff] [blame] | 68 | # site pis can update |
| 69 | site_privs = SitePrivilege.objects.filter(user=user, site=self.site) |
| 70 | for site_priv in site_privs: |
| 71 | if site_priv.role.role == 'pi': |
| 72 | return True |
| 73 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 74 | return False |
| 75 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 76 | @staticmethod |
| 77 | def select_by_user(user): |
| 78 | if user.is_admin: |
| 79 | qs = Slice.objects.all() |
| 80 | else: |
Tony Mack | 33df82b | 2014-08-20 11:29:40 -0400 | [diff] [blame] | 81 | # users can see slices they belong to |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 82 | slice_ids = [sp.slice.id for sp in SlicePrivilege.objects.filter(user=user)] |
Tony Mack | 33df82b | 2014-08-20 11:29:40 -0400 | [diff] [blame] | 83 | # pis can see slices at their sites |
| 84 | sites = [sp.site for sp in SitePrivilege.objects.filter(user=user)\ |
| 85 | if sp.role.role == 'pi'] |
| 86 | slice_ids.extend([s.id for s in Slice.objects.filter(site__in=sites)]) |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 87 | qs = Slice.objects.filter(id__in=slice_ids) |
| 88 | return qs |
| 89 | |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 90 | class SliceRole(PlCoreBase): |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 91 | ROLE_CHOICES = (('admin','Admin'),('default','Default')) |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 92 | |
| 93 | role = models.CharField(choices=ROLE_CHOICES, unique=True, max_length=30) |
| 94 | |
| 95 | def __unicode__(self): return u'%s' % (self.role) |
| 96 | |
| 97 | class SlicePrivilege(PlCoreBase): |
| 98 | user = models.ForeignKey('User', related_name='slice_privileges') |
| 99 | slice = models.ForeignKey('Slice', related_name='slice_privileges') |
| 100 | role = models.ForeignKey('SliceRole') |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 101 | |
| 102 | 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] | 103 | |
| 104 | def can_update(self, user): |
Tony Mack | b7b4f84 | 2014-02-04 19:50:31 -0500 | [diff] [blame] | 105 | return self.slice.can_update(user) |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 106 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 107 | @staticmethod |
| 108 | def select_by_user(user): |
| 109 | if user.is_admin: |
| 110 | qs = SlicePrivilege.objects.all() |
| 111 | else: |
| 112 | sp_ids = [sp.id for sp in SlicePrivilege.objects.filter(user=user)] |
| 113 | qs = SlicePrivilege.objects.filter(id__in=sp_ids) |
| 114 | return qs |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 115 | |
| 116 | class SliceDeployments(PlCoreBase): |
Sapan Bhatia | a16be69 | 2014-09-22 14:55:08 -0400 | [diff] [blame] | 117 | objects = DeploymentLinkManager() |
| 118 | deleted_objects = DeploymentLinkDeletionManager() |
| 119 | |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 120 | slice = models.ForeignKey(Slice) |
| 121 | deployment = models.ForeignKey(Deployment) |
Tony Mack | c218b59 | 2014-03-19 15:17:53 -0400 | [diff] [blame] | 122 | tenant_id = models.CharField(null=True, blank=True, max_length=200, help_text="Keystone tenant id") |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 123 | network_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum network") |
| 124 | router_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum router id") |
| 125 | subnet_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum subnet id") |
| 126 | |
Tony Mack | fe4be2e | 2014-03-17 19:18:55 -0400 | [diff] [blame] | 127 | def __unicode__(self): return u'%s %s' % (self.slice, self.deployment) |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 128 | |
| 129 | @staticmethod |
| 130 | def select_by_user(user): |
| 131 | if user.is_admin: |
| 132 | qs = SliceDeployments.objects.all() |
| 133 | else: |
| 134 | slices = Slice.select_by_user(user) |
| 135 | qs = SliceDeployments.objects.filter(slice__in=slices) |
| 136 | return qs |