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 |
| 10 | |
| 11 | # This REST API endpoint contains a bunch of misc information that the |
| 12 | # tenant view needs to display |
| 13 | |
| 14 | BLESSED_DEPLOYMENTS = ["ViCCI"] # ["US-MaxPlanck", "US-GeorgiaTech", "US-Princeton", "US-Washington", "US-Stanford"] |
| 15 | |
Scott Baker | c94091a | 2015-01-12 12:57:44 -0800 | [diff] [blame] | 16 | def getTenantViewDict(user): |
Scott Baker | 0332483 | 2015-01-08 22:38:17 -0800 | [diff] [blame] | 17 | blessed_sites = [] |
| 18 | for site in Site.objects.all(): |
| 19 | good=False |
| 20 | for deployment in site.deployments.all(): |
| 21 | if deployment.name in BLESSED_DEPLOYMENTS: |
| 22 | good=True |
| 23 | if good: |
| 24 | blessed_sites.append(site) |
| 25 | |
| 26 | blessed_images=[] |
| 27 | for image in Image.objects.all(): |
| 28 | good = False |
| 29 | for deployment in image.deployments.all(): |
| 30 | if deployment.name in BLESSED_DEPLOYMENTS: |
| 31 | good=True |
| 32 | if good: |
| 33 | blessed_images.append(image) |
| 34 | |
Scott Baker | 2a11f25 | 2015-01-12 13:37:01 -0800 | [diff] [blame] | 35 | blessed_flavors=[] |
| 36 | for flavor in Flavor.objects.all(): |
| 37 | good = False |
| 38 | for deployment in flavor.deployments.all(): |
| 39 | if deployment.name in BLESSED_DEPLOYMENTS: |
| 40 | good=True |
| 41 | if good: |
| 42 | blessed_flavors.append(flavor) |
| 43 | |
Scott Baker | 0332483 | 2015-01-08 22:38:17 -0800 | [diff] [blame] | 44 | volumes=[] |
| 45 | for volume in Volume.objects.all(): |
| 46 | if not volume.private:
|
| 47 | volumes.append(volume) |
| 48 | |
Scott Baker | 97acad9 | 2015-01-12 19:45:40 -0800 | [diff] [blame] | 49 | site_users=[] |
Scott Baker | ce0dfb8 | 2015-01-14 09:54:19 -0800 | [diff] [blame] | 50 | user_site_roles=[] |
Scott Baker | 9eebcb6 | 2015-01-18 17:04:35 -0800 | [diff] [blame] | 51 | user_site_id=None |
| 52 | user_site_login_base=None |
| 53 | if not user.site: |
| 54 | pass # this is probably an error |
| 55 | else: |
| 56 | user_site_id = user.site.id |
| 57 | user_site_login_base = user.site.login_base |
| 58 | for auser in user.site.users.all(): |
| 59 | site_users.append(auser) |
| 60 | |
| 61 | for priv in user.site.siteprivileges.filter(user=user): |
| 62 | user_site_roles.append(priv.role.role) |
Scott Baker | ce0dfb8 | 2015-01-14 09:54:19 -0800 | [diff] [blame] | 63 | |
Scott Baker | 2a11f25 | 2015-01-12 13:37:01 -0800 | [diff] [blame] | 64 | blessed_service_classes = [ServiceClass.objects.get(name="Best Effort")] |
| 65 | |
Scott Baker | 0332483 | 2015-01-08 22:38:17 -0800 | [diff] [blame] | 66 | return {"id": 0, |
| 67 | "blessed_deployment_names": BLESSED_DEPLOYMENTS, |
| 68 | "blessed_site_names": [site.name for site in blessed_sites], |
| 69 | "blessed_sites": [site.id for site in blessed_sites], |
| 70 | "blessed_image_names": [image.name for image in blessed_images], |
| 71 | "blessed_images": [image.id for image in blessed_images], |
Scott Baker | 2a11f25 | 2015-01-12 13:37:01 -0800 | [diff] [blame] | 72 | "blessed_flavor_names": [flavor.name for flavor in blessed_flavors], |
| 73 | "blessed_flavors": [flavor.id for flavor in blessed_flavors], |
| 74 | "blessed_service_class_names": [serviceclass.name for serviceclass in blessed_service_classes], |
| 75 | "blessed_service_classes": [serviceclass.id for serviceclass in blessed_service_classes], |
Scott Baker | 0332483 | 2015-01-08 22:38:17 -0800 | [diff] [blame] | 76 | "public_volume_names": [volume.name for volume in volumes], |
| 77 | "public_volumes": [volume.id for volume in volumes], |
Scott Baker | 9eebcb6 | 2015-01-18 17:04:35 -0800 | [diff] [blame] | 78 | "current_user_site_id": user_site_id, |
| 79 | "current_user_login_base": user_site_login_base, |
Scott Baker | 97acad9 | 2015-01-12 19:45:40 -0800 | [diff] [blame] | 80 | "current_user_site_users": [auser.id for auser in site_users], |
| 81 | "current_user_site_user_names": [auser.email for auser in site_users], |
Scott Baker | ce0dfb8 | 2015-01-14 09:54:19 -0800 | [diff] [blame] | 82 | "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] | 83 | "current_user_id": user.id, |
Scott Baker | 0332483 | 2015-01-08 22:38:17 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | class TenantList(APIView): |
| 87 | method_kind = "list" |
| 88 | method_name = "tenantview" |
| 89 | |
| 90 | def get(self, request, format=None): |
Scott Baker | c94091a | 2015-01-12 12:57:44 -0800 | [diff] [blame] | 91 | return Response( getTenantViewDict(request.user) ) |
Scott Baker | 0332483 | 2015-01-08 22:38:17 -0800 | [diff] [blame] | 92 | |
| 93 | class TenantDetail(APIView): |
| 94 | method_kind = "detail" |
| 95 | method_name = "tenantview" |
| 96 | |
| 97 | def get(self, request, format=None, pk=0): |
Scott Baker | c94091a | 2015-01-12 12:57:44 -0800 | [diff] [blame] | 98 | return Response( [getTenantViewDict(request.user)] ) |
Scott Baker | 0332483 | 2015-01-08 22:38:17 -0800 | [diff] [blame] | 99 | |