blob: 5694374129c4ab72403df1c8b1e897e65dacc930 [file] [log] [blame]
Scott Baker45a09b12015-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 Baker20101af2015-02-02 10:41:12 -080010from django.core.exceptions import PermissionDenied
Scott Baker45a09b12015-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 Baker48b84d12015-01-12 12:57:44 -080017def getTenantViewDict(user):
Scott Baker45a09b12015-01-08 22:38:17 -080018 blessed_sites = []
19 for site in Site.objects.all():
20 good=False
21 for deployment in site.deployments.all():
22 if deployment.name in BLESSED_DEPLOYMENTS:
Scott Bakera8c0b242015-01-22 15:16:34 -080023 # only bless sites that have at least one node in the deployment
24 sitedeployments = SiteDeployment.objects.filter(site=site, deployment=deployment)
25 for sd in sitedeployments.all():
26 if sd.nodes.count()>0:
27 good=True
Scott Baker45a09b12015-01-08 22:38:17 -080028 if good:
29 blessed_sites.append(site)
30
31 blessed_images=[]
32 for image in Image.objects.all():
33 good = False
34 for deployment in image.deployments.all():
35 if deployment.name in BLESSED_DEPLOYMENTS:
36 good=True
37 if good:
38 blessed_images.append(image)
39
Scott Bakeredaa0ed2015-01-12 13:37:01 -080040 blessed_flavors=[]
41 for flavor in Flavor.objects.all():
42 good = False
43 for deployment in flavor.deployments.all():
44 if deployment.name in BLESSED_DEPLOYMENTS:
45 good=True
46 if good:
47 blessed_flavors.append(flavor)
48
Scott Baker45a09b12015-01-08 22:38:17 -080049 volumes=[]
50 for volume in Volume.objects.all():
51 if not volume.private:
52 volumes.append(volume)
53
Scott Bakerfcf655e2015-01-12 19:45:40 -080054 site_users=[]
Scott Baker05d2e032015-01-14 09:54:19 -080055 user_site_roles=[]
Scott Baker71b3a452015-01-18 17:04:35 -080056 user_site_id=None
57 user_site_login_base=None
58 if not user.site:
59 pass # this is probably an error
60 else:
61 user_site_id = user.site.id
62 user_site_login_base = user.site.login_base
63 for auser in user.site.users.all():
64 site_users.append(auser)
65
66 for priv in user.site.siteprivileges.filter(user=user):
67 user_site_roles.append(priv.role.role)
Scott Baker05d2e032015-01-14 09:54:19 -080068
Scott Bakeredaa0ed2015-01-12 13:37:01 -080069 blessed_service_classes = [ServiceClass.objects.get(name="Best Effort")]
70
Scott Baker45a09b12015-01-08 22:38:17 -080071 return {"id": 0,
72 "blessed_deployment_names": BLESSED_DEPLOYMENTS,
73 "blessed_site_names": [site.name for site in blessed_sites],
74 "blessed_sites": [site.id for site in blessed_sites],
75 "blessed_image_names": [image.name for image in blessed_images],
76 "blessed_images": [image.id for image in blessed_images],
Scott Bakeredaa0ed2015-01-12 13:37:01 -080077 "blessed_flavor_names": [flavor.name for flavor in blessed_flavors],
78 "blessed_flavors": [flavor.id for flavor in blessed_flavors],
79 "blessed_service_class_names": [serviceclass.name for serviceclass in blessed_service_classes],
80 "blessed_service_classes": [serviceclass.id for serviceclass in blessed_service_classes],
Scott Baker45a09b12015-01-08 22:38:17 -080081 "public_volume_names": [volume.name for volume in volumes],
82 "public_volumes": [volume.id for volume in volumes],
Scott Baker71b3a452015-01-18 17:04:35 -080083 "current_user_site_id": user_site_id,
84 "current_user_login_base": user_site_login_base,
Scott Bakerfcf655e2015-01-12 19:45:40 -080085 "current_user_site_users": [auser.id for auser in site_users],
86 "current_user_site_user_names": [auser.email for auser in site_users],
Scott Baker05d2e032015-01-14 09:54:19 -080087 "current_user_can_create_slice": user.is_admin or ("pi" in user_site_roles) or ("admin" in user_site_roles),
Scott Bakerdcfcefd2015-01-19 08:56:16 -080088 "current_user_id": user.id,
Scott Baker45a09b12015-01-08 22:38:17 -080089 }
90
91class TenantList(APIView):
92 method_kind = "list"
93 method_name = "tenantview"
94
95 def get(self, request, format=None):
Scott Baker20101af2015-02-02 10:41:12 -080096 if (not request.user.is_authenticated()):
97 raise PermissionDenied("You must be authenticated in order to use this API")
Scott Baker48b84d12015-01-12 12:57:44 -080098 return Response( getTenantViewDict(request.user) )
Scott Baker45a09b12015-01-08 22:38:17 -080099
100class TenantDetail(APIView):
101 method_kind = "detail"
102 method_name = "tenantview"
103
104 def get(self, request, format=None, pk=0):
Scott Baker20101af2015-02-02 10:41:12 -0800105 if (not request.user.is_authenticated()):
106 raise PermissionDenied("You must be authenticated in order to use this API")
Scott Baker48b84d12015-01-12 12:57:44 -0800107 return Response( [getTenantViewDict(request.user)] )
Scott Baker45a09b12015-01-08 22:38:17 -0800108