blob: 10323a27c9bd649669643a93d9d0625ebd6121a0 [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
Scott Bakerc94091a2015-01-12 12:57:44 -080015def getTenantViewDict(user):
Scott Bakerecc55ac2015-02-17 13:34:32 -080016 # compute blessed_deployments by looking for the tenant view, and seeing what
17 # deployments are attached to it.
18 blessed_deployments=[]
19 for dash in DashboardView.objects.all():
20 if (dash.url=="template:xosTenant"):
21 for deployment in dash.deployments.all():
22 if deployment not in blessed_deployments:
23 blessed_deployments.append(deployment)
24
25 blessed_deployment_ids = [d.id for d in blessed_deployments]
Scott Baker012a7062015-02-09 10:53:13 -080026
Scott Baker03324832015-01-08 22:38:17 -080027 blessed_sites = []
28 for site in Site.objects.all():
29 good=False
30 for deployment in site.deployments.all():
Scott Bakerecc55ac2015-02-17 13:34:32 -080031 if deployment.id in blessed_deployment_ids:
Scott Bakerf1b40152015-01-22 15:16:34 -080032 # only bless sites that have at least one node in the deployment
33 sitedeployments = SiteDeployment.objects.filter(site=site, deployment=deployment)
34 for sd in sitedeployments.all():
35 if sd.nodes.count()>0:
36 good=True
Scott Baker03324832015-01-08 22:38:17 -080037 if good:
38 blessed_sites.append(site)
39
40 blessed_images=[]
41 for image in Image.objects.all():
42 good = False
43 for deployment in image.deployments.all():
Scott Bakerecc55ac2015-02-17 13:34:32 -080044 if deployment.id in blessed_deployment_ids:
Scott Baker03324832015-01-08 22:38:17 -080045 good=True
46 if good:
47 blessed_images.append(image)
48
Scott Baker2a11f252015-01-12 13:37:01 -080049 blessed_flavors=[]
50 for flavor in Flavor.objects.all():
51 good = False
52 for deployment in flavor.deployments.all():
Scott Bakerecc55ac2015-02-17 13:34:32 -080053 if deployment.id in blessed_deployment_ids:
Scott Baker2a11f252015-01-12 13:37:01 -080054 good=True
55 if good:
56 blessed_flavors.append(flavor)
57
Scott Baker03324832015-01-08 22:38:17 -080058 volumes=[]
59 for volume in Volume.objects.all():
60 if not volume.private:
61 volumes.append(volume)
62
Scott Baker97acad92015-01-12 19:45:40 -080063 site_users=[]
Scott Bakerce0dfb82015-01-14 09:54:19 -080064 user_site_roles=[]
Scott Baker9eebcb62015-01-18 17:04:35 -080065 user_site_id=None
66 user_site_login_base=None
67 if not user.site:
68 pass # this is probably an error
69 else:
70 user_site_id = user.site.id
71 user_site_login_base = user.site.login_base
72 for auser in user.site.users.all():
73 site_users.append(auser)
74
75 for priv in user.site.siteprivileges.filter(user=user):
76 user_site_roles.append(priv.role.role)
Scott Bakerce0dfb82015-01-14 09:54:19 -080077
Scott Baker2a11f252015-01-12 13:37:01 -080078 blessed_service_classes = [ServiceClass.objects.get(name="Best Effort")]
79
Scott Baker03324832015-01-08 22:38:17 -080080 return {"id": 0,
Scott Bakerecc55ac2015-02-17 13:34:32 -080081 "blessed_deployment_names": [deployment.name for deployment in blessed_deployments],
Scott Baker012a7062015-02-09 10:53:13 -080082 "blessed_deployments": [deployment.id for deployment in blessed_deployments],
Scott Baker03324832015-01-08 22:38:17 -080083 "blessed_site_names": [site.name for site in blessed_sites],
84 "blessed_sites": [site.id for site in blessed_sites],
85 "blessed_image_names": [image.name for image in blessed_images],
86 "blessed_images": [image.id for image in blessed_images],
Scott Baker2a11f252015-01-12 13:37:01 -080087 "blessed_flavor_names": [flavor.name for flavor in blessed_flavors],
88 "blessed_flavors": [flavor.id for flavor in blessed_flavors],
89 "blessed_service_class_names": [serviceclass.name for serviceclass in blessed_service_classes],
90 "blessed_service_classes": [serviceclass.id for serviceclass in blessed_service_classes],
Scott Baker03324832015-01-08 22:38:17 -080091 "public_volume_names": [volume.name for volume in volumes],
92 "public_volumes": [volume.id for volume in volumes],
Scott Baker9eebcb62015-01-18 17:04:35 -080093 "current_user_site_id": user_site_id,
94 "current_user_login_base": user_site_login_base,
Scott Baker97acad92015-01-12 19:45:40 -080095 "current_user_site_users": [auser.id for auser in site_users],
96 "current_user_site_user_names": [auser.email for auser in site_users],
Scott Bakerce0dfb82015-01-14 09:54:19 -080097 "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 -080098 "current_user_id": user.id,
Scott Baker03324832015-01-08 22:38:17 -080099 }
100
101class TenantList(APIView):
102 method_kind = "list"
103 method_name = "tenantview"
104
105 def get(self, request, format=None):
Scott Baker960431e2015-02-02 10:41:12 -0800106 if (not request.user.is_authenticated()):
107 raise PermissionDenied("You must be authenticated in order to use this API")
Scott Bakerc94091a2015-01-12 12:57:44 -0800108 return Response( getTenantViewDict(request.user) )
Scott Baker03324832015-01-08 22:38:17 -0800109
110class TenantDetail(APIView):
111 method_kind = "detail"
112 method_name = "tenantview"
113
114 def get(self, request, format=None, pk=0):
Scott Baker960431e2015-02-02 10:41:12 -0800115 if (not request.user.is_authenticated()):
116 raise PermissionDenied("You must be authenticated in order to use this API")
Scott Bakerc94091a2015-01-12 12:57:44 -0800117 return Response( [getTenantViewDict(request.user)] )
Scott Baker03324832015-01-08 22:38:17 -0800118