2020-02-20 15:52:50 +00:00
|
|
|
from django.shortcuts import render
|
2020-02-21 10:32:41 +00:00
|
|
|
from django.contrib.auth import get_user_model
|
|
|
|
from django.contrib.auth.models import Group
|
2020-02-20 15:52:50 +00:00
|
|
|
|
2020-02-21 23:22:42 +00:00
|
|
|
from rest_framework import viewsets, permissions, generics
|
2020-02-26 10:31:17 +00:00
|
|
|
|
2020-02-21 23:22:42 +00:00
|
|
|
from rest_framework.views import APIView
|
|
|
|
from rest_framework.response import Response
|
|
|
|
|
2020-02-26 10:31:17 +00:00
|
|
|
from uncloud_vm.models import VMProduct
|
|
|
|
from .models import VMSnapshotProduct
|
|
|
|
from .serializers import UserSerializer, GroupSerializer, VMSnapshotSerializer, VMSnapshotCreateSerializer
|
2020-02-20 17:58:07 +00:00
|
|
|
|
2020-02-20 18:38:43 +00:00
|
|
|
|
2020-02-26 10:31:17 +00:00
|
|
|
import inspect
|
|
|
|
import sys
|
|
|
|
import re
|
2020-02-20 18:38:43 +00:00
|
|
|
|
2020-02-26 10:31:17 +00:00
|
|
|
|
|
|
|
# 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):
|
2020-02-20 18:38:43 +00:00
|
|
|
permission_classes = [permissions.IsAuthenticated]
|
|
|
|
|
2020-02-26 10:31:17 +00:00
|
|
|
def list(self, request):
|
|
|
|
queryset = VMSnapshotProduct.objects.filter(owner=request.user)
|
|
|
|
serializer = VMSnapshotSerializer(queryset, many=True, context={'request': request})
|
|
|
|
return Response(serializer.data)
|
2020-02-20 17:58:07 +00:00
|
|
|
|
2020-02-26 10:31:17 +00:00
|
|
|
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)
|
2020-02-20 18:38:43 +00:00
|
|
|
|
2020-02-26 10:31:17 +00:00
|
|
|
def create(self, request):
|
|
|
|
print(request.data)
|
|
|
|
serializer = VMSnapshotCreateSerializer(data=request.data)
|
2020-02-20 17:58:07 +00:00
|
|
|
|
2020-02-26 10:31:17 +00:00
|
|
|
serializer.gb_ssd = 12
|
|
|
|
serializer.gb_hdd = 120
|
|
|
|
print("F")
|
|
|
|
serializer.is_valid(raise_exception=True)
|
2020-02-20 17:58:07 +00:00
|
|
|
|
2020-02-26 10:31:17 +00:00
|
|
|
print(serializer)
|
|
|
|
print("A")
|
|
|
|
serializer.save()
|
|
|
|
print("B")
|
2020-02-20 18:38:43 +00:00
|
|
|
|
2020-02-21 23:22:42 +00:00
|
|
|
|
2020-02-26 10:31:17 +00:00
|
|
|
# snapshot = VMSnapshotProduct(owner=request.user,
|
|
|
|
# **serialzer.data)
|
2020-02-21 23:22:42 +00:00
|
|
|
|
2020-02-26 10:31:17 +00:00
|
|
|
return Response(serializer.data)
|
2020-02-21 23:22:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2020-02-26 10:31:17 +00:00
|
|
|
# maybe drop or not --- we need something to guide the user!
|
|
|
|
# class ProductsViewSet(viewsets.ViewSet):
|
|
|
|
# permission_classes = [permissions.IsAuthenticated]
|
|
|
|
|
|
|
|
# def list(self, request):
|
|
|
|
|
|
|
|
# clsmembers = []
|
|
|
|
# for modules in [ 'uncloud_api.models', 'uncloud_vm.models' ]:
|
|
|
|
# clsmembers.extend(inspect.getmembers(sys.modules[modules], 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)
|
|
|
|
|
|
|
|
|
|
|
|
class UserViewSet(viewsets.ModelViewSet):
|
|
|
|
serializer_class = UserSerializer
|
|
|
|
permission_classes = [permissions.IsAuthenticated]
|
2020-02-21 23:22:42 +00:00
|
|
|
|
2020-02-26 10:31:17 +00:00
|
|
|
def get_queryset(self):
|
|
|
|
return self.request.user
|