Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 1 | import os |
| 2 | from django.db import models |
Sapan Bhatia | c6680c1 | 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 |
Sapan Bhatia | 699a044 | 2014-09-22 17:25:06 -0400 | [diff] [blame] | 8 | from planetstack.config import Config |
| 9 | |
| 10 | config = Config() |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 11 | |
Sapan Bhatia | 71058ec | 2014-09-19 16:48:36 -0400 | [diff] [blame] | 12 | class 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 Bhatia | 005d761 | 2014-09-19 16:48:11 -0400 | [diff] [blame] | 25 | class 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 Bhatia | d8a6eb5 | 2014-09-19 16:47:40 -0400 | [diff] [blame] | 37 | class 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 Bhatia | c6680c1 | 2014-09-19 16:47:07 -0400 | [diff] [blame] | 50 | class 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 Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 62 | class Site(PlCoreBase): |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 63 | """ |
| 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 Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 66 | 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 Tully | 567e3e6 | 2013-06-21 18:03:16 -0400 | [diff] [blame] | 69 | location = GeopositionField() |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 70 | 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 Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 76 | #deployments = models.ManyToManyField('Deployment', blank=True, related_name='sites') |
Sapan Bhatia | 378baea | 2014-06-13 13:37:46 -0400 | [diff] [blame] | 77 | 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] | 78 | tags = generic.GenericRelation(Tag) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 79 | |
| 80 | def __unicode__(self): return u'%s' % (self.name) |
| 81 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 82 | def can_update(self, user): |
Tony Mack | b7b4f84 | 2014-02-04 19:50:31 -0500 | [diff] [blame] | 83 | if user.is_readonly: |
| 84 | return False |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 85 | 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 Mack | b7b4f84 | 2014-02-04 19:50:31 -0500 | [diff] [blame] | 89 | if site_priv.role.role == 'pi': |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 90 | return True |
| 91 | return False |
| 92 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 93 | @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 Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 104 | class SiteRole(PlCoreBase): |
| 105 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 106 | ROLE_CHOICES = (('admin','Admin'),('pi','PI'),('tech','Tech'),('billing','Billing')) |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 107 | role = models.CharField(choices=ROLE_CHOICES, unique=True, max_length=30) |
| 108 | |
| 109 | def __unicode__(self): return u'%s' % (self.role) |
| 110 | |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 111 | class SitePrivilege(PlCoreBase): |
| 112 | |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 113 | user = models.ForeignKey('User', related_name='site_privileges') |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 114 | site = models.ForeignKey('Site', related_name='site_privileges') |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 115 | role = models.ForeignKey('SiteRole') |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 116 | |
| 117 | def __unicode__(self): return u'%s %s %s' % (self.site, self.user, self.role) |
| 118 | |
Tony Mack | 00d361f | 2013-04-28 10:28:42 -0400 | [diff] [blame] | 119 | def save(self, *args, **kwds): |
Tony Mack | 00d361f | 2013-04-28 10:28:42 -0400 | [diff] [blame] | 120 | super(SitePrivilege, self).save(*args, **kwds) |
| 121 | |
| 122 | def delete(self, *args, **kwds): |
Tony Mack | 00d361f | 2013-04-28 10:28:42 -0400 | [diff] [blame] | 123 | super(SitePrivilege, self).delete(*args, **kwds) |
| 124 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 125 | def can_update(self, user): |
Tony Mack | b7b4f84 | 2014-02-04 19:50:31 -0500 | [diff] [blame] | 126 | return self.site.can_update(user) |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 127 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 128 | @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 Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 137 | class Deployment(PlCoreBase): |
Sapan Bhatia | c0754e7 | 2014-09-22 14:54:10 -0400 | [diff] [blame] | 138 | objects = DeploymentManager() |
| 139 | deleted_objects = DeploymentDeletionManager() |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 140 | 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] | 141 | 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 Bhatia | 369ed46 | 2014-09-18 00:13:46 -0400 | [diff] [blame] | 145 | 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] | 146 | |
| 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 Baker | cb95fde | 2014-06-06 16:09:51 -0700 | [diff] [blame] | 175 | @staticmethod |
| 176 | def select_by_acl(user): |
| 177 | ids = [] |
Scott Baker | 5380c52 | 2014-06-06 14:49:43 -0700 | [diff] [blame] | 178 | for deployment in Deployment.objects.all(): |
Scott Baker | cb95fde | 2014-06-06 16:09:51 -0700 | [diff] [blame] | 179 | acl = deployment.get_acl() |
Scott Baker | 01a4cd0 | 2014-06-09 13:12:40 -0700 | [diff] [blame] | 180 | if acl.test(user) == "allow": |
Scott Baker | cb95fde | 2014-06-06 16:09:51 -0700 | [diff] [blame] | 181 | ids.append(deployment.id) |
| 182 | |
| 183 | return Deployment.objects.filter(id__in=ids) |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 184 | |
| 185 | def __unicode__(self): return u'%s' % (self.name) |
| 186 | |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 187 | @staticmethod |
| 188 | def select_by_user(user): |
| 189 | return Deployment.objects.all() |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 190 | |
| 191 | class DeploymentRole(PlCoreBase): |
Sapan Bhatia | d2a9854 | 2014-09-22 14:54:26 -0400 | [diff] [blame] | 192 | objects = DeploymentLinkManager() |
| 193 | deleted_objects = DeploymentLinkDeletionManager() |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 194 | |
| 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 | |
| 200 | class DeploymentPrivilege(PlCoreBase): |
Sapan Bhatia | b80b654 | 2014-09-22 14:54:39 -0400 | [diff] [blame] | 201 | objects = DeploymentLinkManager() |
| 202 | deleted_objects = DeploymentLinkDeletionManager() |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 203 | |
| 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 Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 210 | 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 Mack | b7b4f84 | 2014-02-04 19:50:31 -0500 | [diff] [blame] | 217 | if dpriv.role.role == 'admin': |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 218 | return True |
| 219 | return False |
| 220 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 221 | @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 Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 230 | class SiteDeployments(PlCoreBase): |
Sapan Bhatia | 6fc6e2b | 2014-09-22 14:54:56 -0400 | [diff] [blame] | 231 | objects = DeploymentLinkManager() |
| 232 | deleted_objects = DeploymentLinkDeletionManager() |
| 233 | |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 234 | site = models.ForeignKey(Site) |
| 235 | deployment = models.ForeignKey(Deployment) |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 236 | 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 Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 241 | |
Tony Mack | 929af70 | 2014-02-04 19:36:52 -0500 | [diff] [blame] | 242 | #class Meta: |
| 243 | # db_table = 'core_site_deployments' |
| 244 | # #auto_created = Site |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 245 | |