blob: 77adf575dcab4ef011162d143655b457e14e8066 [file] [log] [blame]
Scott Baker03324832015-01-08 22:38:17 -08001from rest_framework.decorators import api_view
2from rest_framework.response import Response
3from rest_framework.reverse import reverse
4from rest_framework import serializers
5from rest_framework import generics
6from rest_framework.views import APIView
7from core.models import *
8from django.forms import widgets
9from syndicate_storage.models import Volume
Scott Baker960431e2015-02-02 10:41:12 -080010from django.core.exceptions import PermissionDenied
Scott Baker03324832015-01-08 22:38:17 -080011
12# This REST API endpoint contains a bunch of misc information that the
13# tenant view needs to display
14
15BLESSED_DEPLOYMENTS = ["ViCCI"] # ["US-MaxPlanck", "US-GeorgiaTech", "US-Princeton", "US-Washington", "US-Stanford"]
16
Scott Bakerc94091a2015-01-12 12:57:44 -080017def getTenantViewDict(user):
Scott Baker012a7062015-02-09 10:53:13 -080018 blessed_deployments = []
19 for deployment in Deployment.objects.all():
20 if deployment.name in BLESSED_DEPLOYMENTS:
21 blessed_deployments.append(deployment)
22
Scott Baker03324832015-01-08 22:38:17 -080023 blessed_sites = []
24 for site in Site.objects.all():
25 good=False
26 for deployment in site.deployments.all():
27 if deployment.name in BLESSED_DEPLOYMENTS:
Scott Bakerf1b40152015-01-22 15:16:34 -080028 # only bless sites that have at least one node in the deployment
29 sitedeployments = SiteDeployment.objects.filter(site=site, deployment=deployment)
30 for sd in sitedeployments.all():
31 if sd.nodes.count()>0:
32 good=True
Scott Baker03324832015-01-08 22:38:17 -080033 if good:
34 blessed_sites.append(site)
35
36 blessed_images=[]
37 for image in Image.objects.all():
38 good = False
39 for deployment in image.deployments.all():
40 if deployment.name in BLESSED_DEPLOYMENTS:
41 good=True
42 if good:
43 blessed_images.append(image)
44
Scott Baker2a11f252015-01-12 13:37:01 -080045 blessed_flavors=[]
46 for flavor in Flavor.objects.all():
47 good = False
48 for deployment in flavor.deployments.all():
49 if deployment.name in BLESSED_DEPLOYMENTS:
50 good=True
51 if good:
52 blessed_flavors.append(flavor)
53
Scott Baker03324832015-01-08 22:38:17 -080054 volumes=[]
55 for volume in Volume.objects.all():
56 if not volume.private:
57 volumes.append(volume)
58
Scott Baker97acad92015-01-12 19:45:40 -080059 site_users=[]
Scott Bakerce0dfb82015-01-14 09:54:19 -080060 user_site_roles=[]
Scott Baker9eebcb62015-01-18 17:04:35 -080061 user_site_id=None
62 user_site_login_base=None
63 if not user.site:
64 pass # this is probably an error
65 else:
66 user_site_id = user.site.id
67 user_site_login_base = user.site.login_base
68 for auser in user.site.users.all():
69 site_users.append(auser)
70
71 for priv in user.site.siteprivileges.filter(user=user):
72 user_site_roles.append(priv.role.role)
Scott Bakerce0dfb82015-01-14 09:54:19 -080073
Scott Baker2a11f252015-01-12 13:37:01 -080074 blessed_service_classes = [ServiceClass.objects.get(name="Best Effort")]
75
Scott Baker03324832015-01-08 22:38:17 -080076 return {"id": 0,
77 "blessed_deployment_names": BLESSED_DEPLOYMENTS,
Scott Baker012a7062015-02-09 10:53:13 -080078 "blessed_deployments": [deployment.id for deployment in blessed_deployments],
Scott Baker03324832015-01-08 22:38:17 -080079 "blessed_site_names": [site.name for site in blessed_sites],
80 "blessed_sites": [site.id for site in blessed_sites],
81 "blessed_image_names": [image.name for image in blessed_images],
82 "blessed_images": [image.id for image in blessed_images],
Scott Baker2a11f252015-01-12 13:37:01 -080083 "blessed_flavor_names": [flavor.name for flavor in blessed_flavors],
84 "blessed_flavors": [flavor.id for flavor in blessed_flavors],
85 "blessed_service_class_names": [serviceclass.name for serviceclass in blessed_service_classes],
86 "blessed_service_classes": [serviceclass.id for serviceclass in blessed_service_classes],
Scott Baker03324832015-01-08 22:38:17 -080087 "public_volume_names": [volume.name for volume in volumes],
88 "public_volumes": [volume.id for volume in volumes],
Scott Baker9eebcb62015-01-18 17:04:35 -080089 "current_user_site_id": user_site_id,
90 "current_user_login_base": user_site_login_base,
Scott Baker97acad92015-01-12 19:45:40 -080091 "current_user_site_users": [auser.id for auser in site_users],
92 "current_user_site_user_names": [auser.email for auser in site_users],
Scott Bakerce0dfb82015-01-14 09:54:19 -080093 "current_user_can_create_slice": user.is_admin or ("pi" in user_site_roles) or ("admin" in user_site_roles),
Scott Bakerc8984372015-01-19 08:56:16 -080094 "current_user_id": user.id,
Scott Baker03324832015-01-08 22:38:17 -080095 }
96
97class TenantList(APIView):
98 method_kind = "list"
99 method_name = "tenantview"
100
101 def get(self, request, format=None):
Scott Baker960431e2015-02-02 10:41:12 -0800102 if (not request.user.is_authenticated()):
103 raise PermissionDenied("You must be authenticated in order to use this API")
Scott Bakerc94091a2015-01-12 12:57:44 -0800104 return Response( getTenantViewDict(request.user) )
Scott Baker03324832015-01-08 22:38:17 -0800105
106class TenantDetail(APIView):
107 method_kind = "detail"
108 method_name = "tenantview"
109
110 def get(self, request, format=None, pk=0):
Scott Baker960431e2015-02-02 10:41:12 -0800111 if (not request.user.is_authenticated()):
112 raise PermissionDenied("You must be authenticated in order to use this API")
Scott Bakerc94091a2015-01-12 12:57:44 -0800113 return Response( [getTenantViewDict(request.user)] )
Scott Baker03324832015-01-08 22:38:17 -0800114