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