Scott Baker | 0332483 | 2015-01-08 22:38:17 -0800 | [diff] [blame] | 1 | from rest_framework.decorators import api_view |
| 2 | from rest_framework.response import Response
|
| 3 | from rest_framework.reverse import reverse
|
| 4 | from rest_framework import serializers
|
| 5 | from rest_framework import generics
|
| 6 | from rest_framework.views import APIView
|
| 7 | from core.models import *
|
| 8 | from django.forms import widgets |
| 9 | from syndicate_storage.models import Volume |
Scott Baker | 960431e | 2015-02-02 10:41:12 -0800 | [diff] [blame] | 10 | from django.core.exceptions import PermissionDenied |
Scott Baker | 0332483 | 2015-01-08 22:38:17 -0800 | [diff] [blame] | 11 | |
| 12 | # This REST API endpoint contains a bunch of misc information that the |
| 13 | # tenant view needs to display |
| 14 | |
Scott Baker | c94091a | 2015-01-12 12:57:44 -0800 | [diff] [blame] | 15 | def getTenantViewDict(user): |
Scott Baker | ecc55ac | 2015-02-17 13:34:32 -0800 | [diff] [blame] | 16 | # compute blessed_deployments by looking for the tenant view, and seeing what |
| 17 | # deployments are attached to it. |
| 18 | blessed_deployments=[] |
| 19 | for dash in DashboardView.objects.all(): |
| 20 | if (dash.url=="template:xosTenant"): |
| 21 | for deployment in dash.deployments.all(): |
| 22 | if deployment not in blessed_deployments: |
| 23 | blessed_deployments.append(deployment) |
| 24 | |
| 25 | blessed_deployment_ids = [d.id for d in blessed_deployments] |
Scott Baker | 012a706 | 2015-02-09 10:53:13 -0800 | [diff] [blame] | 26 | |
Scott Baker | 0332483 | 2015-01-08 22:38:17 -0800 | [diff] [blame] | 27 | blessed_sites = [] |
| 28 | for site in Site.objects.all(): |
| 29 | good=False |
| 30 | for deployment in site.deployments.all(): |
Scott Baker | ecc55ac | 2015-02-17 13:34:32 -0800 | [diff] [blame] | 31 | if deployment.id in blessed_deployment_ids: |
Scott Baker | f1b4015 | 2015-01-22 15:16:34 -0800 | [diff] [blame] | 32 | # only bless sites that have at least one node in the deployment |
| 33 | sitedeployments = SiteDeployment.objects.filter(site=site, deployment=deployment) |
| 34 | for sd in sitedeployments.all(): |
| 35 | if sd.nodes.count()>0: |
| 36 | good=True |
Scott Baker | 0332483 | 2015-01-08 22:38:17 -0800 | [diff] [blame] | 37 | if good: |
| 38 | blessed_sites.append(site) |
| 39 | |
| 40 | blessed_images=[] |
| 41 | for image in Image.objects.all(): |
| 42 | good = False |
| 43 | for deployment in image.deployments.all(): |
Scott Baker | ecc55ac | 2015-02-17 13:34:32 -0800 | [diff] [blame] | 44 | if deployment.id in blessed_deployment_ids: |
Scott Baker | 0332483 | 2015-01-08 22:38:17 -0800 | [diff] [blame] | 45 | good=True |
| 46 | if good: |
| 47 | blessed_images.append(image) |
| 48 | |
Scott Baker | 2a11f25 | 2015-01-12 13:37:01 -0800 | [diff] [blame] | 49 | blessed_flavors=[] |
| 50 | for flavor in Flavor.objects.all(): |
| 51 | good = False |
| 52 | for deployment in flavor.deployments.all(): |
Scott Baker | ecc55ac | 2015-02-17 13:34:32 -0800 | [diff] [blame] | 53 | if deployment.id in blessed_deployment_ids: |
Scott Baker | 2a11f25 | 2015-01-12 13:37:01 -0800 | [diff] [blame] | 54 | good=True |
| 55 | if good: |
| 56 | blessed_flavors.append(flavor) |
| 57 | |
Scott Baker | 0332483 | 2015-01-08 22:38:17 -0800 | [diff] [blame] | 58 | volumes=[] |
| 59 | for volume in Volume.objects.all(): |
| 60 | if not volume.private:
|
| 61 | volumes.append(volume) |
| 62 | |
Scott Baker | 97acad9 | 2015-01-12 19:45:40 -0800 | [diff] [blame] | 63 | site_users=[] |
Scott Baker | ce0dfb8 | 2015-01-14 09:54:19 -0800 | [diff] [blame] | 64 | user_site_roles=[] |
Scott Baker | 9eebcb6 | 2015-01-18 17:04:35 -0800 | [diff] [blame] | 65 | user_site_id=None |
| 66 | user_site_login_base=None |
| 67 | if not user.site: |
| 68 | pass # this is probably an error |
| 69 | else: |
| 70 | user_site_id = user.site.id |
| 71 | user_site_login_base = user.site.login_base |
| 72 | for auser in user.site.users.all(): |
| 73 | site_users.append(auser) |
| 74 | |
| 75 | for priv in user.site.siteprivileges.filter(user=user): |
| 76 | user_site_roles.append(priv.role.role) |
Scott Baker | ce0dfb8 | 2015-01-14 09:54:19 -0800 | [diff] [blame] | 77 | |
Scott Baker | 2a11f25 | 2015-01-12 13:37:01 -0800 | [diff] [blame] | 78 | blessed_service_classes = [ServiceClass.objects.get(name="Best Effort")] |
| 79 | |
Scott Baker | 0332483 | 2015-01-08 22:38:17 -0800 | [diff] [blame] | 80 | return {"id": 0, |
Scott Baker | ecc55ac | 2015-02-17 13:34:32 -0800 | [diff] [blame] | 81 | "blessed_deployment_names": [deployment.name for deployment in blessed_deployments], |
Scott Baker | 012a706 | 2015-02-09 10:53:13 -0800 | [diff] [blame] | 82 | "blessed_deployments": [deployment.id for deployment in blessed_deployments], |
Scott Baker | 0332483 | 2015-01-08 22:38:17 -0800 | [diff] [blame] | 83 | "blessed_site_names": [site.name for site in blessed_sites], |
| 84 | "blessed_sites": [site.id for site in blessed_sites], |
| 85 | "blessed_image_names": [image.name for image in blessed_images], |
| 86 | "blessed_images": [image.id for image in blessed_images], |
Scott Baker | 2a11f25 | 2015-01-12 13:37:01 -0800 | [diff] [blame] | 87 | "blessed_flavor_names": [flavor.name for flavor in blessed_flavors], |
| 88 | "blessed_flavors": [flavor.id for flavor in blessed_flavors], |
| 89 | "blessed_service_class_names": [serviceclass.name for serviceclass in blessed_service_classes], |
| 90 | "blessed_service_classes": [serviceclass.id for serviceclass in blessed_service_classes], |
Scott Baker | 0332483 | 2015-01-08 22:38:17 -0800 | [diff] [blame] | 91 | "public_volume_names": [volume.name for volume in volumes], |
| 92 | "public_volumes": [volume.id for volume in volumes], |
Scott Baker | 9eebcb6 | 2015-01-18 17:04:35 -0800 | [diff] [blame] | 93 | "current_user_site_id": user_site_id, |
| 94 | "current_user_login_base": user_site_login_base, |
Scott Baker | 97acad9 | 2015-01-12 19:45:40 -0800 | [diff] [blame] | 95 | "current_user_site_users": [auser.id for auser in site_users], |
| 96 | "current_user_site_user_names": [auser.email for auser in site_users], |
Scott Baker | ce0dfb8 | 2015-01-14 09:54:19 -0800 | [diff] [blame] | 97 | "current_user_can_create_slice": user.is_admin or ("pi" in user_site_roles) or ("admin" in user_site_roles), |
Scott Baker | c898437 | 2015-01-19 08:56:16 -0800 | [diff] [blame] | 98 | "current_user_id": user.id, |
Scott Baker | 0332483 | 2015-01-08 22:38:17 -0800 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | class TenantList(APIView): |
| 102 | method_kind = "list" |
| 103 | method_name = "tenantview" |
| 104 | |
| 105 | def get(self, request, format=None): |
Scott Baker | 960431e | 2015-02-02 10:41:12 -0800 | [diff] [blame] | 106 | if (not request.user.is_authenticated()): |
| 107 | raise PermissionDenied("You must be authenticated in order to use this API") |
Scott Baker | c94091a | 2015-01-12 12:57:44 -0800 | [diff] [blame] | 108 | return Response( getTenantViewDict(request.user) ) |
Scott Baker | 0332483 | 2015-01-08 22:38:17 -0800 | [diff] [blame] | 109 | |
| 110 | class TenantDetail(APIView): |
| 111 | method_kind = "detail" |
| 112 | method_name = "tenantview" |
| 113 | |
| 114 | def get(self, request, format=None, pk=0): |
Scott Baker | 960431e | 2015-02-02 10:41:12 -0800 | [diff] [blame] | 115 | if (not request.user.is_authenticated()): |
| 116 | raise PermissionDenied("You must be authenticated in order to use this API") |
Scott Baker | c94091a | 2015-01-12 12:57:44 -0800 | [diff] [blame] | 117 | return Response( [getTenantViewDict(request.user)] ) |
Scott Baker | 0332483 | 2015-01-08 22:38:17 -0800 | [diff] [blame] | 118 | |