blob: 55f494504fe087d9b08b7bd7f37e671ae1b38d5c [file] [log] [blame]
Siobhan Tully4bc09f22013-04-10 21:15:21 -04001import os
2from django.db import models
Sapan Bhatia4a62a2f2014-09-22 18:47:53 -04003from django.db.models import Q
Siobhan Tullyde5450d2013-06-21 11:35:33 -04004from django.contrib.contenttypes import generic
Tony Macka6928d62015-03-29 09:29:12 -04005from django.core.exceptions import PermissionDenied
Siobhan Tully567e3e62013-06-21 18:03:16 -04006from geoposition.fields import GeopositionField
Tony Mack50e12212015-03-09 13:03:56 -04007from core.models import PlCoreBase,PlCoreBaseManager,PlCoreBaseDeletionManager
8from core.models import Tag
9from core.models.plcorebase import StrippedCharField
Scott Baker5380c522014-06-06 14:49:43 -070010from core.acl import AccessControlList
Scott Baker86e132c2015-02-11 21:38:09 -080011from xos.config import Config
Sapan Bhatia1d1b2b12014-09-22 17:25:06 -040012
13config = Config()
Siobhan Tully4bc09f22013-04-10 21:15:21 -040014
Tony Mack51c4a7d2014-11-30 15:33:35 -050015class ControllerLinkDeletionManager(PlCoreBaseDeletionManager):
Sapan Bhatiac4b98072014-09-19 16:48:36 -040016 def get_queryset(self):
Tony Mack06c8e472014-11-30 15:53:08 -050017 parent=super(ControllerLinkDeletionManager, self)
Sapan Bhatiad0a176c2014-09-23 13:14:31 -040018 try:
19 backend_type = config.observer_backend_type
20 except AttributeError:
21 backend_type = None
22
23 parent_queryset = parent.get_queryset() if hasattr(parent, "get_queryset") else parent.get_query_set()
24 if (backend_type):
Tony Mack51c4a7d2014-11-30 15:33:35 -050025 return parent_queryset.filter(Q(controller__backend_type=backend_type))
Sapan Bhatiac4b98072014-09-19 16:48:36 -040026 else:
Sapan Bhatiad0a176c2014-09-23 13:14:31 -040027 return parent_queryset
Sapan Bhatiac4b98072014-09-19 16:48:36 -040028
29 # deprecated in django 1.7 in favor of get_queryset().
30 def get_query_set(self):
31 return self.get_queryset()
32
33
Tony Mack51c4a7d2014-11-30 15:33:35 -050034class ControllerDeletionManager(PlCoreBaseDeletionManager):
Sapan Bhatia15fdcdb2014-09-19 16:48:11 -040035 def get_queryset(self):
Tony Mack51c4a7d2014-11-30 15:33:35 -050036 parent=super(ControllerDeletionManager, self)
Sapan Bhatiad0a176c2014-09-23 13:14:31 -040037
38 try:
39 backend_type = config.observer_backend_type
40 except AttributeError:
41 backend_type = None
42
43 parent_queryset = parent.get_queryset() if hasattr(parent, "get_queryset") else parent.get_query_set()
44
45 if backend_type:
Sapan Bhatia40e18132014-10-08 09:38:21 -040046 return parent_queryset.filter(Q(backend_type=backend_type))
Sapan Bhatia15fdcdb2014-09-19 16:48:11 -040047 else:
Sapan Bhatiad0a176c2014-09-23 13:14:31 -040048 return parent_queryset
Sapan Bhatia15fdcdb2014-09-19 16:48:11 -040049
50 # deprecated in django 1.7 in favor of get_queryset().
51 def get_query_set(self):
52 return self.get_queryset()
53
Tony Mack51c4a7d2014-11-30 15:33:35 -050054class ControllerLinkManager(PlCoreBaseManager):
Sapan Bhatia48df09f2014-09-19 16:47:40 -040055 def get_queryset(self):
Tony Mack51c4a7d2014-11-30 15:33:35 -050056 parent=super(ControllerLinkManager, self)
Sapan Bhatiad0a176c2014-09-23 13:14:31 -040057
58 try:
59 backend_type = config.observer_backend_type
60 except AttributeError:
61 backend_type = None
62
63 parent_queryset = parent.get_queryset() if hasattr(parent, "get_queryset") else parent.get_query_set()
64
65 if backend_type:
Tony Mack51c4a7d2014-11-30 15:33:35 -050066 return parent_queryset.filter(Q(controller__backend_type=backend_type))
Sapan Bhatia48df09f2014-09-19 16:47:40 -040067 else:
Sapan Bhatiad0a176c2014-09-23 13:14:31 -040068 return parent_queryset
Sapan Bhatia48df09f2014-09-19 16:47:40 -040069
70 # deprecated in django 1.7 in favor of get_queryset().
71 def get_query_set(self):
72 return self.get_queryset()
73
74
Tony Mack51c4a7d2014-11-30 15:33:35 -050075class ControllerManager(PlCoreBaseManager):
Sapan Bhatiaad6dbd82014-09-19 16:47:07 -040076 def get_queryset(self):
Tony Mack06c8e472014-11-30 15:53:08 -050077 parent=super(ControllerManager, self)
Sapan Bhatiad0a176c2014-09-23 13:14:31 -040078
79 try:
80 backend_type = config.observer_backend_type
81 except AttributeError:
82 backend_type = None
83
84 parent_queryset = parent.get_queryset() if hasattr(parent, "get_queryset") else parent.get_query_set()
85
86 if backend_type:
Sapan Bhatia40e18132014-10-08 09:38:21 -040087 return parent_queryset.filter(Q(backend_type=backend_type))
Sapan Bhatiaad6dbd82014-09-19 16:47:07 -040088 else:
Sapan Bhatiaf77d01a2014-09-24 00:34:44 -040089 return parent_queryset
Sapan Bhatiaad6dbd82014-09-19 16:47:07 -040090
91 # deprecated in django 1.7 in favor of get_queryset().
92 def get_query_set(self):
93 return self.get_queryset()
94
Siobhan Tully4bc09f22013-04-10 21:15:21 -040095class Site(PlCoreBase):
Siobhan Tullycf04fb62014-01-11 11:25:57 -050096 """
97 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.
98 """
Tony Mack50e12212015-03-09 13:03:56 -040099 name = StrippedCharField(max_length=200, help_text="Name for this Site")
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400100 site_url = models.URLField(null=True, blank=True, max_length=512, help_text="Site's Home URL Page")
101 enabled = models.BooleanField(default=True, help_text="Status for this Site")
Tony Mack6a388472015-08-04 17:21:55 -0400102 hosts_nodes = models.BooleanField(default=True, help_text="Indicates whether or not the site host nodes")
103 hosts_users = models.BooleanField(default=True, help_text="Indicates whether or not the site manages user accounts")
Siobhan Tully567e3e62013-06-21 18:03:16 -0400104 location = GeopositionField()
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400105 longitude = models.FloatField(null=True, blank=True)
106 latitude = models.FloatField(null=True, blank=True)
Tony Mack50e12212015-03-09 13:03:56 -0400107 login_base = StrippedCharField(max_length=50, unique=True, help_text="Prefix for Slices associated with this Site")
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400108 is_public = models.BooleanField(default=True, help_text="Indicates the visibility of this site to other members")
Tony Mack50e12212015-03-09 13:03:56 -0400109 abbreviated_name = StrippedCharField(max_length=80)
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400110
Tony Macke4be32f2014-03-11 20:45:25 -0400111 #deployments = models.ManyToManyField('Deployment', blank=True, related_name='sites')
Tony Mack3066a952015-01-05 22:48:11 -0500112 deployments = models.ManyToManyField('Deployment', through='SiteDeployment', 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 -0400113 tags = generic.GenericRelation(Tag)
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400114
115 def __unicode__(self): return u'%s' % (self.name)
116
Tony Mack5b061472014-02-04 07:57:10 -0500117 def can_update(self, user):
Tony Mack3428e6e2015-02-08 21:38:41 -0500118 return user.can_update_site(self, allow=['pi'])
Tony Mack5b061472014-02-04 07:57:10 -0500119
Siobhan Tullybfd11dc2013-09-03 12:59:24 -0400120class SiteRole(PlCoreBase):
121
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500122 ROLE_CHOICES = (('admin','Admin'),('pi','PI'),('tech','Tech'),('billing','Billing'))
Tony Mack50e12212015-03-09 13:03:56 -0400123 role = StrippedCharField(choices=ROLE_CHOICES, unique=True, max_length=30)
Siobhan Tullybfd11dc2013-09-03 12:59:24 -0400124
125 def __unicode__(self): return u'%s' % (self.role)
126
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400127class SitePrivilege(PlCoreBase):
128
Sapan Bhatia6bfa2ca2014-11-11 21:47:45 -0500129 user = models.ForeignKey('User', related_name='siteprivileges')
130 site = models.ForeignKey('Site', related_name='siteprivileges')
131 role = models.ForeignKey('SiteRole',related_name='siteprivileges')
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400132
133 def __unicode__(self): return u'%s %s %s' % (self.site, self.user, self.role)
134
Tony Mack00d361f2013-04-28 10:28:42 -0400135 def save(self, *args, **kwds):
Tony Macka6928d62015-03-29 09:29:12 -0400136 if not self.user.is_active:
137 raise PermissionDenied, "Cannot modify role(s) of a disabled user"
Tony Mack00d361f2013-04-28 10:28:42 -0400138 super(SitePrivilege, self).save(*args, **kwds)
139
140 def delete(self, *args, **kwds):
Tony Mack00d361f2013-04-28 10:28:42 -0400141 super(SitePrivilege, self).delete(*args, **kwds)
142
Tony Mack5b061472014-02-04 07:57:10 -0500143 def can_update(self, user):
Tony Mack3428e6e2015-02-08 21:38:41 -0500144 return user.can_update_site(self, allow=['pi'])
Tony Mack5b061472014-02-04 07:57:10 -0500145
Tony Mack5b061472014-02-04 07:57:10 -0500146 @staticmethod
147 def select_by_user(user):
148 if user.is_admin:
149 qs = SitePrivilege.objects.all()
150 else:
151 sp_ids = [sp.id for sp in SitePrivilege.objects.filter(user=user)]
152 qs = SitePrivilege.objects.filter(id__in=sp_ids)
153 return qs
154
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500155class Deployment(PlCoreBase):
Tony Mack06c8e472014-11-30 15:53:08 -0500156 #objects = Controllermanager()
Tony Mack51c4a7d2014-11-30 15:33:35 -0500157 #deleted_objects = DeploymentDeletionManager()
Tony Mack50e12212015-03-09 13:03:56 -0400158 name = StrippedCharField(max_length=200, unique=True, help_text="Name of the Deployment")
159 #admin_user = StrippedCharField(max_length=200, null=True, blank=True, help_text="Username of an admin user at this deployment")
160 #admin_password = StrippedCharField(max_length=200, null=True, blank=True, help_text="Password of theadmin user at this deployment")
161 #admin_tenant = StrippedCharField(max_length=200, null=True, blank=True, help_text="Name of the tenant the admin user belongs to")
162 #auth_url = StrippedCharField(max_length=200, null=True, blank=True, help_text="Auth url for the deployment")
163 #backend_type = StrippedCharField(max_length=200, null=True, blank=True, help_text="Type of deployment, e.g. EC2, OpenStack, or OpenStack version")
164 #availability_zone = StrippedCharField(max_length=200, null=True, blank=True, help_text="OpenStack availability zone")
Scott Baker5380c522014-06-06 14:49:43 -0700165
166 # smbaker: the default of 'allow all' is intended for evolutions of existing
167 # deployments. When new deployments are created via the GUI, they are
168 # given a default of 'allow site <site_of_creator>'
169 accessControl = models.TextField(max_length=200, blank=False, null=False, default="allow all",
170 help_text="Access control list that specifies which sites/users may use nodes in this deployment")
Sapan Bhatiae464c9c2015-10-07 17:31:58 +0200171 def __init__(self, *args, **kwargs):
172 super(Deployment, self).__init__(*args, **kwargs)
173 self.no_sync=True
Scott Baker5380c522014-06-06 14:49:43 -0700174
175 def get_acl(self):
176 return AccessControlList(self.accessControl)
177
178 def test_acl(self, slice=None, user=None):
179 potential_users=[]
180
181 if user:
182 potential_users.append(user)
183
184 if slice:
185 potential_users.append(slice.creator)
Sapan Bhatiad2d5dd82014-11-11 21:10:07 -0500186 for priv in slice.sliceprivileges.all():
Scott Baker5380c522014-06-06 14:49:43 -0700187 if priv.user not in potential_users:
188 potential_users.append(priv.user)
189
190 acl = self.get_acl()
191 for user in potential_users:
192 if acl.test(user) == "allow":
193 return True
194
195 return False
196
Scott Bakercb95fde2014-06-06 16:09:51 -0700197 @staticmethod
198 def select_by_acl(user):
199 ids = []
Scott Baker5380c522014-06-06 14:49:43 -0700200 for deployment in Deployment.objects.all():
Scott Bakercb95fde2014-06-06 16:09:51 -0700201 acl = deployment.get_acl()
Scott Baker01a4cd02014-06-09 13:12:40 -0700202 if acl.test(user) == "allow":
Scott Bakercb95fde2014-06-06 16:09:51 -0700203 ids.append(deployment.id)
204
205 return Deployment.objects.filter(id__in=ids)
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500206
Tony Mackcf29cfa2015-02-05 06:13:04 -0500207 def can_update(self, user):
S.Çağlar Onurb9cf3232015-02-09 14:53:40 -0500208 return user.can_update_deployment(self)
Tony Mack3428e6e2015-02-08 21:38:41 -0500209
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500210 def __unicode__(self): return u'%s' % (self.name)
211
Tony Mack68a1e422014-12-08 16:43:02 -0500212class DeploymentRole(PlCoreBase):
213 #objects = DeploymentLinkManager()
214 #deleted_objects = DeploymentLinkDeletionManager()
215 ROLE_CHOICES = (('admin','Admin'),)
Tony Mack50e12212015-03-09 13:03:56 -0400216 role = StrippedCharField(choices=ROLE_CHOICES, unique=True, max_length=30)
Tony Mack68a1e422014-12-08 16:43:02 -0500217
218 def __unicode__(self): return u'%s' % (self.role)
219
220class DeploymentPrivilege(PlCoreBase):
221 #objects = DeploymentLinkManager()
222 #deleted_objects = DeploymentLinkDeletionManager()
223
224 user = models.ForeignKey('User', related_name='deploymentprivileges')
225 deployment = models.ForeignKey('Deployment', related_name='deploymentprivileges')
226 role = models.ForeignKey('DeploymentRole',related_name='deploymentprivileges')
Tony Mack5d93a9e2015-04-11 12:17:59 -0400227 class Meta:
228 unique_together = ('user', 'deployment', 'role')
Tony Mack68a1e422014-12-08 16:43:02 -0500229
230 def __unicode__(self): return u'%s %s %s' % (self.deployment, self.user, self.role)
231
232 def can_update(self, user):
S.Çağlar Onurb9cf3232015-02-09 14:53:40 -0500233 return user.can_update_deployment(self)
Tony Mack68a1e422014-12-08 16:43:02 -0500234
235 @staticmethod
236 def select_by_user(user):
237 if user.is_admin:
238 qs = DeploymentPrivilege.objects.all()
239 else:
240 dpriv_ids = [dp.id for dp in DeploymentPrivilege.objects.filter(user=user)]
241 qs = DeploymentPrivilege.objects.filter(id__in=dpriv_ids)
242 return qs
243
Tony Mack51c4a7d2014-11-30 15:33:35 -0500244class ControllerRole(PlCoreBase):
Tony Mack06c8e472014-11-30 15:53:08 -0500245 #objects = ControllerLinkManager()
246 #deleted_objects = ControllerLinkDeletionManager()
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500247
248 ROLE_CHOICES = (('admin','Admin'),)
Tony Mack50e12212015-03-09 13:03:56 -0400249 role = StrippedCharField(choices=ROLE_CHOICES, unique=True, max_length=30)
Siobhan Tullycf04fb62014-01-11 11:25:57 -0500250
251 def __unicode__(self): return u'%s' % (self.role)
252
Tony Mackd14d48f2014-12-05 17:13:08 -0500253class Controller(PlCoreBase):
254
Tony Mack51c4a7d2014-11-30 15:33:35 -0500255 objects = ControllerManager()
256 deleted_objects = ControllerDeletionManager()
Sapan Bhatia51ddecd2014-09-22 14:54:56 -0400257
Tony Mack50e12212015-03-09 13:03:56 -0400258 name = StrippedCharField(max_length=200, unique=True, help_text="Name of the Controller")
259 backend_type = StrippedCharField(max_length=200, help_text="Type of compute controller, e.g. EC2, OpenStack, or OpenStack version")
260 version = StrippedCharField(max_length=200, help_text="Controller version")
261 auth_url = StrippedCharField(max_length=200, null=True, blank=True, help_text="Auth url for the compute controller")
262 admin_user = StrippedCharField(max_length=200, null=True, blank=True, help_text="Username of an admin user at this controller")
263 admin_password = StrippedCharField(max_length=200, null=True, blank=True, help_text="Password of theadmin user at this controller")
264 admin_tenant = StrippedCharField(max_length=200, null=True, blank=True, help_text="Name of the tenant the admin user belongs to")
265 domain = StrippedCharField(max_length=200, null=True, blank=True, help_text="Name of the domain this controller belongs to")
svavilapaf911082015-10-13 23:50:03 -0400266 rabbit_host = StrippedCharField(max_length=200, null=True, blank=True, help_text="IP address of rabbitmq server at this controller")
267 rabbit_user = StrippedCharField(max_length=200, null=True, blank=True, help_text="Username of rabbitmq server at this controller")
268 rabbit_password = StrippedCharField(max_length=200, null=True, blank=True, help_text="Password of rabbitmq server at this controller")
Tony Mack5817cb42015-02-16 19:54:24 -0500269 deployment = models.ForeignKey(Deployment,related_name='controllerdeployments')
Sapan Bhatiae464c9c2015-10-07 17:31:58 +0200270
271 def __init__(self, *args, **kwargs):
272 super(Controller, self).__init__(*args, **kwargs)
273 self.no_sync=True
Tony Mack51c4a7d2014-11-30 15:33:35 -0500274
Tony Mack625e10e2014-12-26 12:15:42 -0500275 def __unicode__(self): return u'%s %s %s' % (self.name, self.backend_type, self.version)
Tony Mack51c4a7d2014-11-30 15:33:35 -0500276
Tony Mackba6de702015-09-13 23:59:47 +0000277 @property
278 def auth_url_v3(self):
279 if self.auth_url and self.auth_url[-1] == '/':
280 return '{}/v3/'.format('/'.join(self.auth_url.split('/')[:-2]))
281 else:
282 return '{}/v3/'.format('/'.join(self.auth_url.split('/')[:-1]))
283
Tony Mack78fc1362015-02-18 11:41:36 -0500284 @staticmethod
285 def select_by_user(user):
286
287 if user.is_admin:
288 qs = Controller.objects.all()
289 else:
290 deployments = [dp.deployment for dp in DeploymentPrivilege.objects.filter(user=user, role__role__in=['Admin', 'admin'])]
291 qs = Controller.objects.filter(deployment__in=deployments)
Tony Mackf8e8cd22015-02-18 13:54:58 -0500292 return qs
Tony Mack78fc1362015-02-18 11:41:36 -0500293
Tony Mack3066a952015-01-05 22:48:11 -0500294class SiteDeployment(PlCoreBase):
Tony Mack51c4a7d2014-11-30 15:33:35 -0500295 objects = ControllerLinkManager()
Tony Mackd14d48f2014-12-05 17:13:08 -0500296 deleted_objects = ControllerLinkDeletionManager()
Tony Mack51c4a7d2014-11-30 15:33:35 -0500297
Tony Mack30dfcd72015-01-10 23:08:10 -0500298 site = models.ForeignKey(Site,related_name='sitedeployments')
299 deployment = models.ForeignKey(Deployment,related_name='sitedeployments')
300 controller = models.ForeignKey(Controller, null=True, blank=True, related_name='sitedeployments')
Tony Mack50e12212015-03-09 13:03:56 -0400301 availability_zone = StrippedCharField(max_length=200, null=True, blank=True, help_text="OpenStack availability zone")
Tony Mackd14d48f2014-12-05 17:13:08 -0500302
Tony Mack5d93a9e2015-04-11 12:17:59 -0400303 class Meta:
304 unique_together = ('site', 'deployment', 'controller')
Tony Mackc8836df2015-03-09 17:13:14 -0400305
Tony Mack81ad09e2015-01-03 17:26:06 -0500306 def __unicode__(self): return u'%s %s' % (self.deployment, self.site)
Tony Mack3066a952015-01-05 22:48:11 -0500307
308class ControllerSite(PlCoreBase):
309
310 site = models.ForeignKey(Site,related_name='controllersite')
311 controller = models.ForeignKey(Controller, null=True, blank=True, related_name='controllersite')
Tony Mack50e12212015-03-09 13:03:56 -0400312 tenant_id = StrippedCharField(null=True, blank=True, max_length=200, db_index=True, help_text="Keystone tenant id")
Sapan Bhatia5b2ec272016-02-10 17:51:26 +0100313
314 def delete(self, *args, **kwds):
Sapan Bhatia5b2ec272016-02-10 17:51:26 +0100315 super(ControllerSite, self).delete(*args, **kwds)
316
Tony Mackc8836df2015-03-09 17:13:14 -0400317
Tony Mack5d93a9e2015-04-11 12:17:59 -0400318 class Meta:
319 unique_together = ('site', 'controller')
Sapan Bhatiabcff59f2016-03-24 07:56:38 +0100320
321class Diag(PlCoreBase):
322 name = StrippedCharField(max_length=200, help_text="Name of the synchronizer")
323
324 @property
325 def enacted(self):
326 return None