blob: 0ecc99d797a3dbea28488167310899d04e6d2ed4 [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
Siobhan Tullybf1153a2013-05-27 20:53:48 -04008from core.models import Deployment
Scott Bakere8d596f2013-05-13 23:17:13 -07009from core.models import ServiceClass
Siobhan Tullyde5450d2013-06-21 11:35:33 -040010from core.models import Tag
11from django.contrib.contenttypes import generic
Siobhan Tullyce652d02013-10-08 21:52:35 -040012from core.models import Service
Tony Macke4be32f2014-03-11 20:45:25 -040013from core.models import Deployment
Tony Mackdac85762014-09-03 13:19:42 -040014from django.core.exceptions import ValidationError
Siobhan Tully4bc09f22013-04-10 21:15:21 -040015
16# Create your models here.
17
18class Slice(PlCoreBase):
Tony Mack48952032013-04-12 11:49:34 -040019 name = models.CharField(unique=True, help_text="The Name of the Slice", max_length=80)
Siobhan Tully4bc09f22013-04-10 21:15:21 -040020 enabled = models.BooleanField(default=True, help_text="Status for this Slice")
Siobhan Tully4bc09f22013-04-10 21:15:21 -040021 omf_friendly = models.BooleanField()
22 description=models.TextField(blank=True,help_text="High level description of the slice and expected activities", max_length=1024)
23 slice_url = models.URLField(blank=True, max_length=512)
Tony Mack360afb82013-12-17 13:32:45 -050024 site = models.ForeignKey(Site, related_name='slices', help_text="The Site this Slice belongs to")
Tony Mack29bf5e82014-04-29 21:40:24 -040025 max_slivers = models.IntegerField(default=10)
Scott Baker27b5a8a2014-04-22 22:34:44 -070026 imagePreference = models.CharField(default="Ubuntu 12.04 LTS", null=True, blank=True, max_length=256)
Siobhan Tullyce652d02013-10-08 21:52:35 -040027 service = models.ForeignKey(Service, related_name='service', null=True, blank=True)
Scott Baker4b0cedd2014-04-22 21:01:21 -070028 network = models.CharField(default="Private Only",null=True, blank=True, max_length=256)
29 mountDataSets = models.CharField(default="GenBank",null=True, blank=True, max_length=256)
Siobhan Tullyde5450d2013-06-21 11:35:33 -040030 tags = generic.GenericRelation(Tag)
31
Scott Bakere8d596f2013-05-13 23:17:13 -070032 serviceClass = models.ForeignKey(ServiceClass, related_name = "slices", null=True, default=ServiceClass.get_default)
Tony Mack2bd5b412013-06-11 21:05:06 -040033 creator = models.ForeignKey(User, related_name='slices', blank=True, null=True)
Siobhan Tully231f4c82013-05-02 05:47:24 -040034
Siobhan Tully4bc09f22013-04-10 21:15:21 -040035 def __unicode__(self): return u'%s' % (self.name)
36
Tony Mack2721d6f2014-08-11 11:14:58 -040037 @property
38 def slicename(self):
39 return "%s_%s" % (self.site.login_base, self.name)
40
Tony Mack62bc59a2013-04-14 23:27:12 -040041 def save(self, *args, **kwds):
Tony Mackdac85762014-09-03 13:19:42 -040042
43 site = Site.objects.get(id=self.site.id)
44 if not self.name.startswith(site.login_base):
45 raise ValidationError('slice name must begin with %s' % site.login_base)
46
Scott Bakere8d596f2013-05-13 23:17:13 -070047 if self.serviceClass is None:
48 # We allowed None=True for serviceClass because Django evolution
49 # will fail unless it is allowed. But, we we really don't want it to
50 # ever save None, so fix it up here.
51 self.serviceClass = ServiceClass.get_default()
Tony Mack2bd5b412013-06-11 21:05:06 -040052 if not self.creator and hasattr(self, 'caller'):
53 self.creator = self.caller
Tony Mack62bc59a2013-04-14 23:27:12 -040054 super(Slice, self).save(*args, **kwds)
55
Tony Mack5b061472014-02-04 07:57:10 -050056 def can_update(self, user):
57 if user.is_readonly:
58 return False
59 if user.is_admin:
60 return True
Tony Mackc195d722014-08-20 11:15:17 -040061 # slice admins can update
Tony Mack5b061472014-02-04 07:57:10 -050062 slice_privs = SlicePrivilege.objects.filter(user=user, slice=self)
63 for slice_priv in slice_privs:
Tony Mackc195d722014-08-20 11:15:17 -040064 if slice_priv.role.role == 'admin':
Tony Mack5b061472014-02-04 07:57:10 -050065 return True
Tony Mackc195d722014-08-20 11:15:17 -040066 # site pis can update
67 site_privs = SitePrivilege.objects.filter(user=user, site=self.site)
68 for site_priv in site_privs:
69 if site_priv.role.role == 'pi':
70 return True
71
Tony Mack5b061472014-02-04 07:57:10 -050072 return False
73
Tony Mack5b061472014-02-04 07:57:10 -050074 @staticmethod
75 def select_by_user(user):
76 if user.is_admin:
77 qs = Slice.objects.all()
78 else:
Tony Mack33df82b2014-08-20 11:29:40 -040079 # users can see slices they belong to
Tony Mack5b061472014-02-04 07:57:10 -050080 slice_ids = [sp.slice.id for sp in SlicePrivilege.objects.filter(user=user)]
Tony Mack33df82b2014-08-20 11:29:40 -040081 # pis can see slices at their sites
82 sites = [sp.site for sp in SitePrivilege.objects.filter(user=user)\
83 if sp.role.role == 'pi']
84 slice_ids.extend([s.id for s in Slice.objects.filter(site__in=sites)])
Tony Mack5b061472014-02-04 07:57:10 -050085 qs = Slice.objects.filter(id__in=slice_ids)
86 return qs
87
Siobhan Tullybfd11dc2013-09-03 12:59:24 -040088class SliceRole(PlCoreBase):
Siobhan Tullycf04fb62014-01-11 11:25:57 -050089 ROLE_CHOICES = (('admin','Admin'),('default','Default'))
Siobhan Tullybfd11dc2013-09-03 12:59:24 -040090
91 role = models.CharField(choices=ROLE_CHOICES, unique=True, max_length=30)
92
93 def __unicode__(self): return u'%s' % (self.role)
94
95class SlicePrivilege(PlCoreBase):
96 user = models.ForeignKey('User', related_name='slice_privileges')
97 slice = models.ForeignKey('Slice', related_name='slice_privileges')
98 role = models.ForeignKey('SliceRole')
Siobhan Tully4bc09f22013-04-10 21:15:21 -040099
100 def __unicode__(self): return u'%s %s %s' % (self.slice, self.user, self.role)
Tony Mack5b061472014-02-04 07:57:10 -0500101
102 def can_update(self, user):
Tony Mackb7b4f842014-02-04 19:50:31 -0500103 return self.slice.can_update(user)
Tony Mack5b061472014-02-04 07:57:10 -0500104
Tony Mack5b061472014-02-04 07:57:10 -0500105 @staticmethod
106 def select_by_user(user):
107 if user.is_admin:
108 qs = SlicePrivilege.objects.all()
109 else:
110 sp_ids = [sp.id for sp in SlicePrivilege.objects.filter(user=user)]
111 qs = SlicePrivilege.objects.filter(id__in=sp_ids)
112 return qs
Tony Macke4be32f2014-03-11 20:45:25 -0400113
114class SliceDeployments(PlCoreBase):
115 slice = models.ForeignKey(Slice)
116 deployment = models.ForeignKey(Deployment)
Tony Mackc218b592014-03-19 15:17:53 -0400117 tenant_id = models.CharField(null=True, blank=True, max_length=200, help_text="Keystone tenant id")
Tony Macke4be32f2014-03-11 20:45:25 -0400118 network_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum network")
119 router_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum router id")
120 subnet_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum subnet id")
121
Tony Mackfe4be2e2014-03-17 19:18:55 -0400122 def __unicode__(self): return u'%s %s' % (self.slice, self.deployment)
Tony Macke4be32f2014-03-11 20:45:25 -0400123
124 @staticmethod
125 def select_by_user(user):
126 if user.is_admin:
127 qs = SliceDeployments.objects.all()
128 else:
129 slices = Slice.select_by_user(user)
130 qs = SliceDeployments.objects.filter(slice__in=slices)
131 return qs