2020-02-22 10:49:00 +00:00
|
|
|
import json
|
|
|
|
|
|
|
|
from rest_framework import generics
|
|
|
|
from rest_framework.authentication import SessionAuthentication, BasicAuthentication
|
|
|
|
from rest_framework.permissions import IsAuthenticated, IsAdminUser
|
|
|
|
|
2020-02-21 15:33:37 +00:00
|
|
|
from .models import VM
|
|
|
|
from .serializers import VMSerializer
|
|
|
|
|
|
|
|
class VMList(generics.ListAPIView):
|
2020-02-22 10:49:00 +00:00
|
|
|
authentication_classes = [SessionAuthentication, BasicAuthentication]
|
|
|
|
permission_classes = [IsAuthenticated, IsAdminUser]
|
2020-02-21 15:33:37 +00:00
|
|
|
queryset = VM.objects.all()
|
|
|
|
serializer_class = VMSerializer
|
|
|
|
|
|
|
|
|
|
|
|
class VMDetail(generics.RetrieveAPIView):
|
2020-02-22 10:49:00 +00:00
|
|
|
authentication_classes = [SessionAuthentication, BasicAuthentication]
|
|
|
|
permission_classes = [IsAuthenticated, IsAdminUser]
|
|
|
|
lookup_field = 'uuid'
|
2020-02-21 15:33:37 +00:00
|
|
|
queryset = VM.objects.all()
|
|
|
|
serializer_class = VMSerializer
|
2020-02-22 10:49:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
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")
|