2 from django.db import models
3 from core.models import PlCoreBase
4 from core.models import Site
5 from core.models.site import SitePrivilege
6 from core.models import User
7 from core.models import Role
8 from core.models import Deployment,DeploymentLinkManager,DeploymentLinkDeletionManager
9 from core.models import ServiceClass
10 from core.models.serviceclass import get_default_serviceclass
11 from core.models import Tag
12 from django.contrib.contenttypes import generic
13 from core.models import Service
14 from core.models import Deployment
15 from django.core.exceptions import ValidationError
17 # Create your models here.
19 class Slice(PlCoreBase):
20 name = models.CharField(unique=True, help_text="The Name of the Slice", max_length=80)
21 enabled = models.BooleanField(default=True, help_text="Status for this Slice")
22 omf_friendly = models.BooleanField(default=False)
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)
25 site = models.ForeignKey(Site, related_name='slices', help_text="The Site this Slice belongs to")
26 max_slivers = models.IntegerField(default=10)
27 imagePreference = models.CharField(default="Ubuntu 12.04 LTS", null=True, blank=True, max_length=256)
28 service = models.ForeignKey(Service, related_name='service', null=True, blank=True)
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)
31 tags = generic.GenericRelation(Tag)
33 serviceClass = models.ForeignKey(ServiceClass, related_name = "slices", null=True, default=get_default_serviceclass)
34 creator = models.ForeignKey(User, related_name='slices', blank=True, null=True)
36 def __unicode__(self): return u'%s' % (self.name)
40 return "%s_%s" % (self.site.login_base, self.name)
42 def save(self, *args, **kwds):
44 site = Site.objects.get(id=self.site.id)
45 # allow preexisting slices to keep their original name for now
46 if not self.id and not self.name.startswith(site.login_base):
47 raise ValidationError('slice name must begin with %s' % site.login_base)
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()
54 if not self.creator and hasattr(self, 'caller'):
55 self.creator = self.caller
56 super(Slice, self).save(*args, **kwds)
58 def can_update(self, user):
63 # slice admins can update
64 slice_privs = SlicePrivilege.objects.filter(user=user, slice=self)
65 for slice_priv in slice_privs:
66 if slice_priv.role.role == 'admin':
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':
77 def select_by_user(user):
79 qs = Slice.objects.all()
81 # users can see slices they belong to
82 slice_ids = [sp.slice.id for sp in SlicePrivilege.objects.filter(user=user)]
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)])
87 qs = Slice.objects.filter(id__in=slice_ids)
90 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)
95 super(SliceAdmin, self).delete(*args, **kwds)
97 class SliceRole(PlCoreBase):
98 ROLE_CHOICES = (('admin','Admin'),('default','Default'))
100 role = models.CharField(choices=ROLE_CHOICES, unique=True, max_length=30)
102 def __unicode__(self): return u'%s' % (self.role)
104 class SlicePrivilege(PlCoreBase):
105 user = models.ForeignKey('User', related_name='slice_privileges')
106 slice = models.ForeignKey('Slice', related_name='slice_privileges')
107 role = models.ForeignKey('SliceRole')
109 def __unicode__(self): return u'%s %s %s' % (self.slice, self.user, self.role)
111 def can_update(self, user):
112 return self.slice.can_update(user)
115 def select_by_user(user):
117 qs = SlicePrivilege.objects.all()
119 sp_ids = [sp.id for sp in SlicePrivilege.objects.filter(user=user)]
120 qs = SlicePrivilege.objects.filter(id__in=sp_ids)
123 class SliceDeployments(PlCoreBase):
124 objects = DeploymentLinkManager()
125 deleted_objects = DeploymentLinkDeletionManager()
127 slice = models.ForeignKey(Slice)
128 deployment = models.ForeignKey(Deployment)
129 tenant_id = models.CharField(null=True, blank=True, max_length=200, help_text="Keystone tenant id")
130 network_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum network")
131 router_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum router id")
132 subnet_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum subnet id")
134 def __unicode__(self): return u'%s %s' % (self.slice, self.deployment)
137 def select_by_user(user):
139 qs = SliceDeployments.objects.all()
141 slices = Slice.select_by_user(user)
142 qs = SliceDeployments.objects.filter(slice__in=slices)