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 |
Scott Baker | 960431e | 2015-02-02 10:41:12 -0800 | [diff] [blame] | 10 | from django.core.exceptions import PermissionDenied |
Scott Baker | 0332483 | 2015-01-08 22:38:17 -0800 | [diff] [blame] | 11 | |
| 12 | # This REST API endpoint contains a bunch of misc information that the |
| 13 | # tenant view needs to display |
| 14 | |
| 15 | BLESSED_DEPLOYMENTS = ["ViCCI"] # ["US-MaxPlanck", "US-GeorgiaTech", "US-Princeton", "US-Washington", "US-Stanford"] |
| 16 | |
Scott Baker | c94091a | 2015-01-12 12:57:44 -0800 | [diff] [blame] | 17 | def getTenantViewDict(user): |
Scott Baker | 012a706 | 2015-02-09 10:53:13 -0800 | [diff] [blame] | 18 | blessed_deployments = [] |
| 19 | for deployment in Deployment.objects.all(): |
| 20 | if deployment.name in BLESSED_DEPLOYMENTS: |
| 21 | blessed_deployments.append(deployment) |
| 22 | |
Scott Baker | 0332483 | 2015-01-08 22:38:17 -0800 | [diff] [blame] | 23 | 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 Baker | f1b4015 | 2015-01-22 15:16:34 -0800 | [diff] [blame] | 28 | # 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 Baker | 0332483 | 2015-01-08 22:38:17 -0800 | [diff] [blame] | 33 | 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 Baker | 2a11f25 | 2015-01-12 13:37:01 -0800 | [diff] [blame] | 45 | 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 Baker | 0332483 | 2015-01-08 22:38:17 -0800 | [diff] [blame] | 54 | volumes=[] |
| 55 | for volume in Volume.objects.all(): |
| 56 | if not volume.private:
|
| 57 | volumes.append(volume) |
| 58 | |
Scott Baker | 97acad9 | 2015-01-12 19:45:40 -0800 | [diff] [blame] | 59 | site_users=[] |
Scott Baker | ce0dfb8 | 2015-01-14 09:54:19 -0800 | [diff] [blame] | 60 | user_site_roles=[] |
Scott Baker | 9eebcb6 | 2015-01-18 17:04:35 -0800 | [diff] [blame] | 61 | 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 Baker | ce0dfb8 | 2015-01-14 09:54:19 -0800 | [diff] [blame] | 73 | |
Scott Baker | 2a11f25 | 2015-01-12 13:37:01 -0800 | [diff] [blame] | 74 | blessed_service_classes = [ServiceClass.objects.get(name="Best Effort")] |
| 75 | |
Scott Baker | 0332483 | 2015-01-08 22:38:17 -0800 | [diff] [blame] | 76 | return {"id": 0, |
| 77 | "blessed_deployment_names": BLESSED_DEPLOYMENTS, |
Scott Baker | 012a706 | 2015-02-09 10:53:13 -0800 | [diff] [blame] | 78 | "blessed_deployments": [deployment.id for deployment in blessed_deployments], |
Scott Baker | 0332483 | 2015-01-08 22:38:17 -0800 | [diff] [blame] | 79 | "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 Baker | 2a11f25 | 2015-01-12 13:37:01 -0800 | [diff] [blame] | 83 | "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 Baker | 0332483 | 2015-01-08 22:38:17 -0800 | [diff] [blame] | 87 | "public_volume_names": [volume.name for volume in volumes], |
| 88 | "public_volumes": [volume.id for volume in volumes], |
Scott Baker | 9eebcb6 | 2015-01-18 17:04:35 -0800 | [diff] [blame] | 89 | "current_user_site_id": user_site_id, |
| 90 | "current_user_login_base": user_site_login_base, |
Scott Baker | 97acad9 | 2015-01-12 19:45:40 -0800 | [diff] [blame] | 91 | "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 Baker | ce0dfb8 | 2015-01-14 09:54:19 -0800 | [diff] [blame] | 93 | "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] | 94 | "current_user_id": user.id, |
Scott Baker | 0332483 | 2015-01-08 22:38:17 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | class TenantList(APIView): |
| 98 | method_kind = "list" |
| 99 | method_name = "tenantview" |
| 100 | |
| 101 | def get(self, request, format=None): |
Scott Baker | 960431e | 2015-02-02 10:41:12 -0800 | [diff] [blame] | 102 | if (not request.user.is_authenticated()): |
| 103 | raise PermissionDenied("You must be authenticated in order to use this API") |
Scott Baker | c94091a | 2015-01-12 12:57:44 -0800 | [diff] [blame] | 104 | return Response( getTenantViewDict(request.user) ) |
Scott Baker | 0332483 | 2015-01-08 22:38:17 -0800 | [diff] [blame] | 105 | |
| 106 | class TenantDetail(APIView): |
| 107 | method_kind = "detail" |
| 108 | method_name = "tenantview" |
| 109 | |
| 110 | def get(self, request, format=None, pk=0): |
Scott Baker | 960431e | 2015-02-02 10:41:12 -0800 | [diff] [blame] | 111 | if (not request.user.is_authenticated()): |
| 112 | raise PermissionDenied("You must be authenticated in order to use this API") |
Scott Baker | c94091a | 2015-01-12 12:57:44 -0800 | [diff] [blame] | 113 | return Response( [getTenantViewDict(request.user)] ) |
Scott Baker | 0332483 | 2015-01-08 22:38:17 -0800 | [diff] [blame] | 114 | |