Commit a94cb580 by Chif Gergő

Add instance list item serializer, fixes in create views

parent 1315d487
......@@ -2,17 +2,6 @@ from rest_framework import serializers
from .models import Flavor, Instance, Lease
class InstanceSerializer(serializers.ModelSerializer):
lease = serializers.PrimaryKeyRelatedField(read_only=True)
flavor = serializers.PrimaryKeyRelatedField(read_only=True)
class Meta:
model = Instance
fields = "__all__"
read_only_fields = ("id", "status" "password", "template",
"time_of_suspend", "time_of_delete")
class FlavorSerializer(serializers.ModelSerializer):
class Meta:
model = Flavor
......@@ -23,3 +12,22 @@ class LeaseSerializer(serializers.ModelSerializer):
class Meta:
model = Lease
fields = "__all__"
class InstanceListItemSerializer(serializers.ModelSerializer):
lease = LeaseSerializer(read_only=True)
class Meta:
model = Instance
fields = ["id", "name", "status", "system_type", "distro", "status", "lease"]
class InstanceSerializer(serializers.ModelSerializer):
lease = serializers.PrimaryKeyRelatedField(read_only=True)
flavor = serializers.PrimaryKeyRelatedField(read_only=True)
class Meta:
model = Instance
fields = "__all__"
read_only_fields = ("id", "status" "password", "template",
"time_of_suspend", "time_of_delete")
from instance.serializers import InstanceSerializer, FlavorSerializer, LeaseSerializer
from instance.serializers import (InstanceSerializer, InstanceListItemSerializer, FlavorSerializer,
LeaseSerializer)
from django.http import Http404
from django.db.models import Q
from rest_framework.viewsets import ViewSet, ModelViewSet
from rest_framework.response import Response
from rest_framework import status
from rest_framework.decorators import action
from rest_framework.permissions import IsAuthenticated
from template.serializers import InstanceFromTemplateSerializer
from instance.models import Instance, Flavor, Lease
from template.models import ImageTemplate
from template.serializers import ImageTemplateModelSerializer
from authorization.mixins import AuthorizationMixin
from guardian.shortcuts import get_perms_for_model, assign_perm
authorization = {
......@@ -49,6 +53,7 @@ update_actions = [
class InstanceViewSet(AuthorizationMixin, ViewSet):
authorization = authorization
permission_classes = [IsAuthenticated]
def get_object(self, pk):
try:
......@@ -71,28 +76,35 @@ class InstanceViewSet(AuthorizationMixin, ViewSet):
def list(self, request):
instances = self.get_objects_with_perms(request.user, "list", Instance)
return Response(InstanceSerializer(instances, many=True).data)
return Response(InstanceListItemSerializer(instances, many=True).data)
def create(self, request):
if not self.has_perms_for_model(request.user, 'create'):
return Response({"error": "No permission to create Virtual Machine."},
status=status.HTTP_401_UNAUTHORIZED)
params = request.data
template = ImageTemplate.objects.get(pk=params["template"])
template = ImageTemplate.objects.get(Q(pk=params["template"]) &
(Q(owned_by=request.user) | Q(type="SYSTEM")))
if not template:
return Response({"error": "No permission to use this template."},
status=status.HTTP_401_UNAUTHORIZED)
flavor = Flavor.objects.get(pk=params["flavor"]) if "flavor" in params else template.flavor
lease = Lease.objects.get(pk=params["lease"]) if "lease" in params else template.lease
newInstance = Instance.create_instance_from_template(
params={"name": params["name"],
"description": params["description"],
"access_method": params["access"],
"system": params["system"],
},
flavor=flavor,
lease=lease,
networks=[{"uuid": template.network_id}],
template=template,
flavor=flavor,
networks=[{"uuid": template.network_id}],
owner=request.user,
)
for perm in get_perms_for_model(Instance):
print(perm)
assign_perm(perm, request.user, newInstance)
return Response(InstanceSerializer(newInstance).data)
def retrieve(self, request, pk):
......@@ -202,12 +214,15 @@ class FlavorViewSet(ViewSet):
def create(self, request, format=None):
data = request.data
prior = 1
if "priority" in data:
prior = data["priority"]
new_flavor = Flavor.create(name=data["name"],
description=data["description"],
ram=data["ram"],
vcpu=data["vcpu"],
initial_disk=data["initial_disk"],
priority=data["priority"])
priority=prior)
return Response(new_flavor.pk)
def update(self, request, pk):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment