2 from django.db import models
3 from django.db.models import Q
4 from core.models import PlCoreBase,PlCoreBaseManager,PlCoreBaseDeletionManager
5 from core.models import Tag
6 from django.contrib.contenttypes import generic
7 from geoposition.fields import GeopositionField
8 from core.acl import AccessControlList
9 from planetstack.config import Config
13 class ControllerLinkDeletionManager(PlCoreBaseDeletionManager):
14 def get_queryset(self):
15 parent=super(ControllerLinkDeletionManager, self)
17 backend_type = config.observer_backend_type
18 except AttributeError:
21 parent_queryset = parent.get_queryset() if hasattr(parent, "get_queryset") else parent.get_query_set()
23 return parent_queryset.filter(Q(controller__backend_type=backend_type))
25 return parent_queryset
27 # deprecated in django 1.7 in favor of get_queryset().
28 def get_query_set(self):
29 return self.get_queryset()
32 class ControllerDeletionManager(PlCoreBaseDeletionManager):
33 def get_queryset(self):
34 parent=super(ControllerDeletionManager, self)
37 backend_type = config.observer_backend_type
38 except AttributeError:
41 parent_queryset = parent.get_queryset() if hasattr(parent, "get_queryset") else parent.get_query_set()
44 return parent_queryset.filter(Q(backend_type=backend_type))
46 return parent_queryset
48 # deprecated in django 1.7 in favor of get_queryset().
49 def get_query_set(self):
50 return self.get_queryset()
52 class ControllerLinkManager(PlCoreBaseManager):
53 def get_queryset(self):
54 parent=super(ControllerLinkManager, self)
57 backend_type = config.observer_backend_type
58 except AttributeError:
61 parent_queryset = parent.get_queryset() if hasattr(parent, "get_queryset") else parent.get_query_set()
64 return parent_queryset.filter(Q(controller__backend_type=backend_type))
66 return parent_queryset
68 # deprecated in django 1.7 in favor of get_queryset().
69 def get_query_set(self):
70 return self.get_queryset()
73 class ControllerManager(PlCoreBaseManager):
74 def get_queryset(self):
75 parent=super(ControllerManager, self)
78 backend_type = config.observer_backend_type
79 except AttributeError:
82 parent_queryset = parent.get_queryset() if hasattr(parent, "get_queryset") else parent.get_query_set()
85 return parent_queryset.filter(Q(backend_type=backend_type))
87 return parent_queryset
89 # deprecated in django 1.7 in favor of get_queryset().
90 def get_query_set(self):
91 return self.get_queryset()
93 class Site(PlCoreBase):
95 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.
97 name = models.CharField(max_length=200, help_text="Name for this Site")
98 site_url = models.URLField(null=True, blank=True, max_length=512, help_text="Site's Home URL Page")
99 enabled = models.BooleanField(default=True, help_text="Status for this Site")
100 location = GeopositionField()
101 longitude = models.FloatField(null=True, blank=True)
102 latitude = models.FloatField(null=True, blank=True)
103 login_base = models.CharField(max_length=50, unique=True, help_text="Prefix for Slices associated with this Site")
104 is_public = models.BooleanField(default=True, help_text="Indicates the visibility of this site to other members")
105 abbreviated_name = models.CharField(max_length=80)
107 #deployments = models.ManyToManyField('Deployment', blank=True, related_name='sites')
108 deployments = models.ManyToManyField('Deployment', through='SiteDeployments', blank=True, help_text="Select which sites are allowed to host nodes in this deployment", related_name='sites')
109 tags = generic.GenericRelation(Tag)
111 def __unicode__(self): return u'%s' % (self.name)
113 def can_update(self, user):
118 site_privs = SitePrivilege.objects.filter(user=user, site=self)
119 for site_priv in site_privs:
120 if site_priv.role.role == 'pi':
125 def select_by_user(user):
127 qs = Site.objects.all()
129 site_ids = [sp.site.id for sp in SitePrivilege.objects.filter(user=user)]
130 site_ids.append(user.site.id)
131 qs = Site.objects.filter(id__in=site_ids)
135 class SiteRole(PlCoreBase):
137 ROLE_CHOICES = (('admin','Admin'),('pi','PI'),('tech','Tech'),('billing','Billing'))
138 role = models.CharField(choices=ROLE_CHOICES, unique=True, max_length=30)
140 def __unicode__(self): return u'%s' % (self.role)
142 class SitePrivilege(PlCoreBase):
144 user = models.ForeignKey('User', related_name='siteprivileges')
145 site = models.ForeignKey('Site', related_name='siteprivileges')
146 role = models.ForeignKey('SiteRole',related_name='siteprivileges')
148 def __unicode__(self): return u'%s %s %s' % (self.site, self.user, self.role)
150 def save(self, *args, **kwds):
151 super(SitePrivilege, self).save(*args, **kwds)
153 def delete(self, *args, **kwds):
154 super(SitePrivilege, self).delete(*args, **kwds)
156 def can_update(self, user):
157 return self.site.can_update(user)
160 def select_by_user(user):
162 qs = SitePrivilege.objects.all()
164 sp_ids = [sp.id for sp in SitePrivilege.objects.filter(user=user)]
165 qs = SitePrivilege.objects.filter(id__in=sp_ids)
168 class Deployment(PlCoreBase):
169 #objects = Controllermanager()
170 #deleted_objects = DeploymentDeletionManager()
171 name = models.CharField(max_length=200, unique=True, help_text="Name of the Deployment")
172 #admin_user = models.CharField(max_length=200, null=True, blank=True, help_text="Username of an admin user at this deployment")
173 #admin_password = models.CharField(max_length=200, null=True, blank=True, help_text="Password of theadmin user at this deployment")
\r
174 #admin_tenant = models.CharField(max_length=200, null=True, blank=True, help_text="Name of the tenant the admin user belongs to")
\r
175 #auth_url = models.CharField(max_length=200, null=True, blank=True, help_text="Auth url for the deployment")
176 #backend_type = models.CharField(max_length=200, null=True, blank=True, help_text="Type of deployment, e.g. EC2, OpenStack, or OpenStack version")
177 #availability_zone = models.CharField(max_length=200, null=True, blank=True, help_text="OpenStack availability zone")
179 # smbaker: the default of 'allow all' is intended for evolutions of existing
180 # deployments. When new deployments are created via the GUI, they are
181 # given a default of 'allow site <site_of_creator>'
182 accessControl = models.TextField(max_length=200, blank=False, null=False, default="allow all",
183 help_text="Access control list that specifies which sites/users may use nodes in this deployment")
186 return AccessControlList(self.accessControl)
188 def test_acl(self, slice=None, user=None):
192 potential_users.append(user)
195 potential_users.append(slice.creator)
196 for priv in slice.sliceprivileges.all():
197 if priv.user not in potential_users:
198 potential_users.append(priv.user)
201 for user in potential_users:
202 if acl.test(user) == "allow":
208 def select_by_acl(user):
210 for deployment in Deployment.objects.all():
211 acl = deployment.get_acl()
212 if acl.test(user) == "allow":
213 ids.append(deployment.id)
215 return Deployment.objects.filter(id__in=ids)
217 def __unicode__(self): return u'%s' % (self.name)
219 class DeploymentRole(PlCoreBase):
220 #objects = DeploymentLinkManager()
221 #deleted_objects = DeploymentLinkDeletionManager()
222 ROLE_CHOICES = (('admin','Admin'),)
223 role = models.CharField(choices=ROLE_CHOICES, unique=True, max_length=30)
225 def __unicode__(self): return u'%s' % (self.role)
227 class DeploymentPrivilege(PlCoreBase):
228 #objects = DeploymentLinkManager()
229 #deleted_objects = DeploymentLinkDeletionManager()
231 user = models.ForeignKey('User', related_name='deploymentprivileges')
232 deployment = models.ForeignKey('Deployment', related_name='deploymentprivileges')
233 role = models.ForeignKey('DeploymentRole',related_name='deploymentprivileges')
235 def __unicode__(self): return u'%s %s %s' % (self.deployment, self.user, self.role)
237 def can_update(self, user):
242 dprivs = DeploymentPrivilege.objects.filter(user=user)
244 if dpriv.role.role == 'admin':
249 def select_by_user(user):
251 qs = DeploymentPrivilege.objects.all()
253 dpriv_ids = [dp.id for dp in DeploymentPrivilege.objects.filter(user=user)]
254 qs = DeploymentPrivilege.objects.filter(id__in=dpriv_ids)
257 class ControllerRole(PlCoreBase):
258 #objects = ControllerLinkManager()
259 #deleted_objects = ControllerLinkDeletionManager()
261 ROLE_CHOICES = (('admin','Admin'),)
262 role = models.CharField(choices=ROLE_CHOICES, unique=True, max_length=30)
264 def __unicode__(self): return u'%s' % (self.role)
266 class ControllerPrivilege(PlCoreBase):
267 objects = ControllerLinkManager()
268 deleted_objects = ControllerLinkDeletionManager()
270 user = models.ForeignKey('User', related_name='controllerprivileges')
271 controller = models.ForeignKey('Controller', related_name='controllerprivileges')
272 role = models.ForeignKey('ControllerRole',related_name='controllerprivileges')
274 def __unicode__(self): return u'%s %s %s' % (self.controller, self.user, self.role)
276 def can_update(self, user):
281 cprivs = ControllerPrivilege.objects.filter(user=user)
283 if cpriv.role.role == 'admin':
288 def select_by_user(user):
290 qs = ControllerPrivilege.objects.all()
292 cpriv_ids = [cp.id for cp in ControllerPrivilege.objects.filter(user=user)]
293 qs = ControllerPrivilege.objects.filter(id__in=cpriv_ids)
296 class Controller(PlCoreBase):
298 objects = ControllerManager()
299 deleted_objects = ControllerDeletionManager()
301 name = models.CharField(max_length=200, unique=True, help_text="Name of the Controller")
302 version = models.CharField(max_length=200, unique=True, help_text="Controller version")
303 backend_type = models.CharField(max_length=200, null=True, blank=True, help_text="Type of compute controller, e.g. EC2, OpenStack, or OpenStack version")
304 auth_url = models.CharField(max_length=200, null=True, blank=True, help_text="Auth url for the compute controller")
305 admin_user = models.CharField(max_length=200, null=True, blank=True, help_text="Username of an admin user at this controller")
306 admin_password = models.CharField(max_length=200, null=True, blank=True, help_text="Password of theadmin user at this controller")
307 admin_tenant = models.CharField(max_length=200, null=True, blank=True, help_text="Name of the tenant the admin user belongs to")
309 def __unicode__(self): return u'%s %s' % (self.name, self.backend_type)
311 class SiteDeployments(PlCoreBase):
312 objects = ControllerLinkManager()
313 deleted_objects = ControllerLinkDeletionManager()
315 site = models.ForeignKey(Site,related_name='sitedeployments')
316 deployment = models.ForeignKey(Deployment,related_name='sitedeployments')
317 controller = models.ForeignKey(Controller, null=True, blank=True, related_name='sitedeployments')
318 availability_zone = models.CharField(max_length=200, null=True, blank=True, help_text="OpenStack availability zone")
320 def __unicode__(self): return u'%s %s' % (self.deployment, self.site)
322 class ControllerSiteDeployments(PlCoreBase):
323 objects = ControllerLinkManager()
324 deleted_objects = ControllerLinkDeletionManager()
326 controller = models.ForeignKey(Controller, related_name='controllersitedeployments')
327 site_deployment = models.ForeignKey(SiteDeployments, related_name='controllersitedeployments')
328 tenant_id = models.CharField(null=True, blank=True, max_length=200, help_text="Keystone tenant id")
330 def __unicode__(self): return u'%s %s' % (self.controller, self.site_deployment)