aboutsummaryrefslogtreecommitdiffstats
path: root/src/api/views.py
diff options
context:
space:
mode:
authorAdam Hassick <ahassick@iol.unh.edu>2021-06-29 16:49:27 -0400
committerAdam Hassick <ahassick@iol.unh.edu>2021-07-23 16:22:54 +0000
commit6ffb1fdf6ce7825770148bada5a4c54899e4ed36 (patch)
treeda5d8390a4d46a898840083a761809af47bd7f52 /src/api/views.py
parent49e2b407003b69551ddafa851639e83ec42a5b09 (diff)
Cobbler model changes, new endpoints
Signed-off-by: Adam Hassick <ahassick@iol.unh.edu> Change-Id: If0a94730e92747127cef121ec4930a4c8bae6c92 Signed-off-by: Sawyer Bergeron <sbergeron@iol.unh.edu> Signed-off-by: Adam Hassick <ahassick@iol.unh.edu>
Diffstat (limited to 'src/api/views.py')
-rw-r--r--src/api/views.py90
1 files changed, 86 insertions, 4 deletions
diff --git a/src/api/views.py b/src/api/views.py
index 3a3effa..4b887e6 100644
--- a/src/api/views.py
+++ b/src/api/views.py
@@ -14,6 +14,7 @@ from django.shortcuts import redirect
from django.utils.decorators import method_decorator
from django.utils import timezone
from django.views import View
+from django.http import QueryDict
from django.http.response import JsonResponse, HttpResponse
from rest_framework import viewsets
from rest_framework.authtoken.models import Token
@@ -25,9 +26,14 @@ from api.serializers.old_serializers import UserSerializer
from api.forms import DowntimeForm
from account.models import UserProfile
from booking.models import Booking
-from api.models import LabManagerTracker, get_task, CloudInitFile
+from api.models import LabManagerTracker, get_task, CloudInitFile, Job
from notifier.manager import NotificationHandler
from analytics.models import ActiveVPNUser
+from resource_inventory.models import (
+ Image,
+ Opsys
+)
+
import json
"""
@@ -80,6 +86,81 @@ def lab_host(request, lab_name="", host_id=""):
if request.method == "POST":
return JsonResponse(lab_manager.update_host(host_id, request.POST), safe=False)
+# API extension for Cobbler integration
+
+def all_images(request, lab_name=""):
+ a = []
+ for i in Image.objects.all():
+ a.append(i.serialize())
+ return JsonResponse(a, safe=False)
+
+
+def all_opsyss(request, lab_name=""):
+ a = []
+ for opsys in Opsys.objects.all():
+ a.append(opsys.serialize())
+
+ return JsonResponse(a, safe=False)
+
+@csrf_exempt
+def single_image(request, lab_name="", image_id=""):
+ lab_token = request.META.get('HTTP_AUTH_TOKEN')
+ lab_manager = LabManagerTracker.get(lab_name, lab_token)
+ img = lab_manager.get_image(image_id).first()
+
+ if request.method == "GET":
+ if not img:
+ return HttpResponse(status=404)
+ return JsonResponse(img.serialize(), safe=False)
+
+ if request.method == "POST":
+ # get POST data
+ data = json.loads(request.body.decode('utf-8'))
+ if img:
+ img.update(data)
+ else:
+ # append lab name and the ID from the URL
+ data['from_lab_id'] = lab_name
+ data['lab_id'] = image_id
+
+ # create and save a new Image object
+ img = Image.new_from_data(data)
+
+ img.save()
+
+ # indicate success in response
+ return HttpResponse(status=200)
+ return HttpResponse(status=405)
+
+
+@csrf_exempt
+def single_opsys(request, lab_name="", opsys_id=""):
+ lab_token = request.META.get('HTTP_AUTH_TOKEN')
+ lab_manager = LabManagerTracker.get(lab_name, lab_token)
+ opsys = lab_manager.get_opsys(opsys_id).first()
+
+ if request.method == "GET":
+ if not opsys:
+ return HttpResponse(status=404)
+ return JsonResponse(opsys.serialize(), safe=False)
+
+ if request.method == "POST":
+ data = json.loads(request.body.decode('utf-8'))
+ if opsys:
+ opsys.update(data)
+ else:
+ # only name, available, and obsolete are needed to create an Opsys
+ # other fields are derived from the URL parameters
+
+ data['from_lab_id'] = lab_name
+ data['lab_id'] = opsys_id
+ opsys = Opsys.new_from_data(data)
+
+ opsys.save()
+ return HttpResponse(status=200)
+ return HttpResponse(status=405)
+
+# end API extension
def get_pdf(request, lab_name="", booking_id=""):
lab_token = request.META.get('HTTP_AUTH_TOKEN')
@@ -168,10 +249,11 @@ def specific_job(request, lab_name="", job_id=""):
@csrf_exempt
def resource_cidata(request, lab_name="", job_id="", resource_id=""):
- lab_token = request.META.get('HTTP_AUTH_TOKEN')
- lab_manager = LabManagerTracker.get(lab_name, lab_token)
+ #lab_token = request.META.get('HTTP_AUTH_TOKEN')
+ #lab_manager = LabManagerTracker.get(lab_name, lab_token)
- job = lab_manager.get_job(job_id)
+ #job = lab_manager.get_job(job_id)
+ job = Job.objects.get(id=job_id)
cifile = None
try: