Scott Baker | 5f4770d | 2014-07-13 11:17:58 -0700 | [diff] [blame] | 1 | from core.models.slice import Slice |
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 | 98b26b6 | 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 | df54cf2 | 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 | 8b89d30 | 2015-01-08 22:34:51 -0800 | [diff] [blame] | 29 | @property |
Scott Baker | dcf6fbf | 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 | d3a6b2c | 2015-01-08 22:37:34 -0800 | [diff] [blame] | 38 | def network_ports(self): |
Scott Baker | 8b89d30 | 2015-01-08 22:34:51 -0800 | [diff] [blame] | 39 | # XXX this assumes there is only one network that can have ports bound |
| 40 | # to it for a given slice. This is intended for the tenant view, which |
| 41 | # will obey this field. |
| 42 | networkPorts = "" |
| 43 | for networkSlice in self.networkslices.all(): |
| 44 | network = networkSlice.network |
| 45 | if network.ports: |
| 46 | networkPorts = network.ports |
| 47 | |
| 48 | return networkPorts |
| 49 | |
Scott Baker | d3a6b2c | 2015-01-08 22:37:34 -0800 | [diff] [blame] | 50 | @network_ports.setter |
| 51 | def network_ports(self, value): |
Scott Baker | 8b89d30 | 2015-01-08 22:34:51 -0800 | [diff] [blame] | 52 | print "XXX set networkPorts to", value |
| 53 | |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 54 | @staticmethod |
| 55 | def select_by_user(user): |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 56 | if user.is_admin: |
| 57 | qs = SlicePlus.objects.all() |
| 58 | else: |
| 59 | slice_ids = [sp.slice.id for sp in SlicePrivilege.objects.filter(user=user)] |
| 60 | qs = SlicePlus.objects.filter(id__in=slice_ids) |
Scott Baker | 88e3437 | 2014-07-13 11:46:36 -0700 | [diff] [blame] | 61 | return qs |