Commit 8179e8f6 by Karsa Zoltán István

add activities t rest api

parent ec9774e5
...@@ -2,10 +2,15 @@ from rest_framework.renderers import JSONRenderer ...@@ -2,10 +2,15 @@ from rest_framework.renderers import JSONRenderer
from rest_framework import serializers from rest_framework import serializers
from django.contrib.auth.models import Group, User from django.contrib.auth.models import Group, User
from vm.models import Instance, InstanceTemplate, Lease, Interface, Node from vm.models import Instance, InstanceTemplate, Lease, Interface, Node, InstanceActivity
from firewall.models import Vlan from firewall.models import Vlan
from storage.models import Disk from storage.models import Disk
class InstanceActivitySerializer(serializers.ModelSerializer):
class Meta:
model = InstanceActivity
fields = ('id', 'instance', 'resultant_state', 'interruptible', 'activity_code', 'readable_name_data', 'parent',
'task_uuid', 'user', 'started', 'finished', 'succeeded', 'result_data', 'created', 'modified')
class GroupSerializer(serializers.ModelSerializer): class GroupSerializer(serializers.ModelSerializer):
class Meta: class Meta:
......
...@@ -60,6 +60,7 @@ from .views import ( ...@@ -60,6 +60,7 @@ from .views import (
GetLeaseREST, GetDiskRest, DeployInstanceREST, CreateDiskREST, GetLeaseREST, GetDiskRest, DeployInstanceREST, CreateDiskREST,
VlanREST, ResizeDiskREST, GetVlanREST, VlanREST, ResizeDiskREST, GetVlanREST,
StorageDetail, DiskDetail, UserREST, GroupREST, StorageDetail, DiskDetail, UserREST, GroupREST,
InstanceActivityREST, GetInstanceActivityREST,
MessageList, MessageDetail, MessageCreate, MessageDelete, MessageList, MessageDetail, MessageCreate, MessageDelete,
EnableTwoFactorView, DisableTwoFactorView, EnableTwoFactorView, DisableTwoFactorView,
AclUserGroupAutocomplete, AclUserAutocomplete, AclUserGroupAutocomplete, AclUserAutocomplete,
...@@ -69,6 +70,8 @@ from .views.node import node_ops, NodeREST, GetNodeREST ...@@ -69,6 +70,8 @@ from .views.node import node_ops, NodeREST, GetNodeREST
from .views.vm import vm_ops, vm_mass_ops from .views.vm import vm_ops, vm_mass_ops
urlpatterns = [ urlpatterns = [
path('acpi/vmact/', InstanceActivityREST.as_view()),
path('acpi/vmact/<int:pk>/', GetInstanceActivityREST.as_view()),
path('acpi/user/', UserREST.as_view()), path('acpi/user/', UserREST.as_view()),
path('acpi/group/', GroupREST.as_view()), path('acpi/group/', GroupREST.as_view()),
path('acpi/vm/', InstanceREST.as_view()), path('acpi/vm/', InstanceREST.as_view()),
......
...@@ -51,8 +51,17 @@ from braces.views import LoginRequiredMixin ...@@ -51,8 +51,17 @@ from braces.views import LoginRequiredMixin
from braces.views._access import AccessMixin from braces.views._access import AccessMixin
from celery.exceptions import TimeoutError from celery.exceptions import TimeoutError
from rest_framework import status
from rest_framework.views import APIView
from rest_framework.parsers import JSONParser
from rest_framework.authentication import TokenAuthentication, BasicAuthentication
from rest_framework.permissions import IsAdminUser
from dashboard.serializers import InstanceActivitySerializer
from common.models import HumanReadableException, HumanReadableObject from common.models import HumanReadableException, HumanReadableObject
from ..models import GroupProfile, Profile from ..models import GroupProfile, Profile
from vm.models import InstanceActivity
from ..forms import TransferOwnershipForm from ..forms import TransferOwnershipForm
...@@ -67,6 +76,26 @@ logger = logging.getLogger(__name__) ...@@ -67,6 +76,26 @@ logger = logging.getLogger(__name__)
saml_available = hasattr(settings, "SAML_CONFIG") saml_available = hasattr(settings, "SAML_CONFIG")
class InstanceActivityREST(APIView):
authentication_classes = [TokenAuthentication,BasicAuthentication]
permission_classes = [IsAdminUser]
def get(self, request, format=None):
templates = InstanceActivity.objects.all()
serializer = InstanceActivitySerializer(templates, many=True)
return JsonResponse(serializer.data, safe=False)
class GetInstanceActivityREST(APIView):
authentication_classes = [TokenAuthentication,BasicAuthentication]
permission_classes = [IsAdminUser]
def get(self, request, pk, format=None):
interface = InstanceActivity.objects.get(pk=pk)
serializer = InstanceActivitySerializer(interface, many=False)
return JsonResponse(serializer.data, safe=False)
class RedirectToLoginMixin(AccessMixin): class RedirectToLoginMixin(AccessMixin):
redirect_exception_classes = (PermissionDenied, ) redirect_exception_classes = (PermissionDenied, )
......
...@@ -98,7 +98,7 @@ from rest_framework.permissions import IsAdminUser ...@@ -98,7 +98,7 @@ from rest_framework.permissions import IsAdminUser
from dashboard.serializers import ( from dashboard.serializers import (
DiskSerializer, InstanceSerializer, InterfaceSerializer, CreateDiskSerializer, DownloadDiskSerializer, DiskSerializer, InstanceSerializer, InterfaceSerializer, CreateDiskSerializer, DownloadDiskSerializer,
VMDeploySerializer, VlanSerializer, ResizeDiskSerializer VMDeploySerializer, VlanSerializer, ResizeDiskSerializer, InstanceActivitySerializer
) )
class VlanREST(APIView): class VlanREST(APIView):
...@@ -268,13 +268,10 @@ class DownloadDiskREST(APIView): ...@@ -268,13 +268,10 @@ class DownloadDiskREST(APIView):
disk_url = str(data['url']) disk_url = str(data['url'])
disk_name = str(data['name']) disk_name = str(data['name'])
instance = Instance.objects.get(pk=vm_id) instance = Instance.objects.get(pk=vm_id)
old_disks = list(instance.disks.all())
DownloadDiskOperation(instance)._async(name=disk_name, url=disk_url, user=instance.owner) DownloadDiskOperation(instance)._async(name=disk_name, url=disk_url, user=instance.owner)
new_disks = instance.disks.all() act = instance.get_latest_activity_in_progress()
for d in new_disks: serializer = InstanceActivitySerializer(act, many=False)
if d not in old_disks: return JsonResponse(serializer.data, status=201)
ret = DiskSerializer(d, many=False)
return JsonResponse(ret.data, status=201)
return JsonResponse(serializer.errors, status=400) return JsonResponse(serializer.errors, status=400)
def size_util(size: str): def size_util(size: str):
......
...@@ -7683,8 +7683,8 @@ msgstr "" ...@@ -7683,8 +7683,8 @@ msgstr ""
#: vm/operations.py:344 #: vm/operations.py:344
#, python-format #, python-format
msgid "download %(name)s" msgid "download %(name)s (id: %(disk_id)s)"
msgstr "%(name)s letöltése" msgstr "%(name)s letöltése (id: %(disk_id)s)"
#: vm/operations.py:347 #: vm/operations.py:347
#, python-format #, python-format
......
...@@ -341,12 +341,13 @@ class DownloadDiskOperation(InstanceOperation): ...@@ -341,12 +341,13 @@ class DownloadDiskOperation(InstanceOperation):
disk.save() disk.save()
self.instance.disks.add(disk) self.instance.disks.add(disk)
activity.readable_name = create_readable( activity.readable_name = create_readable(
ugettext_noop("download %(name)s"), name=disk.name) ugettext_noop("download %(name)s (id: %(disk_id)s)"), name=disk.name, disk_id=str(disk.id))
activity.result = create_readable(ugettext_noop( activity.result = create_readable(ugettext_noop(
"Downloading %(url)s is finished. The file md5sum " "Downloading %(url)s is finished. The file md5sum "
"is: '%(checksum)s'."), "is: '%(checksum)s' (id: %(disk_id)s)."),
url=url, checksum=disk.checksum) url=url, checksum=disk.checksum, disk_id=str(disk.id))
activity.result_data['params']['disk_id'] = disk.id
# TODO iso (cd) hot-plug is not supported by kvm/guests # TODO iso (cd) hot-plug is not supported by kvm/guests
if self.instance.is_running and disk.type not in ["iso"]: if self.instance.is_running and disk.type not in ["iso"]:
self.instance._attach_disk(parent_activity=activity, disk=disk) self.instance._attach_disk(parent_activity=activity, disk=disk)
......
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