blob: d031766863f1e10a053cc2c059531b5e19e24297 [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
5from core.models import User
6from core.models import Role
Siobhan Tullybf1153a2013-05-27 20:53:48 -04007from core.models import Deployment
Scott Bakere8d596f2013-05-13 23:17:13 -07008from core.models import ServiceClass
Siobhan Tullyde5450d2013-06-21 11:35:33 -04009from core.models import Tag
10from django.contrib.contenttypes import generic
Siobhan Tullyce652d02013-10-08 21:52:35 -040011from core.models import Service
Tony Macke4be32f2014-03-11 20:45:25 -040012from core.models import Deployment
Siobhan Tully4bc09f22013-04-10 21:15:21 -040013
14# Create your models here.
15
16class Slice(PlCoreBase):
Tony Mackcdec0902013-04-15 00:38:49 -040017 tenant_id = models.CharField(max_length=200, help_text="Keystone tenant id")
Tony Mack48952032013-04-12 11:49:34 -040018 name = models.CharField(unique=True, help_text="The Name of the Slice", max_length=80)
Siobhan Tully4bc09f22013-04-10 21:15:21 -040019 enabled = models.BooleanField(default=True, help_text="Status for this Slice")
Siobhan Tully4bc09f22013-04-10 21:15:21 -040020 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 Mack360afb82013-12-17 13:32:45 -050023 site = models.ForeignKey(Site, related_name='slices', help_text="The Site this Slice belongs to")
Tony Mack85d18832013-05-09 17:02:31 -040024 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")
Scott Baker537d8232014-04-08 23:40:56 -070027 imagePreference = models.CharField(default="abc",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)
Siobhan Tully4bc09f22013-04-10 21:15:21 -040029
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 Mack62bc59a2013-04-14 23:27:12 -040037 def save(self, *args, **kwds):
Scott Bakere8d596f2013-05-13 23:17:13 -070038 if self.serviceClass is None:
39 # We allowed None=True for serviceClass because Django evolution
40 # will fail unless it is allowed. But, we we really don't want it to
41 # ever save None, so fix it up here.
42 self.serviceClass = ServiceClass.get_default()
Tony Mack2bd5b412013-06-11 21:05:06 -040043 if not self.creator and hasattr(self, 'caller'):
44 self.creator = self.caller
Tony Mack62bc59a2013-04-14 23:27:12 -040045 super(Slice, self).save(*args, **kwds)
46
Tony Mack5b061472014-02-04 07:57:10 -050047 def can_update(self, user):
48 if user.is_readonly:
49 return False
50 if user.is_admin:
51 return True
52 slice_privs = SlicePrivilege.objects.filter(user=user, slice=self)
53 for slice_priv in slice_privs:
Tony Mackb7b4f842014-02-04 19:50:31 -050054 if slice_priv.role.role == 'admin':
Tony Mack5b061472014-02-04 07:57:10 -050055 return True
56 return False
57
Tony Mack5b061472014-02-04 07:57:10 -050058 @staticmethod
59 def select_by_user(user):
60 if user.is_admin:
61 qs = Slice.objects.all()
62 else:
63 slice_ids = [sp.slice.id for sp in SlicePrivilege.objects.filter(user=user)]
64 qs = Slice.objects.filter(id__in=slice_ids)
65 return qs
66
Siobhan Tullybfd11dc2013-09-03 12:59:24 -040067class SliceRole(PlCoreBase):
Siobhan Tullycf04fb62014-01-11 11:25:57 -050068 ROLE_CHOICES = (('admin','Admin'),('default','Default'))
Siobhan Tullybfd11dc2013-09-03 12:59:24 -040069
70 role = models.CharField(choices=ROLE_CHOICES, unique=True, max_length=30)
71
72 def __unicode__(self): return u'%s' % (self.role)
73
74class SlicePrivilege(PlCoreBase):
75 user = models.ForeignKey('User', related_name='slice_privileges')
76 slice = models.ForeignKey('Slice', related_name='slice_privileges')
77 role = models.ForeignKey('SliceRole')
Siobhan Tully4bc09f22013-04-10 21:15:21 -040078
79 def __unicode__(self): return u'%s %s %s' % (self.slice, self.user, self.role)
Tony Mack5b061472014-02-04 07:57:10 -050080
81 def can_update(self, user):
Tony Mackb7b4f842014-02-04 19:50:31 -050082 return self.slice.can_update(user)
Tony Mack5b061472014-02-04 07:57:10 -050083
Tony Mack5b061472014-02-04 07:57:10 -050084 @staticmethod
85 def select_by_user(user):
86 if user.is_admin:
87 qs = SlicePrivilege.objects.all()
88 else:
89 sp_ids = [sp.id for sp in SlicePrivilege.objects.filter(user=user)]
90 qs = SlicePrivilege.objects.filter(id__in=sp_ids)
91 return qs
Tony Macke4be32f2014-03-11 20:45:25 -040092
93class SliceDeployments(PlCoreBase):
94 slice = models.ForeignKey(Slice)
95 deployment = models.ForeignKey(Deployment)
Tony Mackc218b592014-03-19 15:17:53 -040096 tenant_id = models.CharField(null=True, blank=True, max_length=200, help_text="Keystone tenant id")
Tony Macke4be32f2014-03-11 20:45:25 -040097 network_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum network")
98 router_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum router id")
99 subnet_id = models.CharField(null=True, blank=True, max_length=256, help_text="Quantum subnet id")
Tony Mackc218b592014-03-19 15:17:53 -0400100 keyname = models.CharField(null=True, blank=True, max_length=256, help_text="Nova keypair name")
Tony Macke4be32f2014-03-11 20:45:25 -0400101
Tony Mackfe4be2e2014-03-17 19:18:55 -0400102 def __unicode__(self): return u'%s %s' % (self.slice, self.deployment)
Tony Macke4be32f2014-03-11 20:45:25 -0400103
104 @staticmethod
105 def select_by_user(user):
106 if user.is_admin:
107 qs = SliceDeployments.objects.all()
108 else:
109 slices = Slice.select_by_user(user)
110 qs = SliceDeployments.objects.filter(slice__in=slices)
111 return qs