Skip to content

Commit

Permalink
add vlab_instances API endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
gpelouze committed Sep 20, 2023
1 parent 38e8544 commit 9180b98
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 7 deletions.
3 changes: 2 additions & 1 deletion vreapis/virtual_labs/admin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.contrib import admin
from virtual_labs.models import VirtualLab
from virtual_labs.models import VirtualLab, VirtualLabInstance

admin.site.register(VirtualLab)
admin.site.register(VirtualLabInstance)
5 changes: 5 additions & 0 deletions vreapis/virtual_labs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,8 @@ def __str__(self):

class Meta:
verbose_name = "KeyCloak Auth"


class VirtualLabInstance(models.Model):
vlab = models.ForeignKey(VirtualLab, on_delete=models.CASCADE, null=True)
username = models.CharField(max_length=100, null=True)
26 changes: 25 additions & 1 deletion vreapis/virtual_labs/serializers.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from dataclasses import fields
from pyexpat import model
from rest_framework import serializers
from rest_framework.validators import UniqueTogetherValidator
from django.contrib.auth.models import User
from virtual_labs.models import VM, SDIAProvision, Topology, VLProfile, VirtualLab
from virtual_labs.models import (VM, SDIAProvision, Topology, VLProfile,
VirtualLab, VirtualLabInstance)
from workflows.models import Workflow
from workflows.serializers import WorkflowSerializer

Expand Down Expand Up @@ -97,3 +99,25 @@ class Meta:
'description',
'endpoint'
)


class VirtualLabInstanceSerializer(serializers.ModelSerializer):

vlab = serializers.SlugRelatedField(
slug_field='slug',
queryset=VirtualLab.objects.all()
)

class Meta:
model = VirtualLabInstance
fields = (
'vlab',
'username',
)

validators = [
UniqueTogetherValidator(
queryset=VirtualLabInstance.objects.all(),
fields=['vlab', 'username'],
)
]
19 changes: 18 additions & 1 deletion vreapis/virtual_labs/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from sys import stdout
from rest_framework import viewsets
from rest_framework import mixins, viewsets
from rest_framework.decorators import action
from . import serializers
from . import models
Expand All @@ -19,3 +19,20 @@ class VirtualLabViewSet(GetSerializerMixin, viewsets.ModelViewSet):
}


class VirtualLabInstanceViewSet(
GetSerializerMixin,
mixins.ListModelMixin,
mixins.CreateModelMixin,
viewsets.GenericViewSet,
):

model = models.VirtualLabInstance
queryset = model.objects.all()
serializer_class = serializers.VirtualLabInstanceSerializer

def get_queryset(self):
vlab_slug = self.request.query_params.get('vlab_slug', None)
if vlab_slug:
return self.model.objects.filter(vlab__slug=vlab_slug)
else:
return self.model.objects.all()
6 changes: 2 additions & 4 deletions vreapis/vreapis/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from django.contrib import admin
from django.urls import path, include
from rest_framework import routers
from virtual_labs.views import VirtualLabViewSet
from virtual_labs.views import VirtualLabViewSet, VirtualLabInstanceViewSet
from cells.views import CellsViewSet
from workflows.views import WorkflowViewSet
from data_products.views import DataProductsViewSet, GeoDataProductsViewSet
Expand All @@ -25,13 +25,11 @@
from vreapis.settings.base import BASE_PATH



from vreapis.settings.base import BASE_PATH

admin.site.site_header = 'Virtual Labs Administration'

router = routers.DefaultRouter()
router.register(r'vlabs', VirtualLabViewSet)
router.register(r'vlab_instances', VirtualLabInstanceViewSet)
router.register(r'workflows', WorkflowViewSet)
router.register(r'cells', CellsViewSet)
router.register(r'dataprods', DataProductsViewSet)
Expand Down

0 comments on commit 9180b98

Please sign in to comment.