Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 1 | import os |
| 2 | from django.db import models |
Sapan Bhatia | ad6dbd8 | 2014-09-19 16:47:07 -0400 | [diff] [blame] | 3 | from core.models import PlCoreBase,PlCoreBaseManager,PlCoreBaseDeletionManager |
Siobhan Tully | de5450d | 2013-06-21 11:35:33 -0400 | [diff] [blame] | 4 | from core.models import Tag |
| 5 | from django.contrib.contenttypes import generic |
Siobhan Tully | 567e3e6 | 2013-06-21 18:03:16 -0400 | [diff] [blame] | 6 | from geoposition.fields import GeopositionField |
Scott Baker | 5380c52 | 2014-06-06 14:49:43 -0700 | [diff] [blame] | 7 | from core.acl import AccessControlList |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 8 | |
Sapan Bhatia | c4b9807 | 2014-09-19 16:48:36 -0400 | [diff] [blame] | 9 | class DeploymentLinkDeletionManager(PlCoreBaseDeletionManager): |
| 10 | def get_queryset(self): |
| 11 | parent=super(DeploymentLinkDeletionManager, self) |
| 12 | if hasattr(parent, "get_queryset"): |
| 13 | return parent.get_queryset().filter(Q(deployment__backend_type=config.observer_backend_type)|Q(backend_type=None)) |
| 14 | else: |
| 15 | return parent.get_queryset().filter(Q(deployment__backend_type=config.observer_backend_type)|Q(backend_type=None)) |
| 16 | |
| 17 | # deprecated in django 1.7 in favor of get_queryset(). |
| 18 | def get_query_set(self): |
| 19 | return self.get_queryset() |
| 20 | |
| 21 | |
Sapan Bhatia | 15fdcdb | 2014-09-19 16:48:11 -0400 | [diff] [blame] | 22 | class DeploymentDeletionManager(PlCoreBaseDeletionManager): |
| 23 | def get_queryset(self): |
| 24 | parent=super(DeploymentDeletionManager, self) |
| 25 | if hasattr(parent, "get_queryset"): |
| 26 | return parent.get_queryset().filter(Q(backend_type=config.observer_backend_type)|Q(backend_type=None)) |
| 27 | else: |
| 28 | return parent.get_queryset().filter(Q(backend_type=config.observer_backend_type)|Q(backend_type=None)) |
| 29 | |
| 30 | # deprecated in django 1.7 in favor of get_queryset(). |
| 31 | def get_query_set(self): |
| 32 | return self.get_queryset() |
| 33 | |
Sapan Bhatia | 48df09f | 2014-09-19 16:47:40 -0400 | [diff] [blame] | 34 | class DeploymentLinkManager(PlCoreBaseManager): |
| 35 | def get_queryset(self): |
| 36 | parent=super(DeploymentLinkManager, self) |
| 37 | if hasattr(parent, "get_queryset"): |
| 38 | return parent.get_queryset().filter(Q(deployment__backend_type=config.observer_backend_type)|Q(backend_type=None)) |
| 39 | else: |
| 40 | return parent.get_queryset().filter(Q(deployment__backend_type=config.observer_backend_type)|Q(backend_type=None)) |
| 41 | |
| 42 | # deprecated in django 1.7 in favor of get_queryset(). |
| 43 | def get_query_set(self): |
| 44 | return self.get_queryset() |
| 45 | |
| 46 | |
Sapan Bhatia | ad6dbd8 | 2014-09-19 16:47:07 -0400 | [diff] [blame] | 47 | class DeploymentManager(PlCoreBaseManager): |
| 48 | def get_queryset(self): |
| 49 | parent=super(DeploymentManager, self) |
| 50 | if hasattr(parent, "get_queryset"): |
| 51 | return parent.get_queryset().filter(Q(backend_type=config.observer_backend_type)|Q(backend_type=None)) |
| 52 | else: |
| 53 | return parent.get_queryset().filter(Q(backend_type=config.observer_backend_type)|Q(backend_type=None)) |
| 54 | |
| 55 | # deprecated in django 1.7 in favor of get_queryset(). |
| 56 | def get_query_set(self): |
| 57 | return self.get_queryset() |
| 58 | |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 59 | class Site(PlCoreBase): |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 60 | """ |
| 61 | 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. |
| 62 | """ |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 63 | name = models.CharField(max_length=200, help_text="Name for this Site") |
| 64 | site_url = models.URLField(null=True, blank=True, max_length=512, help_text="Site's Home URL Page") |
| 65 | enabled = models.BooleanField(default=True, help_text="Status for this Site") |
Siobhan Tully | 567e3e6 | 2013-06-21 18:03:16 -0400 | [diff] [blame] | 66 | location = GeopositionField() |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 67 | longitude = models.FloatField(null=True, blank=True) |
| 68 | latitude = models.FloatField(null=True, blank=True) |
| 69 | login_base = models.CharField(max_length=50, unique=True, help_text="Prefix for Slices associated with this Site") |
| 70 | is_public = models.BooleanField(default=True, help_text="Indicates the visibility of this site to other members") |
| 71 | abbreviated_name = models.CharField(max_length=80) |
| 72 | |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 73 | #deployments = models.ManyToManyField('Deployment', blank=True, related_name='sites') |
Sapan Bhatia | 378baea | 2014-06-13 13:37:46 -0400 | [diff] [blame] | 74 | 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 Tully | de5450d | 2013-06-21 11:35:33 -0400 | [diff] [blame] | 75 | tags = generic.GenericRelation(Tag) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 76 | |
| 77 | def __unicode__(self): return u'%s' % (self.name) |
| 78 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 79 | def can_update(self, user): |
Tony Mack | b7b4f84 | 2014-02-04 19:50:31 -0500 | [diff] [blame] | 80 | if user.is_readonly: |
| 81 | return False |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 82 | if user.is_admin: |
| 83 | return True |
| 84 | site_privs = SitePrivilege.objects.filter(user=user, site=self) |
| 85 | for site_priv in site_privs: |
Tony Mack | b7b4f84 | 2014-02-04 19:50:31 -0500 | [diff] [blame] | 86 | if site_priv.role.role == 'pi': |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 87 | return True |
| 88 | return False |
| 89 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 90 | @staticmethod |
| 91 | def select_by_user(user): |
| 92 | if user.is_admin: |
| 93 | qs = Site.objects.all() |
| 94 | else: |
| 95 | site_ids = [sp.site.id for sp in SitePrivilege.objects.filter(user=user)] |
| 96 | site_ids.append(user.site.id) |
| 97 | qs = Site.objects.filter(id__in=site_ids) |
| 98 | return qs |
| 99 | |
| 100 | |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 101 | class SiteRole(PlCoreBase): |
| 102 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 103 | ROLE_CHOICES = (('admin','Admin'),('pi','PI'),('tech','Tech'),('billing','Billing')) |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 104 | role = models.CharField(choices=ROLE_CHOICES, unique=True, max_length=30) |
| 105 | |
| 106 | def __unicode__(self): return u'%s' % (self.role) |
| 107 | |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 108 | class SitePrivilege(PlCoreBase): |
| 109 | |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 110 | user = models.ForeignKey('User', related_name='site_privileges') |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 111 | site = models.ForeignKey('Site', related_name='site_privileges') |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 112 | role = models.ForeignKey('SiteRole') |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 113 | |
| 114 | def __unicode__(self): return u'%s %s %s' % (self.site, self.user, self.role) |
| 115 | |
Tony Mack | 00d361f | 2013-04-28 10:28:42 -0400 | [diff] [blame] | 116 | def save(self, *args, **kwds): |
Tony Mack | 00d361f | 2013-04-28 10:28:42 -0400 | [diff] [blame] | 117 | super(SitePrivilege, self).save(*args, **kwds) |
| 118 | |
| 119 | def delete(self, *args, **kwds): |
Tony Mack | 00d361f | 2013-04-28 10:28:42 -0400 | [diff] [blame] | 120 | super(SitePrivilege, self).delete(*args, **kwds) |
| 121 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 122 | def can_update(self, user): |
Tony Mack | b7b4f84 | 2014-02-04 19:50:31 -0500 | [diff] [blame] | 123 | return self.site.can_update(user) |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 124 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 125 | @staticmethod |
| 126 | def select_by_user(user): |
| 127 | if user.is_admin: |
| 128 | qs = SitePrivilege.objects.all() |
| 129 | else: |
| 130 | sp_ids = [sp.id for sp in SitePrivilege.objects.filter(user=user)] |
| 131 | qs = SitePrivilege.objects.filter(id__in=sp_ids) |
| 132 | return qs |
| 133 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 134 | class Deployment(PlCoreBase): |
Sapan Bhatia | abfdb80 | 2014-09-22 14:54:10 -0400 | [diff] [blame] | 135 | objects = DeploymentManager() |
| 136 | deleted_objects = DeploymentDeletionManager() |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 137 | name = models.CharField(max_length=200, unique=True, help_text="Name of the Deployment") |
Scott Baker | 57ec5d3 | 2014-06-06 14:56:20 -0700 | [diff] [blame] | 138 | admin_user = models.CharField(max_length=200, null=True, blank=True, help_text="Username of an admin user at this deployment") |
| 139 | admin_password = models.CharField(max_length=200, null=True, blank=True, help_text="Password of theadmin user at this deployment")
|
| 140 | admin_tenant = models.CharField(max_length=200, null=True, blank=True, help_text="Name of the tenant the admin user belongs to")
|
| 141 | auth_url = models.CharField(max_length=200, null=True, blank=True, help_text="Auth url for the deployment") |
Sapan Bhatia | ed9cd4f | 2014-09-18 00:13:46 -0400 | [diff] [blame] | 142 | backend_type = models.CharField(max_length=200, null=True, blank=True, help_text="Type of deployment, e.g. EC2, OpenStack, or OpenStack version") |
Scott Baker | 5380c52 | 2014-06-06 14:49:43 -0700 | [diff] [blame] | 143 | |
| 144 | # smbaker: the default of 'allow all' is intended for evolutions of existing |
| 145 | # deployments. When new deployments are created via the GUI, they are |
| 146 | # given a default of 'allow site <site_of_creator>' |
| 147 | accessControl = models.TextField(max_length=200, blank=False, null=False, default="allow all", |
| 148 | help_text="Access control list that specifies which sites/users may use nodes in this deployment") |
| 149 | |
| 150 | def get_acl(self): |
| 151 | return AccessControlList(self.accessControl) |
| 152 | |
| 153 | def test_acl(self, slice=None, user=None): |
| 154 | potential_users=[] |
| 155 | |
| 156 | if user: |
| 157 | potential_users.append(user) |
| 158 | |
| 159 | if slice: |
| 160 | potential_users.append(slice.creator) |
| 161 | for priv in slice.slice_privileges.all(): |
| 162 | if priv.user not in potential_users: |
| 163 | potential_users.append(priv.user) |
| 164 | |
| 165 | acl = self.get_acl() |
| 166 | for user in potential_users: |
| 167 | if acl.test(user) == "allow": |
| 168 | return True |
| 169 | |
| 170 | return False |
| 171 | |
Scott Baker | cb95fde | 2014-06-06 16:09:51 -0700 | [diff] [blame] | 172 | @staticmethod |
| 173 | def select_by_acl(user): |
| 174 | ids = [] |
Scott Baker | 5380c52 | 2014-06-06 14:49:43 -0700 | [diff] [blame] | 175 | for deployment in Deployment.objects.all(): |
Scott Baker | cb95fde | 2014-06-06 16:09:51 -0700 | [diff] [blame] | 176 | acl = deployment.get_acl() |
Scott Baker | 01a4cd0 | 2014-06-09 13:12:40 -0700 | [diff] [blame] | 177 | if acl.test(user) == "allow": |
Scott Baker | cb95fde | 2014-06-06 16:09:51 -0700 | [diff] [blame] | 178 | ids.append(deployment.id) |
| 179 | |
| 180 | return Deployment.objects.filter(id__in=ids) |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 181 | |
| 182 | def __unicode__(self): return u'%s' % (self.name) |
| 183 | |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 184 | @staticmethod |
| 185 | def select_by_user(user): |
| 186 | return Deployment.objects.all() |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 187 | |
| 188 | class DeploymentRole(PlCoreBase): |
Sapan Bhatia | 9d21dc7 | 2014-09-22 14:54:26 -0400 | [diff] [blame] | 189 | objects = DeploymentLinkManager() |
| 190 | deleted_objects = DeploymentLinkDeletionManager() |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 191 | |
| 192 | ROLE_CHOICES = (('admin','Admin'),) |
| 193 | role = models.CharField(choices=ROLE_CHOICES, unique=True, max_length=30) |
| 194 | |
| 195 | def __unicode__(self): return u'%s' % (self.role) |
| 196 | |
| 197 | class DeploymentPrivilege(PlCoreBase): |
Sapan Bhatia | 16778cd | 2014-09-22 14:54:39 -0400 | [diff] [blame] | 198 | objects = DeploymentLinkManager() |
| 199 | deleted_objects = DeploymentLinkDeletionManager() |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 200 | |
| 201 | user = models.ForeignKey('User', related_name='deployment_privileges') |
| 202 | deployment = models.ForeignKey('Deployment', related_name='deployment_privileges') |
| 203 | role = models.ForeignKey('DeploymentRole') |
| 204 | |
| 205 | def __unicode__(self): return u'%s %s %s' % (self.deployment, self.user, self.role) |
| 206 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 207 | def can_update(self, user): |
| 208 | if user.is_readonly: |
| 209 | return False |
| 210 | if user.is_admin: |
| 211 | return True |
| 212 | dprivs = DeploymentPrivilege.objects.filter(user=user) |
| 213 | for dpriv in dprivs: |
Tony Mack | b7b4f84 | 2014-02-04 19:50:31 -0500 | [diff] [blame] | 214 | if dpriv.role.role == 'admin': |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 215 | return True |
| 216 | return False |
| 217 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 218 | @staticmethod |
| 219 | def select_by_user(user): |
| 220 | if user.is_admin: |
| 221 | qs = DeploymentPrivilege.objects.all() |
| 222 | else: |
| 223 | dpriv_ids = [dp.id for dp in DeploymentPrivilege.objects.filter(user=user)] |
| 224 | qs = DeploymentPrivilege.objects.filter(id__in=dpriv_ids) |
| 225 | return qs |
| 226 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 227 | class SiteDeployments(PlCoreBase): |
Sapan Bhatia | 51ddecd | 2014-09-22 14:54:56 -0400 | [diff] [blame] | 228 | objects = DeploymentLinkManager() |
| 229 | deleted_objects = DeploymentLinkDeletionManager() |
| 230 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 231 | site = models.ForeignKey(Site) |
| 232 | deployment = models.ForeignKey(Deployment) |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 233 | tenant_id = models.CharField(null=True, blank=True, max_length=200, help_text="Keystone tenant id") |
| 234 | |
| 235 | @staticmethod |
| 236 | def select_by_user(user): |
| 237 | return SiteDeployments.objects.all() |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 238 | |
Tony Mack | 929af70 | 2014-02-04 19:36:52 -0500 | [diff] [blame] | 239 | #class Meta: |
| 240 | # db_table = 'core_site_deployments' |
| 241 | # #auto_created = Site |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 242 | |