forked from uncloud/uncloud
d4b170f813
Signed-off-by: Nico Schottelius <nico@nico-notebook.schottelius.org>
91 lines
2.8 KiB
Python
91 lines
2.8 KiB
Python
from django.shortcuts import render
|
|
from django.contrib.auth import get_user_model
|
|
from django.contrib.auth.models import Group
|
|
|
|
from rest_framework import viewsets, permissions, generics
|
|
|
|
from rest_framework.views import APIView
|
|
from rest_framework.response import Response
|
|
|
|
from uncloud_vm.models import VMProduct
|
|
from .models import VMSnapshotProduct
|
|
from .serializers import UserSerializer, GroupSerializer, VMSnapshotSerializer, VMSnapshotCreateSerializer
|
|
|
|
|
|
import inspect
|
|
import sys
|
|
import re
|
|
|
|
|
|
|
|
class UserViewSet(viewsets.ModelViewSet):
|
|
|
|
"""
|
|
API endpoint that allows users to be viewed or edited.
|
|
"""
|
|
queryset = get_user_model().objects.all().order_by('-date_joined')
|
|
serializer_class = UserSerializer
|
|
|
|
permission_classes = [permissions.IsAuthenticated]
|
|
|
|
|
|
# POST /vm/snapshot/ vmuuid=... => create snapshot, returns snapshot uuid
|
|
# GET /vm/snapshot => list
|
|
# DEL /vm/snapshot/<uuid:uuid> => delete
|
|
# create-list -> get, post => ListCreateAPIView
|
|
# del on other!
|
|
class VMSnapshotView(viewsets.ViewSet):
|
|
permission_classes = [permissions.IsAuthenticated]
|
|
|
|
def list(self, request):
|
|
queryset = VMSnapshotProduct.objects.filter(owner=request.user)
|
|
serializer = VMSnapshotSerializer(queryset, many=True, context={'request': request})
|
|
return Response(serializer.data)
|
|
|
|
def retrieve(self, request, pk=None):
|
|
queryset = VMSnapshotProduct.objects.filter(owner=request.user)
|
|
vm = get_object_or_404(queryset, pk=pk)
|
|
serializer = VMSnapshotSerializer(vm, context={'request': request})
|
|
return Response(serializer.data)
|
|
|
|
def create(self, request):
|
|
print(request.data)
|
|
serializer = VMSnapshotCreateSerializer(data=request.data)
|
|
|
|
serializer.gb_ssd = 12
|
|
serializer.gb_hdd = 120
|
|
print("F")
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
print(serializer)
|
|
print("A")
|
|
serializer.save()
|
|
print("B")
|
|
|
|
|
|
# snapshot = VMSnapshotProduct(owner=request.user,
|
|
# **serialzer.data)
|
|
|
|
return Response(serializer.data)
|
|
|
|
|
|
# Next: create /order/<productname> urls
|
|
# Next: strip off "Product" at the end
|
|
class ProductsView(APIView):
|
|
def get(self, request, format=None):
|
|
clsmembers = inspect.getmembers(sys.modules['uncloud_api.models'], inspect.isclass)
|
|
products = []
|
|
for name, c in clsmembers:
|
|
# Include everything that ends in Product, but not Product itself
|
|
m = re.match(r'(?P<pname>.+)Product$', name)
|
|
if m:
|
|
products.append({
|
|
'name': m.group('pname'),
|
|
'description': c.description,
|
|
'recurring_period': c.recurring_period,
|
|
'pricing_model': c.pricing_model()
|
|
}
|
|
)
|
|
|
|
|
|
return Response(products)
|