blob: 128d6055cb607ea1faef0ca682a320598a05b1d1 [file] [log] [blame]
Siobhan Tully4bc09f22013-04-10 21:15:21 -04001import os
2from django.db import models
Siobhan Tully30fd4292013-05-10 08:59:56 -04003from core.models import PlCoreBase
4from core.models import Site
Tony Mackc195d722014-08-20 11:15:17 -04005from core.models.site import SitePrivilege
Siobhan Tully30fd4292013-05-10 08:59:56 -04006from core.models import User
7from core.models import Role
Sapan Bhatiaa16be692014-09-22 14:55:08 -04008from core.models import Deployment,DeploymentLinkManager,DeploymentLinkDeletionManager
Scott Bakere8d596f2013-05-13 23:17:13 -07009from core.models import ServiceClass
Scott Bakercfe0fd92014-09-12 12:24:24 -070010from core.models.serviceclass import get_default_serviceclass
Siobhan Tullyde5450d2013-06-21 11:35:33 -040011from core.models import Tag
12from django.contrib.contenttypes import generic
Siobhan Tullyce652d02013-10-08 21:52:35 -040013from core.models import Service
Tony Macke4be32f2014-03-11 20:45:25 -040014from core.models import Deployment
Tony Mackdac85762014-09-03 13:19:42 -040015from django.core.exceptions import ValidationError
Siobhan Tully4bc09f22013-04-10 21:15:21 -040016
17# Create your models here.
18
19class Slice(PlCoreBase):
Tony Mack48952032013-04-12 11:49:34 -040020 name = models.CharField(unique=True, help_text="The Name of the Slice", max_length=80)
Siobhan Tully4bc09f22013-04-10 21:15:21 -040021 enabled = models.BooleanField(default=True, help_text="Status for this Slice")
Scott Bakercfe0fd92014-09-12 12:24:24 -070022 omf_friendly = models.BooleanField(default=False)
Siobhan Tully4bc09f22013-04-10 21:15:21 -040023 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 Mack360afb82013-12-17 13:32:45 -050025 site = models.ForeignKey(Site, related_name='slices', help_text="The Site this Slice belongs to")
Scott Bakercfe0fd92014-09-12 12:24:24 -070026 max_slivers = models.IntegerField(default=10)
Scott Baker27b5a8a2014-04-22 22:34:44 -070027 imagePreference = models.CharField(default="Ubuntu 12.04 LTS", null=True, blank=True, max_length=256)
Siobhan Tullyce652d02013-10-08 21:52:35 -040028 service = models.ForeignKey(Service, related_name='service', null=True, blank=True)
Scott Baker4b0cedd2014-04-22 21:01:21 -070029 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 Tullyde5450d2013-06-21 11:35:33 -040031 tags = generic.GenericRelation(Tag)
32
Scott Bakercfe0fd92014-09-12 12:24:24 -070033 serviceClass = models.ForeignKey(ServiceClass, related_name = "slices", null=True, default=get_default_serviceclass)
Tony Mack2bd5b412013-06-11 21:05:06 -040034 creator = models.ForeignKey(User, related_name='slices', blank=True, null=True)
Siobhan Tully231f4c82013-05-02 05:47:24 -040035
Siobhan Tully4bc09f22013-04-10 21:15:21 -040036 def __unicode__(self): return u'%s' % (self.name)
37
Tony Mack2721d6f2014-08-11 11:14:58 -040038 @property
39 def slicename(self):
40 return "%s_%s" % (self.site.login_base, self.name)
41
Tony Mack62bc59a2013-04-14 23:27:12 -040042 def save(self, *args, **kwds):
Tony Mackdac85762014-09-03 13:19:42 -040043
44 site = Site.objects.get(id=self.site.id)
Tony Mackab5d8872014-09-24 12:44:22 -040045 # allow preexisting slices to keep their original name for now
46 if not self.id and not self.name.startswith(site.login_base):
Tony Mackdac85762014-09-03 13:19:42 -040047 raise ValidationError('slice name must begin with %s' % site.login_base)
48
Scott Bakere8d596f2013-05-13 23:17:13 -070049 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 Mack2bd5b412013-06-11 21:05:06 -040054 if not self.creator and hasattr(self, 'caller'):
55 self.creator = self.caller
Tony Mack62bc59a2013-04-14 23:27:12 -040056 super(Slice, self).save(*args, **kwds)
57
Tony Mack5b061472014-02-04 07:57:10 -050058 def can_update(self, user):
59 if user.is_readonly:
60 return False
61 if user.is_admin:
62 return True
Tony Mackc195d722014-08-20 11:15:17 -040063 # slice admins can update
Tony Mack5b061472014-02-04 07:57:10 -050064 slice_privs = SlicePrivilege.objects.filter(user=user, slice=self)
65 for slice_priv in slice_privs:
Tony Mackc195d722014-08-20 11:15:17 -040066 if slice_priv.role.role == 'admin':
Tony Mack5b061472014-02-04 07:57:10 -050067 return True
Tony Mackc195d722014-08-20 11:15:17 -040068 # 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 Mack5b061472014-02-04 07:57:10 -050074 return False
75
Tony Mack5b061472014-02-04 07:57:10 -050076 @staticmethod
77 def select_by_user(user):
78 if user.is_admin:
79 qs = Slice.objects.all()
80 else:
Tony Mack33df82b2014-08-20 11:29:40 -040081 # users can see slices they belong to
Tony Mack5b061472014-02-04 07:57:10 -050082 slice_ids = [sp.slice.id for sp in SlicePrivilege.objects.filter(user=user)]
Tony Mack33df82b2014-08-20 11:29:40 -040083 # 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 Mack5b061472014-02-04 07:57:10 -050087 qs = Slice.objects.filter(id__in=slice_ids)
88 return qs
89
Tony Mackc767c982014-10-22 13:30:41 -040090 def delete(self, *args, **kwds):
91 # delete networks associated with this slice
92 from core.models.network import Network
93 nets = Network.objects.filter(slices=self)
94 nets.delete()
Tony Mack9b53a4c2014-11-12 10:01:26 -050095 # delete slice deployments
Sapan Bhatiabf7856b2014-11-19 15:09:38 -050096 slice_deployments = SliceDeploymentss.objects.filter(slice=self)
Tony Mack9b53a4c2014-11-12 10:01:26 -050097 slice_deployments.delete()
98 # delete slice privilege
99 slice_privileges = SlicePrivilege.objects.filter(slice=self)
100 slice_privileges.delete()
101 # continue with normal delete
Tony Mackc767c982014-10-22 13:30:41 -0400102 super(SliceAdmin, self).delete(*args, **kwds)
Tony Mack9b53a4c2014-11-12 10:01:26 -0500103
Tony Mackc767c982014-10-22 13:30:41 -0400104
Siobhan Tullybfd11dc2013-09-03 12:59:24 -0400105class SliceRole(PlCoreBase):
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500106 ROLE_CHOICES = (('admin','Admin'),('default','Default'))
Siobhan Tullybfd11dc2013-09-03 12:59:24 -0400107
108 role = models.CharField(choices=ROLE_CHOICES, unique=True, max_length=30)
109
110 def __unicode__(self): return u'%s' % (self.role)
111
112class SlicePrivilege(PlCoreBase):
Sapan Bhatia13d2db92014-11-11 21:47:45 -0500113 user = models.ForeignKey('User', related_name='sliceprivileges')
114 slice = models.ForeignKey('Slice', related_name='sliceprivileges')
115 role = models.ForeignKey('SliceRole',related_name='sliceprivileges')
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400116
117 def __unicode__(self): return u'%s %s %s' % (self.slice, self.user, self.role)
Tony Mack5b061472014-02-04 07:57:10 -0500118
119 def can_update(self, user):
Tony Mackb7b4f842014-02-04 19:50:31 -0500120 return self.slice.can_update(user)
Tony Mack5b061472014-02-04 07:57:10 -0500121
Tony Mack5b061472014-02-04 07:57:10 -0500122 @staticmethod
123 def select_by_user(user):
124 if user.is_admin:
125 qs = SlicePrivilege.objects.all()
126 else:
127 sp_ids = [sp.id for sp in SlicePrivilege.objects.filter(user=user)]
128 qs = SlicePrivilege.objects.filter(id__in=sp_ids)
129 return qs
Tony Macke4be32f2014-03-11 20:45:25 -0400130
Sapan Bhatiabf7856b2014-11-19 15:09:38 -0500131class SliceDeployments(PlCoreBase):
Sapan Bhatiaa16be692014-09-22 14:55:08 -0400132 objects = DeploymentLinkManager()
133 deleted_objects = DeploymentLinkDeletionManager()
134
Sapan Bhatia475c5972014-11-05 10:32:41 -0500135 slice = models.ForeignKey(Slice, related_name='slicedeployments')
136 deployment = models.ForeignKey(Deployment, related_name='slicedeployments')
Tony Mackc218b592014-03-19 15:17:53 -0400137 tenant_id = models.CharField(null=True, blank=True, max_length=200, help_text="Keystone tenant id")
Tony Macke4be32f2014-03-11 20:45:25 -0400138 network_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum network")
139 router_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum router id")
140 subnet_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum subnet id")
141
Tony Mackfe4be2e2014-03-17 19:18:55 -0400142 def __unicode__(self): return u'%s %s' % (self.slice, self.deployment)
Tony Macke4be32f2014-03-11 20:45:25 -0400143
144 @staticmethod
145 def select_by_user(user):
146 if user.is_admin:
Sapan Bhatiabf7856b2014-11-19 15:09:38 -0500147 qs = SliceDeployments.objects.all()
Tony Macke4be32f2014-03-11 20:45:25 -0400148 else:
149 slices = Slice.select_by_user(user)
Sapan Bhatiabf7856b2014-11-19 15:09:38 -0500150 qs = SliceDeployments.objects.filter(slice__in=slices)
Tony Macke4be32f2014-03-11 20:45:25 -0400151 return qs