Commit 5485381c by Karsa Zoltán István

lease and template serializers and api points

parent 70545e0d
from rest_framework.renderers import JSONRenderer
from rest_framework import serializers
from vm.models import InstanceTemplate, Lease
class InstanceTemplateSerializer(serializers.ModelSerializer):
class Meta:
model = InstanceTemplate
resource_name = 'template'
fields = [ 'id', 'name', 'description', 'parent', 'owner', 'access_method', 'boot_menu',
'lease', 'raw_data', 'cloud_init', 'ci_meta_data', 'ci_user_data', 'system',
'has_agent', 'num_cores', 'ram_size', 'max_ram_size', 'arch', 'priority']
class LeaseSerializer(serializers.ModelSerializer):
class Meta:
model = Lease
resource_name = 'lease'
fields = [ 'id', 'name', 'suspend_interval_seconds', 'delete_interval_seconds']
......@@ -54,7 +54,7 @@ from .views import (
TransferTemplateOwnershipView, TransferTemplateOwnershipConfirmView,
OpenSearchDescriptionView,
NodeActivityView,
UserList,
UserList, TemplateREST, LeaseREST,
StorageDetail, DiskDetail,
MessageList, MessageDetail, MessageCreate, MessageDelete,
EnableTwoFactorView, DisableTwoFactorView,
......@@ -65,7 +65,9 @@ from .views.node import node_ops
from .views.vm import vm_ops, vm_mass_ops
urlpatterns = [
path('acpi/vmlist', iac_vm_list),
path('acpi/vm/', iac_vm_list),
path('acpi/template/', TemplateREST.as_view()),
path('acpi/lease/', LeaseREST.as_view()),
url(r'^$', IndexView.as_view(), name="dashboard.index"),
url(r"^profile/list/$", UserList.as_view(),
name="dashboard.views.user-list"),
......
......@@ -26,7 +26,7 @@ from django.contrib.messages.views import SuccessMessageMixin
from django.urls import reverse, reverse_lazy
from django.core.exceptions import PermissionDenied, SuspiciousOperation
from django.db.models import Count
from django.http import HttpResponse, HttpResponseRedirect
from django.http import HttpResponse, HttpResponseRedirect, JsonResponse
from django.shortcuts import redirect, get_object_or_404
from django.utils import timezone
from django.utils.translation import ugettext as _, ugettext_noop
......@@ -56,6 +56,14 @@ from .util import (
GraphMixin
)
from dashboard.serializers import InstanceTemplateSerializer, LeaseSerializer
from rest_framework.authentication import TokenAuthentication, BasicAuthentication
from rest_framework.permissions import IsAdminUser
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.parsers import JSONParser
try:
# Python 2: "unicode" is built-in
unicode
......@@ -184,6 +192,43 @@ class TemplateCreate(SuccessMessageMixin, CreateView):
return reverse_lazy("dashboard.views.template-list")
class TemplateREST(APIView):
authentication_classes = [TokenAuthentication,BasicAuthentication]
permission_classes = [IsAdminUser]
def get(self, request, format=None):
templates = InstanceTemplate.objects.all()
serializer = InstanceTemplateSerializer(templates, many=True)
return JsonResponse({ 'templates': serializer.data}, safe=False)
def post(self, request, format=None):
data = JSONParser().parse(request)
serializer = InstanceTemplateSerializer(data=data)
if serializer.is_valid():
serializer.save()
return JsonResponse(serializer.data, status=201)
return JsonResponse(serializer.errors, status=400)
class LeaseREST(APIView):
authentication_classes = [TokenAuthentication, BasicAuthentication]
permission_classes = [IsAdminUser]
def get(self, request, format=None):
templates = Lease.objects.all()
serializer = LeaseSerializer(templates, many=True)
return JsonResponse({ 'leases': serializer.data}, safe=False)
def post(self, request, format=None):
data = JSONParser().parse(request)
serializer = LeaseSerializer(data=data)
if serializer.is_valid():
serializer.save()
return JsonResponse(serializer.data, status=201)
return JsonResponse(serializer.errors, status=400)
class TemplateAclUpdateView(AclUpdateView):
model = InstanceTemplate
......
......@@ -105,10 +105,25 @@ def iac_vm_list(request):
'host': i.short_hostname,
'status': i.get_status_display(),
'owner': (i.owner.profile.get_display_name()),
'node': i.node,
'timesuspend': i.time_of_suspend,
'timedelete': i.time_of_delete,
'timedestroy': i.destroyed_at,
'userdata': i.get_user_data,
'metadata': i.get_meta_data,
'template': i.template.pk if i.template else None,
'users': list({ 'username': u.username, 'level': l } for u, l in i.get_users_with_level()),
} for i in instances]
return Response(instances)
@api_view(['POST'])
@authentication_classes([TokenAuthentication])
@permission_classes([IsAdminUser])
def iac_vm_create(request):
return Response()
class VmDetailVncTokenView(CheckedDetailView):
template_name = "dashboard/vm-detail.html"
model = Instance
......
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