Scott Baker | fcf655e | 2015-01-12 19:45:40 -0800 | [diff] [blame] | 1 | from core.models.slice import Slice, SlicePrivilege |
Scott Baker | e791dc6 | 2014-08-28 14:02:54 -0700 | [diff] [blame] | 2 | from plus import PlusObjectMixin |
Scott Baker | 5f4770d | 2014-07-13 11:17:58 -0700 | [diff] [blame] | 3 | |
Scott Baker | e791dc6 | 2014-08-28 14:02:54 -0700 | [diff] [blame] | 4 | class SlicePlus(Slice, PlusObjectMixin): |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 5 | class Meta: |
| 6 | proxy = True |
| 7 | |
Scott Baker | 5f4770d | 2014-07-13 11:17:58 -0700 | [diff] [blame] | 8 | def getSliceInfo(self, user=None): |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 9 | used_sites = {} |
| 10 | used_deployments = {} |
Scott Baker | 5f4770d | 2014-07-13 11:17:58 -0700 | [diff] [blame] | 11 | sliverCount = 0 |
| 12 | for sliver in self.slivers.all(): |
Scott Baker | dc84412 | 2015-01-07 11:15:08 -0800 | [diff] [blame] | 13 | site = sliver.node.site_deployment.site |
| 14 | deployment = sliver.node.site_deployment.deployment |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 15 | used_sites[site.name] = used_sites.get(site.name, 0) + 1 |
Scott Baker | 5f4770d | 2014-07-13 11:17:58 -0700 | [diff] [blame] | 16 | used_deployments[deployment.name] = used_deployments.get(deployment.name, 0) + 1 |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 17 | sliverCount = sliverCount + 1 |
Scott Baker | 5f4770d | 2014-07-13 11:17:58 -0700 | [diff] [blame] | 18 | |
| 19 | roles = [] |
| 20 | if (user!=None): |
Scott Baker | c865508 | 2014-12-23 11:03:58 -0800 | [diff] [blame] | 21 | roles = [x.role.role for x in self.sliceprivileges.filter(user=user)] |
Scott Baker | 5f4770d | 2014-07-13 11:17:58 -0700 | [diff] [blame] | 22 | |
| 23 | return {"sitesUsed": used_sites, |
| 24 | "deploymentsUsed": used_deployments, |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 25 | "sliverCount": sliverCount, |
Scott Baker | 5f4770d | 2014-07-13 11:17:58 -0700 | [diff] [blame] | 26 | "siteCount": len(used_sites.keys()), |
| 27 | "roles": roles} |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 28 | |
Scott Baker | 9062b0c | 2015-01-08 22:34:51 -0800 | [diff] [blame] | 29 | @property |
Scott Baker | 03696ee | 2015-01-11 13:45:19 -0800 | [diff] [blame] | 30 | def site_allocation(self): |
| 31 | return self.getSliceInfo()["sitesUsed"] |
| 32 | |
| 33 | @site_allocation.setter |
| 34 | def site_allocation(self, value): |
| 35 | print "XXX set sitesUsed to", value |
| 36 | |
| 37 | @property |
Scott Baker | fcf655e | 2015-01-12 19:45:40 -0800 | [diff] [blame] | 38 | def users(self): |
| 39 | user_ids = [] |
| 40 | for priv in SlicePrivilege.objects.filter(slice=self): |
| 41 | if not (priv.user.id in user_ids): |
| 42 | user_ids.append(priv.user.id) |
| 43 | return user_ids |
| 44 | |
| 45 | @users.setter |
| 46 | def users(self, value): |
| 47 | print "XXX set users to", value |
| 48 | |
| 49 | @property |
Scott Baker | 697512d | 2015-01-08 22:37:34 -0800 | [diff] [blame] | 50 | def network_ports(self): |
Scott Baker | 9062b0c | 2015-01-08 22:34:51 -0800 | [diff] [blame] | 51 | # XXX this assumes there is only one network that can have ports bound |
| 52 | # to it for a given slice. This is intended for the tenant view, which |
| 53 | # will obey this field. |
| 54 | networkPorts = "" |
| 55 | for networkSlice in self.networkslices.all(): |
| 56 | network = networkSlice.network |
| 57 | if network.ports: |
| 58 | networkPorts = network.ports |
| 59 | |
| 60 | return networkPorts |
| 61 | |
Scott Baker | 697512d | 2015-01-08 22:37:34 -0800 | [diff] [blame] | 62 | @network_ports.setter |
| 63 | def network_ports(self, value): |
Scott Baker | 9062b0c | 2015-01-08 22:34:51 -0800 | [diff] [blame] | 64 | print "XXX set networkPorts to", value |
| 65 | |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 66 | @staticmethod |
| 67 | def select_by_user(user): |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 68 | if user.is_admin: |
| 69 | qs = SlicePlus.objects.all() |
| 70 | else: |
| 71 | slice_ids = [sp.slice.id for sp in SlicePrivilege.objects.filter(user=user)] |
| 72 | qs = SlicePlus.objects.filter(id__in=slice_ids) |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 73 | return qs |