Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 1 | import os |
| 2 | from django.db import models |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 3 | from core.models import PlCoreBase |
| 4 | from core.models import Site |
Tony Mack | c195d72 | 2014-08-20 11:15:17 -0400 | [diff] [blame] | 5 | from core.models.site import SitePrivilege |
Siobhan Tully | 30fd429 | 2013-05-10 08:59:56 -0400 | [diff] [blame] | 6 | from core.models import User |
| 7 | from core.models import Role |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 8 | from core.models import Controller,ControllerLinkManager,ControllerLinkDeletionManager |
Scott Baker | e8d596f | 2013-05-13 23:17:13 -0700 | [diff] [blame] | 9 | from core.models import ServiceClass |
Scott Baker | d6dc505 | 2014-09-12 12:24:24 -0700 | [diff] [blame] | 10 | from core.models.serviceclass import get_default_serviceclass |
Siobhan Tully | de5450d | 2013-06-21 11:35:33 -0400 | [diff] [blame] | 11 | from core.models import Tag |
| 12 | from django.contrib.contenttypes import generic |
Siobhan Tully | ce652d0 | 2013-10-08 21:52:35 -0400 | [diff] [blame] | 13 | from core.models import Service |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 14 | from core.models import Controller |
Scott Baker | 3c2b5cd | 2015-01-12 13:13:05 -0800 | [diff] [blame] | 15 | from core.models import Flavor, Image |
Tony Mack | d0cf612 | 2015-02-01 19:51:39 -0500 | [diff] [blame] | 16 | from django.core.exceptions import PermissionDenied, ValidationError |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 17 | |
| 18 | # Create your models here. |
| 19 | |
| 20 | class Slice(PlCoreBase): |
Tony Mack | 4895203 | 2013-04-12 11:49:34 -0400 | [diff] [blame] | 21 | name = models.CharField(unique=True, help_text="The Name of the Slice", max_length=80) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 22 | enabled = models.BooleanField(default=True, help_text="Status for this Slice") |
Scott Baker | d6dc505 | 2014-09-12 12:24:24 -0700 | [diff] [blame] | 23 | omf_friendly = models.BooleanField(default=False) |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 24 | description=models.TextField(blank=True,help_text="High level description of the slice and expected activities", max_length=1024) |
| 25 | slice_url = models.URLField(blank=True, max_length=512) |
Tony Mack | 360afb8 | 2013-12-17 13:32:45 -0500 | [diff] [blame] | 26 | site = models.ForeignKey(Site, related_name='slices', help_text="The Site this Slice belongs to") |
Scott Baker | d6dc505 | 2014-09-12 12:24:24 -0700 | [diff] [blame] | 27 | max_slivers = models.IntegerField(default=10) |
Siobhan Tully | ce652d0 | 2013-10-08 21:52:35 -0400 | [diff] [blame] | 28 | service = models.ForeignKey(Service, related_name='service', null=True, blank=True) |
Scott Baker | 4b0cedd | 2014-04-22 21:01:21 -0700 | [diff] [blame] | 29 | network = models.CharField(default="Private Only",null=True, blank=True, max_length=256) |
Siobhan Tully | de5450d | 2013-06-21 11:35:33 -0400 | [diff] [blame] | 30 | tags = generic.GenericRelation(Tag) |
Scott Baker | d6dc505 | 2014-09-12 12:24:24 -0700 | [diff] [blame] | 31 | serviceClass = models.ForeignKey(ServiceClass, related_name = "slices", null=True, default=get_default_serviceclass) |
Tony Mack | 2bd5b41 | 2013-06-11 21:05:06 -0400 | [diff] [blame] | 32 | creator = models.ForeignKey(User, related_name='slices', blank=True, null=True) |
Siobhan Tully | 231f4c8 | 2013-05-02 05:47:24 -0400 | [diff] [blame] | 33 | |
Scott Baker | ffce785 | 2015-01-03 16:26:38 -0800 | [diff] [blame] | 34 | # for tenant view |
Scott Baker | 3c2b5cd | 2015-01-12 13:13:05 -0800 | [diff] [blame] | 35 | default_flavor = models.ForeignKey(Flavor, related_name = "slices", null=True, blank=True) |
| 36 | default_image = models.ForeignKey(Image, related_name = "slices", null=True, blank=True); |
Scott Baker | ffce785 | 2015-01-03 16:26:38 -0800 | [diff] [blame] | 37 | mount_data_sets = models.CharField(default="GenBank",null=True, blank=True, max_length=256) |
| 38 | |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 39 | def __unicode__(self): return u'%s' % (self.name) |
| 40 | |
Tony Mack | 2721d6f | 2014-08-11 11:14:58 -0400 | [diff] [blame] | 41 | @property |
| 42 | def slicename(self): |
| 43 | return "%s_%s" % (self.site.login_base, self.name) |
| 44 | |
Tony Mack | 62bc59a | 2013-04-14 23:27:12 -0400 | [diff] [blame] | 45 | def save(self, *args, **kwds): |
Tony Mack | dac8576 | 2014-09-03 13:19:42 -0400 | [diff] [blame] | 46 | site = Site.objects.get(id=self.site.id) |
Tony Mack | 7807035 | 2014-09-24 12:44:22 -0400 | [diff] [blame] | 47 | # allow preexisting slices to keep their original name for now |
| 48 | if not self.id and not self.name.startswith(site.login_base): |
Tony Mack | dac8576 | 2014-09-03 13:19:42 -0400 | [diff] [blame] | 49 | raise ValidationError('slice name must begin with %s' % site.login_base) |
Scott Baker | 8c52051 | 2015-01-29 17:16:10 -0800 | [diff] [blame] | 50 | |
| 51 | if self.name == site.login_base+"_": |
| 52 | raise ValidationError('slice name is too short') |
| 53 | |
| 54 | if " " in self.name: |
| 55 | raise ValidationError('slice name must not contain spaces') |
| 56 | |
Scott Baker | e8d596f | 2013-05-13 23:17:13 -0700 | [diff] [blame] | 57 | if self.serviceClass is None: |
| 58 | # We allowed None=True for serviceClass because Django evolution |
| 59 | # will fail unless it is allowed. But, we we really don't want it to |
| 60 | # ever save None, so fix it up here. |
| 61 | self.serviceClass = ServiceClass.get_default() |
Tony Mack | d0cf612 | 2015-02-01 19:51:39 -0500 | [diff] [blame] | 62 | |
| 63 | # set creator on first save |
Tony Mack | 2bd5b41 | 2013-06-11 21:05:06 -0400 | [diff] [blame] | 64 | if not self.creator and hasattr(self, 'caller'): |
| 65 | self.creator = self.caller |
Tony Mack | d0cf612 | 2015-02-01 19:51:39 -0500 | [diff] [blame] | 66 | |
| 67 | # only admins change a slice's creator |
| 68 | if 'creator' in self.changed_fields and \ |
| 69 | (not hasattr(self, 'caller') or not self.caller.is_admin): |
Scott Baker | 8cfe3d2 | 2015-02-02 11:00:53 -0800 | [diff] [blame] | 70 | |
Scott Baker | bd46a92 | 2015-02-03 15:02:17 -0800 | [diff] [blame] | 71 | if (self._initial["creator"]==None) and (self.creator==getattr(self,"caller",None)): |
Scott Baker | 8cfe3d2 | 2015-02-02 11:00:53 -0800 | [diff] [blame] | 72 | # it's okay if the creator is being set by the caller to |
| 73 | # himeself on a new slice object. |
Scott Baker | 8cfe3d2 | 2015-02-02 11:00:53 -0800 | [diff] [blame] | 74 | pass |
| 75 | else: |
| 76 | raise PermissionDenied("Insufficient privileges to change slice creator") |
Tony Mack | d0cf612 | 2015-02-01 19:51:39 -0500 | [diff] [blame] | 77 | |
Scott Baker | 9a9a9ab | 2015-01-29 17:53:26 -0800 | [diff] [blame] | 78 | if not self.creator: |
| 79 | raise ValidationError('slice has no creator') |
Tony Mack | d0cf612 | 2015-02-01 19:51:39 -0500 | [diff] [blame] | 80 | |
Tony Mack | 62bc59a | 2013-04-14 23:27:12 -0400 | [diff] [blame] | 81 | super(Slice, self).save(*args, **kwds) |
| 82 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 83 | def can_update(self, user): |
Tony Mack | 3428e6e | 2015-02-08 21:38:41 -0500 | [diff] [blame] | 84 | return user.can_update_slice(self) |
| 85 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 86 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 87 | @staticmethod |
| 88 | def select_by_user(user): |
| 89 | if user.is_admin: |
| 90 | qs = Slice.objects.all() |
| 91 | else: |
Tony Mack | 33df82b | 2014-08-20 11:29:40 -0400 | [diff] [blame] | 92 | # users can see slices they belong to |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 93 | slice_ids = [sp.slice.id for sp in SlicePrivilege.objects.filter(user=user)] |
Tony Mack | 33df82b | 2014-08-20 11:29:40 -0400 | [diff] [blame] | 94 | # pis can see slices at their sites |
| 95 | sites = [sp.site for sp in SitePrivilege.objects.filter(user=user)\ |
| 96 | if sp.role.role == 'pi'] |
| 97 | slice_ids.extend([s.id for s in Slice.objects.filter(site__in=sites)]) |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 98 | qs = Slice.objects.filter(id__in=slice_ids) |
| 99 | return qs |
| 100 | |
Tony Mack | 82d901b | 2014-10-22 13:30:41 -0400 | [diff] [blame] | 101 | def delete(self, *args, **kwds): |
| 102 | # delete networks associated with this slice |
| 103 | from core.models.network import Network |
| 104 | nets = Network.objects.filter(slices=self) |
| 105 | nets.delete() |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 106 | # delete slice controllers |
Tony Mack | 3066a95 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 107 | slice_controllers = ControllerSlice.objects.filter(slice=self) |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 108 | slice_controllers.delete() |
Tony Mack | a881224 | 2014-11-12 10:01:26 -0500 | [diff] [blame] | 109 | # delete slice privilege |
| 110 | slice_privileges = SlicePrivilege.objects.filter(slice=self) |
| 111 | slice_privileges.delete() |
| 112 | # continue with normal delete |
Sapan | a283f50 | 2014-11-24 16:44:48 -0500 | [diff] [blame] | 113 | super(Slice, self).delete(*args, **kwds) |
Tony Mack | a881224 | 2014-11-12 10:01:26 -0500 | [diff] [blame] | 114 | |
Tony Mack | 82d901b | 2014-10-22 13:30:41 -0400 | [diff] [blame] | 115 | |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 116 | class SliceRole(PlCoreBase): |
Siobhan Tully | cf04fb6 | 2014-01-11 11:25:57 -0500 | [diff] [blame] | 117 | ROLE_CHOICES = (('admin','Admin'),('default','Default')) |
Siobhan Tully | bfd11dc | 2013-09-03 12:59:24 -0400 | [diff] [blame] | 118 | |
| 119 | role = models.CharField(choices=ROLE_CHOICES, unique=True, max_length=30) |
| 120 | |
| 121 | def __unicode__(self): return u'%s' % (self.role) |
| 122 | |
| 123 | class SlicePrivilege(PlCoreBase): |
Sapan Bhatia | 6bfa2ca | 2014-11-11 21:47:45 -0500 | [diff] [blame] | 124 | user = models.ForeignKey('User', related_name='sliceprivileges') |
| 125 | slice = models.ForeignKey('Slice', related_name='sliceprivileges') |
| 126 | role = models.ForeignKey('SliceRole',related_name='sliceprivileges') |
Siobhan Tully | 4bc09f2 | 2013-04-10 21:15:21 -0400 | [diff] [blame] | 127 | |
| 128 | def __unicode__(self): return u'%s %s %s' % (self.slice, self.user, self.role) |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 129 | |
| 130 | def can_update(self, user): |
Tony Mack | 3428e6e | 2015-02-08 21:38:41 -0500 | [diff] [blame] | 131 | return user.can_update_slice(self.slice) |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 132 | |
Tony Mack | 5b06147 | 2014-02-04 07:57:10 -0500 | [diff] [blame] | 133 | @staticmethod |
| 134 | def select_by_user(user): |
| 135 | if user.is_admin: |
| 136 | qs = SlicePrivilege.objects.all() |
| 137 | else: |
| 138 | sp_ids = [sp.id for sp in SlicePrivilege.objects.filter(user=user)] |
| 139 | qs = SlicePrivilege.objects.filter(id__in=sp_ids) |
| 140 | return qs |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 141 | |
Tony Mack | 3066a95 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 142 | class ControllerSlice(PlCoreBase): |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 143 | objects = ControllerLinkManager() |
| 144 | deleted_objects = ControllerLinkDeletionManager() |
Sapan Bhatia | a660903 | 2014-09-22 14:55:08 -0400 | [diff] [blame] | 145 | |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 146 | controller = models.ForeignKey(Controller, related_name='controllerslices') |
| 147 | slice = models.ForeignKey(Slice, related_name='controllerslices') |
Tony Mack | c218b59 | 2014-03-19 15:17:53 -0400 | [diff] [blame] | 148 | tenant_id = models.CharField(null=True, blank=True, max_length=200, help_text="Keystone tenant id") |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 149 | |
Tony Mack | 51c4a7d | 2014-11-30 15:33:35 -0500 | [diff] [blame] | 150 | def __unicode__(self): return u'%s %s' % (self.slice, self.controller) |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 151 | |
| 152 | @staticmethod |
| 153 | def select_by_user(user): |
| 154 | if user.is_admin: |
Tony Mack | 3066a95 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 155 | qs = ControllerSlice.objects.all() |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 156 | else: |
| 157 | slices = Slice.select_by_user(user) |
Tony Mack | 3066a95 | 2015-01-05 22:48:11 -0500 | [diff] [blame] | 158 | qs = ControllerSlice.objects.filter(slice__in=slices) |
Tony Mack | e4be32f | 2014-03-11 20:45:25 -0400 | [diff] [blame] | 159 | return qs |
Sapan Bhatia | 772c7c2 | 2014-12-16 01:09:04 -0500 | [diff] [blame] | 160 | |
| 161 | def get_cpu_stats(self): |
| 162 | filter = 'project_id=%s'%self.tenant_id |
| 163 | return monitor.get_meter('cpu',filter,None) |
| 164 | |
| 165 | def get_bw_stats(self): |
| 166 | filter = 'project_id=%s'%self.tenant_id |
| 167 | return monitor.get_meter('network.outgoing.bytes',filter,None) |
| 168 | |
| 169 | def get_node_stats(self): |
| 170 | return len(self.slice.slivers) |