blob: 44db514c957f10671de95d32033fe5a8a06b1aa6 [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
10
11# This REST API endpoint contains a bunch of misc information that the
12# tenant view needs to display
13
14BLESSED_DEPLOYMENTS = ["ViCCI"] # ["US-MaxPlanck", "US-GeorgiaTech", "US-Princeton", "US-Washington", "US-Stanford"]
15
Scott Bakerc94091a2015-01-12 12:57:44 -080016def getTenantViewDict(user):
Scott Baker03324832015-01-08 22:38:17 -080017 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 Bakerf1b40152015-01-22 15:16:34 -080022 # 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 Baker03324832015-01-08 22:38:17 -080027 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 Baker2a11f252015-01-12 13:37:01 -080039 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 Baker03324832015-01-08 22:38:17 -080048 volumes=[]
49 for volume in Volume.objects.all():
50 if not volume.private:
51 volumes.append(volume)
52
Scott Baker97acad92015-01-12 19:45:40 -080053 site_users=[]
Scott Bakerce0dfb82015-01-14 09:54:19 -080054 user_site_roles=[]
Scott Baker9eebcb62015-01-18 17:04:35 -080055 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 Bakerce0dfb82015-01-14 09:54:19 -080067
Scott Baker2a11f252015-01-12 13:37:01 -080068 blessed_service_classes = [ServiceClass.objects.get(name="Best Effort")]
69
Scott Baker03324832015-01-08 22:38:17 -080070 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 Baker2a11f252015-01-12 13:37:01 -080076 "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 Baker03324832015-01-08 22:38:17 -080080 "public_volume_names": [volume.name for volume in volumes],
81 "public_volumes": [volume.id for volume in volumes],
Scott Baker9eebcb62015-01-18 17:04:35 -080082 "current_user_site_id": user_site_id,
83 "current_user_login_base": user_site_login_base,
Scott Baker97acad92015-01-12 19:45:40 -080084 "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 Bakerce0dfb82015-01-14 09:54:19 -080086 "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 -080087 "current_user_id": user.id,
Scott Baker03324832015-01-08 22:38:17 -080088 }
89
90class TenantList(APIView):
91 method_kind = "list"
92 method_name = "tenantview"
93
94 def get(self, request, format=None):
Scott Bakerc94091a2015-01-12 12:57:44 -080095 return Response( getTenantViewDict(request.user) )
Scott Baker03324832015-01-08 22:38:17 -080096
97class TenantDetail(APIView):
98 method_kind = "detail"
99 method_name = "tenantview"
100
101 def get(self, request, format=None, pk=0):
Scott Bakerc94091a2015-01-12 12:57:44 -0800102 return Response( [getTenantViewDict(request.user)] )
Scott Baker03324832015-01-08 22:38:17 -0800103