From e2b5b5d102aa64736833148ddc670d837404fd6d Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Sun, 23 Feb 2020 15:33:26 +0100 Subject: [PATCH] opennebula -> router --- uncloud/opennebula/models.py | 25 +++++++++++++++++++------ uncloud/opennebula/serializers.py | 6 ++++++ uncloud/opennebula/views.py | 20 +++++++++++++------- uncloud/uncloud/urls.py | 8 +++++--- 4 files changed, 43 insertions(+), 16 deletions(-) diff --git a/uncloud/opennebula/models.py b/uncloud/opennebula/models.py index ff0e49c..0b0f307 100644 --- a/uncloud/opennebula/models.py +++ b/uncloud/opennebula/models.py @@ -29,9 +29,22 @@ class VM(models.Model): If there are multiple disks, we have a list of dictionaries in the database. """ - if not 'DISK' in self.data['TEMPLATE']['DISK']: - return [] - elif type(self.data['TEMPLATE']['DISK']) is dict: - return [ self.data['TEMPLATE']['DISK'] ] - else: - return self.data['TEMPLATE']['DISK'] + disks = [] + + if 'DISK' in self.data['TEMPLATE']: + + if type(self.data['TEMPLATE']['DISK']) is dict: + disks = [ self.data['TEMPLATE']['DISK'] ] + else: + disks = self.data['TEMPLATE']['DISK'] + + disks = [ + { + 'size_in_gb': int(d['SIZE'])/1024. , + 'opennebula_source': d['SOURCE'], + 'opennebula_name': d['IMAGE'], + } + for d in disks + ] + + return disks diff --git a/uncloud/opennebula/serializers.py b/uncloud/opennebula/serializers.py index c84f2ab..30bd20a 100644 --- a/uncloud/opennebula/serializers.py +++ b/uncloud/opennebula/serializers.py @@ -6,3 +6,9 @@ class VMSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = VM fields = ['vmid', 'owner', 'data'] + + +class OpenNebulaVMSerializer(serializers.HyperlinkedModelSerializer): + class Meta: + model = VM + fields = ['vmid', 'owner', 'cores', 'ram_in_gb', 'disks' ] diff --git a/uncloud/opennebula/views.py b/uncloud/opennebula/views.py index 7f2b537..5505b32 100644 --- a/uncloud/opennebula/views.py +++ b/uncloud/opennebula/views.py @@ -1,16 +1,22 @@ -from rest_framework import viewsets, generics +from rest_framework import viewsets, generics, permissions from .models import VM -from .serializers import VMSerializer +from .serializers import VMSerializer, OpenNebulaVMSerializer -class VMList(generics.ListAPIView): +#class VMList(generics.ListAPIView): +# queryset = VM.objects.all() +# serializer_class = VMSerializer + + +class RawVMViewSet(viewsets.ModelViewSet): +# lookup_field = 'vmid' queryset = VM.objects.all() serializer_class = VMSerializer + permission_classes = [permissions.IsAuthenticated] -class VMDetail(generics.RetrieveAPIView): - lookup_field = 'vmid' +class VMViewSet(viewsets.ModelViewSet): queryset = VM.objects.all() - serializer_class = VMSerializer + serializer_class = OpenNebulaVMSerializer -class VMViewSet( + permission_classes = [permissions.IsAuthenticated] diff --git a/uncloud/uncloud/urls.py b/uncloud/uncloud/urls.py index c7ce9b6..0291b7f 100644 --- a/uncloud/uncloud/urls.py +++ b/uncloud/uncloud/urls.py @@ -24,6 +24,8 @@ from opennebula import views as oneviews router = routers.DefaultRouter() router.register(r'users', views.UserViewSet) router.register(r'groups', views.GroupViewSet) +router.register(r'opennebula', oneviews.VMViewSet) +router.register(r'opennebula_raw', oneviews.RawVMViewSet) # Wire up our API using automatic URL routing. # Additionally, we include login URLs for the browsable API. @@ -31,8 +33,8 @@ urlpatterns = [ path('', include(router.urls)), path('admin/', admin.site.urls), path('products/', views.ProductsView.as_view(), name='products'), - path('api-auth/', include('rest_framework.urls', namespace='rest_framework')), - path('vm/list/', oneviews.VMList.as_view(), name='vm_list'), - path('vm/detail//', oneviews.VMDetail.as_view(), name='vm_detail'), + path('api-auth/', include('rest_framework.urls', namespace='rest_framework')) +# path('vm/list/', oneviews.VMList.as_view(), name='vm_list'), +# path('vm/detail//', oneviews.VMDetail.as_view(), name='vm_detail'), ]