uncloud-mravi/nicohack202002/uncloud/opennebula/views.py

59 lines
1.9 KiB
Python

import json
from rest_framework import generics
from rest_framework.authentication import SessionAuthentication, BasicAuthentication
from rest_framework.permissions import IsAuthenticated, IsAdminUser
from .models import VM
from .serializers import VMSerializer
class VMList(generics.ListAPIView):
authentication_classes = [SessionAuthentication, BasicAuthentication]
permission_classes = [IsAuthenticated, IsAdminUser]
queryset = VM.objects.all()
serializer_class = VMSerializer
class VMDetail(generics.RetrieveAPIView):
authentication_classes = [SessionAuthentication, BasicAuthentication]
permission_classes = [IsAuthenticated, IsAdminUser]
lookup_field = 'uuid'
queryset = VM.objects.all()
serializer_class = VMSerializer
class UserVMList(generics.ListAPIView):
authentication_classes = [SessionAuthentication, BasicAuthentication]
permission_classes = [IsAuthenticated]
serializer_class = VMSerializer
def get_queryset(self):
user_email = self.request.user.ldap_user.attrs.data['mail']
vms = []
for mail in user_email:
vms += VM.objects.filter(owner__username=mail)
for vm in vms:
data = json.loads(vm.data)
vm_template = data['TEMPLATE']
vm.data = {
'cpu': vm_template['VCPU'],
'ram': vm_template['MEMORY'],
'nic': vm_template['NIC'],
'disks': vm_template['DISK']
}
return vms
#######################################
# Following for quick experimentation #
#######################################
# from django.http import HttpResponse
#
# def test(request):
# user_email = request.user.ldap_user.attrs.data['mail']
# vms = []
# for mail in user_email:
# vms += VM.objects.filter(owner__username=mail)
# return HttpResponse("Hello World")