blob: d3aef81e58a2025b3c7eb398766c43331e2fe835 [file] [log] [blame]
Siobhan Tully4bc09f22013-04-10 21:15:21 -04001import os
2from django.db import models
Sapan Bhatiac6680c12014-09-19 16:47:07 -04003from core.models import PlCoreBase,PlCoreBaseManager,PlCoreBaseDeletionManager
Siobhan Tullyde5450d2013-06-21 11:35:33 -04004from core.models import Tag
5from django.contrib.contenttypes import generic
Siobhan Tully567e3e62013-06-21 18:03:16 -04006from geoposition.fields import GeopositionField
Scott Baker5380c522014-06-06 14:49:43 -07007from core.acl import AccessControlList
Sapan Bhatia699a0442014-09-22 17:25:06 -04008from planetstack.config import Config
9
10config = Config()
Siobhan Tully4bc09f22013-04-10 21:15:21 -040011
Sapan Bhatia71058ec2014-09-19 16:48:36 -040012class DeploymentLinkDeletionManager(PlCoreBaseDeletionManager):
13 def get_queryset(self):
14 parent=super(DeploymentLinkDeletionManager, self)
15 if hasattr(parent, "get_queryset"):
16 return parent.get_queryset().filter(Q(deployment__backend_type=config.observer_backend_type)|Q(backend_type=None))
17 else:
18 return parent.get_queryset().filter(Q(deployment__backend_type=config.observer_backend_type)|Q(backend_type=None))
19
20 # deprecated in django 1.7 in favor of get_queryset().
21 def get_query_set(self):
22 return self.get_queryset()
23
24
Sapan Bhatia005d7612014-09-19 16:48:11 -040025class DeploymentDeletionManager(PlCoreBaseDeletionManager):
26 def get_queryset(self):
27 parent=super(DeploymentDeletionManager, self)
28 if hasattr(parent, "get_queryset"):
29 return parent.get_queryset().filter(Q(backend_type=config.observer_backend_type)|Q(backend_type=None))
30 else:
31 return parent.get_queryset().filter(Q(backend_type=config.observer_backend_type)|Q(backend_type=None))
32
33 # deprecated in django 1.7 in favor of get_queryset().
34 def get_query_set(self):
35 return self.get_queryset()
36
Sapan Bhatiad8a6eb52014-09-19 16:47:40 -040037class DeploymentLinkManager(PlCoreBaseManager):
38 def get_queryset(self):
39 parent=super(DeploymentLinkManager, self)
40 if hasattr(parent, "get_queryset"):
41 return parent.get_queryset().filter(Q(deployment__backend_type=config.observer_backend_type)|Q(backend_type=None))
42 else:
43 return parent.get_queryset().filter(Q(deployment__backend_type=config.observer_backend_type)|Q(backend_type=None))
44
45 # deprecated in django 1.7 in favor of get_queryset().
46 def get_query_set(self):
47 return self.get_queryset()
48
49
Sapan Bhatiac6680c12014-09-19 16:47:07 -040050class DeploymentManager(PlCoreBaseManager):
51 def get_queryset(self):
52 parent=super(DeploymentManager, self)
53 if hasattr(parent, "get_queryset"):
54 return parent.get_queryset().filter(Q(backend_type=config.observer_backend_type)|Q(backend_type=None))
55 else:
56 return parent.get_queryset().filter(Q(backend_type=config.observer_backend_type)|Q(backend_type=None))
57
58 # deprecated in django 1.7 in favor of get_queryset().
59 def get_query_set(self):
60 return self.get_queryset()
61
Siobhan Tully4bc09f22013-04-10 21:15:21 -040062class Site(PlCoreBase):
Siobhan Tullycf04fb62014-01-11 11:25:57 -050063 """
64 A logical grouping of Nodes that are co-located at the same geographic location, which also typically corresponds to the Nodes' location in the physical network.
65 """
Siobhan Tully4bc09f22013-04-10 21:15:21 -040066 name = models.CharField(max_length=200, help_text="Name for this Site")
67 site_url = models.URLField(null=True, blank=True, max_length=512, help_text="Site's Home URL Page")
68 enabled = models.BooleanField(default=True, help_text="Status for this Site")
Siobhan Tully567e3e62013-06-21 18:03:16 -040069 location = GeopositionField()
Siobhan Tully4bc09f22013-04-10 21:15:21 -040070 longitude = models.FloatField(null=True, blank=True)
71 latitude = models.FloatField(null=True, blank=True)
72 login_base = models.CharField(max_length=50, unique=True, help_text="Prefix for Slices associated with this Site")
73 is_public = models.BooleanField(default=True, help_text="Indicates the visibility of this site to other members")
74 abbreviated_name = models.CharField(max_length=80)
75
Tony Macke4be32f2014-03-11 20:45:25 -040076 #deployments = models.ManyToManyField('Deployment', blank=True, related_name='sites')
Sapan Bhatia378baea2014-06-13 13:37:46 -040077 deployments = models.ManyToManyField('Deployment', through='SiteDeployments', blank=True, help_text="Select which sites are allowed to host nodes in this deployment", related_name='sites')
Siobhan Tullyde5450d2013-06-21 11:35:33 -040078 tags = generic.GenericRelation(Tag)
Siobhan Tully4bc09f22013-04-10 21:15:21 -040079
80 def __unicode__(self): return u'%s' % (self.name)
81
Tony Mack5b061472014-02-04 07:57:10 -050082 def can_update(self, user):
Tony Mackb7b4f842014-02-04 19:50:31 -050083 if user.is_readonly:
84 return False
Tony Mack5b061472014-02-04 07:57:10 -050085 if user.is_admin:
86 return True
87 site_privs = SitePrivilege.objects.filter(user=user, site=self)
88 for site_priv in site_privs:
Tony Mackb7b4f842014-02-04 19:50:31 -050089 if site_priv.role.role == 'pi':
Tony Mack5b061472014-02-04 07:57:10 -050090 return True
91 return False
92
Tony Mack5b061472014-02-04 07:57:10 -050093 @staticmethod
94 def select_by_user(user):
95 if user.is_admin:
96 qs = Site.objects.all()
97 else:
98 site_ids = [sp.site.id for sp in SitePrivilege.objects.filter(user=user)]
99 site_ids.append(user.site.id)
100 qs = Site.objects.filter(id__in=site_ids)
101 return qs
102
103
Siobhan Tullybfd11dc2013-09-03 12:59:24 -0400104class SiteRole(PlCoreBase):
105
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500106 ROLE_CHOICES = (('admin','Admin'),('pi','PI'),('tech','Tech'),('billing','Billing'))
Siobhan Tullybfd11dc2013-09-03 12:59:24 -0400107 role = models.CharField(choices=ROLE_CHOICES, unique=True, max_length=30)
108
109 def __unicode__(self): return u'%s' % (self.role)
110
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400111class SitePrivilege(PlCoreBase):
112
Siobhan Tully30fd4292013-05-10 08:59:56 -0400113 user = models.ForeignKey('User', related_name='site_privileges')
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400114 site = models.ForeignKey('Site', related_name='site_privileges')
Siobhan Tullybfd11dc2013-09-03 12:59:24 -0400115 role = models.ForeignKey('SiteRole')
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400116
117 def __unicode__(self): return u'%s %s %s' % (self.site, self.user, self.role)
118
Tony Mack00d361f2013-04-28 10:28:42 -0400119 def save(self, *args, **kwds):
Tony Mack00d361f2013-04-28 10:28:42 -0400120 super(SitePrivilege, self).save(*args, **kwds)
121
122 def delete(self, *args, **kwds):
Tony Mack00d361f2013-04-28 10:28:42 -0400123 super(SitePrivilege, self).delete(*args, **kwds)
124
Tony Mack5b061472014-02-04 07:57:10 -0500125 def can_update(self, user):
Tony Mackb7b4f842014-02-04 19:50:31 -0500126 return self.site.can_update(user)
Tony Mack5b061472014-02-04 07:57:10 -0500127
Tony Mack5b061472014-02-04 07:57:10 -0500128 @staticmethod
129 def select_by_user(user):
130 if user.is_admin:
131 qs = SitePrivilege.objects.all()
132 else:
133 sp_ids = [sp.id for sp in SitePrivilege.objects.filter(user=user)]
134 qs = SitePrivilege.objects.filter(id__in=sp_ids)
135 return qs
136
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500137class Deployment(PlCoreBase):
Sapan Bhatiac0754e72014-09-22 14:54:10 -0400138 objects = DeploymentManager()
139 deleted_objects = DeploymentDeletionManager()
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500140 name = models.CharField(max_length=200, unique=True, help_text="Name of the Deployment")
Scott Baker57ec5d32014-06-06 14:56:20 -0700141 admin_user = models.CharField(max_length=200, null=True, blank=True, help_text="Username of an admin user at this deployment")
142 admin_password = models.CharField(max_length=200, null=True, blank=True, help_text="Password of theadmin user at this deployment")
143 admin_tenant = models.CharField(max_length=200, null=True, blank=True, help_text="Name of the tenant the admin user belongs to")
144 auth_url = models.CharField(max_length=200, null=True, blank=True, help_text="Auth url for the deployment")
Sapan Bhatia369ed462014-09-18 00:13:46 -0400145 backend_type = models.CharField(max_length=200, null=True, blank=True, help_text="Type of deployment, e.g. EC2, OpenStack, or OpenStack version")
Scott Baker5380c522014-06-06 14:49:43 -0700146
147 # smbaker: the default of 'allow all' is intended for evolutions of existing
148 # deployments. When new deployments are created via the GUI, they are
149 # given a default of 'allow site <site_of_creator>'
150 accessControl = models.TextField(max_length=200, blank=False, null=False, default="allow all",
151 help_text="Access control list that specifies which sites/users may use nodes in this deployment")
152
153 def get_acl(self):
154 return AccessControlList(self.accessControl)
155
156 def test_acl(self, slice=None, user=None):
157 potential_users=[]
158
159 if user:
160 potential_users.append(user)
161
162 if slice:
163 potential_users.append(slice.creator)
164 for priv in slice.slice_privileges.all():
165 if priv.user not in potential_users:
166 potential_users.append(priv.user)
167
168 acl = self.get_acl()
169 for user in potential_users:
170 if acl.test(user) == "allow":
171 return True
172
173 return False
174
Scott Bakercb95fde2014-06-06 16:09:51 -0700175 @staticmethod
176 def select_by_acl(user):
177 ids = []
Scott Baker5380c522014-06-06 14:49:43 -0700178 for deployment in Deployment.objects.all():
Scott Bakercb95fde2014-06-06 16:09:51 -0700179 acl = deployment.get_acl()
Scott Baker01a4cd02014-06-09 13:12:40 -0700180 if acl.test(user) == "allow":
Scott Bakercb95fde2014-06-06 16:09:51 -0700181 ids.append(deployment.id)
182
183 return Deployment.objects.filter(id__in=ids)
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500184
185 def __unicode__(self): return u'%s' % (self.name)
186
Tony Macke4be32f2014-03-11 20:45:25 -0400187 @staticmethod
188 def select_by_user(user):
189 return Deployment.objects.all()
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500190
191class DeploymentRole(PlCoreBase):
Sapan Bhatiad2a98542014-09-22 14:54:26 -0400192 objects = DeploymentLinkManager()
193 deleted_objects = DeploymentLinkDeletionManager()
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500194
195 ROLE_CHOICES = (('admin','Admin'),)
196 role = models.CharField(choices=ROLE_CHOICES, unique=True, max_length=30)
197
198 def __unicode__(self): return u'%s' % (self.role)
199
200class DeploymentPrivilege(PlCoreBase):
Sapan Bhatiab80b6542014-09-22 14:54:39 -0400201 objects = DeploymentLinkManager()
202 deleted_objects = DeploymentLinkDeletionManager()
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500203
204 user = models.ForeignKey('User', related_name='deployment_privileges')
205 deployment = models.ForeignKey('Deployment', related_name='deployment_privileges')
206 role = models.ForeignKey('DeploymentRole')
207
208 def __unicode__(self): return u'%s %s %s' % (self.deployment, self.user, self.role)
209
Tony Mack5b061472014-02-04 07:57:10 -0500210 def can_update(self, user):
211 if user.is_readonly:
212 return False
213 if user.is_admin:
214 return True
215 dprivs = DeploymentPrivilege.objects.filter(user=user)
216 for dpriv in dprivs:
Tony Mackb7b4f842014-02-04 19:50:31 -0500217 if dpriv.role.role == 'admin':
Tony Mack5b061472014-02-04 07:57:10 -0500218 return True
219 return False
220
Tony Mack5b061472014-02-04 07:57:10 -0500221 @staticmethod
222 def select_by_user(user):
223 if user.is_admin:
224 qs = DeploymentPrivilege.objects.all()
225 else:
226 dpriv_ids = [dp.id for dp in DeploymentPrivilege.objects.filter(user=user)]
227 qs = DeploymentPrivilege.objects.filter(id__in=dpriv_ids)
228 return qs
229
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500230class SiteDeployments(PlCoreBase):
Sapan Bhatia6fc6e2b2014-09-22 14:54:56 -0400231 objects = DeploymentLinkManager()
232 deleted_objects = DeploymentLinkDeletionManager()
233
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500234 site = models.ForeignKey(Site)
235 deployment = models.ForeignKey(Deployment)
Tony Macke4be32f2014-03-11 20:45:25 -0400236 tenant_id = models.CharField(null=True, blank=True, max_length=200, help_text="Keystone tenant id")
237
238 @staticmethod
239 def select_by_user(user):
240 return SiteDeployments.objects.all()
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500241
Tony Mack929af702014-02-04 19:36:52 -0500242 #class Meta:
243 # db_table = 'core_site_deployments'
244 # #auto_created = Site
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400245