Scott Baker | 45a09b1 | 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 | 20101af | 2015-02-02 10:41:12 -0800 | [diff] [blame] | 10 | from django.core.exceptions import PermissionDenied |
Scott Baker | 45a09b1 | 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 | 48b84d1 | 2015-01-12 12:57:44 -0800 | [diff] [blame] | 17 | def getTenantViewDict(user): |
Scott Baker | 45a09b1 | 2015-01-08 22:38:17 -0800 | [diff] [blame] | 18 | 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 Baker | a8c0b24 | 2015-01-22 15:16:34 -0800 | [diff] [blame] | 23 | # 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 Baker | 45a09b1 | 2015-01-08 22:38:17 -0800 | [diff] [blame] | 28 | 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 Baker | edaa0ed | 2015-01-12 13:37:01 -0800 | [diff] [blame] | 40 | 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 Baker | 45a09b1 | 2015-01-08 22:38:17 -0800 | [diff] [blame] | 49 | volumes=[] |
| 50 | for volume in Volume.objects.all(): |
| 51 | if not volume.private:
|
| 52 | volumes.append(volume) |
| 53 | |
Scott Baker | fcf655e | 2015-01-12 19:45:40 -0800 | [diff] [blame] | 54 | site_users=[] |
Scott Baker | 05d2e03 | 2015-01-14 09:54:19 -0800 | [diff] [blame] | 55 | user_site_roles=[] |
Scott Baker | 71b3a45 | 2015-01-18 17:04:35 -0800 | [diff] [blame] | 56 | 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 Baker | 05d2e03 | 2015-01-14 09:54:19 -0800 | [diff] [blame] | 68 | |
Scott Baker | edaa0ed | 2015-01-12 13:37:01 -0800 | [diff] [blame] | 69 | blessed_service_classes = [ServiceClass.objects.get(name="Best Effort")] |
| 70 | |
Scott Baker | 45a09b1 | 2015-01-08 22:38:17 -0800 | [diff] [blame] | 71 | 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 Baker | edaa0ed | 2015-01-12 13:37:01 -0800 | [diff] [blame] | 77 | "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 Baker | 45a09b1 | 2015-01-08 22:38:17 -0800 | [diff] [blame] | 81 | "public_volume_names": [volume.name for volume in volumes], |
| 82 | "public_volumes": [volume.id for volume in volumes], |
Scott Baker | 71b3a45 | 2015-01-18 17:04:35 -0800 | [diff] [blame] | 83 | "current_user_site_id": user_site_id, |
| 84 | "current_user_login_base": user_site_login_base, |
Scott Baker | fcf655e | 2015-01-12 19:45:40 -0800 | [diff] [blame] | 85 | "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 Baker | 05d2e03 | 2015-01-14 09:54:19 -0800 | [diff] [blame] | 87 | "current_user_can_create_slice": user.is_admin or ("pi" in user_site_roles) or ("admin" in user_site_roles), |
Scott Baker | dcfcefd | 2015-01-19 08:56:16 -0800 | [diff] [blame] | 88 | "current_user_id": user.id, |
Scott Baker | 45a09b1 | 2015-01-08 22:38:17 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | class TenantList(APIView): |
| 92 | method_kind = "list" |
| 93 | method_name = "tenantview" |
| 94 | |
| 95 | def get(self, request, format=None): |
Scott Baker | 20101af | 2015-02-02 10:41:12 -0800 | [diff] [blame] | 96 | if (not request.user.is_authenticated()): |
| 97 | raise PermissionDenied("You must be authenticated in order to use this API") |
Scott Baker | 48b84d1 | 2015-01-12 12:57:44 -0800 | [diff] [blame] | 98 | return Response( getTenantViewDict(request.user) ) |
Scott Baker | 45a09b1 | 2015-01-08 22:38:17 -0800 | [diff] [blame] | 99 | |
| 100 | class TenantDetail(APIView): |
| 101 | method_kind = "detail" |
| 102 | method_name = "tenantview" |
| 103 | |
| 104 | def get(self, request, format=None, pk=0): |
Scott Baker | 20101af | 2015-02-02 10:41:12 -0800 | [diff] [blame] | 105 | if (not request.user.is_authenticated()): |
| 106 | raise PermissionDenied("You must be authenticated in order to use this API") |
Scott Baker | 48b84d1 | 2015-01-12 12:57:44 -0800 | [diff] [blame] | 107 | return Response( [getTenantViewDict(request.user)] ) |
Scott Baker | 45a09b1 | 2015-01-08 22:38:17 -0800 | [diff] [blame] | 108 | |